xref: /petsc/src/dm/impls/plex/plex.c (revision bb197d405633226ad1e3f91c2d2114bd5d5103c0)
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 /*@
16412e9a14SMatthew G. Knepley   DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells
17e5337592SStefano Zampini 
18412e9a14SMatthew G. Knepley   Input Parameter:
19412e9a14SMatthew G. Knepley + dm     - The DMPlex object
20412e9a14SMatthew G. Knepley - height - The cell height in the Plex, 0 is the default
21e5337592SStefano Zampini 
22e5337592SStefano Zampini   Output Parameters:
23412e9a14SMatthew G. Knepley + cStart - The first "normal" cell
24412e9a14SMatthew G. Knepley - cEnd   - The upper bound on "normal"" cells
25e5337592SStefano Zampini 
26412e9a14SMatthew G. Knepley   Note: This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.
27e5337592SStefano Zampini 
28412e9a14SMatthew G. Knepley   Level: developer
29e5337592SStefano Zampini 
30412e9a14SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum()
31e5337592SStefano Zampini @*/
32412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
33e5337592SStefano Zampini {
34412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
35412e9a14SMatthew G. Knepley   PetscInt       cS, cE, c;
36e5337592SStefano Zampini   PetscErrorCode ierr;
37e5337592SStefano Zampini 
38e5337592SStefano Zampini   PetscFunctionBegin;
39412e9a14SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);CHKERRQ(ierr);
40412e9a14SMatthew G. Knepley   for (c = cS; c < cE; ++c) {
41412e9a14SMatthew G. Knepley     DMPolytopeType cct;
42412e9a14SMatthew G. Knepley 
43412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &cct);CHKERRQ(ierr);
44412e9a14SMatthew G. Knepley     if ((PetscInt) cct < 0) break;
45412e9a14SMatthew G. Knepley     switch (cct) {
46ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_POINT:
47ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_SEGMENT:
48ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TRIANGLE:
49ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_QUADRILATERAL:
50ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TETRAHEDRON:
51ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_HEXAHEDRON:
52412e9a14SMatthew G. Knepley         ct = cct;
53e5337592SStefano Zampini         break;
54412e9a14SMatthew G. Knepley       default: break;
55e5337592SStefano Zampini     }
56412e9a14SMatthew G. Knepley     if (ct != DM_POLYTOPE_UNKNOWN) break;
57e5337592SStefano Zampini   }
58412e9a14SMatthew G. Knepley   if (ct != DM_POLYTOPE_UNKNOWN) {
59412e9a14SMatthew G. Knepley     DMLabel ctLabel;
60412e9a14SMatthew G. Knepley 
61412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
62412e9a14SMatthew G. Knepley     ierr = DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);CHKERRQ(ierr);
63e5337592SStefano Zampini   }
64412e9a14SMatthew G. Knepley   if (cStart) *cStart = cS;
65412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = cE;
66e5337592SStefano Zampini   PetscFunctionReturn(0);
67e5337592SStefano Zampini }
68e5337592SStefano Zampini 
697afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
707e42fee7SMatthew G. Knepley {
71412e9a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
72a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
737e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
747e42fee7SMatthew G. Knepley 
757e42fee7SMatthew G. Knepley   PetscFunctionBegin;
76e630c359SToby Isaac   *ft  = PETSC_VTK_INVALID;
77f094498dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
787e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
79412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
807e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
817e42fee7SMatthew G. Knepley   if (field >= 0) {
82a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
83a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
847e42fee7SMatthew G. Knepley   } else {
85a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
86a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
877e42fee7SMatthew G. Knepley   }
885483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
89a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
907e42fee7SMatthew G. Knepley     *sStart = vStart;
917e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
92f094498dSMatthew G. Knepley     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
937e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_POINT_FIELD;
94a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
957e42fee7SMatthew G. Knepley     *sStart = cStart;
967e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
97f094498dSMatthew G. Knepley     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
987e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_CELL_FIELD;
99e630c359SToby Isaac   } else {
100e630c359SToby Isaac     if (field >= 0) {
101e630c359SToby Isaac       const char *fieldname;
102e630c359SToby Isaac 
103e630c359SToby Isaac       ierr = PetscSectionGetFieldName(section, field, &fieldname);CHKERRQ(ierr);
104e630c359SToby Isaac       ierr = PetscInfo2((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);CHKERRQ(ierr);
105e630c359SToby Isaac     } else {
106e630c359SToby Isaac       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output typp of section\"%s\"\n");CHKERRQ(ierr);
107e630c359SToby Isaac     }
108e630c359SToby Isaac   }
1097e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1107e42fee7SMatthew G. Knepley }
1117e42fee7SMatthew G. Knepley 
1127cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
113e412dcbdSMatthew G. Knepley {
114e412dcbdSMatthew G. Knepley   DM                 dm;
115d1df6f1dSMatthew G. Knepley   PetscSection       s;
116e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
117e412dcbdSMatthew G. Knepley   DM                 cdm;
118e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
119e412dcbdSMatthew G. Knepley   Vec                coordinates;
120e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
121e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
122339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
123339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
124d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
125e412dcbdSMatthew G. Knepley   const char        *name;
126339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
127e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
128e412dcbdSMatthew G. Knepley 
129e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
130d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
131d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
132d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
133d1df6f1dSMatthew G. Knepley 
134e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
135e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1368135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
13792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
138d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
139e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
140e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
14192fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
142e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
143e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
144e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
145e412dcbdSMatthew G. Knepley 
146e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
147339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
148e412dcbdSMatthew G. Knepley 
149e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
150e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
151e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1520c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1530c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
154e412dcbdSMatthew G. Knepley   }
155e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
156e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley 
158d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
159d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
160d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
161d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
162d1df6f1dSMatthew G. Knepley     IS   fis;
163d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
164d1df6f1dSMatthew G. Knepley     const char *fname;
165d1df6f1dSMatthew G. Knepley 
166d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
167d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
168d1df6f1dSMatthew G. Knepley 
169a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
170d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
171d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
172d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
173d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
174a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
175a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
176d1df6f1dSMatthew G. Knepley     }
177d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
178d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
179d1df6f1dSMatthew G. Knepley 
180d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
181d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
182d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
183d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
184d1df6f1dSMatthew G. Knepley 
185d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
186d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
187339e3443SMatthew G. Knepley       if (!flg) {
188d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
189d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
190d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
191339e3443SMatthew G. Knepley       }
192e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
193339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
194d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
195e412dcbdSMatthew G. Knepley 
196d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
197e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
19899a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
199e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
200e412dcbdSMatthew G. Knepley 
201d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
202339e3443SMatthew G. Knepley         if (a) {
203d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
204339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
205339e3443SMatthew G. Knepley         } else {
206339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
207339e3443SMatthew G. Knepley           PetscInt     numVals, va;
208339e3443SMatthew G. Knepley 
209d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
210d1df6f1dSMatthew 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);
211d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
212d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
213d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
214d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
215339e3443SMatthew G. Knepley             break;
216d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
217d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
218d1df6f1dSMatthew 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]);
219d1df6f1dSMatthew G. Knepley             break;
220d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
221339e3443SMatthew G. Knepley           }
222d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
223339e3443SMatthew G. Knepley         }
224e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
225e412dcbdSMatthew G. Knepley         switch (numCoords) {
226e412dcbdSMatthew G. Knepley         case 6:
227339e3443SMatthew 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);
228e412dcbdSMatthew G. Knepley           break;
229e412dcbdSMatthew G. Knepley         case 8:
230339e3443SMatthew 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);
231339e3443SMatthew 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);
232e412dcbdSMatthew G. Knepley           break;
233e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
234e412dcbdSMatthew G. Knepley         }
235e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
236e412dcbdSMatthew G. Knepley       }
237d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
238e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
239e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
240e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
241d1df6f1dSMatthew G. Knepley     }
242d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
243d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
244d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
245d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
246d1df6f1dSMatthew G. Knepley     }
247d1df6f1dSMatthew G. Knepley   }
248e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
249e412dcbdSMatthew G. Knepley }
250e412dcbdSMatthew G. Knepley 
251684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
252684b87d9SLisandro Dalcin {
253684b87d9SLisandro Dalcin   DM                      dm;
254684b87d9SLisandro Dalcin   Vec                     locv;
255684b87d9SLisandro Dalcin   const char              *name;
256684b87d9SLisandro Dalcin   PetscSection            section;
257684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
258e630c359SToby Isaac   PetscInt                numFields;
259684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
260684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
261684b87d9SLisandro Dalcin 
262684b87d9SLisandro Dalcin   PetscFunctionBegin;
263684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
264684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
265684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
266684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
267684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
26892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
269e630c359SToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
270e630c359SToby Isaac   if (!numFields) {
271684b87d9SLisandro Dalcin     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
272e630c359SToby Isaac     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
273e630c359SToby Isaac   } else {
274e630c359SToby Isaac     PetscInt f;
275e630c359SToby Isaac 
276e630c359SToby Isaac     for (f = 0; f < numFields; f++) {
277e630c359SToby Isaac       ierr = DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);CHKERRQ(ierr);
278e630c359SToby Isaac       if (ft == PETSC_VTK_INVALID) continue;
279e630c359SToby Isaac       ierr = PetscObjectReference((PetscObject)locv);CHKERRQ(ierr);
280e630c359SToby Isaac       ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
281e630c359SToby Isaac     }
282e630c359SToby Isaac     ierr = VecDestroy(&locv);CHKERRQ(ierr);
283e630c359SToby Isaac   }
284684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
285684b87d9SLisandro Dalcin }
286684b87d9SLisandro Dalcin 
287552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
288552f7358SJed Brown {
289552f7358SJed Brown   DM             dm;
290684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
291552f7358SJed Brown   PetscErrorCode ierr;
292552f7358SJed Brown 
293552f7358SJed Brown   PetscFunctionBegin;
294552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
29582f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
296552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
297b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
298f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
2998135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
300684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
301684b87d9SLisandro Dalcin     PetscInt    i,numFields;
302684b87d9SLisandro Dalcin     PetscObject fe;
303ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
304684b87d9SLisandro Dalcin     Vec         locv = v;
305684b87d9SLisandro Dalcin     const char  *name;
306684b87d9SLisandro Dalcin     PetscInt    step;
307684b87d9SLisandro Dalcin     PetscReal   time;
308ef31f671SMatthew G. Knepley 
309ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
310684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
31144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &fe);CHKERRQ(ierr);
312684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
313ef31f671SMatthew G. Knepley     }
314684b87d9SLisandro Dalcin     if (fem) {
315684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
316684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
317684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
318684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
319684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
320684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
321ef31f671SMatthew G. Knepley     }
322552f7358SJed Brown     if (isvtk) {
323684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
324b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
325b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
326684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
327b136c2c9SMatthew G. Knepley #else
328b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
329b136c2c9SMatthew G. Knepley #endif
330f13a32a3SMatthew G. Knepley     } else if (isdraw) {
331684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
332684b87d9SLisandro Dalcin     } else if (isglvis) {
333684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
334684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
335684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
336684b87d9SLisandro Dalcin     }
337684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
338552f7358SJed Brown   } else {
339684b87d9SLisandro Dalcin     PetscBool isseq;
340684b87d9SLisandro Dalcin 
341684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
34255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
34355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
344552f7358SJed Brown   }
345552f7358SJed Brown   PetscFunctionReturn(0);
346552f7358SJed Brown }
347552f7358SJed Brown 
348552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
349552f7358SJed Brown {
350552f7358SJed Brown   DM             dm;
351684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
352552f7358SJed Brown   PetscErrorCode ierr;
353552f7358SJed Brown 
354552f7358SJed Brown   PetscFunctionBegin;
355552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
35682f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
357552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
35833c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
359e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3608135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
361684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
362552f7358SJed Brown     Vec         locv;
363552f7358SJed Brown     const char *name;
364552f7358SJed Brown 
365c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
366552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
367552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
368552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
369552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
370552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
371c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
372b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
373b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
37439d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
375b136c2c9SMatthew G. Knepley #else
376b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
377b136c2c9SMatthew G. Knepley #endif
378552f7358SJed Brown   } else {
379684b87d9SLisandro Dalcin     PetscBool isseq;
380684b87d9SLisandro Dalcin 
381684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
38255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
38355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
384552f7358SJed Brown   }
385552f7358SJed Brown   PetscFunctionReturn(0);
386552f7358SJed Brown }
387552f7358SJed Brown 
388d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
389d930f514SMatthew G. Knepley {
390d930f514SMatthew G. Knepley   DM                dm;
391d930f514SMatthew G. Knepley   MPI_Comm          comm;
392d930f514SMatthew G. Knepley   PetscViewerFormat format;
393d930f514SMatthew G. Knepley   Vec               v;
394d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
395d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
396d930f514SMatthew G. Knepley 
397d930f514SMatthew G. Knepley   PetscFunctionBegin;
398d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
399d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4002c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
401d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
402d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
403d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
404d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
405a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
406a8ad634aSStefano Zampini     /* this need a better fix */
407a8ad634aSStefano Zampini     if (dm->useNatural) {
408a8ad634aSStefano Zampini       if (dm->sfNatural) {
409d930f514SMatthew G. Knepley         const char *vecname;
410d930f514SMatthew G. Knepley         PetscInt    n, nroots;
411d930f514SMatthew G. Knepley 
412ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
413d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley         if (n == nroots) {
415d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
417d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
418d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
419d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
421d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
422a8ad634aSStefano Zampini     } else v = originalv;
423a8ad634aSStefano Zampini   } else v = originalv;
424a8ad634aSStefano Zampini 
425d930f514SMatthew G. Knepley   if (ishdf5) {
426d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
42739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley #else
429d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
430d930f514SMatthew G. Knepley #endif
431d930f514SMatthew G. Knepley   } else if (isvtk) {
432d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
433d930f514SMatthew G. Knepley   } else {
434d930f514SMatthew G. Knepley     PetscBool isseq;
435d930f514SMatthew G. Knepley 
436d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
437d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
438d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
439d930f514SMatthew G. Knepley   }
440a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
441d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
442d930f514SMatthew G. Knepley }
443d930f514SMatthew G. Knepley 
4442c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4452c40f234SMatthew G. Knepley {
4462c40f234SMatthew G. Knepley   DM             dm;
4472c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4482c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4492c40f234SMatthew G. Knepley 
4502c40f234SMatthew G. Knepley   PetscFunctionBegin;
4512c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4522c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4532c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4542c40f234SMatthew G. Knepley   if (ishdf5) {
4552c40f234SMatthew G. Knepley     DM          dmBC;
4562c40f234SMatthew G. Knepley     Vec         gv;
4572c40f234SMatthew G. Knepley     const char *name;
4582c40f234SMatthew G. Knepley 
4592c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4602c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4612c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4632c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4652c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4672c40f234SMatthew G. Knepley   } else {
4682c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4692c40f234SMatthew G. Knepley   }
4702c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4712c40f234SMatthew G. Knepley }
4722c40f234SMatthew G. Knepley 
4732c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4742c40f234SMatthew G. Knepley {
4752c40f234SMatthew G. Knepley   DM             dm;
4762c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4772c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4782c40f234SMatthew G. Knepley 
4792c40f234SMatthew G. Knepley   PetscFunctionBegin;
4802c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4812c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4822c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4832c40f234SMatthew G. Knepley   if (ishdf5) {
484878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
48539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
486b136c2c9SMatthew G. Knepley #else
487b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
488878b459fSMatthew G. Knepley #endif
4892c40f234SMatthew G. Knepley   } else {
4902c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
491552f7358SJed Brown   }
492552f7358SJed Brown   PetscFunctionReturn(0);
493552f7358SJed Brown }
494552f7358SJed Brown 
495d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
496d930f514SMatthew G. Knepley {
497d930f514SMatthew G. Knepley   DM                dm;
498d930f514SMatthew G. Knepley   PetscViewerFormat format;
499d930f514SMatthew G. Knepley   PetscBool         ishdf5;
500d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
501d930f514SMatthew G. Knepley 
502d930f514SMatthew G. Knepley   PetscFunctionBegin;
503d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
504d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
505d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
506d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
507d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
508a8ad634aSStefano Zampini     if (dm->useNatural) {
509d930f514SMatthew G. Knepley       if (dm->sfNatural) {
510d930f514SMatthew G. Knepley         if (ishdf5) {
511d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
512d930f514SMatthew G. Knepley           Vec         v;
513d930f514SMatthew G. Knepley           const char *vecname;
514d930f514SMatthew G. Knepley 
515d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
51839d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
519d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
520d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
521d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
522d930f514SMatthew G. Knepley #else
523d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
524d930f514SMatthew G. Knepley #endif
525d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
526d930f514SMatthew G. Knepley       }
527a8ad634aSStefano Zampini     } else {
528a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
529a8ad634aSStefano Zampini     }
530d930f514SMatthew G. Knepley   }
531d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
532d930f514SMatthew G. Knepley }
533d930f514SMatthew G. Knepley 
5347cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
535731e8ddeSMatthew G. Knepley {
536731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
537731e8ddeSMatthew G. Knepley   Vec                coordinates;
538ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
539731e8ddeSMatthew G. Knepley   const char        *name[4];
540731e8ddeSMatthew G. Knepley   const PetscScalar *a;
541731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
542731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
543731e8ddeSMatthew G. Knepley 
544731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
545731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
546731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
547731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
548731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
549ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
550731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
551731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
554731e8ddeSMatthew G. Knepley   name[1]     = "edge";
555731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
556731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
557731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
558731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
559ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
560731e8ddeSMatthew G. Knepley 
561ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
562ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
564731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
565731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
566731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
567731e8ddeSMatthew G. Knepley 
568731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
569731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
570731e8ddeSMatthew G. Knepley       if (!dof) continue;
571731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
572731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
573f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
575731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
577731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
578087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
579731e8ddeSMatthew G. Knepley         }
580731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
581731e8ddeSMatthew G. Knepley       }
582731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley     }
584731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
586731e8ddeSMatthew G. Knepley   }
587731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
588731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
589731e8ddeSMatthew G. Knepley }
590731e8ddeSMatthew G. Knepley 
5917cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
592552f7358SJed Brown {
593552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
594552f7358SJed Brown   DM                cdm;
595412e9a14SMatthew G. Knepley   DMLabel           markers, celltypes;
596552f7358SJed Brown   PetscSection      coordSection;
597552f7358SJed Brown   Vec               coordinates;
598552f7358SJed Brown   PetscViewerFormat format;
599552f7358SJed Brown   PetscErrorCode    ierr;
600552f7358SJed Brown 
601552f7358SJed Brown   PetscFunctionBegin;
602552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
60392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
604552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
605552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
606552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
607552f7358SJed Brown     const char *name;
608f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
609552f7358SJed Brown     PetscInt    pStart, pEnd, p;
610552f7358SJed Brown     PetscMPIInt rank, size;
611552f7358SJed Brown 
61282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
61382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
614552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
615552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
616552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
617f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
618f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
619f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
620f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
621f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
622e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6234d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
624e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
625552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
626552f7358SJed Brown       PetscInt dof, off, s;
627552f7358SJed Brown 
628552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
629552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
630552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
631e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
632552f7358SJed Brown       }
633552f7358SJed Brown     }
634552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
635e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
636e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
637552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
638552f7358SJed Brown       PetscInt dof, off, c;
639552f7358SJed Brown 
640552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
641552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
642552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
643e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
644552f7358SJed Brown       }
645552f7358SJed Brown     }
646552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6474d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6483d2e540fSStefano Zampini     if (coordSection && coordinates) {
64980180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6503d2e540fSStefano Zampini     }
651c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6527422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
653412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &celltypes);CHKERRQ(ierr);
654412e9a14SMatthew G. Knepley     if (celltypes) {ierr = DMLabelView(celltypes, viewer);CHKERRQ(ierr);}
655552f7358SJed Brown     if (size > 1) {
656552f7358SJed Brown       PetscSF sf;
657552f7358SJed Brown 
658552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
659552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
660552f7358SJed Brown     }
661552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
662552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6630588280cSMatthew G. Knepley     const char  *name, *color;
6640588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6650588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
666fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
667552f7358SJed Brown     PetscReal    scale         = 2.0;
66878081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6690588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6700588280cSMatthew G. Knepley     double       tcoords[3];
671552f7358SJed Brown     PetscScalar *coords;
6720588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
673552f7358SJed Brown     PetscMPIInt  rank, size;
6740588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
675fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
676fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
677fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
678552f7358SJed Brown 
6790588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
680552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
681c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6820588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6830588280cSMatthew G. Knepley     numColors  = 10;
6840588280cSMatthew G. Knepley     numLColors = 10;
6850588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
686c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
68778081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
688c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
689c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6900588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
691c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6920588280cSMatthew G. Knepley     if (!useColors) {
6930588280cSMatthew G. Knepley       numColors = 3;
6940588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6950588280cSMatthew G. Knepley     }
696c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6970588280cSMatthew G. Knepley     if (!useColors) {
6980588280cSMatthew G. Knepley       numLColors = 4;
6990588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7000588280cSMatthew G. Knepley     }
701589a23caSBarry Smith     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);CHKERRQ(ierr);
702202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
703202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
704202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
705202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
706fe1cc32dSStefano Zampini 
707fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
708fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
709fe1cc32dSStefano Zampini     if (lflg) {
710fe1cc32dSStefano Zampini       DMLabel lbl;
711fe1cc32dSStefano Zampini 
712fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
713fe1cc32dSStefano Zampini       if (lbl) {
714fe1cc32dSStefano Zampini         PetscInt val, defval;
715fe1cc32dSStefano Zampini 
716fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
717fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
718fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
719fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
720fe1cc32dSStefano Zampini           PetscInt  closureSize;
721fe1cc32dSStefano Zampini 
722fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
723fe1cc32dSStefano Zampini           if (val == defval) continue;
724fe1cc32dSStefano Zampini 
725fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
726fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
727fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
728fe1cc32dSStefano Zampini           }
729fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
730fe1cc32dSStefano Zampini         }
731fe1cc32dSStefano Zampini       }
732fe1cc32dSStefano Zampini     }
733fe1cc32dSStefano Zampini 
73482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
73582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
736552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
737770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7380588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
739552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
740552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
741552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7420588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7430588280cSMatthew G. Knepley     if (size > 1) {
744f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
745770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
746770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
747770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
748770b213bSMatthew G Knepley         } else if (p > 0) {
749770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
750770b213bSMatthew G Knepley         }
751770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
752770b213bSMatthew G Knepley       }
7530588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7540588280cSMatthew G. Knepley     }
755087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
756fe1cc32dSStefano Zampini 
757552f7358SJed Brown     /* Plot vertices */
758552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
759552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7604d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
761552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
762552f7358SJed Brown       PetscInt  off, dof, d;
7630588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
764552f7358SJed Brown 
765fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
766552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
767552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7680588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
769f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7700588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7710588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
772c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7730588280cSMatthew G. Knepley       }
7740588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7750588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
776552f7358SJed Brown       for (d = 0; d < dof; ++d) {
777552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
778087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
779552f7358SJed Brown       }
7800588280cSMatthew G. Knepley       color = colors[rank%numColors];
7810588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7820588280cSMatthew G. Knepley         PetscInt val;
783c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7840588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7850588280cSMatthew G. Knepley       }
7860588280cSMatthew G. Knepley       if (useNumbers) {
787e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7880588280cSMatthew G. Knepley       } else {
789e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7900588280cSMatthew G. Knepley       }
791552f7358SJed Brown     }
792552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
793552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
794846a3e8bSMatthew G. Knepley     /* Plot cells */
795846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
79678081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
797846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
798846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
799846a3e8bSMatthew G. Knepley         const PetscInt *cone;
800846a3e8bSMatthew G. Knepley 
801fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
802846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
803846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
804846a3e8bSMatthew G. Knepley           PetscInt val;
805846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
806846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
807846a3e8bSMatthew G. Knepley         }
808846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
809846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
810846a3e8bSMatthew G. Knepley       }
811846a3e8bSMatthew G. Knepley     } else {
812846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
813846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
814846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
815846a3e8bSMatthew G. Knepley 
816fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
817846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
818846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
819846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
820846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
821846a3e8bSMatthew G. Knepley 
822846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
823846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
824846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
825846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
826846a3e8bSMatthew G. Knepley         }
827846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
828846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
829846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
830846a3e8bSMatthew G. Knepley       }
831846a3e8bSMatthew G. Knepley     }
832846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
833846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
834846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
835846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
836846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
837846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
838846a3e8bSMatthew G. Knepley 
839fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
840846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
841846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
842846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
843846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
844846a3e8bSMatthew G. Knepley         PetscInt       off;
845846a3e8bSMatthew G. Knepley 
846846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
847846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
848846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
849846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
850846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
851846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
852846a3e8bSMatthew G. Knepley         }
853846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
854846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
855846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
856846a3e8bSMatthew G. Knepley         ++n;
857846a3e8bSMatthew G. Knepley       }
858846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
859846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
860846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
861846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
862087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
863846a3e8bSMatthew G. Knepley       }
864846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
865846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
866846a3e8bSMatthew G. Knepley         PetscInt val;
867846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
868846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
869846a3e8bSMatthew G. Knepley       }
870846a3e8bSMatthew G. Knepley       if (useNumbers) {
871846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
872846a3e8bSMatthew G. Knepley       } else {
873846a3e8bSMatthew 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);
874846a3e8bSMatthew G. Knepley       }
875846a3e8bSMatthew G. Knepley     }
876846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
877552f7358SJed Brown     /* Plot edges */
878202fd40aSStefano Zampini     if (plotEdges) {
879552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
880552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
881552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
882552f7358SJed Brown         const PetscInt *cone;
883552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
884552f7358SJed Brown 
885fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
886552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
887f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
888552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
889552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
890552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
891552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
892552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
893552f7358SJed Brown         for (d = 0; d < dof; ++d) {
894202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
895c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
896552f7358SJed Brown         }
8970588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
8980588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
8990588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9000588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
901f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9020588280cSMatthew G. Knepley         }
9030588280cSMatthew G. Knepley         color = colors[rank%numColors];
9040588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9050588280cSMatthew G. Knepley           PetscInt val;
906c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9070750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9080588280cSMatthew G. Knepley         }
909e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
910552f7358SJed Brown       }
911552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
912552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
913552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9140588280cSMatthew G. Knepley     }
915552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9164d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9170588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
918770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9190588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9200588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9210588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9220588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
923fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9240f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9250f7d6e4aSStefano Zampini     Vec                    cown,acown;
9260f7d6e4aSStefano Zampini     VecScatter             sct;
9270f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9280f7d6e4aSStefano Zampini     IS                     gid,acis;
9290f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9300f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9310f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9320f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9330f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9340f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9350f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9360f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
9370f7d6e4aSStefano Zampini 
9380f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
9390f7d6e4aSStefano Zampini     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
940b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
9410f7d6e4aSStefano Zampini     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRQ(ierr);
9420f7d6e4aSStefano Zampini #endif
9430f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
9440f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(comm,&ggroup);CHKERRQ(ierr);
9450f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRQ(ierr);
9460f7d6e4aSStefano Zampini       d1   = 0;
9470f7d6e4aSStefano Zampini       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRQ(ierr);
9480f7d6e4aSStefano Zampini       nid  = d2;
9490f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ggroup);CHKERRQ(ierr);
9500f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ngroup);CHKERRQ(ierr);
9510f7d6e4aSStefano Zampini       ierr = MPI_Comm_free(&ncomm);CHKERRQ(ierr);
9520f7d6e4aSStefano Zampini     } else nid = 0.0;
9530f7d6e4aSStefano Zampini 
9540f7d6e4aSStefano Zampini     /* Get connectivity */
9550f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
9560f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
9570f7d6e4aSStefano Zampini 
9580f7d6e4aSStefano Zampini     /* filter overlapped local cells */
9590f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
9600f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
9610f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
9620f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
9630f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
9640f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
9650f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
9660f7d6e4aSStefano Zampini     }
9670f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
9680f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
9690f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9700f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
9710f7d6e4aSStefano Zampini 
9720f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
9730f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
9740f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
9750f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
9760f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
9770f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
9780f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
9790f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
9800f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9810f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9820f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
9830f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
9840f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
9850f7d6e4aSStefano Zampini 
9860f7d6e4aSStefano Zampini     /* compute edgeCut */
9870f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
9880f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
9890f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
9900f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
9910f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9920f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
9930f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
9940f7d6e4aSStefano Zampini       PetscInt totl;
9950f7d6e4aSStefano Zampini 
9960f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
9970f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
9980f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
9990f7d6e4aSStefano Zampini         if (work[i] < 0) {
10000f7d6e4aSStefano Zampini           ect  += 1;
10010f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10020f7d6e4aSStefano Zampini         }
10030f7d6e4aSStefano Zampini       }
10040f7d6e4aSStefano Zampini     }
10050f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10060f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10070f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10080f7d6e4aSStefano Zampini     lm[1] = -numVertices;
10090f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRQ(ierr);
10100f7d6e4aSStefano 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);
10110f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10120f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10130f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
10140f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRQ(ierr);
10150f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1016b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
10170f7d6e4aSStefano 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);
10180f7d6e4aSStefano Zampini #else
10190f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10200f7d6e4aSStefano Zampini #endif
10210f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10220f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10230f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10240f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1025552f7358SJed Brown   } else {
1026412e9a14SMatthew G. Knepley     const char    *name;
1027d80ece95SMatthew G. Knepley     PetscInt      *sizes, *hybsizes, *ghostsizes;
1028412e9a14SMatthew G. Knepley     PetscInt       locDepth, depth, cellHeight, dim, d;
1029d80ece95SMatthew G. Knepley     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1030a57dd577SMatthew G Knepley     PetscInt       numLabels, l;
1031412e9a14SMatthew G. Knepley     DMPolytopeType ct0;
1032412e9a14SMatthew G. Knepley     MPI_Comm       comm;
1033412e9a14SMatthew G. Knepley     PetscMPIInt    size, rank;
1034552f7358SJed Brown 
103582f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
1036552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1037412e9a14SMatthew G. Knepley     ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1038c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1039f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10405f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1041f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1042f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1043f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1044552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1045b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
1046d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1047d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1048d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1049412e9a14SMatthew G. Knepley     for (d = 0; d <= depth; d++) {
1050412e9a14SMatthew G. Knepley       PetscInt Nc[2] = {0, 0}, ict;
1051412e9a14SMatthew G. Knepley 
1052552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1053412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, pStart, &ct0);CHKERRQ(ierr);
1054412e9a14SMatthew G. Knepley       ict  = ct0;
1055412e9a14SMatthew G. Knepley       ierr = MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1056412e9a14SMatthew G. Knepley       ct0  = (DMPolytopeType) ict;
1057412e9a14SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1058412e9a14SMatthew G. Knepley         DMPolytopeType ct;
1059412e9a14SMatthew G. Knepley 
1060412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
1061412e9a14SMatthew G. Knepley         if (ct == ct0) ++Nc[0];
1062412e9a14SMatthew G. Knepley         else           ++Nc[1];
1063412e9a14SMatthew G. Knepley       }
1064412e9a14SMatthew G. Knepley       ierr = MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1065412e9a14SMatthew G. Knepley       ierr = MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1066412e9a14SMatthew G. Knepley       if (d == depth) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);}
1067412e9a14SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1068834065abSMatthew G. Knepley       for (p = 0; p < size; ++p) {
1069cbb7f117SMark Adams         if (!rank) {
1070412e9a14SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);CHKERRQ(ierr);
1071412e9a14SMatthew G. Knepley           if (hybsizes[p]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);CHKERRQ(ierr);}
1072412e9a14SMatthew G. Knepley           if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1073834065abSMatthew G. Knepley         }
1074cbb7f117SMark Adams       }
1075552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1076552f7358SJed Brown     }
1077d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
1078c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1079a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1080a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1081a57dd577SMatthew G Knepley       DMLabel         label;
1082a57dd577SMatthew G Knepley       const char     *name;
1083a57dd577SMatthew G Knepley       IS              valueIS;
1084a57dd577SMatthew G Knepley       const PetscInt *values;
1085a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1086a57dd577SMatthew G Knepley 
1087c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1088c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1089a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1090d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1091a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1092a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1093120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1094a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1095a57dd577SMatthew G Knepley         PetscInt size;
1096a57dd577SMatthew G Knepley 
1097a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1098a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1099d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1100a57dd577SMatthew G Knepley       }
1101a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1102120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1103a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11044d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1105a57dd577SMatthew G Knepley     }
110634aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
110734aa8a36SMatthew G. Knepley     if (dm->Nf) {
110834aa8a36SMatthew G. Knepley       PetscInt f;
110934aa8a36SMatthew G. Knepley 
111034aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
111134aa8a36SMatthew G. Knepley         const char *name;
111234aa8a36SMatthew G. Knepley 
111334aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
111434aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
111534aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
111634aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
111734aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
111834aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1119ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
112034aa8a36SMatthew G. Knepley         } else {
112134aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
112234aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
112334aa8a36SMatthew G. Knepley         }
112434aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
112534aa8a36SMatthew G. Knepley       }
112634aa8a36SMatthew G. Knepley     }
1127a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11288e7ff633SMatthew G. Knepley     if (cdm) {
11298e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11308e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11318e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11328e7ff633SMatthew G. Knepley     }
1133552f7358SJed Brown   }
1134552f7358SJed Brown   PetscFunctionReturn(0);
1135552f7358SJed Brown }
1136552f7358SJed Brown 
1137e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1138e5c487bfSMatthew G. Knepley {
1139e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1140e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1141e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1142e5c487bfSMatthew G. Knepley 
1143e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1144e5c487bfSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1145e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1146e5c487bfSMatthew G. Knepley   switch (ct) {
1147e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1148e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1149e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1150e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1151e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1152e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1153e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1154e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1155e5c487bfSMatthew G. Knepley     break;
1156e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
1157e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1158e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1159e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1160e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1161e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1162e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1163e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1164e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1165e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1166e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1167e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1168e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1169e5c487bfSMatthew G. Knepley     break;
1170e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1171e5c487bfSMatthew G. Knepley   }
1172e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1173e5c487bfSMatthew G. Knepley }
1174e5c487bfSMatthew G. Knepley 
1175e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1176e5c487bfSMatthew G. Knepley {
1177e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1178e5c487bfSMatthew G. Knepley   PetscReal      centroid[2] = {0., 0.};
1179e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1180e5c487bfSMatthew G. Knepley   PetscInt       fillColor, v, e, d;
1181e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1182e5c487bfSMatthew G. Knepley 
1183e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1184e5c487bfSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1185e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1186e5c487bfSMatthew G. Knepley   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1187e5c487bfSMatthew G. Knepley   switch (ct) {
1188e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1189e5c487bfSMatthew G. Knepley     {
1190e5c487bfSMatthew G. Knepley       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
1191e5c487bfSMatthew G. Knepley 
1192e5c487bfSMatthew G. Knepley       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1193e5c487bfSMatthew G. Knepley       for (e = 0; e < 3; ++e) {
1194e5c487bfSMatthew G. Knepley         refCoords[0] = refVertices[e*2+0];
1195e5c487bfSMatthew G. Knepley         refCoords[1] = refVertices[e*2+1];
1196e5c487bfSMatthew G. Knepley         for (d = 1; d <= edgeDiv; ++d) {
1197e5c487bfSMatthew G. Knepley           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1198e5c487bfSMatthew G. Knepley           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1199e5c487bfSMatthew G. Knepley         }
1200e5c487bfSMatthew G. Knepley         ierr = DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);CHKERRQ(ierr);
1201e5c487bfSMatthew G. Knepley         for (d = 0; d < edgeDiv; ++d) {
1202e5c487bfSMatthew G. Knepley           ierr = PetscDrawTriangle(draw, centroid[0], centroid[1], edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], fillColor, fillColor, fillColor);CHKERRQ(ierr);
1203e5c487bfSMatthew G. Knepley           ierr = PetscDrawLine(draw, edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], PETSC_DRAW_BLACK);CHKERRQ(ierr);
1204e5c487bfSMatthew G. Knepley         }
1205e5c487bfSMatthew G. Knepley       }
1206e5c487bfSMatthew G. Knepley     }
1207e5c487bfSMatthew G. Knepley     break;
1208e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1209e5c487bfSMatthew G. Knepley   }
1210e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1211e5c487bfSMatthew G. Knepley }
1212e5c487bfSMatthew G. Knepley 
12137cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1214e412dcbdSMatthew G. Knepley {
1215e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1216e412dcbdSMatthew G. Knepley   DM                 cdm;
1217e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1218e412dcbdSMatthew G. Knepley   Vec                coordinates;
1219e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
122029494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1221e5c487bfSMatthew G. Knepley   PetscReal         *refCoords, *edgeCoords;
1222e5c487bfSMatthew G. Knepley   PetscBool          isnull, drawAffine = PETSC_TRUE;
1223e5c487bfSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1224e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1225e412dcbdSMatthew G. Knepley 
1226e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1227e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1228e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1229e5c487bfSMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);CHKERRQ(ierr);
1230e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);CHKERRQ(ierr);}
1231e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
123292fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1233e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1234e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1235e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1236e412dcbdSMatthew G. Knepley 
1237e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1238e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1239e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1240e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1241e412dcbdSMatthew G. Knepley 
1242e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1243e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1244e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
12450c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
12460c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1247e412dcbdSMatthew G. Knepley   }
1248e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
124929494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
125029494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
125129494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1252e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1253e412dcbdSMatthew G. Knepley 
1254cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1255cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1256ba2698f1SMatthew G. Knepley     PetscInt     numCoords;
1257cf3064d3SMatthew G. Knepley 
1258e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);CHKERRQ(ierr);
1259e5c487bfSMatthew G. Knepley     if (drawAffine) {
1260e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCell(dm, draw, c, coords);CHKERRQ(ierr);
1261e5c487bfSMatthew G. Knepley     } else {
1262e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);CHKERRQ(ierr);
1263cf3064d3SMatthew G. Knepley     }
1264cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1265cf3064d3SMatthew G. Knepley   }
1266e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscFree2(refCoords, edgeCoords);CHKERRQ(ierr);}
1267e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1268e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1269e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1270e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1271e412dcbdSMatthew G. Knepley }
1272e412dcbdSMatthew G. Knepley 
12731e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
12741e50132fSMatthew G. Knepley #include <exodusII.h>
12751e50132fSMatthew G. Knepley #endif
12761e50132fSMatthew G. Knepley 
1277552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1278552f7358SJed Brown {
12791e50132fSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1280002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1281552f7358SJed Brown   PetscErrorCode ierr;
1282552f7358SJed Brown 
1283552f7358SJed Brown   PetscFunctionBegin;
1284552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1285552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1286552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);CHKERRQ(ierr);
1287fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
1288c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
1289e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
12908135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
12911e50132fSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);CHKERRQ(ierr);
1292552f7358SJed Brown   if (iascii) {
12938135c375SStefano Zampini     PetscViewerFormat format;
12948135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12958135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
12968135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12978135c375SStefano Zampini     } else {
1298552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
12998135c375SStefano Zampini     }
1300c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1301c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
130239d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1303c6ccd67eSMatthew G. Knepley #else
1304c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1305552f7358SJed Brown #endif
1306e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1307fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1308e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1309e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
13108135c375SStefano Zampini   } else if (isglvis) {
13118135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13121e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
13131e50132fSMatthew G. Knepley   } else if (isexodus) {
13141e50132fSMatthew G. Knepley     int exoid;
13151e50132fSMatthew G. Knepley     PetscInt cStart, cEnd, c;
13161e50132fSMatthew G. Knepley 
13171e50132fSMatthew G. Knepley     ierr = DMCreateLabel(dm, "Cell Sets");CHKERRQ(ierr);
13181e50132fSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
13191e50132fSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {ierr = DMSetLabelValue(dm, "Cell Sets", c, 1);CHKERRQ(ierr);}
13201e50132fSMatthew G. Knepley     ierr = PetscViewerExodusIIGetId(viewer, &exoid);CHKERRQ(ierr);
13211e50132fSMatthew G. Knepley     ierr = DMPlexView_ExodusII_Internal(dm, exoid, 1);CHKERRQ(ierr);
13221e50132fSMatthew G. Knepley #endif
132362201deeSVaclav Hapla   } else {
1324a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1325fcf6c8fdSToby Isaac   }
1326cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1327cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1328cb3ba0daSMatthew G. Knepley   if (flg) {
1329cb3ba0daSMatthew G. Knepley     Vec ranks;
1330cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1331cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1332cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1333cb3ba0daSMatthew G. Knepley   }
1334002a2709SMatthew G. Knepley   /* Optionally view a label */
1335589a23caSBarry Smith   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);CHKERRQ(ierr);
1336002a2709SMatthew G. Knepley   if (flg) {
1337002a2709SMatthew G. Knepley     DMLabel label;
1338002a2709SMatthew G. Knepley     Vec     val;
1339002a2709SMatthew G. Knepley 
1340002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1341002a2709SMatthew 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);
1342002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1343002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1344002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1345002a2709SMatthew G. Knepley   }
1346552f7358SJed Brown   PetscFunctionReturn(0);
1347552f7358SJed Brown }
1348552f7358SJed Brown 
13492c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13502c40f234SMatthew G. Knepley {
1351d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13522c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13532c40f234SMatthew G. Knepley 
13542c40f234SMatthew G. Knepley   PetscFunctionBegin;
13552c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13562c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13572c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1358d4f5a9a0SVaclav Hapla   if (ishdf5) {
13592c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13609c48423bSVaclav Hapla     PetscViewerFormat format;
13619c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13629c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13639c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1364509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
136539d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1366509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
13672c40f234SMatthew G. Knepley #else
13682c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1369552f7358SJed Brown #endif
1370d4f5a9a0SVaclav Hapla   } else {
1371d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1372552f7358SJed Brown   }
1373552f7358SJed Brown   PetscFunctionReturn(0);
1374552f7358SJed Brown }
1375552f7358SJed Brown 
1376552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1377552f7358SJed Brown {
1378552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1379552f7358SJed Brown   PetscErrorCode ierr;
1380552f7358SJed Brown 
1381552f7358SJed Brown   PetscFunctionBegin;
13828135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13838135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
138428d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
13850d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1386552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1387552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1388552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1389552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1390be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1391552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1392552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1393d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1394d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
139577623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1396a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
139797d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
1398552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1399552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1400a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1401a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1402d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1403d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1404d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1405d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1406d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1407d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1408fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
14090a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
1410552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1411552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1412552f7358SJed Brown   PetscFunctionReturn(0);
1413552f7358SJed Brown }
1414552f7358SJed Brown 
1415b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1416552f7358SJed Brown {
14178d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1418acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1419552f7358SJed Brown   PetscInt               localSize;
1420837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1421552f7358SJed Brown   PetscErrorCode         ierr;
1422b412c318SBarry Smith   MatType                mtype;
14231428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1424552f7358SJed Brown 
1425552f7358SJed Brown   PetscFunctionBegin;
1426607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1427b412c318SBarry Smith   mtype = dm->mattype;
1428e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1429552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1430552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
143182f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1432552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1433552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1434552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1435acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1436acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1437552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1438552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1439552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1440552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1441552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1442552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1443552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1444837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1445552f7358SJed Brown   if (!isShell) {
1446be36d101SStefano Zampini     PetscSection subSection;
1447837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
14480be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1449fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1450552f7358SJed Brown 
145100140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1452be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1453be36d101SStefano Zampini       PetscSection section;
1454be36d101SStefano Zampini       PetscInt     size;
1455be36d101SStefano Zampini 
145692fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1457be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1458be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1459be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1460be36d101SStefano Zampini     } else {
146100140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1462be36d101SStefano Zampini     }
1463552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1464be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1465a9d99c84SMatthew G. Knepley       PetscInt bdof;
1466a9d99c84SMatthew G. Knepley 
1467552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1468fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
14691d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
14701d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
14711d17a0a3SMatthew G. Knepley       if (dof) {
14721d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1473be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1474be36d101SStefano Zampini       }
1475be36d101SStefano Zampini       if (isMatIS) {
1476be36d101SStefano Zampini         PetscInt loff,c,off;
1477be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1478be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1479be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1480552f7358SJed Brown       }
14812a28c762SMatthew G Knepley     }
14822a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14830be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14840be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14850be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14860be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14876fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
14886fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
14894fa26246SMatthew G. Knepley       PetscInt l;
14906fd5c86aSStefano Zampini 
14916fd5c86aSStefano Zampini       lsize = lsize/bs;
14926fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
14934fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1494be36d101SStefano Zampini     }
1495be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1496be36d101SStefano Zampini     if (isMatIS) {
1497be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1498be36d101SStefano Zampini     }
14991795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
15008d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1501552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1502552f7358SJed Brown   }
1503b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1504552f7358SJed Brown   PetscFunctionReturn(0);
1505552f7358SJed Brown }
1506552f7358SJed Brown 
15077cd05799SMatthew G. Knepley /*@
1508a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1509be36d101SStefano Zampini 
1510be36d101SStefano Zampini   Not collective
1511be36d101SStefano Zampini 
1512be36d101SStefano Zampini   Input Parameter:
1513be36d101SStefano Zampini . mesh - The DMPlex
1514be36d101SStefano Zampini 
1515be36d101SStefano Zampini   Output Parameters:
1516be36d101SStefano Zampini . subsection - The subdomain section
1517be36d101SStefano Zampini 
1518be36d101SStefano Zampini   Level: developer
1519be36d101SStefano Zampini 
1520be36d101SStefano Zampini .seealso:
15217cd05799SMatthew G. Knepley @*/
1522be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1523be36d101SStefano Zampini {
1524be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1525be36d101SStefano Zampini   PetscErrorCode ierr;
1526be36d101SStefano Zampini 
1527be36d101SStefano Zampini   PetscFunctionBegin;
1528be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1529be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1530be36d101SStefano Zampini     PetscSection section;
1531be36d101SStefano Zampini     PetscSF      sf;
1532be36d101SStefano Zampini 
1533be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
153492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1535be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1536be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1537be36d101SStefano Zampini   }
1538be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1539be36d101SStefano Zampini   PetscFunctionReturn(0);
1540be36d101SStefano Zampini }
1541be36d101SStefano Zampini 
1542552f7358SJed Brown /*@
1543552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1544552f7358SJed Brown 
1545552f7358SJed Brown   Not collective
1546552f7358SJed Brown 
1547552f7358SJed Brown   Input Parameter:
1548552f7358SJed Brown . mesh - The DMPlex
1549552f7358SJed Brown 
1550552f7358SJed Brown   Output Parameters:
1551552f7358SJed Brown + pStart - The first mesh point
1552552f7358SJed Brown - pEnd   - The upper bound for mesh points
1553552f7358SJed Brown 
1554552f7358SJed Brown   Level: beginner
1555552f7358SJed Brown 
1556552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1557552f7358SJed Brown @*/
1558552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1559552f7358SJed Brown {
1560552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1561552f7358SJed Brown   PetscErrorCode ierr;
1562552f7358SJed Brown 
1563552f7358SJed Brown   PetscFunctionBegin;
1564552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1565552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1566552f7358SJed Brown   PetscFunctionReturn(0);
1567552f7358SJed Brown }
1568552f7358SJed Brown 
1569552f7358SJed Brown /*@
1570552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1571552f7358SJed Brown 
1572552f7358SJed Brown   Not collective
1573552f7358SJed Brown 
1574552f7358SJed Brown   Input Parameters:
1575552f7358SJed Brown + mesh - The DMPlex
1576552f7358SJed Brown . pStart - The first mesh point
1577552f7358SJed Brown - pEnd   - The upper bound for mesh points
1578552f7358SJed Brown 
1579552f7358SJed Brown   Output Parameters:
1580552f7358SJed Brown 
1581552f7358SJed Brown   Level: beginner
1582552f7358SJed Brown 
1583552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1584552f7358SJed Brown @*/
1585552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1586552f7358SJed Brown {
1587552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1588552f7358SJed Brown   PetscErrorCode ierr;
1589552f7358SJed Brown 
1590552f7358SJed Brown   PetscFunctionBegin;
1591552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1592552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1593552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1594552f7358SJed Brown   PetscFunctionReturn(0);
1595552f7358SJed Brown }
1596552f7358SJed Brown 
1597552f7358SJed Brown /*@
1598eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1599552f7358SJed Brown 
1600552f7358SJed Brown   Not collective
1601552f7358SJed Brown 
1602552f7358SJed Brown   Input Parameters:
1603552f7358SJed Brown + mesh - The DMPlex
1604eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1605552f7358SJed Brown 
1606552f7358SJed Brown   Output Parameter:
1607552f7358SJed Brown . size - The cone size for point p
1608552f7358SJed Brown 
1609552f7358SJed Brown   Level: beginner
1610552f7358SJed Brown 
1611552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1612552f7358SJed Brown @*/
1613552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1614552f7358SJed Brown {
1615552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1616552f7358SJed Brown   PetscErrorCode ierr;
1617552f7358SJed Brown 
1618552f7358SJed Brown   PetscFunctionBegin;
1619552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1620552f7358SJed Brown   PetscValidPointer(size, 3);
1621552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1622552f7358SJed Brown   PetscFunctionReturn(0);
1623552f7358SJed Brown }
1624552f7358SJed Brown 
1625552f7358SJed Brown /*@
1626eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1627552f7358SJed Brown 
1628552f7358SJed Brown   Not collective
1629552f7358SJed Brown 
1630552f7358SJed Brown   Input Parameters:
1631552f7358SJed Brown + mesh - The DMPlex
1632eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1633552f7358SJed Brown - size - The cone size for point p
1634552f7358SJed Brown 
1635552f7358SJed Brown   Output Parameter:
1636552f7358SJed Brown 
1637552f7358SJed Brown   Note:
1638552f7358SJed Brown   This should be called after DMPlexSetChart().
1639552f7358SJed Brown 
1640552f7358SJed Brown   Level: beginner
1641552f7358SJed Brown 
1642552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1643552f7358SJed Brown @*/
1644552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1645552f7358SJed Brown {
1646552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1647552f7358SJed Brown   PetscErrorCode ierr;
1648552f7358SJed Brown 
1649552f7358SJed Brown   PetscFunctionBegin;
1650552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1651552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
16520d644c17SKarl Rupp 
1653552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1654552f7358SJed Brown   PetscFunctionReturn(0);
1655552f7358SJed Brown }
1656552f7358SJed Brown 
1657f5a469b9SMatthew G. Knepley /*@
1658eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1659f5a469b9SMatthew G. Knepley 
1660f5a469b9SMatthew G. Knepley   Not collective
1661f5a469b9SMatthew G. Knepley 
1662f5a469b9SMatthew G. Knepley   Input Parameters:
1663f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1664eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1665f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1666f5a469b9SMatthew G. Knepley 
1667f5a469b9SMatthew G. Knepley   Output Parameter:
1668f5a469b9SMatthew G. Knepley 
1669f5a469b9SMatthew G. Knepley   Note:
1670f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1671f5a469b9SMatthew G. Knepley 
1672f5a469b9SMatthew G. Knepley   Level: beginner
1673f5a469b9SMatthew G. Knepley 
1674f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1675f5a469b9SMatthew G. Knepley @*/
1676f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1677f5a469b9SMatthew G. Knepley {
1678f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1679f5a469b9SMatthew G. Knepley   PetscInt       csize;
1680f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1681f5a469b9SMatthew G. Knepley 
1682f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1683f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1684f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1685f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1686f5a469b9SMatthew G. Knepley 
1687f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1688f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1689f5a469b9SMatthew G. Knepley }
1690f5a469b9SMatthew G. Knepley 
1691552f7358SJed Brown /*@C
1692eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1693552f7358SJed Brown 
1694552f7358SJed Brown   Not collective
1695552f7358SJed Brown 
1696552f7358SJed Brown   Input Parameters:
1697833c876bSVaclav Hapla + dm - The DMPlex
1698eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1699552f7358SJed Brown 
1700552f7358SJed Brown   Output Parameter:
1701552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1702552f7358SJed Brown 
1703552f7358SJed Brown   Level: beginner
1704552f7358SJed Brown 
17053813dfbdSMatthew G Knepley   Fortran Notes:
17063813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17073813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1708922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1709922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
17103813dfbdSMatthew G Knepley 
17110ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1712552f7358SJed Brown @*/
1713552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1714552f7358SJed Brown {
1715552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1716552f7358SJed Brown   PetscInt       off;
1717552f7358SJed Brown   PetscErrorCode ierr;
1718552f7358SJed Brown 
1719552f7358SJed Brown   PetscFunctionBegin;
1720552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1721552f7358SJed Brown   PetscValidPointer(cone, 3);
1722552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1723552f7358SJed Brown   *cone = &mesh->cones[off];
1724552f7358SJed Brown   PetscFunctionReturn(0);
1725552f7358SJed Brown }
1726552f7358SJed Brown 
17270ce7577fSVaclav Hapla /*@C
17280ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
17290ce7577fSVaclav Hapla 
17300ce7577fSVaclav Hapla   Not collective
17310ce7577fSVaclav Hapla 
17320ce7577fSVaclav Hapla   Input Parameters:
17330ce7577fSVaclav Hapla + dm - The DMPlex
17340ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
17350ce7577fSVaclav Hapla 
17360ce7577fSVaclav Hapla   Output Parameter:
17370ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
17380ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
17390ce7577fSVaclav Hapla 
17400ce7577fSVaclav Hapla   Level: intermediate
17410ce7577fSVaclav Hapla 
1742d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
17430ce7577fSVaclav Hapla @*/
17440ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
17450ce7577fSVaclav Hapla {
17460ce7577fSVaclav Hapla   PetscSection        cs, newcs;
17470ce7577fSVaclav Hapla   PetscInt            *cones;
17480ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
17490ce7577fSVaclav Hapla   PetscInt            n;
17500ce7577fSVaclav Hapla   PetscErrorCode      ierr;
17510ce7577fSVaclav Hapla 
17520ce7577fSVaclav Hapla   PetscFunctionBegin;
17530ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
17540ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
17550ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
17560ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
17570ce7577fSVaclav Hapla   if (pCones) {
17580ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
17590ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
17600ce7577fSVaclav Hapla   }
17610ce7577fSVaclav Hapla   PetscFunctionReturn(0);
17620ce7577fSVaclav Hapla }
17630ce7577fSVaclav Hapla 
1764af9eab45SVaclav Hapla /*@
1765af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1766d4636a37SVaclav Hapla 
1767d4636a37SVaclav Hapla   Not collective
1768d4636a37SVaclav Hapla 
1769d4636a37SVaclav Hapla   Input Parameters:
1770d4636a37SVaclav Hapla + dm - The DMPlex
1771af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1772d4636a37SVaclav Hapla 
1773d4636a37SVaclav Hapla   Output Parameter:
1774af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1775d4636a37SVaclav Hapla 
1776d4636a37SVaclav Hapla   Level: advanced
1777d4636a37SVaclav Hapla 
1778af9eab45SVaclav Hapla   Notes:
1779af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1780af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1781af9eab45SVaclav Hapla 
1782af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1783d4636a37SVaclav Hapla @*/
1784af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1785d4636a37SVaclav Hapla {
1786af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1787af9eab45SVaclav Hapla   PetscInt            depth;
1788d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1789d4636a37SVaclav Hapla 
1790d4636a37SVaclav Hapla   PetscFunctionBegin;
1791af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1792af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1793af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1794af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1795af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1796af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1797af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1798af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1799af9eab45SVaclav Hapla }
1800af9eab45SVaclav Hapla 
1801af9eab45SVaclav Hapla /*@
1802af9eab45SVaclav 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).
1803af9eab45SVaclav Hapla 
1804af9eab45SVaclav Hapla   Not collective
1805af9eab45SVaclav Hapla 
1806af9eab45SVaclav Hapla   Input Parameters:
1807af9eab45SVaclav Hapla + dm - The DMPlex
1808af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1809af9eab45SVaclav Hapla 
1810af9eab45SVaclav Hapla   Output Parameter:
1811af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1812af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1813af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1814af9eab45SVaclav Hapla 
1815af9eab45SVaclav Hapla   Level: advanced
1816af9eab45SVaclav Hapla 
1817af9eab45SVaclav Hapla   Notes:
1818af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1819af9eab45SVaclav Hapla 
1820af9eab45SVaclav 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.
1821af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1822af9eab45SVaclav Hapla 
1823af9eab45SVaclav 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:
1824af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1825af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1826af9eab45SVaclav Hapla 
1827af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1828af9eab45SVaclav Hapla @*/
1829af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1830af9eab45SVaclav Hapla {
1831af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1832af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1833af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1834af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1835af9eab45SVaclav Hapla   PetscSection        *sections_;
1836af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1837af9eab45SVaclav Hapla 
1838af9eab45SVaclav Hapla   PetscFunctionBegin;
1839af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1840af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1841af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1842af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1843af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1844af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1845af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1846af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1847af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1848af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1849af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1850af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1851af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1852af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1853af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1854af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
1855af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
1856af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
1857af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
1858af9eab45SVaclav Hapla       } else {
1859af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
1860af9eab45SVaclav Hapla       }
1861af9eab45SVaclav Hapla     }
1862af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
1863af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
1864af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
1865af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1866af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
1867af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
1868af9eab45SVaclav Hapla       if (cn > 1) {
1869af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
1870af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
1871af9eab45SVaclav Hapla       } else {
1872af9eab45SVaclav Hapla         newarr[co] = arr[i];
1873af9eab45SVaclav Hapla       }
1874af9eab45SVaclav Hapla     }
1875af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
1876af9eab45SVaclav Hapla     arr = newarr;
1877af9eab45SVaclav Hapla     n = newn;
1878af9eab45SVaclav Hapla   }
1879ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
1880af9eab45SVaclav Hapla   *depth = depth_;
1881af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
1882af9eab45SVaclav Hapla   else {
1883af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
1884af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
1885af9eab45SVaclav Hapla   }
1886af9eab45SVaclav Hapla   if (sections) *sections = sections_;
1887af9eab45SVaclav Hapla   else {
1888af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
1889af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
1890af9eab45SVaclav Hapla   }
1891af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1892af9eab45SVaclav Hapla }
1893af9eab45SVaclav Hapla 
1894af9eab45SVaclav Hapla /*@
1895af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
1896af9eab45SVaclav Hapla 
1897af9eab45SVaclav Hapla   Not collective
1898af9eab45SVaclav Hapla 
1899af9eab45SVaclav Hapla   Input Parameters:
1900af9eab45SVaclav Hapla + dm - The DMPlex
1901af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1902af9eab45SVaclav Hapla 
1903af9eab45SVaclav Hapla   Output Parameter:
1904af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1905af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
1906af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1907af9eab45SVaclav Hapla 
1908af9eab45SVaclav Hapla   Level: advanced
1909af9eab45SVaclav Hapla 
1910af9eab45SVaclav Hapla   Notes:
1911af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
1912af9eab45SVaclav Hapla 
1913af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1914af9eab45SVaclav Hapla @*/
1915af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1916af9eab45SVaclav Hapla {
1917af9eab45SVaclav Hapla   PetscInt            d, depth_;
1918af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1919af9eab45SVaclav Hapla 
1920af9eab45SVaclav Hapla   PetscFunctionBegin;
1921af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1922af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1923af9eab45SVaclav Hapla   if (depth) *depth = 0;
1924af9eab45SVaclav Hapla   if (expandedPoints) {
1925af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
1926af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
1927af9eab45SVaclav Hapla   }
1928af9eab45SVaclav Hapla   if (sections)  {
1929af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
1930af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
1931af9eab45SVaclav Hapla   }
1932d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1933d4636a37SVaclav Hapla }
1934d4636a37SVaclav Hapla 
1935552f7358SJed Brown /*@
193692371b87SBarry 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
1937552f7358SJed Brown 
1938552f7358SJed Brown   Not collective
1939552f7358SJed Brown 
1940552f7358SJed Brown   Input Parameters:
1941552f7358SJed Brown + mesh - The DMPlex
1942eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1943552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1944552f7358SJed Brown 
1945552f7358SJed Brown   Output Parameter:
1946552f7358SJed Brown 
1947552f7358SJed Brown   Note:
1948552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1949552f7358SJed Brown 
195092371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
195192371b87SBarry Smith 
1952552f7358SJed Brown   Level: beginner
1953552f7358SJed Brown 
195492371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1955552f7358SJed Brown @*/
1956552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1957552f7358SJed Brown {
1958552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1959552f7358SJed Brown   PetscInt       pStart, pEnd;
1960552f7358SJed Brown   PetscInt       dof, off, c;
1961552f7358SJed Brown   PetscErrorCode ierr;
1962552f7358SJed Brown 
1963552f7358SJed Brown   PetscFunctionBegin;
1964552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1965552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1966552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1967552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1968552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
196982f516ccSBarry 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);
1970552f7358SJed Brown   for (c = 0; c < dof; ++c) {
197182f516ccSBarry 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);
1972552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1973552f7358SJed Brown   }
1974552f7358SJed Brown   PetscFunctionReturn(0);
1975552f7358SJed Brown }
1976552f7358SJed Brown 
1977552f7358SJed Brown /*@C
1978eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1979552f7358SJed Brown 
1980552f7358SJed Brown   Not collective
1981552f7358SJed Brown 
1982552f7358SJed Brown   Input Parameters:
1983552f7358SJed Brown + mesh - The DMPlex
1984eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1985552f7358SJed Brown 
1986552f7358SJed Brown   Output Parameter:
1987552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1988552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1989552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1990552f7358SJed Brown                     the index of the cone point on which to start.
1991552f7358SJed Brown 
1992552f7358SJed Brown   Level: beginner
1993552f7358SJed Brown 
19943813dfbdSMatthew G Knepley   Fortran Notes:
19953813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19963813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19973b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1998922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
19993813dfbdSMatthew G Knepley 
2000552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2001552f7358SJed Brown @*/
2002552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2003552f7358SJed Brown {
2004552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2005552f7358SJed Brown   PetscInt       off;
2006552f7358SJed Brown   PetscErrorCode ierr;
2007552f7358SJed Brown 
2008552f7358SJed Brown   PetscFunctionBegin;
2009552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
201076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2011552f7358SJed Brown     PetscInt dof;
2012552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2013552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2014552f7358SJed Brown   }
2015552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
20160d644c17SKarl Rupp 
2017552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2018552f7358SJed Brown   PetscFunctionReturn(0);
2019552f7358SJed Brown }
2020552f7358SJed Brown 
2021552f7358SJed Brown /*@
2022eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2023552f7358SJed Brown 
2024552f7358SJed Brown   Not collective
2025552f7358SJed Brown 
2026552f7358SJed Brown   Input Parameters:
2027552f7358SJed Brown + mesh - The DMPlex
2028eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2029552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2030552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2031552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2032552f7358SJed Brown                     the index of the cone point on which to start.
2033552f7358SJed Brown 
2034552f7358SJed Brown   Output Parameter:
2035552f7358SJed Brown 
2036552f7358SJed Brown   Note:
2037552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2038552f7358SJed Brown 
2039552f7358SJed Brown   Level: beginner
2040552f7358SJed Brown 
2041552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2042552f7358SJed Brown @*/
2043552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2044552f7358SJed Brown {
2045552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2046552f7358SJed Brown   PetscInt       pStart, pEnd;
2047552f7358SJed Brown   PetscInt       dof, off, c;
2048552f7358SJed Brown   PetscErrorCode ierr;
2049552f7358SJed Brown 
2050552f7358SJed Brown   PetscFunctionBegin;
2051552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2052552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2053552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2054552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2055552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
205682f516ccSBarry 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);
2057552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2058552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2059552f7358SJed Brown 
2060552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
206182f516ccSBarry 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);
2062552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2063552f7358SJed Brown   }
2064552f7358SJed Brown   PetscFunctionReturn(0);
2065552f7358SJed Brown }
2066552f7358SJed Brown 
20677cd05799SMatthew G. Knepley /*@
2068eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
20697cd05799SMatthew G. Knepley 
20707cd05799SMatthew G. Knepley   Not collective
20717cd05799SMatthew G. Knepley 
20727cd05799SMatthew G. Knepley   Input Parameters:
20737cd05799SMatthew G. Knepley + mesh - The DMPlex
2074eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20757cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20767cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
20777cd05799SMatthew G. Knepley 
20787cd05799SMatthew G. Knepley   Level: beginner
20797cd05799SMatthew G. Knepley 
20807cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20817cd05799SMatthew G. Knepley @*/
2082552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2083552f7358SJed Brown {
2084552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2085552f7358SJed Brown   PetscInt       pStart, pEnd;
2086552f7358SJed Brown   PetscInt       dof, off;
2087552f7358SJed Brown   PetscErrorCode ierr;
2088552f7358SJed Brown 
2089552f7358SJed Brown   PetscFunctionBegin;
2090552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2091552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
209282f516ccSBarry 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);
209382f516ccSBarry 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);
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);
2097552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2098552f7358SJed Brown   PetscFunctionReturn(0);
2099552f7358SJed Brown }
2100552f7358SJed Brown 
21017cd05799SMatthew G. Knepley /*@
2102eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
21037cd05799SMatthew G. Knepley 
21047cd05799SMatthew G. Knepley   Not collective
21057cd05799SMatthew G. Knepley 
21067cd05799SMatthew G. Knepley   Input Parameters:
21077cd05799SMatthew G. Knepley + mesh - The DMPlex
2108eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
21097cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
21107cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
21117cd05799SMatthew G. Knepley 
21127cd05799SMatthew G. Knepley   Level: beginner
21137cd05799SMatthew G. Knepley 
21147cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
21157cd05799SMatthew G. Knepley @*/
211677c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
211777c88f5bSMatthew G Knepley {
211877c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
211977c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
212077c88f5bSMatthew G Knepley   PetscInt       dof, off;
212177c88f5bSMatthew G Knepley   PetscErrorCode ierr;
212277c88f5bSMatthew G Knepley 
212377c88f5bSMatthew G Knepley   PetscFunctionBegin;
212477c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
212577c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
212677c88f5bSMatthew 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);
212777c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
212877c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
212977c88f5bSMatthew 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);
213077c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
213177c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
213277c88f5bSMatthew G Knepley }
213377c88f5bSMatthew G Knepley 
2134552f7358SJed Brown /*@
2135eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2136552f7358SJed Brown 
2137552f7358SJed Brown   Not collective
2138552f7358SJed Brown 
2139552f7358SJed Brown   Input Parameters:
2140552f7358SJed Brown + mesh - The DMPlex
2141eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2142552f7358SJed Brown 
2143552f7358SJed Brown   Output Parameter:
2144552f7358SJed Brown . size - The support size for point p
2145552f7358SJed Brown 
2146552f7358SJed Brown   Level: beginner
2147552f7358SJed Brown 
2148552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2149552f7358SJed Brown @*/
2150552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2151552f7358SJed Brown {
2152552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2153552f7358SJed Brown   PetscErrorCode ierr;
2154552f7358SJed Brown 
2155552f7358SJed Brown   PetscFunctionBegin;
2156552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2157552f7358SJed Brown   PetscValidPointer(size, 3);
2158552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2159552f7358SJed Brown   PetscFunctionReturn(0);
2160552f7358SJed Brown }
2161552f7358SJed Brown 
2162552f7358SJed Brown /*@
2163eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2164552f7358SJed Brown 
2165552f7358SJed Brown   Not collective
2166552f7358SJed Brown 
2167552f7358SJed Brown   Input Parameters:
2168552f7358SJed Brown + mesh - The DMPlex
2169eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2170552f7358SJed Brown - size - The support size for point p
2171552f7358SJed Brown 
2172552f7358SJed Brown   Output Parameter:
2173552f7358SJed Brown 
2174552f7358SJed Brown   Note:
2175552f7358SJed Brown   This should be called after DMPlexSetChart().
2176552f7358SJed Brown 
2177552f7358SJed Brown   Level: beginner
2178552f7358SJed Brown 
2179552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2180552f7358SJed Brown @*/
2181552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2182552f7358SJed Brown {
2183552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2184552f7358SJed Brown   PetscErrorCode ierr;
2185552f7358SJed Brown 
2186552f7358SJed Brown   PetscFunctionBegin;
2187552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2188552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
21890d644c17SKarl Rupp 
2190552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2191552f7358SJed Brown   PetscFunctionReturn(0);
2192552f7358SJed Brown }
2193552f7358SJed Brown 
2194552f7358SJed Brown /*@C
2195eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2196552f7358SJed Brown 
2197552f7358SJed Brown   Not collective
2198552f7358SJed Brown 
2199552f7358SJed Brown   Input Parameters:
2200552f7358SJed Brown + mesh - The DMPlex
2201eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2202552f7358SJed Brown 
2203552f7358SJed Brown   Output Parameter:
2204552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2205552f7358SJed Brown 
2206552f7358SJed Brown   Level: beginner
2207552f7358SJed Brown 
22083813dfbdSMatthew G Knepley   Fortran Notes:
22093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22113b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2212922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
22133813dfbdSMatthew G Knepley 
2214552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2215552f7358SJed Brown @*/
2216552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2217552f7358SJed Brown {
2218552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2219552f7358SJed Brown   PetscInt       off;
2220552f7358SJed Brown   PetscErrorCode ierr;
2221552f7358SJed Brown 
2222552f7358SJed Brown   PetscFunctionBegin;
2223552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2224552f7358SJed Brown   PetscValidPointer(support, 3);
2225552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2226552f7358SJed Brown   *support = &mesh->supports[off];
2227552f7358SJed Brown   PetscFunctionReturn(0);
2228552f7358SJed Brown }
2229552f7358SJed Brown 
2230552f7358SJed Brown /*@
223192371b87SBarry 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
2232552f7358SJed Brown 
2233552f7358SJed Brown   Not collective
2234552f7358SJed Brown 
2235552f7358SJed Brown   Input Parameters:
2236552f7358SJed Brown + mesh - The DMPlex
2237eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
223892371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2239552f7358SJed Brown 
2240552f7358SJed Brown   Output Parameter:
2241552f7358SJed Brown 
2242552f7358SJed Brown   Note:
2243552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2244552f7358SJed Brown 
2245552f7358SJed Brown   Level: beginner
2246552f7358SJed Brown 
224792371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2248552f7358SJed Brown @*/
2249552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2250552f7358SJed Brown {
2251552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2252552f7358SJed Brown   PetscInt       pStart, pEnd;
2253552f7358SJed Brown   PetscInt       dof, off, c;
2254552f7358SJed Brown   PetscErrorCode ierr;
2255552f7358SJed Brown 
2256552f7358SJed Brown   PetscFunctionBegin;
2257552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2258552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2259552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2260552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2261552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
226282f516ccSBarry 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);
2263552f7358SJed Brown   for (c = 0; c < dof; ++c) {
226482f516ccSBarry 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);
2265552f7358SJed Brown     mesh->supports[off+c] = support[c];
2266552f7358SJed Brown   }
2267552f7358SJed Brown   PetscFunctionReturn(0);
2268552f7358SJed Brown }
2269552f7358SJed Brown 
22707cd05799SMatthew G. Knepley /*@
2271eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
22727cd05799SMatthew G. Knepley 
22737cd05799SMatthew G. Knepley   Not collective
22747cd05799SMatthew G. Knepley 
22757cd05799SMatthew G. Knepley   Input Parameters:
22767cd05799SMatthew G. Knepley + mesh - The DMPlex
2277eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22787cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
22797cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
22807cd05799SMatthew G. Knepley 
22817cd05799SMatthew G. Knepley   Level: beginner
22827cd05799SMatthew G. Knepley 
22837cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22847cd05799SMatthew G. Knepley @*/
2285552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2286552f7358SJed Brown {
2287552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2288552f7358SJed Brown   PetscInt       pStart, pEnd;
2289552f7358SJed Brown   PetscInt       dof, off;
2290552f7358SJed Brown   PetscErrorCode ierr;
2291552f7358SJed Brown 
2292552f7358SJed Brown   PetscFunctionBegin;
2293552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2294552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2295552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2296552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
229782f516ccSBarry 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);
229882f516ccSBarry 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);
229982f516ccSBarry 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);
2300552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2301552f7358SJed Brown   PetscFunctionReturn(0);
2302552f7358SJed Brown }
2303552f7358SJed Brown 
2304552f7358SJed Brown /*@C
2305eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2306552f7358SJed Brown 
2307552f7358SJed Brown   Not collective
2308552f7358SJed Brown 
2309552f7358SJed Brown   Input Parameters:
2310552f7358SJed Brown + mesh - The DMPlex
2311eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2312552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
23130298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2314552f7358SJed Brown 
2315552f7358SJed Brown   Output Parameters:
2316552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2317552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2318552f7358SJed Brown 
2319552f7358SJed Brown   Note:
23200298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2321552f7358SJed Brown 
23223813dfbdSMatthew G Knepley   Fortran Notes:
23233813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23243813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23253813dfbdSMatthew G Knepley 
23263813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23273813dfbdSMatthew G Knepley 
2328552f7358SJed Brown   Level: beginner
2329552f7358SJed Brown 
2330552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2331552f7358SJed Brown @*/
2332552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2333552f7358SJed Brown {
2334552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2335552f7358SJed Brown   PetscInt       *closure, *fifo;
23360298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2337552f7358SJed Brown   PetscInt        tmpSize, t;
2338552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2339552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2340552f7358SJed Brown   PetscErrorCode  ierr;
2341552f7358SJed Brown 
2342552f7358SJed Brown   PetscFunctionBegin;
2343552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2344552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2345552f7358SJed Brown   /* This is only 1-level */
2346552f7358SJed Brown   if (useCone) {
2347552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2348552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2349552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2350552f7358SJed Brown   } else {
2351552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2352552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2353552f7358SJed Brown   }
2354bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2355bfbcdd7aSMatthew G. Knepley     if (*points) {
2356bfbcdd7aSMatthew G. Knepley       closure = *points;
2357bfbcdd7aSMatthew G. Knepley     } else {
2358bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
235969291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2360bfbcdd7aSMatthew G. Knepley     }
2361bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2362bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2363bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2364bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2365bfbcdd7aSMatthew G. Knepley     }
2366bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2367bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2368bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2369bfbcdd7aSMatthew G. Knepley   }
237024c766afSToby Isaac   {
237124c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
237224c766afSToby Isaac 
237324c766afSToby Isaac     c = mesh->maxConeSize;
237424c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
237524c766afSToby Isaac     s = mesh->maxSupportSize;
237624c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
237724c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
237824c766afSToby Isaac   }
237969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2380bfbcdd7aSMatthew G. Knepley   if (*points) {
2381bfbcdd7aSMatthew G. Knepley     closure = *points;
2382bfbcdd7aSMatthew G. Knepley   } else {
238369291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2384bfbcdd7aSMatthew G. Knepley   }
2385bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2386552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2387552f7358SJed Brown     const PetscInt cp = tmp[t];
2388552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2389552f7358SJed Brown 
2390552f7358SJed Brown     closure[closureSize]   = cp;
2391552f7358SJed Brown     closure[closureSize+1] = co;
2392552f7358SJed Brown     fifo[fifoSize]         = cp;
2393552f7358SJed Brown     fifo[fifoSize+1]       = co;
2394552f7358SJed Brown   }
2395bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2396552f7358SJed Brown   while (fifoSize - fifoStart) {
2397552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2398552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2399552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2400552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2401552f7358SJed Brown 
2402552f7358SJed Brown     if (useCone) {
2403552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2404552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2405552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2406552f7358SJed Brown     } else {
2407552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2408552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
24090298fd71SBarry Smith       tmpO = NULL;
2410552f7358SJed Brown     }
2411552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2412552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2413552f7358SJed Brown       const PetscInt cp = tmp[i];
24142e1b13c2SMatthew 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. */
24152e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
24162e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
24172e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2418552f7358SJed Brown       PetscInt       c;
2419552f7358SJed Brown 
24202e1b13c2SMatthew G. Knepley       if (rev) {
24212e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
24222e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
24232e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
24242e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
24252e1b13c2SMatthew G. Knepley       }
2426552f7358SJed Brown       /* Check for duplicate */
2427552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2428552f7358SJed Brown         if (closure[c] == cp) break;
2429552f7358SJed Brown       }
2430552f7358SJed Brown       if (c == closureSize) {
2431552f7358SJed Brown         closure[closureSize]   = cp;
2432552f7358SJed Brown         closure[closureSize+1] = co;
2433552f7358SJed Brown         fifo[fifoSize]         = cp;
2434552f7358SJed Brown         fifo[fifoSize+1]       = co;
2435552f7358SJed Brown         closureSize           += 2;
2436552f7358SJed Brown         fifoSize              += 2;
2437552f7358SJed Brown       }
2438552f7358SJed Brown     }
2439552f7358SJed Brown     fifoStart += 2;
2440552f7358SJed Brown   }
2441552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2442552f7358SJed Brown   if (points)    *points    = closure;
244369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2444552f7358SJed Brown   PetscFunctionReturn(0);
2445552f7358SJed Brown }
2446552f7358SJed Brown 
24479bf0dad6SMatthew G. Knepley /*@C
2448eaf898f9SPatrick 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
24499bf0dad6SMatthew G. Knepley 
24509bf0dad6SMatthew G. Knepley   Not collective
24519bf0dad6SMatthew G. Knepley 
24529bf0dad6SMatthew G. Knepley   Input Parameters:
24539bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2454eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24559bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
24569bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24579bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
24589bf0dad6SMatthew G. Knepley 
24599bf0dad6SMatthew G. Knepley   Output Parameters:
24609bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
24619bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
24629bf0dad6SMatthew G. Knepley 
24639bf0dad6SMatthew G. Knepley   Note:
24649bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
24659bf0dad6SMatthew G. Knepley 
24669bf0dad6SMatthew G. Knepley   Fortran Notes:
24679bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24689bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
24699bf0dad6SMatthew G. Knepley 
24709bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24719bf0dad6SMatthew G. Knepley 
24729bf0dad6SMatthew G. Knepley   Level: beginner
24739bf0dad6SMatthew G. Knepley 
24749bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
24759bf0dad6SMatthew G. Knepley @*/
24769bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
24779bf0dad6SMatthew G. Knepley {
24789bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
24799bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
24809bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
24819bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
24829bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
24839bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
24849bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
24859bf0dad6SMatthew G. Knepley 
24869bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
24879bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24889bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24899bf0dad6SMatthew G. Knepley   /* This is only 1-level */
24909bf0dad6SMatthew G. Knepley   if (useCone) {
24919bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
24929bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
24939bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
24949bf0dad6SMatthew G. Knepley   } else {
24959bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
24969bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
24979bf0dad6SMatthew G. Knepley   }
24989bf0dad6SMatthew G. Knepley   if (depth == 1) {
24999bf0dad6SMatthew G. Knepley     if (*points) {
25009bf0dad6SMatthew G. Knepley       closure = *points;
25019bf0dad6SMatthew G. Knepley     } else {
25029bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
250369291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
25049bf0dad6SMatthew G. Knepley     }
25059bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
25069bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
25079bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
25089bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
25099bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
25109bf0dad6SMatthew G. Knepley     }
25119bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
25129bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
25139bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
25149bf0dad6SMatthew G. Knepley   }
251524c766afSToby Isaac   {
251624c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
251724c766afSToby Isaac 
251824c766afSToby Isaac     c = mesh->maxConeSize;
251924c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
252024c766afSToby Isaac     s = mesh->maxSupportSize;
252124c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
252224c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
252324c766afSToby Isaac   }
252469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25259bf0dad6SMatthew G. Knepley   if (*points) {
25269bf0dad6SMatthew G. Knepley     closure = *points;
25279bf0dad6SMatthew G. Knepley   } else {
252869291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
25299bf0dad6SMatthew G. Knepley   }
25309bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
25319bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
25329bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
25339bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
25349bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
25359bf0dad6SMatthew G. Knepley 
25369bf0dad6SMatthew G. Knepley     if (ornt < 0) {
25379bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
25389bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
253986b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
25409bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25419bf0dad6SMatthew G. Knepley     }
25429bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
25439bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
25449bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
25459bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
25469bf0dad6SMatthew G. Knepley   }
25479bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
25489bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
25499bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
25509bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
25519bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
25529bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
25539bf0dad6SMatthew G. Knepley 
25549bf0dad6SMatthew G. Knepley     if (useCone) {
25559bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
25569bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
25579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
25589bf0dad6SMatthew G. Knepley     } else {
25599bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
25609bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25619bf0dad6SMatthew G. Knepley       tmpO = NULL;
25629bf0dad6SMatthew G. Knepley     }
25639bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
25649bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
25659bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
25669bf0dad6SMatthew 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. */
25679bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25689bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25699bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
25709bf0dad6SMatthew G. Knepley       PetscInt       c;
25719bf0dad6SMatthew G. Knepley 
25729bf0dad6SMatthew G. Knepley       if (rev) {
25739bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
25749bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25759bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25769bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25779bf0dad6SMatthew G. Knepley       }
25789bf0dad6SMatthew G. Knepley       /* Check for duplicate */
25799bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
25809bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
25819bf0dad6SMatthew G. Knepley       }
25829bf0dad6SMatthew G. Knepley       if (c == closureSize) {
25839bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
25849bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
25859bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
25869bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
25879bf0dad6SMatthew G. Knepley         closureSize           += 2;
25889bf0dad6SMatthew G. Knepley         fifoSize              += 2;
25899bf0dad6SMatthew G. Knepley       }
25909bf0dad6SMatthew G. Knepley     }
25919bf0dad6SMatthew G. Knepley     fifoStart += 2;
25929bf0dad6SMatthew G. Knepley   }
25939bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
25949bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
259569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25969bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
25979bf0dad6SMatthew G. Knepley }
25989bf0dad6SMatthew G. Knepley 
2599552f7358SJed Brown /*@C
2600eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2601552f7358SJed Brown 
2602552f7358SJed Brown   Not collective
2603552f7358SJed Brown 
2604552f7358SJed Brown   Input Parameters:
2605552f7358SJed Brown + mesh - The DMPlex
2606eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2607552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2608e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2609e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2610552f7358SJed Brown 
2611552f7358SJed Brown   Note:
26120298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2613552f7358SJed Brown 
26143813dfbdSMatthew G Knepley   Fortran Notes:
26153813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26163813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26173813dfbdSMatthew G Knepley 
26183813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26193813dfbdSMatthew G Knepley 
2620552f7358SJed Brown   Level: beginner
2621552f7358SJed Brown 
2622552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2623552f7358SJed Brown @*/
2624552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2625552f7358SJed Brown {
2626552f7358SJed Brown   PetscErrorCode ierr;
2627552f7358SJed Brown 
2628552f7358SJed Brown   PetscFunctionBegin;
2629552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2630e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2631e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
263269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
26334ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2634552f7358SJed Brown   PetscFunctionReturn(0);
2635552f7358SJed Brown }
2636552f7358SJed Brown 
2637552f7358SJed Brown /*@
2638eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2639552f7358SJed Brown 
2640552f7358SJed Brown   Not collective
2641552f7358SJed Brown 
2642552f7358SJed Brown   Input Parameter:
2643552f7358SJed Brown . mesh - The DMPlex
2644552f7358SJed Brown 
2645552f7358SJed Brown   Output Parameters:
2646552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2647552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2648552f7358SJed Brown 
2649552f7358SJed Brown   Level: beginner
2650552f7358SJed Brown 
2651552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2652552f7358SJed Brown @*/
2653552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2654552f7358SJed Brown {
2655552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2656552f7358SJed Brown 
2657552f7358SJed Brown   PetscFunctionBegin;
2658552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2659552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2660552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2661552f7358SJed Brown   PetscFunctionReturn(0);
2662552f7358SJed Brown }
2663552f7358SJed Brown 
2664552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2665552f7358SJed Brown {
2666552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2667552f7358SJed Brown   PetscInt       size;
2668552f7358SJed Brown   PetscErrorCode ierr;
2669552f7358SJed Brown 
2670552f7358SJed Brown   PetscFunctionBegin;
2671552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2672552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2673552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
26741795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
26751795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2676552f7358SJed Brown   if (mesh->maxSupportSize) {
2677552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2678552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
26791795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2680552f7358SJed Brown   }
2681552f7358SJed Brown   PetscFunctionReturn(0);
2682552f7358SJed Brown }
2683552f7358SJed Brown 
2684276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2685552f7358SJed Brown {
2686552f7358SJed Brown   PetscErrorCode ierr;
2687552f7358SJed Brown 
2688552f7358SJed Brown   PetscFunctionBegin;
26894d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2690792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2691c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2692736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2693f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2694f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2695f94b4a02SBlaise Bourdin 
26963dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
26973dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2698c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
2699f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2700f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2701f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2702f94b4a02SBlaise Bourdin 
2703f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2704c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2705f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2706f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2707f94b4a02SBlaise Bourdin   }
2708552f7358SJed Brown   PetscFunctionReturn(0);
2709552f7358SJed Brown }
2710552f7358SJed Brown 
27112adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
27122adcc780SMatthew G. Knepley {
27132adcc780SMatthew G. Knepley   PetscErrorCode ierr;
27143dcd263cSBlaise Bourdin   PetscInt       i = 0;
27152adcc780SMatthew G. Knepley 
27162adcc780SMatthew G. Knepley   PetscFunctionBegin;
2717435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2718792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2719c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
27203dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
27213dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
27223dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
27233dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
27243dcd263cSBlaise Bourdin 
27253dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
27263dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2727c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
272892fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
27293dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
27303dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
27313dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
27323dcd263cSBlaise Bourdin 
27333dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2734c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
27353dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
27363dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
27373dcd263cSBlaise Bourdin       break;
27383dcd263cSBlaise Bourdin     }
27393dcd263cSBlaise Bourdin   }
27402adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
27412adcc780SMatthew G. Knepley }
27422adcc780SMatthew G. Knepley 
2743552f7358SJed Brown /*@
2744eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2745552f7358SJed Brown 
2746552f7358SJed Brown   Not collective
2747552f7358SJed Brown 
2748552f7358SJed Brown   Input Parameter:
2749552f7358SJed Brown . mesh - The DMPlex
2750552f7358SJed Brown 
2751552f7358SJed Brown   Output Parameter:
2752552f7358SJed Brown 
2753552f7358SJed Brown   Note:
2754552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2755552f7358SJed Brown 
2756552f7358SJed Brown   Level: beginner
2757552f7358SJed Brown 
2758552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2759552f7358SJed Brown @*/
2760552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2761552f7358SJed Brown {
2762552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2763552f7358SJed Brown   PetscInt      *offsets;
2764552f7358SJed Brown   PetscInt       supportSize;
2765552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2766552f7358SJed Brown   PetscErrorCode ierr;
2767552f7358SJed Brown 
2768552f7358SJed Brown   PetscFunctionBegin;
2769552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
277082f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
277130b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2772552f7358SJed Brown   /* Calculate support sizes */
2773552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2774552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2775552f7358SJed Brown     PetscInt dof, off, c;
2776552f7358SJed Brown 
2777552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2778552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2779552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2780552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2781552f7358SJed Brown     }
2782552f7358SJed Brown   }
2783552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2784552f7358SJed Brown     PetscInt dof;
2785552f7358SJed Brown 
2786552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
27870d644c17SKarl Rupp 
2788552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2789552f7358SJed Brown   }
2790552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2791552f7358SJed Brown   /* Calculate supports */
2792552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
27931795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
27941795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2795552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2796552f7358SJed Brown     PetscInt dof, off, c;
2797552f7358SJed Brown 
2798552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2799552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2800552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2801552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2802552f7358SJed Brown       PetscInt       offS;
2803552f7358SJed Brown 
2804552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
28050d644c17SKarl Rupp 
2806552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2807552f7358SJed Brown       ++offsets[q];
2808552f7358SJed Brown     }
2809552f7358SJed Brown   }
2810552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
281130b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2812552f7358SJed Brown   PetscFunctionReturn(0);
2813552f7358SJed Brown }
2814552f7358SJed Brown 
2815277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2816277ea44aSLisandro Dalcin {
2817277ea44aSLisandro Dalcin   IS             stratumIS;
2818277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2819277ea44aSLisandro Dalcin 
2820277ea44aSLisandro Dalcin   PetscFunctionBegin;
2821277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
282276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2823277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2824277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2825277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2826277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2827277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2828277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2829277ea44aSLisandro Dalcin     }
2830277ea44aSLisandro 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);
2831277ea44aSLisandro Dalcin   }
2832277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2833277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2834277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2835277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2836277ea44aSLisandro Dalcin }
2837277ea44aSLisandro Dalcin 
2838552f7358SJed Brown /*@
2839a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
28406dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2841552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2842552f7358SJed Brown   the DAG.
2843552f7358SJed Brown 
2844bf4602e4SToby Isaac   Collective on dm
2845552f7358SJed Brown 
2846552f7358SJed Brown   Input Parameter:
2847552f7358SJed Brown . mesh - The DMPlex
2848552f7358SJed Brown 
2849552f7358SJed Brown   Output Parameter:
2850552f7358SJed Brown 
2851552f7358SJed Brown   Notes:
2852b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
2853b1bb481bSMatthew 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
2854b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2855c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2856150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2857552f7358SJed Brown 
2858b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
2859b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
2860b1bb481bSMatthew 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
2861b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
2862b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
2863b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
2864b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
2865b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
2866b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
2867b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
2868b1bb481bSMatthew Knepley 
2869150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2870552f7358SJed Brown 
2871552f7358SJed Brown   Level: beginner
2872552f7358SJed Brown 
2873ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
2874552f7358SJed Brown @*/
2875552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2876552f7358SJed Brown {
2877df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2878aa50250dSMatthew G. Knepley   DMLabel        label;
2879552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2880552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2881552f7358SJed Brown   PetscErrorCode ierr;
2882552f7358SJed Brown 
2883552f7358SJed Brown   PetscFunctionBegin;
2884552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2885552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2886277ea44aSLisandro Dalcin 
2887277ea44aSLisandro Dalcin   /* Create depth label */
2888aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2889c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2890aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2891277ea44aSLisandro Dalcin 
2892277ea44aSLisandro Dalcin   {
2893552f7358SJed Brown     /* Initialize roots and count leaves */
2894277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2895277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2896552f7358SJed Brown     PetscInt coneSize, supportSize;
2897552f7358SJed Brown 
2898277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2899552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2900552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2901552f7358SJed Brown       if (!coneSize && supportSize) {
2902277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2903277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2904552f7358SJed Brown         ++numRoots;
2905552f7358SJed Brown       } else if (!supportSize && coneSize) {
2906552f7358SJed Brown         ++numLeaves;
2907552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2908552f7358SJed Brown         /* Isolated points */
2909277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2910277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2911552f7358SJed Brown       }
2912552f7358SJed Brown     }
2913277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2914277ea44aSLisandro Dalcin   }
2915277ea44aSLisandro Dalcin 
2916552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2917277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2918277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2919552f7358SJed Brown     PetscInt coneSize, supportSize;
2920552f7358SJed Brown 
2921277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2922552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2923552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2924552f7358SJed Brown       if (!supportSize && coneSize) {
2925277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2926277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2927552f7358SJed Brown       }
2928552f7358SJed Brown     }
2929277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2930552f7358SJed Brown   } else {
2931277ea44aSLisandro Dalcin     PetscInt level = 0;
2932277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2933552f7358SJed Brown 
2934277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2935277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2936277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2937277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
293874ef644bSMatthew G. Knepley 
2939277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
294074ef644bSMatthew G. Knepley         const PetscInt *support;
294174ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
294274ef644bSMatthew G. Knepley 
2943277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2944277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
294574ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2946277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2947277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2948552f7358SJed Brown         }
2949552f7358SJed Brown       }
2950277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2951277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2952277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
295374ef644bSMatthew G. Knepley     }
295474ef644bSMatthew G. Knepley   }
2955bf4602e4SToby Isaac   { /* just in case there is an empty process */
2956bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2957bf4602e4SToby Isaac 
2958bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
29596d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2960bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2961367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2962bf4602e4SToby Isaac     }
2963bf4602e4SToby Isaac   }
2964d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
2965552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2966552f7358SJed Brown   PetscFunctionReturn(0);
2967552f7358SJed Brown }
2968552f7358SJed Brown 
2969412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
2970ba2698f1SMatthew G. Knepley {
2971412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
2972412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
2973ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
2974ba2698f1SMatthew G. Knepley 
2975412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
2976ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2977ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2978ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2979ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
2980ba2698f1SMatthew G. Knepley   if (depth <= 1) {
2981ba2698f1SMatthew G. Knepley     switch (pdepth) {
2982ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
2983ba2698f1SMatthew G. Knepley       case 1:
2984ba2698f1SMatthew G. Knepley         switch (coneSize) {
2985ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
2986ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
2987ba2698f1SMatthew G. Knepley           case 4:
2988ba2698f1SMatthew G. Knepley           switch (dim) {
2989ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
2990ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
2991ba2698f1SMatthew G. Knepley             default: break;
2992ba2698f1SMatthew G. Knepley           }
2993ba2698f1SMatthew G. Knepley           break;
2994ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
2995ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
2996ba2698f1SMatthew G. Knepley         default: break;
2997ba2698f1SMatthew G. Knepley       }
2998ba2698f1SMatthew G. Knepley     }
2999ba2698f1SMatthew G. Knepley   } else {
3000ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
3001ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
3002ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
3003ba2698f1SMatthew G. Knepley       switch (dim) {
3004ba2698f1SMatthew G. Knepley         case 1:
3005ba2698f1SMatthew G. Knepley           switch (coneSize) {
3006ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3007ba2698f1SMatthew G. Knepley             default: break;
3008ba2698f1SMatthew G. Knepley           }
3009ba2698f1SMatthew G. Knepley           break;
3010ba2698f1SMatthew G. Knepley         case 2:
3011ba2698f1SMatthew G. Knepley           switch (coneSize) {
3012ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3013ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3014ba2698f1SMatthew G. Knepley             default: break;
3015ba2698f1SMatthew G. Knepley           }
3016ba2698f1SMatthew G. Knepley           break;
3017ba2698f1SMatthew G. Knepley         case 3:
3018ba2698f1SMatthew G. Knepley           switch (coneSize) {
3019ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3020ba2698f1SMatthew G. Knepley             case 5: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3021ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3022ba2698f1SMatthew G. Knepley             default: break;
3023ba2698f1SMatthew G. Knepley           }
3024ba2698f1SMatthew G. Knepley           break;
3025ba2698f1SMatthew G. Knepley         default: break;
3026ba2698f1SMatthew G. Knepley       }
3027ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
3028ba2698f1SMatthew G. Knepley       switch (coneSize) {
3029ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3030ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3031ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3032ba2698f1SMatthew G. Knepley         default: break;
3033ba2698f1SMatthew G. Knepley       }
3034ba2698f1SMatthew G. Knepley     }
3035ba2698f1SMatthew G. Knepley   }
3036412e9a14SMatthew G. Knepley   *pt = ct;
3037412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3038ba2698f1SMatthew G. Knepley }
3039412e9a14SMatthew G. Knepley 
3040412e9a14SMatthew G. Knepley /*@
3041412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3042412e9a14SMatthew G. Knepley 
3043412e9a14SMatthew G. Knepley   Collective on dm
3044412e9a14SMatthew G. Knepley 
3045412e9a14SMatthew G. Knepley   Input Parameter:
3046412e9a14SMatthew G. Knepley . mesh - The DMPlex
3047412e9a14SMatthew G. Knepley 
3048412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3049412e9a14SMatthew G. Knepley 
3050412e9a14SMatthew G. Knepley   Level: developer
3051412e9a14SMatthew G. Knepley 
3052412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3053412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3054412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3055412e9a14SMatthew G. Knepley 
3056412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3057412e9a14SMatthew G. Knepley @*/
3058412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3059412e9a14SMatthew G. Knepley {
3060412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3061412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3062412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3063412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3064412e9a14SMatthew G. Knepley 
3065412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3066412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3067412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3068412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3069412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3070412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3071412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3072327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3073412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3074412e9a14SMatthew G. Knepley 
3075412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3076412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3077412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3078412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3079412e9a14SMatthew G. Knepley   }
3080412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3081412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3082ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3083ba2698f1SMatthew G. Knepley }
3084ba2698f1SMatthew G. Knepley 
3085552f7358SJed Brown /*@C
3086552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3087552f7358SJed Brown 
3088552f7358SJed Brown   Not Collective
3089552f7358SJed Brown 
3090552f7358SJed Brown   Input Parameters:
3091552f7358SJed Brown + dm - The DMPlex object
3092552f7358SJed Brown . numPoints - The number of input points for the join
3093552f7358SJed Brown - points - The input points
3094552f7358SJed Brown 
3095552f7358SJed Brown   Output Parameters:
3096552f7358SJed Brown + numCoveredPoints - The number of points in the join
3097552f7358SJed Brown - coveredPoints - The points in the join
3098552f7358SJed Brown 
3099552f7358SJed Brown   Level: intermediate
3100552f7358SJed Brown 
3101552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3102552f7358SJed Brown 
31033813dfbdSMatthew G Knepley   Fortran Notes:
31043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31063813dfbdSMatthew G Knepley 
31073813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31083813dfbdSMatthew G Knepley 
3109552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3110552f7358SJed Brown @*/
3111552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3112552f7358SJed Brown {
3113552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3114552f7358SJed Brown   PetscInt      *join[2];
3115552f7358SJed Brown   PetscInt       joinSize, i = 0;
3116552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3117552f7358SJed Brown   PetscErrorCode ierr;
3118552f7358SJed Brown 
3119552f7358SJed Brown   PetscFunctionBegin;
3120552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
312148bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
312248bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
312348bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
312469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
312569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3126552f7358SJed Brown   /* Copy in support of first point */
3127552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3128552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3129552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3130552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3131552f7358SJed Brown   }
3132552f7358SJed Brown   /* Check each successive support */
3133552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3134552f7358SJed Brown     PetscInt newJoinSize = 0;
3135552f7358SJed Brown 
3136552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3137552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3138552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3139552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3140552f7358SJed Brown 
3141552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3142552f7358SJed Brown         if (point == join[i][m]) {
3143552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3144552f7358SJed Brown           break;
3145552f7358SJed Brown         }
3146552f7358SJed Brown       }
3147552f7358SJed Brown     }
3148552f7358SJed Brown     joinSize = newJoinSize;
3149552f7358SJed Brown     i        = 1-i;
3150552f7358SJed Brown   }
3151552f7358SJed Brown   *numCoveredPoints = joinSize;
3152552f7358SJed Brown   *coveredPoints    = join[i];
315369291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3154552f7358SJed Brown   PetscFunctionReturn(0);
3155552f7358SJed Brown }
3156552f7358SJed Brown 
3157552f7358SJed Brown /*@C
3158552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3159552f7358SJed Brown 
3160552f7358SJed Brown   Not Collective
3161552f7358SJed Brown 
3162552f7358SJed Brown   Input Parameters:
3163552f7358SJed Brown + dm - The DMPlex object
3164552f7358SJed Brown . numPoints - The number of input points for the join
3165552f7358SJed Brown - points - The input points
3166552f7358SJed Brown 
3167552f7358SJed Brown   Output Parameters:
3168552f7358SJed Brown + numCoveredPoints - The number of points in the join
3169552f7358SJed Brown - coveredPoints - The points in the join
3170552f7358SJed Brown 
31713813dfbdSMatthew G Knepley   Fortran Notes:
31723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31743813dfbdSMatthew G Knepley 
31753813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31763813dfbdSMatthew G Knepley 
3177552f7358SJed Brown   Level: intermediate
3178552f7358SJed Brown 
3179552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3180552f7358SJed Brown @*/
3181552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3182552f7358SJed Brown {
3183552f7358SJed Brown   PetscErrorCode ierr;
3184552f7358SJed Brown 
3185552f7358SJed Brown   PetscFunctionBegin;
3186552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3187d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3188d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3189d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
319069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3191d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3192552f7358SJed Brown   PetscFunctionReturn(0);
3193552f7358SJed Brown }
3194552f7358SJed Brown 
3195552f7358SJed Brown /*@C
3196552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3197552f7358SJed Brown 
3198552f7358SJed Brown   Not Collective
3199552f7358SJed Brown 
3200552f7358SJed Brown   Input Parameters:
3201552f7358SJed Brown + dm - The DMPlex object
3202552f7358SJed Brown . numPoints - The number of input points for the join
3203552f7358SJed Brown - points - The input points
3204552f7358SJed Brown 
3205552f7358SJed Brown   Output Parameters:
3206552f7358SJed Brown + numCoveredPoints - The number of points in the join
3207552f7358SJed Brown - coveredPoints - The points in the join
3208552f7358SJed Brown 
32093813dfbdSMatthew G Knepley   Fortran Notes:
32103813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32113813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32123813dfbdSMatthew G Knepley 
32133813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32143813dfbdSMatthew G Knepley 
3215552f7358SJed Brown   Level: intermediate
3216552f7358SJed Brown 
3217552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3218552f7358SJed Brown @*/
3219552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3220552f7358SJed Brown {
3221552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3222552f7358SJed Brown   PetscInt      *offsets, **closures;
3223552f7358SJed Brown   PetscInt      *join[2];
3224552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
322524c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3226552f7358SJed Brown   PetscErrorCode ierr;
3227552f7358SJed Brown 
3228552f7358SJed Brown   PetscFunctionBegin;
3229552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
323048bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
323148bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
323248bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3233552f7358SJed Brown 
3234552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
32351795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
323669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
323724c766afSToby Isaac   ms      = mesh->maxSupportSize;
323824c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
323969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
324069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3241552f7358SJed Brown 
3242552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3243552f7358SJed Brown     PetscInt closureSize;
3244552f7358SJed Brown 
3245552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
32460d644c17SKarl Rupp 
3247552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3248552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3249552f7358SJed Brown       PetscInt pStart, pEnd, i;
3250552f7358SJed Brown 
3251552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3252552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3253552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3254552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3255552f7358SJed Brown           break;
3256552f7358SJed Brown         }
3257552f7358SJed Brown       }
3258552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3259552f7358SJed Brown     }
326082f516ccSBarry 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);
3261552f7358SJed Brown   }
3262552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3263552f7358SJed Brown     PetscInt dof;
3264552f7358SJed Brown 
3265552f7358SJed Brown     /* Copy in support of first point */
3266552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3267552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3268552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3269552f7358SJed Brown     }
3270552f7358SJed Brown     /* Check each successive cone */
3271552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3272552f7358SJed Brown       PetscInt newJoinSize = 0;
3273552f7358SJed Brown 
3274552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3275552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3276552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3277552f7358SJed Brown 
3278552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3279552f7358SJed Brown           if (point == join[i][m]) {
3280552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3281552f7358SJed Brown             break;
3282552f7358SJed Brown           }
3283552f7358SJed Brown         }
3284552f7358SJed Brown       }
3285552f7358SJed Brown       joinSize = newJoinSize;
3286552f7358SJed Brown       i        = 1-i;
3287552f7358SJed Brown     }
3288552f7358SJed Brown     if (joinSize) break;
3289552f7358SJed Brown   }
3290552f7358SJed Brown   *numCoveredPoints = joinSize;
3291552f7358SJed Brown   *coveredPoints    = join[i];
3292552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
32930298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3294552f7358SJed Brown   }
3295552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
329669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
329769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3298552f7358SJed Brown   PetscFunctionReturn(0);
3299552f7358SJed Brown }
3300552f7358SJed Brown 
3301552f7358SJed Brown /*@C
3302552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3303552f7358SJed Brown 
3304552f7358SJed Brown   Not Collective
3305552f7358SJed Brown 
3306552f7358SJed Brown   Input Parameters:
3307552f7358SJed Brown + dm - The DMPlex object
3308552f7358SJed Brown . numPoints - The number of input points for the meet
3309552f7358SJed Brown - points - The input points
3310552f7358SJed Brown 
3311552f7358SJed Brown   Output Parameters:
3312552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3313552f7358SJed Brown - coveredPoints - The points in the meet
3314552f7358SJed Brown 
3315552f7358SJed Brown   Level: intermediate
3316552f7358SJed Brown 
3317552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3318552f7358SJed Brown 
33193813dfbdSMatthew G Knepley   Fortran Notes:
33203813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33213813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33223813dfbdSMatthew G Knepley 
33233813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33243813dfbdSMatthew G Knepley 
3325552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3326552f7358SJed Brown @*/
3327552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3328552f7358SJed Brown {
3329552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3330552f7358SJed Brown   PetscInt      *meet[2];
3331552f7358SJed Brown   PetscInt       meetSize, i = 0;
3332552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3333552f7358SJed Brown   PetscErrorCode ierr;
3334552f7358SJed Brown 
3335552f7358SJed Brown   PetscFunctionBegin;
3336552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3337552f7358SJed Brown   PetscValidPointer(points, 2);
3338552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3339552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
334069291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
334169291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3342552f7358SJed Brown   /* Copy in cone of first point */
3343552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3344552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3345552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3346552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3347552f7358SJed Brown   }
3348552f7358SJed Brown   /* Check each successive cone */
3349552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3350552f7358SJed Brown     PetscInt newMeetSize = 0;
3351552f7358SJed Brown 
3352552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3353552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3354552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3355552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3356552f7358SJed Brown 
3357552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3358552f7358SJed Brown         if (point == meet[i][m]) {
3359552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3360552f7358SJed Brown           break;
3361552f7358SJed Brown         }
3362552f7358SJed Brown       }
3363552f7358SJed Brown     }
3364552f7358SJed Brown     meetSize = newMeetSize;
3365552f7358SJed Brown     i        = 1-i;
3366552f7358SJed Brown   }
3367552f7358SJed Brown   *numCoveringPoints = meetSize;
3368552f7358SJed Brown   *coveringPoints    = meet[i];
336969291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3370552f7358SJed Brown   PetscFunctionReturn(0);
3371552f7358SJed Brown }
3372552f7358SJed Brown 
3373552f7358SJed Brown /*@C
3374552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3375552f7358SJed Brown 
3376552f7358SJed Brown   Not Collective
3377552f7358SJed Brown 
3378552f7358SJed Brown   Input Parameters:
3379552f7358SJed Brown + dm - The DMPlex object
3380552f7358SJed Brown . numPoints - The number of input points for the meet
3381552f7358SJed Brown - points - The input points
3382552f7358SJed Brown 
3383552f7358SJed Brown   Output Parameters:
3384552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3385552f7358SJed Brown - coveredPoints - The points in the meet
3386552f7358SJed Brown 
3387552f7358SJed Brown   Level: intermediate
3388552f7358SJed Brown 
33893813dfbdSMatthew G Knepley   Fortran Notes:
33903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33923813dfbdSMatthew G Knepley 
33933813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33943813dfbdSMatthew G Knepley 
3395552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3396552f7358SJed Brown @*/
3397552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3398552f7358SJed Brown {
3399552f7358SJed Brown   PetscErrorCode ierr;
3400552f7358SJed Brown 
3401552f7358SJed Brown   PetscFunctionBegin;
3402552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3403d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3404d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3405d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
340669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3407d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3408552f7358SJed Brown   PetscFunctionReturn(0);
3409552f7358SJed Brown }
3410552f7358SJed Brown 
3411552f7358SJed Brown /*@C
3412552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3413552f7358SJed Brown 
3414552f7358SJed Brown   Not Collective
3415552f7358SJed Brown 
3416552f7358SJed Brown   Input Parameters:
3417552f7358SJed Brown + dm - The DMPlex object
3418552f7358SJed Brown . numPoints - The number of input points for the meet
3419552f7358SJed Brown - points - The input points
3420552f7358SJed Brown 
3421552f7358SJed Brown   Output Parameters:
3422552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3423552f7358SJed Brown - coveredPoints - The points in the meet
3424552f7358SJed Brown 
3425552f7358SJed Brown   Level: intermediate
3426552f7358SJed Brown 
34273813dfbdSMatthew G Knepley   Fortran Notes:
34283813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34293813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34303813dfbdSMatthew G Knepley 
34313813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34323813dfbdSMatthew G Knepley 
3433552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3434552f7358SJed Brown @*/
3435552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3436552f7358SJed Brown {
3437552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3438552f7358SJed Brown   PetscInt      *offsets, **closures;
3439552f7358SJed Brown   PetscInt      *meet[2];
3440552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
344124c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3442552f7358SJed Brown   PetscErrorCode ierr;
3443552f7358SJed Brown 
3444552f7358SJed Brown   PetscFunctionBegin;
3445552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3446552f7358SJed Brown   PetscValidPointer(points, 2);
3447552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3448552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3449552f7358SJed Brown 
3450552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3451785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
345269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
345324c766afSToby Isaac   mc      = mesh->maxConeSize;
345424c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
345569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
345669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3457552f7358SJed Brown 
3458552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3459552f7358SJed Brown     PetscInt closureSize;
3460552f7358SJed Brown 
3461552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
34620d644c17SKarl Rupp 
3463552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3464552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3465552f7358SJed Brown       PetscInt pStart, pEnd, i;
3466552f7358SJed Brown 
3467552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3468552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3469552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3470552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3471552f7358SJed Brown           break;
3472552f7358SJed Brown         }
3473552f7358SJed Brown       }
3474552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3475552f7358SJed Brown     }
347682f516ccSBarry 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);
3477552f7358SJed Brown   }
3478552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3479552f7358SJed Brown     PetscInt dof;
3480552f7358SJed Brown 
3481552f7358SJed Brown     /* Copy in cone of first point */
3482552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3483552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3484552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3485552f7358SJed Brown     }
3486552f7358SJed Brown     /* Check each successive cone */
3487552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3488552f7358SJed Brown       PetscInt newMeetSize = 0;
3489552f7358SJed Brown 
3490552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3491552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3492552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3493552f7358SJed Brown 
3494552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3495552f7358SJed Brown           if (point == meet[i][m]) {
3496552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3497552f7358SJed Brown             break;
3498552f7358SJed Brown           }
3499552f7358SJed Brown         }
3500552f7358SJed Brown       }
3501552f7358SJed Brown       meetSize = newMeetSize;
3502552f7358SJed Brown       i        = 1-i;
3503552f7358SJed Brown     }
3504552f7358SJed Brown     if (meetSize) break;
3505552f7358SJed Brown   }
3506552f7358SJed Brown   *numCoveredPoints = meetSize;
3507552f7358SJed Brown   *coveredPoints    = meet[i];
3508552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
35090298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3510552f7358SJed Brown   }
3511552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
351269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
351369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3514552f7358SJed Brown   PetscFunctionReturn(0);
3515552f7358SJed Brown }
3516552f7358SJed Brown 
35174e3744c5SMatthew G. Knepley /*@C
35184e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
35194e3744c5SMatthew G. Knepley 
35204e3744c5SMatthew G. Knepley   Not Collective
35214e3744c5SMatthew G. Knepley 
35224e3744c5SMatthew G. Knepley   Input Parameters:
35234e3744c5SMatthew G. Knepley + dmA - A DMPlex object
35244e3744c5SMatthew G. Knepley - dmB - A DMPlex object
35254e3744c5SMatthew G. Knepley 
35264e3744c5SMatthew G. Knepley   Output Parameters:
35274e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
35284e3744c5SMatthew G. Knepley 
35294e3744c5SMatthew G. Knepley   Level: intermediate
35304e3744c5SMatthew G. Knepley 
35314e3744c5SMatthew G. Knepley   Notes:
35324e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
35334e3744c5SMatthew G. Knepley 
35344e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
35354e3744c5SMatthew G. Knepley @*/
35364e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
35374e3744c5SMatthew G. Knepley {
35384e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
35394e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
35404e3744c5SMatthew G. Knepley 
35414e3744c5SMatthew G. Knepley   PetscFunctionBegin;
35424e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
35434e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
35444e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
35454e3744c5SMatthew G. Knepley 
35464e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
35474e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
35484e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
35494e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
35504e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
35514e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
35524e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
35534e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35544e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
35554e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
35564e3744c5SMatthew G. Knepley 
35574e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
35584e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
35594e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
35604e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
35614e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
35624e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
35634e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
35644e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
35654e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
35664e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
35674e3744c5SMatthew G. Knepley     }
35684e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
35694e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
35704e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
35714e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
35724e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
35734e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
35744e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
35754e3744c5SMatthew G. Knepley     }
35764e3744c5SMatthew G. Knepley   }
35774e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
35784e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
35794e3744c5SMatthew G. Knepley }
35804e3744c5SMatthew G. Knepley 
35817cd05799SMatthew G. Knepley /*@C
35827cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
35837cd05799SMatthew G. Knepley 
35847cd05799SMatthew G. Knepley   Not Collective
35857cd05799SMatthew G. Knepley 
35867cd05799SMatthew G. Knepley   Input Parameters:
35877cd05799SMatthew G. Knepley + dm         - The DMPlex
35887cd05799SMatthew G. Knepley . cellDim    - The cell dimension
35897cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
35907cd05799SMatthew G. Knepley 
35917cd05799SMatthew G. Knepley   Output Parameters:
35927cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
35937cd05799SMatthew G. Knepley 
35947cd05799SMatthew G. Knepley   Level: developer
35957cd05799SMatthew G. Knepley 
35967cd05799SMatthew G. Knepley   Notes:
35977cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
35987cd05799SMatthew G. Knepley 
35997cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
36007cd05799SMatthew G. Knepley @*/
360118ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3602a6dfd86eSKarl Rupp {
360382f516ccSBarry Smith   MPI_Comm       comm;
3604552f7358SJed Brown   PetscErrorCode ierr;
3605552f7358SJed Brown 
3606552f7358SJed Brown   PetscFunctionBegin;
360782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3608552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3609552f7358SJed Brown   switch (cellDim) {
3610552f7358SJed Brown   case 0:
3611552f7358SJed Brown     *numFaceVertices = 0;
3612552f7358SJed Brown     break;
3613552f7358SJed Brown   case 1:
3614552f7358SJed Brown     *numFaceVertices = 1;
3615552f7358SJed Brown     break;
3616552f7358SJed Brown   case 2:
3617552f7358SJed Brown     switch (numCorners) {
361819436ca2SJed Brown     case 3: /* triangle */
361919436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3620552f7358SJed Brown       break;
362119436ca2SJed Brown     case 4: /* quadrilateral */
362219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3623552f7358SJed Brown       break;
362419436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
362519436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3626552f7358SJed Brown       break;
362719436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
362819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3629552f7358SJed Brown       break;
3630552f7358SJed Brown     default:
3631f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3632552f7358SJed Brown     }
3633552f7358SJed Brown     break;
3634552f7358SJed Brown   case 3:
3635552f7358SJed Brown     switch (numCorners) {
363619436ca2SJed Brown     case 4: /* tetradehdron */
363719436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3638552f7358SJed Brown       break;
363919436ca2SJed Brown     case 6: /* tet cohesive cells */
364019436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3641552f7358SJed Brown       break;
364219436ca2SJed Brown     case 8: /* hexahedron */
364319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3644552f7358SJed Brown       break;
364519436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
364619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3647552f7358SJed Brown       break;
364819436ca2SJed Brown     case 10: /* quadratic tetrahedron */
364919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3650552f7358SJed Brown       break;
365119436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
365219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3653552f7358SJed Brown       break;
365419436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
365519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3656552f7358SJed Brown       break;
365719436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
365819436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3659552f7358SJed Brown       break;
3660552f7358SJed Brown     default:
3661f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3662552f7358SJed Brown     }
3663552f7358SJed Brown     break;
3664552f7358SJed Brown   default:
3665f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3666552f7358SJed Brown   }
3667552f7358SJed Brown   PetscFunctionReturn(0);
3668552f7358SJed Brown }
3669552f7358SJed Brown 
3670552f7358SJed Brown /*@
3671aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3672552f7358SJed Brown 
3673552f7358SJed Brown   Not Collective
3674552f7358SJed Brown 
3675aa50250dSMatthew G. Knepley   Input Parameter:
3676552f7358SJed Brown . dm    - The DMPlex object
3677552f7358SJed Brown 
3678aa50250dSMatthew G. Knepley   Output Parameter:
3679aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3680552f7358SJed Brown 
3681552f7358SJed Brown   Level: developer
3682552f7358SJed Brown 
3683dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
3684aa50250dSMatthew G. Knepley @*/
3685aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3686aa50250dSMatthew G. Knepley {
3687aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3688aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3689aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3690c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3691aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3692aa50250dSMatthew G. Knepley }
3693aa50250dSMatthew G. Knepley 
3694aa50250dSMatthew G. Knepley /*@
3695aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3696aa50250dSMatthew G. Knepley 
3697aa50250dSMatthew G. Knepley   Not Collective
3698aa50250dSMatthew G. Knepley 
3699aa50250dSMatthew G. Knepley   Input Parameter:
3700aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3701aa50250dSMatthew G. Knepley 
3702aa50250dSMatthew G. Knepley   Output Parameter:
3703aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3704aa50250dSMatthew G. Knepley 
3705aa50250dSMatthew G. Knepley   Level: developer
3706552f7358SJed Brown 
3707b1bb481bSMatthew Knepley   Notes:
3708b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3709dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
3710dc287ab2SVaclav Hapla   An empty mesh gives -1.
3711b1bb481bSMatthew Knepley 
3712dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
3713552f7358SJed Brown @*/
3714552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3715552f7358SJed Brown {
3716aa50250dSMatthew G. Knepley   DMLabel        label;
3717aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3718552f7358SJed Brown   PetscErrorCode ierr;
3719552f7358SJed Brown 
3720552f7358SJed Brown   PetscFunctionBegin;
3721552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3722552f7358SJed Brown   PetscValidPointer(depth, 2);
3723aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3724aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3725552f7358SJed Brown   *depth = d-1;
3726552f7358SJed Brown   PetscFunctionReturn(0);
3727552f7358SJed Brown }
3728552f7358SJed Brown 
3729552f7358SJed Brown /*@
3730552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3731552f7358SJed Brown 
3732552f7358SJed Brown   Not Collective
3733552f7358SJed Brown 
3734552f7358SJed Brown   Input Parameters:
3735552f7358SJed Brown + dm           - The DMPlex object
3736552f7358SJed Brown - stratumValue - The requested depth
3737552f7358SJed Brown 
3738552f7358SJed Brown   Output Parameters:
3739552f7358SJed Brown + start - The first point at this depth
3740552f7358SJed Brown - end   - One beyond the last point at this depth
3741552f7358SJed Brown 
3742647867b2SJed Brown   Notes:
3743647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3744647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3745647867b2SJed Brown   higher dimension, e.g., "edges".
3746647867b2SJed Brown 
3747552f7358SJed Brown   Level: developer
3748552f7358SJed Brown 
3749dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
3750552f7358SJed Brown @*/
37510adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37520adebc6cSBarry Smith {
3753aa50250dSMatthew G. Knepley   DMLabel        label;
375463d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3755552f7358SJed Brown   PetscErrorCode ierr;
3756552f7358SJed Brown 
3757552f7358SJed Brown   PetscFunctionBegin;
3758552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
375963d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
376063d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3761552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
37620d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
376363d1a920SMatthew G. Knepley   if (stratumValue < 0) {
376463d1a920SMatthew G. Knepley     if (start) *start = pStart;
376563d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
376663d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3767552f7358SJed Brown   }
3768aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
376963d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
377063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3771552f7358SJed Brown   PetscFunctionReturn(0);
3772552f7358SJed Brown }
3773552f7358SJed Brown 
3774552f7358SJed Brown /*@
3775552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3776552f7358SJed Brown 
3777552f7358SJed Brown   Not Collective
3778552f7358SJed Brown 
3779552f7358SJed Brown   Input Parameters:
3780552f7358SJed Brown + dm           - The DMPlex object
3781552f7358SJed Brown - stratumValue - The requested height
3782552f7358SJed Brown 
3783552f7358SJed Brown   Output Parameters:
3784552f7358SJed Brown + start - The first point at this height
3785552f7358SJed Brown - end   - One beyond the last point at this height
3786552f7358SJed Brown 
3787647867b2SJed Brown   Notes:
3788647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3789647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3790647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3791647867b2SJed Brown 
3792552f7358SJed Brown   Level: developer
3793552f7358SJed Brown 
37943dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
3795552f7358SJed Brown @*/
37960adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37970adebc6cSBarry Smith {
3798aa50250dSMatthew G. Knepley   DMLabel        label;
379963d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3800552f7358SJed Brown   PetscErrorCode ierr;
3801552f7358SJed Brown 
3802552f7358SJed Brown   PetscFunctionBegin;
3803552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
380463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
380563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3806552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
38070d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
380863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
380963d1a920SMatthew G. Knepley     if (start) *start = pStart;
381063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
381163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3812552f7358SJed Brown   }
3813aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
381413903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
381563d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
381663d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3817552f7358SJed Brown   PetscFunctionReturn(0);
3818552f7358SJed Brown }
3819552f7358SJed Brown 
3820ba2698f1SMatthew G. Knepley /*@
3821ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
3822ba2698f1SMatthew G. Knepley 
3823ba2698f1SMatthew G. Knepley   Not Collective
3824ba2698f1SMatthew G. Knepley 
3825ba2698f1SMatthew G. Knepley   Input Parameter:
3826ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
3827ba2698f1SMatthew G. Knepley - point - The point
3828ba2698f1SMatthew G. Knepley 
3829ba2698f1SMatthew G. Knepley   Output Parameter:
3830ba2698f1SMatthew G. Knepley . depth - The depth of the point
3831ba2698f1SMatthew G. Knepley 
3832ba2698f1SMatthew G. Knepley   Level: intermediate
3833ba2698f1SMatthew G. Knepley 
38343dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
3835ba2698f1SMatthew G. Knepley @*/
3836ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
3837ba2698f1SMatthew G. Knepley {
3838ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3839ba2698f1SMatthew G. Knepley 
3840ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3841ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
384240a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
3843ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
3844ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3845ba2698f1SMatthew G. Knepley }
3846ba2698f1SMatthew G. Knepley 
3847ba2698f1SMatthew G. Knepley /*@
38480c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
38490c0a32dcSVaclav Hapla 
38500c0a32dcSVaclav Hapla   Not Collective
38510c0a32dcSVaclav Hapla 
38520c0a32dcSVaclav Hapla   Input Parameter:
38530c0a32dcSVaclav Hapla + dm    - The DMPlex object
38540c0a32dcSVaclav Hapla - point - The point
38550c0a32dcSVaclav Hapla 
38560c0a32dcSVaclav Hapla   Output Parameter:
38570c0a32dcSVaclav Hapla . height - The height of the point
38580c0a32dcSVaclav Hapla 
38590c0a32dcSVaclav Hapla   Level: intermediate
38600c0a32dcSVaclav Hapla 
38613dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
38620c0a32dcSVaclav Hapla @*/
38630c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
38640c0a32dcSVaclav Hapla {
38650c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
38660c0a32dcSVaclav Hapla   PetscErrorCode ierr;
38670c0a32dcSVaclav Hapla 
38680c0a32dcSVaclav Hapla   PetscFunctionBegin;
38690c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38700c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
38710c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
38720c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
38730c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
38740c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
38750c0a32dcSVaclav Hapla }
38760c0a32dcSVaclav Hapla 
38770c0a32dcSVaclav Hapla /*@
3878ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
3879ba2698f1SMatthew G. Knepley 
3880ba2698f1SMatthew G. Knepley   Not Collective
3881ba2698f1SMatthew G. Knepley 
3882ba2698f1SMatthew G. Knepley   Input Parameter:
3883ba2698f1SMatthew G. Knepley . dm - The DMPlex object
3884ba2698f1SMatthew G. Knepley 
3885ba2698f1SMatthew G. Knepley   Output Parameter:
3886ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
3887ba2698f1SMatthew G. Knepley 
3888412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
3889412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
3890412e9a14SMatthew G. Knepley 
3891ba2698f1SMatthew G. Knepley   Level: developer
3892ba2698f1SMatthew G. Knepley 
3893dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
3894ba2698f1SMatthew G. Knepley @*/
3895ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
3896ba2698f1SMatthew G. Knepley {
3897ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3898ba2698f1SMatthew G. Knepley 
3899ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3900ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3901ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
3902ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
3903ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
3904ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3905ba2698f1SMatthew G. Knepley }
3906ba2698f1SMatthew G. Knepley 
3907ba2698f1SMatthew G. Knepley /*@
3908ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
3909ba2698f1SMatthew G. Knepley 
3910ba2698f1SMatthew G. Knepley   Not Collective
3911ba2698f1SMatthew G. Knepley 
3912ba2698f1SMatthew G. Knepley   Input Parameter:
3913ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
3914ba2698f1SMatthew G. Knepley - cell - The cell
3915ba2698f1SMatthew G. Knepley 
3916ba2698f1SMatthew G. Knepley   Output Parameter:
3917ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
3918ba2698f1SMatthew G. Knepley 
3919ba2698f1SMatthew G. Knepley   Level: intermediate
3920ba2698f1SMatthew G. Knepley 
3921ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3922ba2698f1SMatthew G. Knepley @*/
3923ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
3924ba2698f1SMatthew G. Knepley {
3925ba2698f1SMatthew G. Knepley   DMLabel        label;
3926ba2698f1SMatthew G. Knepley   PetscInt       ct;
3927ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3928ba2698f1SMatthew G. Knepley 
3929ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3930ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3931ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
3932ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3933ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
3934ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
3935ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3936ba2698f1SMatthew G. Knepley }
3937ba2698f1SMatthew G. Knepley 
3938412e9a14SMatthew G. Knepley /*@
3939412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
3940412e9a14SMatthew G. Knepley 
3941412e9a14SMatthew G. Knepley   Not Collective
3942412e9a14SMatthew G. Knepley 
3943412e9a14SMatthew G. Knepley   Input Parameters:
3944412e9a14SMatthew G. Knepley + dm   - The DMPlex object
3945412e9a14SMatthew G. Knepley . cell - The cell
3946412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
3947412e9a14SMatthew G. Knepley 
3948412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
3949412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
3950412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
3951412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
3952412e9a14SMatthew G. Knepley 
3953412e9a14SMatthew G. Knepley   Level: advanced
3954412e9a14SMatthew G. Knepley 
3955412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
3956412e9a14SMatthew G. Knepley @*/
3957412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
3958412e9a14SMatthew G. Knepley {
3959412e9a14SMatthew G. Knepley   DMLabel        label;
3960412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3961412e9a14SMatthew G. Knepley 
3962412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3963412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3964412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3965412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
3966412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3967412e9a14SMatthew G. Knepley }
3968412e9a14SMatthew G. Knepley 
39690adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
39700adebc6cSBarry Smith {
3971efe440bfSMatthew G. Knepley   PetscSection   section, s;
3972efe440bfSMatthew G. Knepley   Mat            m;
39733e922f36SToby Isaac   PetscInt       maxHeight;
3974552f7358SJed Brown   PetscErrorCode ierr;
3975552f7358SJed Brown 
3976552f7358SJed Brown   PetscFunctionBegin;
397738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
39783e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
39793e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
398082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
398192fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
39821d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3983efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3984efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3985efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3986efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3987efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
39888f4c458bSMatthew G. Knepley 
39898f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
39908f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3991552f7358SJed Brown   PetscFunctionReturn(0);
3992552f7358SJed Brown }
3993552f7358SJed Brown 
3994f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3995f19dbd58SToby Isaac {
3996f19dbd58SToby Isaac   Vec            coordsLocal;
3997f19dbd58SToby Isaac   DM             coordsDM;
3998f19dbd58SToby Isaac   PetscErrorCode ierr;
3999f19dbd58SToby Isaac 
4000f19dbd58SToby Isaac   PetscFunctionBegin;
4001f19dbd58SToby Isaac   *field = NULL;
4002f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
4003f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
4004f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
4005f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
4006f19dbd58SToby Isaac   }
4007f19dbd58SToby Isaac   PetscFunctionReturn(0);
4008f19dbd58SToby Isaac }
4009f19dbd58SToby Isaac 
40107cd05799SMatthew G. Knepley /*@C
40117cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
40127cd05799SMatthew G. Knepley 
40137cd05799SMatthew G. Knepley   Not Collective
40147cd05799SMatthew G. Knepley 
40157cd05799SMatthew G. Knepley   Input Parameters:
40167cd05799SMatthew G. Knepley . dm        - The DMPlex object
40177cd05799SMatthew G. Knepley 
40187cd05799SMatthew G. Knepley   Output Parameter:
40197cd05799SMatthew G. Knepley . section - The PetscSection object
40207cd05799SMatthew G. Knepley 
40217cd05799SMatthew G. Knepley   Level: developer
40227cd05799SMatthew G. Knepley 
40237cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
40247cd05799SMatthew G. Knepley @*/
40250adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
40260adebc6cSBarry Smith {
4027552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4028552f7358SJed Brown 
4029552f7358SJed Brown   PetscFunctionBegin;
4030552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4031552f7358SJed Brown   if (section) *section = mesh->coneSection;
4032552f7358SJed Brown   PetscFunctionReturn(0);
4033552f7358SJed Brown }
4034552f7358SJed Brown 
40357cd05799SMatthew G. Knepley /*@C
40367cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
40377cd05799SMatthew G. Knepley 
40387cd05799SMatthew G. Knepley   Not Collective
40397cd05799SMatthew G. Knepley 
40407cd05799SMatthew G. Knepley   Input Parameters:
40417cd05799SMatthew G. Knepley . dm        - The DMPlex object
40427cd05799SMatthew G. Knepley 
40437cd05799SMatthew G. Knepley   Output Parameter:
40447cd05799SMatthew G. Knepley . section - The PetscSection object
40457cd05799SMatthew G. Knepley 
40467cd05799SMatthew G. Knepley   Level: developer
40477cd05799SMatthew G. Knepley 
40487cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40497cd05799SMatthew G. Knepley @*/
40508cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
40518cb4d582SMatthew G. Knepley {
40528cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
40538cb4d582SMatthew G. Knepley 
40548cb4d582SMatthew G. Knepley   PetscFunctionBegin;
40558cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40568cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
40578cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
40588cb4d582SMatthew G. Knepley }
40598cb4d582SMatthew G. Knepley 
40607cd05799SMatthew G. Knepley /*@C
40617cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
40627cd05799SMatthew G. Knepley 
40637cd05799SMatthew G. Knepley   Not Collective
40647cd05799SMatthew G. Knepley 
40657cd05799SMatthew G. Knepley   Input Parameters:
40667cd05799SMatthew G. Knepley . dm        - The DMPlex object
40677cd05799SMatthew G. Knepley 
40687cd05799SMatthew G. Knepley   Output Parameter:
40697cd05799SMatthew G. Knepley . cones - The cone for each point
40707cd05799SMatthew G. Knepley 
40717cd05799SMatthew G. Knepley   Level: developer
40727cd05799SMatthew G. Knepley 
40737cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40747cd05799SMatthew G. Knepley @*/
4075a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4076a6dfd86eSKarl Rupp {
4077552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4078552f7358SJed Brown 
4079552f7358SJed Brown   PetscFunctionBegin;
4080552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4081552f7358SJed Brown   if (cones) *cones = mesh->cones;
4082552f7358SJed Brown   PetscFunctionReturn(0);
4083552f7358SJed Brown }
4084552f7358SJed Brown 
40857cd05799SMatthew G. Knepley /*@C
40867cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
40877cd05799SMatthew G. Knepley 
40887cd05799SMatthew G. Knepley   Not Collective
40897cd05799SMatthew G. Knepley 
40907cd05799SMatthew G. Knepley   Input Parameters:
40917cd05799SMatthew G. Knepley . dm        - The DMPlex object
40927cd05799SMatthew G. Knepley 
40937cd05799SMatthew G. Knepley   Output Parameter:
40947cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
40957cd05799SMatthew G. Knepley 
40967cd05799SMatthew G. Knepley   Level: developer
40977cd05799SMatthew G. Knepley 
40987cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40997cd05799SMatthew G. Knepley @*/
4100a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4101a6dfd86eSKarl Rupp {
4102552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4103552f7358SJed Brown 
4104552f7358SJed Brown   PetscFunctionBegin;
4105552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4106552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4107552f7358SJed Brown   PetscFunctionReturn(0);
4108552f7358SJed Brown }
4109552f7358SJed Brown 
4110552f7358SJed Brown /******************************** FEM Support **********************************/
4111552f7358SJed Brown 
41129e8305c2SJed Brown /*
41139e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
41149e8305c2SJed Brown  representing a line in the section.
41159e8305c2SJed Brown */
41169e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
41179e8305c2SJed Brown {
41189e8305c2SJed Brown   PetscErrorCode ierr;
41199e8305c2SJed Brown 
41209e8305c2SJed Brown   PetscFunctionBeginHot;
41219e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4122a433471fSStefano Zampini   if (line < 0) {
4123a433471fSStefano Zampini     *k = 0;
4124a433471fSStefano Zampini     *Nc = 0;
4125a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
41269e8305c2SJed Brown     *k = 1;
41279e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
41289e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
41299e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
41309e8305c2SJed Brown     *k = *k / *Nc + 1;
41319e8305c2SJed Brown   }
41329e8305c2SJed Brown   PetscFunctionReturn(0);
41339e8305c2SJed Brown }
41349e8305c2SJed Brown 
4135a4355906SMatthew Knepley /*@
4136bc1eb3faSJed Brown 
4137bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4138bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
41391bb6d2a8SBarry Smith   section provided (or the section of the DM).
4140a4355906SMatthew Knepley 
4141a4355906SMatthew Knepley   Input Parameters:
4142a4355906SMatthew Knepley + dm      - The DM
4143a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4144a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4145a4355906SMatthew Knepley 
4146a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4147a4355906SMatthew Knepley   degree of the basis.
4148a4355906SMatthew Knepley 
4149bc1eb3faSJed Brown   Example:
4150bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4151bc1eb3faSJed Brown .vb
4152bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4153bc1eb3faSJed Brown 
4154bc1eb3faSJed Brown   v4 -- e6 -- v3
4155bc1eb3faSJed Brown   |           |
4156bc1eb3faSJed Brown   e7    c0    e8
4157bc1eb3faSJed Brown   |           |
4158bc1eb3faSJed Brown   v1 -- e5 -- v2
4159bc1eb3faSJed Brown .ve
4160bc1eb3faSJed Brown 
4161bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4162bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4163bc1eb3faSJed Brown .vb
4164bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4165bc1eb3faSJed Brown     v1 -> [4]
4166bc1eb3faSJed Brown     ...
4167bc1eb3faSJed Brown     e5 -> [8, 9]
4168bc1eb3faSJed Brown .ve
4169bc1eb3faSJed Brown 
4170bc1eb3faSJed Brown   which corresponds to the dofs
4171bc1eb3faSJed Brown .vb
4172bc1eb3faSJed Brown     6   10  11  7
4173bc1eb3faSJed Brown     13  2   3   15
4174bc1eb3faSJed Brown     12  0   1   14
4175bc1eb3faSJed Brown     4   8   9   5
4176bc1eb3faSJed Brown .ve
4177bc1eb3faSJed Brown 
4178bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4179bc1eb3faSJed Brown .vb
4180bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4181bc1eb3faSJed Brown .ve
4182bc1eb3faSJed Brown 
4183bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4184bc1eb3faSJed Brown .vb
4185bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4186bc1eb3faSJed Brown .ve
4187bc1eb3faSJed Brown 
4188a4355906SMatthew Knepley   Level: developer
4189a4355906SMatthew Knepley 
41909df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4191a4355906SMatthew Knepley @*/
4192bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
41933194fc30SMatthew G. Knepley {
41947391a63aSMatthew G. Knepley   DMLabel        label;
4195*bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
41969e8305c2SJed Brown   PetscBool      vertexchart;
41973194fc30SMatthew G. Knepley   PetscErrorCode ierr;
41983194fc30SMatthew G. Knepley 
41993194fc30SMatthew G. Knepley   PetscFunctionBegin;
42003194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4201a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4202a433471fSStefano Zampini   if (point < 0) {
4203a433471fSStefano Zampini     PetscInt sStart,sEnd;
4204a433471fSStefano Zampini 
4205a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4206a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4207a433471fSStefano Zampini   }
42087391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4209a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4210a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
42117391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
42127391a63aSMatthew G. Knepley   else if  (depth == dim) {
42137391a63aSMatthew G. Knepley     const PetscInt *cone;
42147391a63aSMatthew G. Knepley 
42157391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4216d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4217d4e6627bSStefano Zampini     else if (dim == 3) {
4218d4e6627bSStefano Zampini       const PetscInt *cone2;
4219d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4220d4e6627bSStefano Zampini       eStart = cone2[0];
4221d4e6627bSStefano 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);
4222a433471fSStefano 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);
42239e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
42249e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
42259e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
42269e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
42279e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
42289e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
42299e8305c2SJed Brown   }
42303194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4231*bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
4232*bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4233*bb197d40SJed Brown     PetscInt *perm;
4234*bb197d40SJed Brown 
42353194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
42369e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4237*bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
42383194fc30SMatthew G. Knepley     }
42393194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
42403194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4241*bb197d40SJed Brown       switch (d) {
4242babf31e0SJed Brown       case 1:
42439e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4244babf31e0SJed Brown         /*
4245babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4246babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
4247babf31e0SJed Brown          */
4248babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4249babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4250babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4251babf31e0SJed Brown         foffset = offset;
4252babf31e0SJed Brown         break;
425389eabcffSMatthew G. Knepley       case 2:
42543194fc30SMatthew 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} */
42559e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
42563194fc30SMatthew G. Knepley         /* The SEM order is
42573194fc30SMatthew G. Knepley 
42583194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
425989eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
42603194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
42613194fc30SMatthew G. Knepley          */
42623194fc30SMatthew G. Knepley         {
42633194fc30SMatthew G. Knepley           const PetscInt of   = 0;
42643194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
42653194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
42663194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
42673194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
42683194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
42693194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
42703194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
42713194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
42723194fc30SMatthew G. Knepley           PetscInt       o;
42733194fc30SMatthew G. Knepley 
42743194fc30SMatthew G. Knepley           /* bottom */
42753194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
42763194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
42773194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
42783194fc30SMatthew G. Knepley           /* middle */
42793194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
42803194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
42813194fc30SMatthew 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;
42823194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
42833194fc30SMatthew G. Knepley           }
42843194fc30SMatthew G. Knepley           /* top */
42853194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
42863194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
42873194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
42883194fc30SMatthew G. Knepley           foffset = offset;
42893194fc30SMatthew G. Knepley         }
429089eabcffSMatthew G. Knepley         break;
429189eabcffSMatthew G. Knepley       case 3:
429289eabcffSMatthew G. Knepley         /* The original hex closure is
429389eabcffSMatthew G. Knepley 
429489eabcffSMatthew G. Knepley          {c,
429589eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
429689eabcffSMatthew 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,
429789eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
429889eabcffSMatthew G. Knepley          */
42999e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
430089eabcffSMatthew G. Knepley         /* The SEM order is
430189eabcffSMatthew G. Knepley          Bottom Slice
430289eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
430389eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
430489eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
430589eabcffSMatthew G. Knepley 
430689eabcffSMatthew G. Knepley          Middle Slice (j)
430789eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
430889eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
430989eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
431089eabcffSMatthew G. Knepley 
431189eabcffSMatthew G. Knepley          Top Slice
431289eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
431389eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
431489eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
431589eabcffSMatthew G. Knepley          */
431689eabcffSMatthew G. Knepley         {
431789eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
431889eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
431989eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
432089eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
432189eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
432289eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
432389eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
432489eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
432589eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
432689eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
432789eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
432889eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
432989eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
433089eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
433189eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
433289eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
433389eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
433489eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
433589eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
433689eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
433789eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
433889eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
433989eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
434089eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
434189eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
434289eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
434389eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
434489eabcffSMatthew G. Knepley           PetscInt       o, n;
434589eabcffSMatthew G. Knepley 
434689eabcffSMatthew G. Knepley           /* Bottom Slice */
434789eabcffSMatthew G. Knepley           /*   bottom */
434889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
434989eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
435089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
435189eabcffSMatthew G. Knepley           /*   middle */
435289eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
435389eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4354316b7f87SMax 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;}
435589eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
43563194fc30SMatthew G. Knepley           }
435789eabcffSMatthew G. Knepley           /*   top */
435889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
435989eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
436089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
436189eabcffSMatthew G. Knepley 
436289eabcffSMatthew G. Knepley           /* Middle Slice */
436389eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
436489eabcffSMatthew G. Knepley             /*   bottom */
436589eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
436689eabcffSMatthew 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;
436789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
436889eabcffSMatthew G. Knepley             /*   middle */
436989eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
437089eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
437189eabcffSMatthew 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;
437289eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
437389eabcffSMatthew G. Knepley             }
437489eabcffSMatthew G. Knepley             /*   top */
437589eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
437689eabcffSMatthew 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;
437789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
437889eabcffSMatthew G. Knepley           }
437989eabcffSMatthew G. Knepley 
438089eabcffSMatthew G. Knepley           /* Top Slice */
438189eabcffSMatthew G. Knepley           /*   bottom */
438289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
438389eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
438489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
438589eabcffSMatthew G. Knepley           /*   middle */
438689eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
438789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
438889eabcffSMatthew 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;
438989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
439089eabcffSMatthew G. Knepley           }
439189eabcffSMatthew G. Knepley           /*   top */
439289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
439389eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
439489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
439589eabcffSMatthew G. Knepley 
439689eabcffSMatthew G. Knepley           foffset = offset;
439789eabcffSMatthew G. Knepley         }
439889eabcffSMatthew G. Knepley         break;
4399*bb197d40SJed Brown       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
440089eabcffSMatthew G. Knepley       }
440189eabcffSMatthew G. Knepley     }
440289eabcffSMatthew G. Knepley     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
44033194fc30SMatthew G. Knepley     /* Check permutation */
44043194fc30SMatthew G. Knepley     {
44053194fc30SMatthew G. Knepley       PetscInt *check;
44063194fc30SMatthew G. Knepley 
44073194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
44083194fc30SMatthew 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]);}
44093194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
44103194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
44113194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
44123194fc30SMatthew G. Knepley     }
4413*bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
4414*bb197d40SJed Brown   }
44153194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
44163194fc30SMatthew G. Knepley }
44173194fc30SMatthew G. Knepley 
4418e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4419e071409bSToby Isaac {
4420e071409bSToby Isaac   PetscDS        prob;
4421e071409bSToby Isaac   PetscInt       depth, Nf, h;
4422e071409bSToby Isaac   DMLabel        label;
4423e071409bSToby Isaac   PetscErrorCode ierr;
4424e071409bSToby Isaac 
4425e071409bSToby Isaac   PetscFunctionBeginHot;
4426e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4427e071409bSToby Isaac   Nf      = prob->Nf;
4428e071409bSToby Isaac   label   = dm->depthLabel;
4429e071409bSToby Isaac   *dspace = NULL;
4430e071409bSToby Isaac   if (field < Nf) {
4431e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4432e071409bSToby Isaac 
4433e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4434e071409bSToby Isaac       PetscDualSpace dsp;
4435e071409bSToby Isaac 
4436e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4437e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4438e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4439e071409bSToby Isaac       h    = depth - 1 - h;
4440e071409bSToby Isaac       if (h) {
4441e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4442e071409bSToby Isaac       } else {
4443e071409bSToby Isaac         *dspace = dsp;
4444e071409bSToby Isaac       }
4445e071409bSToby Isaac     }
4446e071409bSToby Isaac   }
4447e071409bSToby Isaac   PetscFunctionReturn(0);
4448e071409bSToby Isaac }
4449e071409bSToby Isaac 
4450e071409bSToby Isaac 
44511a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4452a6dfd86eSKarl Rupp {
4453552f7358SJed Brown   PetscScalar    *array, *vArray;
4454d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
44551a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4456552f7358SJed Brown   PetscErrorCode  ierr;
4457552f7358SJed Brown 
44581b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
44592a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
44605a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
44615a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
44625a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
44633f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
44649df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
44659df71ca4SMatthew G. Knepley       PetscInt dof;
4466d9917b9dSMatthew G. Knepley 
44679df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
44689df71ca4SMatthew G. Knepley       size += dof;
44699df71ca4SMatthew G. Knepley     }
44709df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
44719df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
44722a3aaacfSMatthew G. Knepley       PetscInt       dof;
44735a1bb5cfSMatthew G. Knepley 
44745a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
44752a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
44765a1bb5cfSMatthew G. Knepley       size += dof;
44775a1bb5cfSMatthew G. Knepley     }
44783f7cbbe7SMatthew G. Knepley     if (!values) {
44793f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
44803f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
44813f7cbbe7SMatthew G. Knepley     }
448269291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4483982e9ed1SMatthew G. Knepley   } else {
4484982e9ed1SMatthew G. Knepley     array = *values;
4485982e9ed1SMatthew G. Knepley   }
44869df71ca4SMatthew G. Knepley   size = 0;
44875a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
44889df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
44899df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
44909df71ca4SMatthew G. Knepley     PetscScalar *varr;
4491d9917b9dSMatthew G. Knepley 
44929df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
44939df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
44949df71ca4SMatthew G. Knepley     varr = &vArray[off];
44951a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
44961a271a75SMatthew G. Knepley       array[offset] = varr[d];
44979df71ca4SMatthew G. Knepley     }
44989df71ca4SMatthew G. Knepley     size += dof;
44999df71ca4SMatthew G. Knepley   }
45009df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
45019df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
45029df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
45035a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
45045a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
45055a1bb5cfSMatthew G. Knepley 
450652ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
45075a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
45085a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
45095a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
45105a1bb5cfSMatthew G. Knepley     if (o >= 0) {
45111a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
45121a271a75SMatthew G. Knepley         array[offset] = varr[d];
45135a1bb5cfSMatthew G. Knepley       }
45145a1bb5cfSMatthew G. Knepley     } else {
45151a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
45161a271a75SMatthew G. Knepley         array[offset] = varr[d];
45175a1bb5cfSMatthew G. Knepley       }
45185a1bb5cfSMatthew G. Knepley     }
45199df71ca4SMatthew G. Knepley     size += dof;
45205a1bb5cfSMatthew G. Knepley   }
45215a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
45229df71ca4SMatthew G. Knepley   if (!*values) {
45235a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
45245a1bb5cfSMatthew G. Knepley     *values = array;
45259df71ca4SMatthew G. Knepley   } else {
45268ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
45278c312ff3SMatthew G. Knepley     *csize = size;
45289df71ca4SMatthew G. Knepley   }
45295a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
45305a1bb5cfSMatthew G. Knepley }
4531d9917b9dSMatthew G. Knepley 
453227f02ce8SMatthew G. Knepley /* Compress out points not in the section */
453327f02ce8SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
453427f02ce8SMatthew G. Knepley {
453527f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
453627f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
453727f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
453827f02ce8SMatthew G. Knepley 
453927f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
454027f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
454127f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
454227f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
454327f02ce8SMatthew G. Knepley       points[q*2]   = r;
454427f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
454527f02ce8SMatthew G. Knepley       ++q;
454627f02ce8SMatthew G. Knepley     }
454727f02ce8SMatthew G. Knepley   }
454827f02ce8SMatthew G. Knepley   *numPoints = q;
454927f02ce8SMatthew G. Knepley   return 0;
455027f02ce8SMatthew G. Knepley }
455127f02ce8SMatthew G. Knepley 
455227f02ce8SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
455327f02ce8SMatthew G. Knepley {
455427f02ce8SMatthew G. Knepley   const PetscInt *cone, *ornt;
455527f02ce8SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
455627f02ce8SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
455727f02ce8SMatthew G. Knepley   PetscErrorCode  ierr;
455827f02ce8SMatthew G. Knepley 
455927f02ce8SMatthew G. Knepley   PetscFunctionBeginHot;
456027f02ce8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
456127f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
456227f02ce8SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
456327f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
456427f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
456527f02ce8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, np*2, MPIU_INT, &pts);CHKERRQ(ierr);
456627f02ce8SMatthew G. Knepley   c    = 0;
456727f02ce8SMatthew G. Knepley   pts[c*2+0] = point;
456827f02ce8SMatthew G. Knepley   pts[c*2+1] = 0;
456927f02ce8SMatthew G. Knepley   ++c;
457027f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
457127f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
457227f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
457327f02ce8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
457427f02ce8SMatthew G. Knepley   if (dim >= 2) {
457527f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
457627f02ce8SMatthew G. Knepley   }
457727f02ce8SMatthew G. Knepley   if (dim >= 3) {
457827f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
457927f02ce8SMatthew G. Knepley       const PetscInt  fpoint = cone[d];
458027f02ce8SMatthew G. Knepley       const PetscInt *fcone;
458127f02ce8SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
458227f02ce8SMatthew G. Knepley 
458327f02ce8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
458427f02ce8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
458527f02ce8SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
458627f02ce8SMatthew G. Knepley         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
458727f02ce8SMatthew G. Knepley         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
458827f02ce8SMatthew G. Knepley       }
458927f02ce8SMatthew G. Knepley     }
459027f02ce8SMatthew G. Knepley   }
459127f02ce8SMatthew G. Knepley   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
459227f02ce8SMatthew G. Knepley   *numPoints = np;
459327f02ce8SMatthew G. Knepley   *points    = pts;
459427f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
459527f02ce8SMatthew G. Knepley }
459627f02ce8SMatthew G. Knepley 
459797529cf3SJed Brown /* Compressed closure does not apply closure permutation */
45981dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4599923c78e0SToby Isaac {
460027f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
4601923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4602923c78e0SToby Isaac   PetscErrorCode ierr;
4603923c78e0SToby Isaac 
4604923c78e0SToby Isaac   PetscFunctionBeginHot;
4605923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
460627f02ce8SMatthew G. Knepley   if (*clPoints) {
4607923c78e0SToby Isaac     PetscInt dof, off;
4608923c78e0SToby Isaac 
4609923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4610923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4611923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4612923c78e0SToby Isaac     np   = dof/2;
4613923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
461427f02ce8SMatthew G. Knepley   } else {
4615665f567fSMatthew G. Knepley     DMPolytopeType ct;
4616665f567fSMatthew G. Knepley 
461727f02ce8SMatthew G. Knepley     /* Do not make the label if it does not exist */
461827f02ce8SMatthew G. Knepley     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
461927f02ce8SMatthew G. Knepley     else                    {ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);}
462027f02ce8SMatthew G. Knepley     switch (ct) {
462127f02ce8SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
462227f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);CHKERRQ(ierr);
462327f02ce8SMatthew G. Knepley         break;
462427f02ce8SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
462527f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);CHKERRQ(ierr);
462627f02ce8SMatthew G. Knepley         break;
462727f02ce8SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
462827f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);CHKERRQ(ierr);
462927f02ce8SMatthew G. Knepley         break;
463027f02ce8SMatthew G. Knepley       default:
463127f02ce8SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
463227f02ce8SMatthew G. Knepley     }
463327f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
4634923c78e0SToby Isaac   }
4635923c78e0SToby Isaac   *numPoints = np;
4636923c78e0SToby Isaac   *points    = pts;
4637923c78e0SToby Isaac   *clp       = cla;
4638923c78e0SToby Isaac   PetscFunctionReturn(0);
4639923c78e0SToby Isaac }
4640923c78e0SToby Isaac 
46411dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4642923c78e0SToby Isaac {
4643923c78e0SToby Isaac   PetscErrorCode ierr;
4644923c78e0SToby Isaac 
4645923c78e0SToby Isaac   PetscFunctionBeginHot;
4646923c78e0SToby Isaac   if (!*clPoints) {
4647923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4648923c78e0SToby Isaac   } else {
4649923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4650923c78e0SToby Isaac   }
4651923c78e0SToby Isaac   *numPoints = 0;
4652923c78e0SToby Isaac   *points    = NULL;
4653923c78e0SToby Isaac   *clSec     = NULL;
4654923c78e0SToby Isaac   *clPoints  = NULL;
4655923c78e0SToby Isaac   *clp       = NULL;
4656923c78e0SToby Isaac   PetscFunctionReturn(0);
4657923c78e0SToby Isaac }
4658923c78e0SToby Isaac 
465997e99dd9SToby 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[])
46601a271a75SMatthew G. Knepley {
46611a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
466297e99dd9SToby Isaac   const PetscInt    **perms = NULL;
466397e99dd9SToby Isaac   const PetscScalar **flips = NULL;
46641a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
46651a271a75SMatthew G. Knepley 
46661a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4667fe02ba77SJed Brown   *size = 0;
466897e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
466997e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
467097e99dd9SToby Isaac     const PetscInt    point = points[2*p];
467197e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
467297e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
46731a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
46741a271a75SMatthew G. Knepley     const PetscScalar *varr;
46751a271a75SMatthew G. Knepley 
46761a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
46771a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
46781a271a75SMatthew G. Knepley     varr = &vArray[off];
467997e99dd9SToby Isaac     if (clperm) {
468097e99dd9SToby Isaac       if (perm) {
468197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
46821a271a75SMatthew G. Knepley       } else {
468397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
468497e99dd9SToby Isaac       }
468597e99dd9SToby Isaac       if (flip) {
468697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
468797e99dd9SToby Isaac       }
468897e99dd9SToby Isaac     } else {
468997e99dd9SToby Isaac       if (perm) {
469097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
469197e99dd9SToby Isaac       } else {
469297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
469397e99dd9SToby Isaac       }
469497e99dd9SToby Isaac       if (flip) {
469597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
46961a271a75SMatthew G. Knepley       }
46971a271a75SMatthew G. Knepley     }
469897e99dd9SToby Isaac     offset += dof;
469997e99dd9SToby Isaac   }
470097e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
47011a271a75SMatthew G. Knepley   *size = offset;
47021a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
47031a271a75SMatthew G. Knepley }
47041a271a75SMatthew G. Knepley 
470597e99dd9SToby 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[])
47061a271a75SMatthew G. Knepley {
47071a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
47081a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
47091a271a75SMatthew G. Knepley 
47101a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4711fe02ba77SJed Brown   *size = 0;
47121a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
471397e99dd9SToby Isaac     PetscInt          p;
471497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
471597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
47161a271a75SMatthew G. Knepley 
471797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
471897e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
471997e99dd9SToby Isaac       const PetscInt    point = points[2*p];
472097e99dd9SToby Isaac       PetscInt          fdof, foff, b;
47211a271a75SMatthew G. Knepley       const PetscScalar *varr;
472297e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
472397e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
47241a271a75SMatthew G. Knepley 
47251a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
47261a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
47271a271a75SMatthew G. Knepley       varr = &vArray[foff];
472897e99dd9SToby Isaac       if (clperm) {
472997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
473097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
473197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
47321a271a75SMatthew G. Knepley       } else {
473397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
473497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
473597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
47361a271a75SMatthew G. Knepley       }
473797e99dd9SToby Isaac       offset += fdof;
47381a271a75SMatthew G. Knepley     }
473997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
47401a271a75SMatthew G. Knepley   }
47411a271a75SMatthew G. Knepley   *size = offset;
47421a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
47431a271a75SMatthew G. Knepley }
47441a271a75SMatthew G. Knepley 
4745552f7358SJed Brown /*@C
4746552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4747552f7358SJed Brown 
4748552f7358SJed Brown   Not collective
4749552f7358SJed Brown 
4750552f7358SJed Brown   Input Parameters:
4751552f7358SJed Brown + dm - The DM
4752552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4753552f7358SJed Brown . v - The local vector
475422c1ee49SMatthew G. Knepley . point - The point in the DM
475522c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
475622c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4757552f7358SJed Brown 
4758552f7358SJed Brown   Output Parameters:
475922c1ee49SMatthew G. Knepley + csize - The number of values in the closure
476022c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
476122c1ee49SMatthew G. Knepley 
476222c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
476322c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
476422c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
476522c1ee49SMatthew G. Knepley $
476622c1ee49SMatthew G. Knepley $ A typical use could be
476722c1ee49SMatthew G. Knepley $
476822c1ee49SMatthew G. Knepley $  values = NULL;
476922c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
477022c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
477122c1ee49SMatthew G. Knepley $    <Compute on closure>
477222c1ee49SMatthew G. Knepley $  }
477322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
477422c1ee49SMatthew G. Knepley $
477522c1ee49SMatthew G. Knepley $ or
477622c1ee49SMatthew G. Knepley $
477722c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
477822c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
477922c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
478022c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
478122c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
478222c1ee49SMatthew G. Knepley $      <Compute on closure>
478322c1ee49SMatthew G. Knepley $    }
478422c1ee49SMatthew G. Knepley $  }
478522c1ee49SMatthew G. Knepley $  PetscFree(values);
4786552f7358SJed Brown 
4787552f7358SJed Brown   Fortran Notes:
4788552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4789552f7358SJed Brown   include petsc.h90 in your code.
4790552f7358SJed Brown 
4791552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4792552f7358SJed Brown 
4793552f7358SJed Brown   Level: intermediate
4794552f7358SJed Brown 
4795552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4796552f7358SJed Brown @*/
4797552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4798552f7358SJed Brown {
4799552f7358SJed Brown   PetscSection       clSection;
4800d9917b9dSMatthew G. Knepley   IS                 clPoints;
4801552f7358SJed Brown   PetscInt          *points = NULL;
4802c459fbc1SJed Brown   const PetscInt    *clp, *perm;
4803c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
4804552f7358SJed Brown   PetscErrorCode     ierr;
4805552f7358SJed Brown 
4806d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4807552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
480892fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
48091a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
48101a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4811552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4812552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4813552f7358SJed Brown   if (depth == 1 && numFields < 2) {
48141a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4815552f7358SJed Brown     PetscFunctionReturn(0);
4816552f7358SJed Brown   }
48171a271a75SMatthew G. Knepley   /* Get points */
4818923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4819c459fbc1SJed Brown   /* Get sizes */
4820c459fbc1SJed Brown   asize = 0;
4821c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
4822c459fbc1SJed Brown     PetscInt dof;
4823552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
48241a271a75SMatthew G. Knepley     asize += dof;
4825552f7358SJed Brown   }
4826c459fbc1SJed Brown   if (values) {
4827c459fbc1SJed Brown     const PetscScalar *vArray;
4828c459fbc1SJed Brown     PetscInt          size;
4829c459fbc1SJed Brown 
4830c459fbc1SJed Brown     if (*values) {
4831c459fbc1SJed Brown       if (PetscUnlikely(*csize < asize)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Provided array size %D not sufficient to hold closure size %D", *csize, asize);
4832c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
4833c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
48348a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
48351a271a75SMatthew G. Knepley     /* Get values */
4836c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
4837c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
4838c459fbc1SJed Brown     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
48391a271a75SMatthew G. Knepley     /* Cleanup array */
48408a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4841d0f6b257SMatthew G. Knepley   }
4842c459fbc1SJed Brown   if (csize) *csize = asize;
4843c459fbc1SJed Brown   /* Cleanup points */
4844c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4845552f7358SJed Brown   PetscFunctionReturn(0);
4846552f7358SJed Brown }
4847552f7358SJed Brown 
4848e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
4849e5c487bfSMatthew G. Knepley {
4850e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
4851e5c487bfSMatthew G. Knepley   PetscSection       clSection;
4852e5c487bfSMatthew G. Knepley   IS                 clPoints;
4853e5c487bfSMatthew G. Knepley   PetscScalar       *array;
4854e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
4855e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
4856c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
4857c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
4858e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
4859e5c487bfSMatthew G. Knepley 
4860e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
4861e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4862e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
4863e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4864e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4865e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
4866e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
4867e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4868e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
4869e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4870e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
4871e5c487bfSMatthew G. Knepley   }
4872e5c487bfSMatthew G. Knepley   /* Get points */
4873e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4874c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
4875c459fbc1SJed Brown     PetscInt dof;
4876c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
4877c459fbc1SJed Brown     clsize += dof;
4878c459fbc1SJed Brown   }
4879c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
4880e5c487bfSMatthew G. Knepley   /* Filter points */
4881e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
4882e5c487bfSMatthew G. Knepley     PetscInt dep;
4883e5c487bfSMatthew G. Knepley 
4884e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
4885e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
4886e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
4887e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
4888e5c487bfSMatthew G. Knepley     ++Np;
4889e5c487bfSMatthew G. Knepley   }
4890e5c487bfSMatthew G. Knepley   /* Get array */
4891e5c487bfSMatthew G. Knepley   if (!values || !*values) {
4892e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
4893e5c487bfSMatthew G. Knepley 
4894e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
4895e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
4896e5c487bfSMatthew G. Knepley       asize += dof;
4897e5c487bfSMatthew G. Knepley     }
4898e5c487bfSMatthew G. Knepley     if (!values) {
4899e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4900e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
4901e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
4902e5c487bfSMatthew G. Knepley     }
4903e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4904e5c487bfSMatthew G. Knepley   } else {
4905e5c487bfSMatthew G. Knepley     array = *values;
4906e5c487bfSMatthew G. Knepley   }
4907e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
4908e5c487bfSMatthew G. Knepley   /* Get values */
4909e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
4910e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
4911e5c487bfSMatthew G. Knepley   /* Cleanup points */
4912e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4913e5c487bfSMatthew G. Knepley   /* Cleanup array */
4914e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4915e5c487bfSMatthew G. Knepley   if (!*values) {
4916e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
4917e5c487bfSMatthew G. Knepley     *values = array;
4918e5c487bfSMatthew G. Knepley   } else {
4919e5c487bfSMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4920e5c487bfSMatthew G. Knepley     *csize = size;
4921e5c487bfSMatthew G. Knepley   }
4922e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
4923e5c487bfSMatthew G. Knepley }
4924e5c487bfSMatthew G. Knepley 
4925552f7358SJed Brown /*@C
4926552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4927552f7358SJed Brown 
4928552f7358SJed Brown   Not collective
4929552f7358SJed Brown 
4930552f7358SJed Brown   Input Parameters:
4931552f7358SJed Brown + dm - The DM
49320298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4933552f7358SJed Brown . v - The local vector
4934eaf898f9SPatrick Sanan . point - The point in the DM
49350298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4936552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4937552f7358SJed Brown 
493822c1ee49SMatthew 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()
493922c1ee49SMatthew G. Knepley 
49403813dfbdSMatthew G Knepley   Fortran Notes:
49413813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
49423813dfbdSMatthew G Knepley   include petsc.h90 in your code.
49433813dfbdSMatthew G Knepley 
49443813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
49453813dfbdSMatthew G Knepley 
4946552f7358SJed Brown   Level: intermediate
4947552f7358SJed Brown 
4948552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4949552f7358SJed Brown @*/
49507c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4951a6dfd86eSKarl Rupp {
4952552f7358SJed Brown   PetscInt       size = 0;
4953552f7358SJed Brown   PetscErrorCode ierr;
4954552f7358SJed Brown 
4955552f7358SJed Brown   PetscFunctionBegin;
4956552f7358SJed Brown   /* Should work without recalculating size */
495769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4958c9fdaa05SMatthew G. Knepley   *values = NULL;
4959552f7358SJed Brown   PetscFunctionReturn(0);
4960552f7358SJed Brown }
4961552f7358SJed Brown 
4962552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4963552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4964552f7358SJed Brown 
496597e99dd9SToby 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[])
4966552f7358SJed Brown {
4967552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4968552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4969552f7358SJed Brown   PetscScalar    *a;
4970552f7358SJed Brown   PetscInt        off, cind = 0, k;
4971552f7358SJed Brown   PetscErrorCode  ierr;
4972552f7358SJed Brown 
4973552f7358SJed Brown   PetscFunctionBegin;
4974552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4975552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4976552f7358SJed Brown   a    = &array[off];
4977552f7358SJed Brown   if (!cdof || setBC) {
497897e99dd9SToby Isaac     if (clperm) {
497997e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
498097e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4981552f7358SJed Brown     } else {
498297e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
498397e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4984552f7358SJed Brown     }
4985552f7358SJed Brown   } else {
4986552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
498797e99dd9SToby Isaac     if (clperm) {
498897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4989552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
499097e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4991552f7358SJed Brown         }
4992552f7358SJed Brown       } else {
4993552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4994552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
499597e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
499697e99dd9SToby Isaac         }
499797e99dd9SToby Isaac       }
499897e99dd9SToby Isaac     } else {
499997e99dd9SToby Isaac       if (perm) {
500097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
500197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
500297e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
500397e99dd9SToby Isaac         }
500497e99dd9SToby Isaac       } else {
500597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
500697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
500797e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
500897e99dd9SToby Isaac         }
5009552f7358SJed Brown       }
5010552f7358SJed Brown     }
5011552f7358SJed Brown   }
5012552f7358SJed Brown   PetscFunctionReturn(0);
5013552f7358SJed Brown }
5014552f7358SJed Brown 
501597e99dd9SToby 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[])
5016a5e93ea8SMatthew G. Knepley {
5017a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5018a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5019a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5020a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5021a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5022a5e93ea8SMatthew G. Knepley 
5023a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5024a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5025a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5026a5e93ea8SMatthew G. Knepley   a    = &array[off];
5027a5e93ea8SMatthew G. Knepley   if (cdof) {
5028a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
502997e99dd9SToby Isaac     if (clperm) {
503097e99dd9SToby Isaac       if (perm) {
5031a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5032a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
503397e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
503497e99dd9SToby Isaac             cind++;
5035a5e93ea8SMatthew G. Knepley           }
5036a5e93ea8SMatthew G. Knepley         }
5037a5e93ea8SMatthew G. Knepley       } else {
5038a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5039a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
504097e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
504197e99dd9SToby Isaac             cind++;
504297e99dd9SToby Isaac           }
504397e99dd9SToby Isaac         }
504497e99dd9SToby Isaac       }
504597e99dd9SToby Isaac     } else {
504697e99dd9SToby Isaac       if (perm) {
504797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
504897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
504997e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
505097e99dd9SToby Isaac             cind++;
505197e99dd9SToby Isaac           }
505297e99dd9SToby Isaac         }
505397e99dd9SToby Isaac       } else {
505497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
505597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
505697e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
505797e99dd9SToby Isaac             cind++;
505897e99dd9SToby Isaac           }
5059a5e93ea8SMatthew G. Knepley         }
5060a5e93ea8SMatthew G. Knepley       }
5061a5e93ea8SMatthew G. Knepley     }
5062a5e93ea8SMatthew G. Knepley   }
5063a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5064a5e93ea8SMatthew G. Knepley }
5065a5e93ea8SMatthew G. Knepley 
506697e99dd9SToby 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[])
5067a6dfd86eSKarl Rupp {
5068552f7358SJed Brown   PetscScalar    *a;
50691a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
50701a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
507197e99dd9SToby Isaac   PetscInt        cind = 0, b;
5072552f7358SJed Brown   PetscErrorCode  ierr;
5073552f7358SJed Brown 
5074552f7358SJed Brown   PetscFunctionBegin;
5075552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5076552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
50771a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
50781a271a75SMatthew G. Knepley   a    = &array[foff];
5079552f7358SJed Brown   if (!fcdof || setBC) {
508097e99dd9SToby Isaac     if (clperm) {
508197e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
508297e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5083552f7358SJed Brown     } else {
508497e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
508597e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5086552f7358SJed Brown     }
5087552f7358SJed Brown   } else {
5088552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
508997e99dd9SToby Isaac     if (clperm) {
509097e99dd9SToby Isaac       if (perm) {
509197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
509297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
509397e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5094552f7358SJed Brown         }
5095552f7358SJed Brown       } else {
509697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
509797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
509897e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
509997e99dd9SToby Isaac         }
510097e99dd9SToby Isaac       }
510197e99dd9SToby Isaac     } else {
510297e99dd9SToby Isaac       if (perm) {
510397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
510497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
510597e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
510697e99dd9SToby Isaac         }
510797e99dd9SToby Isaac       } else {
510897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
510997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
511097e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5111552f7358SJed Brown         }
5112552f7358SJed Brown       }
5113552f7358SJed Brown     }
5114552f7358SJed Brown   }
51151a271a75SMatthew G. Knepley   *offset += fdof;
5116552f7358SJed Brown   PetscFunctionReturn(0);
5117552f7358SJed Brown }
5118552f7358SJed Brown 
5119ba322698SMatthew 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[])
5120a5e93ea8SMatthew G. Knepley {
5121a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
51221a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
51231a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
51245da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
5125ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
5126a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5127a5e93ea8SMatthew G. Knepley 
5128a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
51295da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
5130a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5131a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
51321a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
51331a271a75SMatthew G. Knepley   a    = &array[foff];
5134a5e93ea8SMatthew G. Knepley   if (fcdof) {
5135ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
5136a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
513797e99dd9SToby Isaac     if (clperm) {
513897e99dd9SToby Isaac       if (perm) {
5139ba322698SMatthew G. Knepley         if (comps) {
5140ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5141ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
51425da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5143ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5144ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5145ba322698SMatthew G. Knepley           }
5146ba322698SMatthew G. Knepley         } else {
514797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
514897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
514997e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5150a5e93ea8SMatthew G. Knepley               ++cind;
5151a5e93ea8SMatthew G. Knepley             }
5152a5e93ea8SMatthew G. Knepley           }
5153ba322698SMatthew G. Knepley         }
5154ba322698SMatthew G. Knepley       } else {
5155ba322698SMatthew G. Knepley         if (comps) {
5156ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5157ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
51585da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5159ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5160ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5161ba322698SMatthew G. Knepley           }
5162a5e93ea8SMatthew G. Knepley         } else {
516397e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
516497e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
516597e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
516697e99dd9SToby Isaac               ++cind;
516797e99dd9SToby Isaac             }
516897e99dd9SToby Isaac           }
516997e99dd9SToby Isaac         }
5170ba322698SMatthew G. Knepley       }
517197e99dd9SToby Isaac     } else {
517297e99dd9SToby Isaac       if (perm) {
5173ba322698SMatthew G. Knepley         if (comps) {
5174ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5175ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
51765da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5177ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5178ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5179ba322698SMatthew G. Knepley           }
5180ba322698SMatthew G. Knepley         } else {
518197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
518297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
518397e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
518497e99dd9SToby Isaac               ++cind;
518597e99dd9SToby Isaac             }
518697e99dd9SToby Isaac           }
5187ba322698SMatthew G. Knepley         }
5188ba322698SMatthew G. Knepley       } else {
5189ba322698SMatthew G. Knepley         if (comps) {
5190ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5191ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
51925da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5193ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5194ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5195ba322698SMatthew G. Knepley           }
519697e99dd9SToby Isaac         } else {
519797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
519897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
519997e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5200a5e93ea8SMatthew G. Knepley               ++cind;
5201a5e93ea8SMatthew G. Knepley             }
5202a5e93ea8SMatthew G. Knepley           }
5203a5e93ea8SMatthew G. Knepley         }
5204a5e93ea8SMatthew G. Knepley       }
5205a5e93ea8SMatthew G. Knepley     }
5206ba322698SMatthew G. Knepley   }
52071a271a75SMatthew G. Knepley   *offset += fdof;
5208a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5209a5e93ea8SMatthew G. Knepley }
5210a5e93ea8SMatthew G. Knepley 
52118f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5212a6dfd86eSKarl Rupp {
5213552f7358SJed Brown   PetscScalar    *array;
52141b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
52151b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5216552f7358SJed Brown   PetscErrorCode  ierr;
5217552f7358SJed Brown 
52181b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5219b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5220b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5221b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5222b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5223b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5224b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5225b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5226b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5227b6ebb6e6SMatthew G. Knepley 
5228b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5229b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5230b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5231b6ebb6e6SMatthew G. Knepley     {
5232b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5233b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5234b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5235b6ebb6e6SMatthew G. Knepley 
5236b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5237b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5238b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5239b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5240b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5241b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5242b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5243b6ebb6e6SMatthew G. Knepley           }
5244b6ebb6e6SMatthew G. Knepley         } else {
5245b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5246b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5247b6ebb6e6SMatthew G. Knepley           }
5248b6ebb6e6SMatthew G. Knepley         }
5249b6ebb6e6SMatthew G. Knepley       } else {
5250b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5251b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5252b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5253b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5254b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5255b6ebb6e6SMatthew G. Knepley           }
5256b6ebb6e6SMatthew G. Knepley         } else {
5257b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5258b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5259b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5260b6ebb6e6SMatthew G. Knepley           }
5261b6ebb6e6SMatthew G. Knepley         }
5262b6ebb6e6SMatthew G. Knepley       }
5263b6ebb6e6SMatthew G. Knepley     }
5264b6ebb6e6SMatthew G. Knepley   }
5265b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5266b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5267b6ebb6e6SMatthew G. Knepley }
52681b406b76SMatthew G. Knepley 
52691b406b76SMatthew G. Knepley /*@C
52701b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
52711b406b76SMatthew G. Knepley 
52721b406b76SMatthew G. Knepley   Not collective
52731b406b76SMatthew G. Knepley 
52741b406b76SMatthew G. Knepley   Input Parameters:
52751b406b76SMatthew G. Knepley + dm - The DM
52761b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
52771b406b76SMatthew G. Knepley . v - The local vector
5278eaf898f9SPatrick Sanan . point - The point in the DM
52791b406b76SMatthew G. Knepley . values - The array of values
528022c1ee49SMatthew 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,
528122c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
52821b406b76SMatthew G. Knepley 
52831b406b76SMatthew G. Knepley   Fortran Notes:
52841b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
52851b406b76SMatthew G. Knepley 
52861b406b76SMatthew G. Knepley   Level: intermediate
52871b406b76SMatthew G. Knepley 
52881b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
52891b406b76SMatthew G. Knepley @*/
52901b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
52911b406b76SMatthew G. Knepley {
52921b406b76SMatthew G. Knepley   PetscSection    clSection;
52931b406b76SMatthew G. Knepley   IS              clPoints;
52941b406b76SMatthew G. Knepley   PetscScalar    *array;
52951b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
529627f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
5297c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
52981b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
52991b406b76SMatthew G. Knepley 
53001a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
53011b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
530292fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
53031a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
53041a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
53051b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
53061b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
53071b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
53088f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
53091b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
53101b406b76SMatthew G. Knepley   }
53111a271a75SMatthew G. Knepley   /* Get points */
5312923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5313c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
5314c459fbc1SJed Brown     PetscInt dof;
5315c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5316c459fbc1SJed Brown     clsize += dof;
5317c459fbc1SJed Brown   }
5318c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
53191a271a75SMatthew G. Knepley   /* Get array */
5320552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
53211a271a75SMatthew G. Knepley   /* Get values */
5322ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
532397e99dd9SToby Isaac     PetscInt offset = 0, f;
5324552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
532597e99dd9SToby Isaac       const PetscInt    **perms = NULL;
532697e99dd9SToby Isaac       const PetscScalar **flips = NULL;
532797e99dd9SToby Isaac 
532897e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5329552f7358SJed Brown       switch (mode) {
5330552f7358SJed Brown       case INSERT_VALUES:
533197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
533297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
533397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
533497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
533597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5336552f7358SJed Brown         } break;
5337552f7358SJed Brown       case INSERT_ALL_VALUES:
533897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
533997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
534097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
534197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
534297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5343552f7358SJed Brown         } break;
5344a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
534597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
534697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
534797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
534897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5349ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5350a5e93ea8SMatthew G. Knepley         } break;
5351552f7358SJed Brown       case ADD_VALUES:
535297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
535397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
535497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
535597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
535697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5357552f7358SJed Brown         } break;
5358552f7358SJed Brown       case ADD_ALL_VALUES:
535997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
536097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
536197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
536297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
536397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5364552f7358SJed Brown         } break;
5365304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
536697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
536797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
536897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
536997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5370ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5371304ab55fSMatthew G. Knepley         } break;
5372552f7358SJed Brown       default:
5373f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5374552f7358SJed Brown       }
537597e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
53761a271a75SMatthew G. Knepley     }
5377552f7358SJed Brown   } else {
53781a271a75SMatthew G. Knepley     PetscInt dof, off;
537997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
538097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
53811a271a75SMatthew G. Knepley 
538297e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5383552f7358SJed Brown     switch (mode) {
5384552f7358SJed Brown     case INSERT_VALUES:
538597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
538697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
538797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
538897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
538997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
539097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5391552f7358SJed Brown       } break;
5392552f7358SJed Brown     case INSERT_ALL_VALUES:
539397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
539497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
539597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
539697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
539797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
539897e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5399552f7358SJed Brown       } break;
5400a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
540197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
540297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
540397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
540497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
540597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
540697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5407a5e93ea8SMatthew G. Knepley       } break;
5408552f7358SJed Brown     case ADD_VALUES:
540997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
541097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
541197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
541297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
541397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
541497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5415552f7358SJed Brown       } break;
5416552f7358SJed Brown     case ADD_ALL_VALUES:
541797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
541897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
541997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
542097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
542197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
542297e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5423552f7358SJed Brown       } break;
5424304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
542597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
542697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
542797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
542897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
542997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
543097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5431304ab55fSMatthew G. Knepley       } break;
5432552f7358SJed Brown     default:
5433f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5434552f7358SJed Brown     }
543597e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5436552f7358SJed Brown   }
54371a271a75SMatthew G. Knepley   /* Cleanup points */
5438923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54391a271a75SMatthew G. Knepley   /* Cleanup array */
5440552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5441552f7358SJed Brown   PetscFunctionReturn(0);
5442552f7358SJed Brown }
5443552f7358SJed Brown 
54445f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
54455f790a90SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
54465f790a90SMatthew G. Knepley {
54475f790a90SMatthew G. Knepley   PetscFunctionBegin;
54485f790a90SMatthew G. Knepley   if (label) {
54495f790a90SMatthew G. Knepley     PetscInt       val, fdof;
54505f790a90SMatthew G. Knepley     PetscErrorCode ierr;
54515f790a90SMatthew G. Knepley 
54525f790a90SMatthew G. Knepley     /* There is a problem with this:
54535f790a90SMatthew G. Knepley          Suppose we have two label values, defining surfaces, interecting along a line in 3D. When we add cells to the label, the cells that
54545f790a90SMatthew G. Knepley        touch both surfaces must pick a label value. Thus we miss setting values for the surface with that other value intersecting that cell.
54555f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
54565f790a90SMatthew G. Knepley     */
54575f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
54585f790a90SMatthew G. Knepley     if (val < 0) {
54595f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
54605f790a90SMatthew G. Knepley       *offset += fdof;
54615f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
54625f790a90SMatthew G. Knepley     }
54635f790a90SMatthew G. Knepley   }
54645f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
54655f790a90SMatthew G. Knepley }
54665f790a90SMatthew G. Knepley 
546797529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
54685f790a90SMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], DMLabel label, PetscInt labelId, const PetscScalar values[], InsertMode mode)
5469e07394fbSMatthew G. Knepley {
5470e07394fbSMatthew G. Knepley   PetscSection      clSection;
5471e07394fbSMatthew G. Knepley   IS                clPoints;
5472e07394fbSMatthew G. Knepley   PetscScalar       *array;
5473e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
547497529cf3SJed Brown   const PetscInt    *clp;
5475e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
547697e99dd9SToby Isaac   PetscInt          offset = 0, f;
5477e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5478e07394fbSMatthew G. Knepley 
5479e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5480e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
548192fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5482e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5483e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5484e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5485e07394fbSMatthew G. Knepley   /* Get points */
5486923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5487e07394fbSMatthew G. Knepley   /* Get array */
5488e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5489e07394fbSMatthew G. Knepley   /* Get values */
5490e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
549197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
549297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
549397e99dd9SToby Isaac 
5494e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5495e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5496e07394fbSMatthew G. Knepley         PetscInt fdof;
5497e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5498e07394fbSMatthew G. Knepley         offset += fdof;
5499e07394fbSMatthew G. Knepley       }
5500e07394fbSMatthew G. Knepley       continue;
5501e07394fbSMatthew G. Knepley     }
550297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5503e07394fbSMatthew G. Knepley     switch (mode) {
5504e07394fbSMatthew G. Knepley     case INSERT_VALUES:
550597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
550697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
550797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
550897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55095f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
551097529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5511e07394fbSMatthew G. Knepley       } break;
5512e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
551397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
551497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
551597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
551697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55175f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
551897529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5519e07394fbSMatthew G. Knepley       } break;
5520e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
552197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
552297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
552397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
552497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55255f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
552697529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5527e07394fbSMatthew G. Knepley       } break;
5528e07394fbSMatthew G. Knepley     case ADD_VALUES:
552997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
553097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
553197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
553297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55335f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
553497529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5535e07394fbSMatthew G. Knepley       } break;
5536e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
553797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
553897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
553997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
554097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55415f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
554297529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5543e07394fbSMatthew G. Knepley       } break;
5544e07394fbSMatthew G. Knepley     default:
5545f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5546e07394fbSMatthew G. Knepley     }
554797e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5548e07394fbSMatthew G. Knepley   }
5549e07394fbSMatthew G. Knepley   /* Cleanup points */
5550923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5551e07394fbSMatthew G. Knepley   /* Cleanup array */
5552e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5553e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5554e07394fbSMatthew G. Knepley }
5555e07394fbSMatthew G. Knepley 
55567cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5557552f7358SJed Brown {
5558552f7358SJed Brown   PetscMPIInt    rank;
5559552f7358SJed Brown   PetscInt       i, j;
5560552f7358SJed Brown   PetscErrorCode ierr;
5561552f7358SJed Brown 
5562552f7358SJed Brown   PetscFunctionBegin;
556382f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
5564eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5565e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5566e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5567b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5568b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5569e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5570b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5571519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
55727eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5573552f7358SJed Brown #else
5574b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5575552f7358SJed Brown #endif
5576552f7358SJed Brown     }
557777a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5578552f7358SJed Brown   }
5579552f7358SJed Brown   PetscFunctionReturn(0);
5580552f7358SJed Brown }
5581552f7358SJed Brown 
558205586334SMatthew G. Knepley /*
558305586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
558405586334SMatthew G. Knepley 
558505586334SMatthew G. Knepley   Input Parameters:
558605586334SMatthew G. Knepley + section - The section for this data layout
558736fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
558805586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
558905586334SMatthew G. Knepley . off     - The global offset of this point
559005586334SMatthew G. Knepley . loff    - The local offset of each field
559105586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
559205586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
559305586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
559405586334SMatthew G. Knepley 
559505586334SMatthew G. Knepley   Output Parameter:
559605586334SMatthew G. Knepley . indices - Indices for dofs on this point
559705586334SMatthew G. Knepley 
559805586334SMatthew G. Knepley   Level: developer
559905586334SMatthew G. Knepley 
560005586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
560105586334SMatthew G. Knepley */
560236fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5603a6dfd86eSKarl Rupp {
5604e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5605552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5606552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5607552f7358SJed Brown   PetscInt        cind = 0, k;
5608552f7358SJed Brown   PetscErrorCode  ierr;
5609552f7358SJed Brown 
5610552f7358SJed Brown   PetscFunctionBegin;
561136fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5612552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5613552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5614552f7358SJed Brown   if (!cdof || setBC) {
561505586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
561605586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
561705586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
561805586334SMatthew G. Knepley 
561905586334SMatthew G. Knepley       indices[ind] = off + k;
5620552f7358SJed Brown     }
5621552f7358SJed Brown   } else {
5622552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
56234acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
562405586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
562505586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
562605586334SMatthew G. Knepley 
56274acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
56284acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
562905586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
56304acb8e1eSToby Isaac         ++cind;
56314acb8e1eSToby Isaac       } else {
563236fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
5633552f7358SJed Brown       }
5634552f7358SJed Brown     }
5635552f7358SJed Brown   }
5636e6ccafaeSMatthew G Knepley   *loff += dof;
5637552f7358SJed Brown   PetscFunctionReturn(0);
5638552f7358SJed Brown }
5639552f7358SJed Brown 
56407e29afd2SMatthew G. Knepley /*
564136fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
56427e29afd2SMatthew G. Knepley 
564336fa2b79SJed Brown  Input Parameters:
564436fa2b79SJed Brown + section - a section (global or local)
564536fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
564636fa2b79SJed Brown . point - point within section
564736fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
564836fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
564936fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
565036fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
565136fa2b79SJed Brown . permsoff - offset
565236fa2b79SJed Brown - indperm - index permutation
565336fa2b79SJed Brown 
565436fa2b79SJed Brown  Output Parameter:
565536fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
565636fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
565736fa2b79SJed Brown 
565836fa2b79SJed Brown  Notes:
565936fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
566036fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
566136fa2b79SJed Brown  in the local vector.
566236fa2b79SJed Brown 
566336fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
566436fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
566536fa2b79SJed Brown 
566636fa2b79SJed Brown  Developer Note:
566736fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
566836fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
566936fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
567036fa2b79SJed Brown 
567136fa2b79SJed Brown  Example:
567236fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
567336fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
567436fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
567536fa2b79SJed Brown  The global vector does not store constrained dofs, so when this function returns global indices, say {110, -112, 111}, the value of -112 is an arbitrary flag that should not be interpreted beyond its sign.
567636fa2b79SJed Brown 
567736fa2b79SJed Brown  Level: developer
56787e29afd2SMatthew G. Knepley */
567936fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5680a6dfd86eSKarl Rupp {
5681552f7358SJed Brown   PetscInt       numFields, foff, f;
5682552f7358SJed Brown   PetscErrorCode ierr;
5683552f7358SJed Brown 
5684552f7358SJed Brown   PetscFunctionBegin;
568536fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5686552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5687552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
56884acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5689552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
56904acb8e1eSToby Isaac     PetscInt        cind = 0, b;
56914acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5692552f7358SJed Brown 
5693552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5694552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5695552f7358SJed Brown     if (!cfdof || setBC) {
569605586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
569705586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
569805586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
569905586334SMatthew G. Knepley 
570005586334SMatthew G. Knepley         indices[ind] = off+foff+b;
570105586334SMatthew G. Knepley       }
5702552f7358SJed Brown     } else {
5703552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
570405586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
570505586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
570605586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
570705586334SMatthew G. Knepley 
57084acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
570905586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5710552f7358SJed Brown           ++cind;
5711552f7358SJed Brown         } else {
571236fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
5713552f7358SJed Brown         }
5714552f7358SJed Brown       }
5715552f7358SJed Brown     }
571636fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
5717552f7358SJed Brown     foffs[f] += fdof;
5718552f7358SJed Brown   }
5719552f7358SJed Brown   PetscFunctionReturn(0);
5720552f7358SJed Brown }
5721552f7358SJed Brown 
57227e29afd2SMatthew G. Knepley /*
57237e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
57247e29afd2SMatthew G. Knepley 
57257e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
5726645102dcSJed Brown 
5727645102dcSJed Brown  Notes:
5728645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
5729645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
57307e29afd2SMatthew G. Knepley */
5731645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
57327e29afd2SMatthew G. Knepley {
57337e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
57347e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
57357e29afd2SMatthew G. Knepley 
57367e29afd2SMatthew G. Knepley   PetscFunctionBegin;
57377e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
57387e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
57397e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
57407e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
57417e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
57427e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
57437e29afd2SMatthew G. Knepley 
57447e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
57457e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
57467e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
5747645102dcSJed Brown     if (!cfdof) {
574805586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
574905586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
575005586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
575105586334SMatthew G. Knepley 
575205586334SMatthew G. Knepley         indices[ind] = foff+b;
575305586334SMatthew G. Knepley       }
57547e29afd2SMatthew G. Knepley     } else {
57557e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
575605586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
575705586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
575805586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
575905586334SMatthew G. Knepley 
57607e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
576105586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
57627e29afd2SMatthew G. Knepley           ++cind;
57637e29afd2SMatthew G. Knepley         } else {
576405586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
57657e29afd2SMatthew G. Knepley         }
57667e29afd2SMatthew G. Knepley       }
57677e29afd2SMatthew G. Knepley     }
57687e29afd2SMatthew G. Knepley     foffs[f] += fdof;
57697e29afd2SMatthew G. Knepley   }
57707e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
57717e29afd2SMatthew G. Knepley }
57727e29afd2SMatthew G. Knepley 
57734acb8e1eSToby 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)
5774d3d1a6afSToby Isaac {
5775d3d1a6afSToby Isaac   Mat             cMat;
5776d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5777d3d1a6afSToby Isaac   IS              aIS;
5778d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5779d3d1a6afSToby Isaac   const PetscInt  *anchors;
5780e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5781d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5782d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5783d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5784d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5785d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5786d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5787d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
57886ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5789d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5790d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5791d3d1a6afSToby Isaac 
5792d3d1a6afSToby Isaac   PetscFunctionBegin;
5793d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5794d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5795d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5796d3d1a6afSToby Isaac 
5797a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5798d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5799d3d1a6afSToby Isaac   if (aSec) {
5800580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5801d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5802d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5803d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5804d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5805d3d1a6afSToby Isaac      * into the global matrix anyway) */
5806d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5807d3d1a6afSToby Isaac       PetscInt b    = points[p];
58084b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5809d3d1a6afSToby Isaac 
58104b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
58114b2f2278SToby Isaac       if (!bSecDof) {
58124b2f2278SToby Isaac         continue;
58134b2f2278SToby Isaac       }
5814d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5815d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5816d3d1a6afSToby Isaac       }
5817d3d1a6afSToby Isaac       if (bDof) {
5818d3d1a6afSToby Isaac         /* this point is constrained */
5819d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5820d3d1a6afSToby Isaac         PetscInt bOff, q;
5821d3d1a6afSToby Isaac 
5822d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5823d3d1a6afSToby Isaac         newNumPoints  += bDof;
5824d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5825d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5826d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5827d3d1a6afSToby Isaac           PetscInt aDof;
5828d3d1a6afSToby Isaac 
5829d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5830d3d1a6afSToby Isaac           newNumIndices += aDof;
5831d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5832d3d1a6afSToby Isaac             PetscInt fDof;
5833d3d1a6afSToby Isaac 
5834d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5835d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5836d3d1a6afSToby Isaac           }
5837d3d1a6afSToby Isaac         }
5838d3d1a6afSToby Isaac       }
5839d3d1a6afSToby Isaac       else {
5840d3d1a6afSToby Isaac         /* this point is not constrained */
5841d3d1a6afSToby Isaac         newNumPoints++;
58424b2f2278SToby Isaac         newNumIndices += bSecDof;
5843d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5844d3d1a6afSToby Isaac           PetscInt fDof;
5845d3d1a6afSToby Isaac 
5846d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5847d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5848d3d1a6afSToby Isaac         }
5849d3d1a6afSToby Isaac       }
5850d3d1a6afSToby Isaac     }
5851d3d1a6afSToby Isaac   }
5852d3d1a6afSToby Isaac   if (!anyConstrained) {
585372b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
585472b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
585572b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
585672b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
585772b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5858d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5859d3d1a6afSToby Isaac   }
5860d3d1a6afSToby Isaac 
58616ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
58626ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
58636ecaa68aSToby Isaac 
5864f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5865d3d1a6afSToby Isaac 
58666ecaa68aSToby Isaac   if (!outPoints && !outValues) {
58676ecaa68aSToby Isaac     if (offsets) {
58686ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
58696ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
58706ecaa68aSToby Isaac       }
58716ecaa68aSToby Isaac     }
58726ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
58736ecaa68aSToby Isaac     PetscFunctionReturn(0);
58746ecaa68aSToby Isaac   }
58756ecaa68aSToby Isaac 
5876f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5877d3d1a6afSToby Isaac 
5878f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5879d3d1a6afSToby Isaac 
5880d3d1a6afSToby Isaac   /* workspaces */
5881d3d1a6afSToby Isaac   if (numFields) {
5882d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
588369291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
588469291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5885d3d1a6afSToby Isaac     }
5886d3d1a6afSToby Isaac   }
5887d3d1a6afSToby Isaac   else {
588869291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
588969291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5890d3d1a6afSToby Isaac   }
5891d3d1a6afSToby Isaac 
5892d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5893d3d1a6afSToby Isaac   if (numFields) {
58944b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
58954b2f2278SToby Isaac 
5896d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5897d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
58984b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5899d3d1a6afSToby Isaac 
59004b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
59014b2f2278SToby Isaac       if (!bSecDof) {
59024b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
59034b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
59044b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
59054b2f2278SToby Isaac         }
59064b2f2278SToby Isaac         continue;
59074b2f2278SToby Isaac       }
5908d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5909d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5910d3d1a6afSToby Isaac       }
5911d3d1a6afSToby Isaac       if (bDof) {
5912d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5913d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5914d3d1a6afSToby Isaac 
5915d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5916d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5917d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5918d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5919d3d1a6afSToby Isaac             PetscInt aFDof;
5920d3d1a6afSToby Isaac 
5921d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5922d3d1a6afSToby Isaac             allFDof += aFDof;
5923d3d1a6afSToby Isaac           }
5924d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5925d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5926d3d1a6afSToby Isaac         }
5927d3d1a6afSToby Isaac       }
5928d3d1a6afSToby Isaac       else {
5929d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5930d3d1a6afSToby Isaac           PetscInt fDof;
5931d3d1a6afSToby Isaac 
5932d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5933d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5934d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5935d3d1a6afSToby Isaac         }
5936d3d1a6afSToby Isaac       }
5937d3d1a6afSToby Isaac     }
59384b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
59394b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
59404b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5941d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5942d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5943d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5944d3d1a6afSToby Isaac       }
594519f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
594619f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
594769291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5948d3d1a6afSToby Isaac     }
5949d3d1a6afSToby Isaac   }
5950d3d1a6afSToby Isaac   else {
5951d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5952d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
59534b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5954d3d1a6afSToby Isaac 
59554b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
59564b2f2278SToby Isaac       if (!bSecDof) {
59574b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
59584b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
59594b2f2278SToby Isaac         continue;
59604b2f2278SToby Isaac       }
5961d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5962d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5963d3d1a6afSToby Isaac       }
5964d3d1a6afSToby Isaac       if (bDof) {
59654b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5966d3d1a6afSToby Isaac 
5967d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5968d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5969d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5970d3d1a6afSToby Isaac 
5971d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5972d3d1a6afSToby Isaac           allDof += aDof;
5973d3d1a6afSToby Isaac         }
5974d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
59754b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5976d3d1a6afSToby Isaac       }
5977d3d1a6afSToby Isaac       else {
59784b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5979d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5980d3d1a6afSToby Isaac       }
5981d3d1a6afSToby Isaac     }
5982d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5983d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5984d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5985d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5986d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5987d3d1a6afSToby Isaac     }
598869291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5989d3d1a6afSToby Isaac   }
5990d3d1a6afSToby Isaac 
59916ecaa68aSToby Isaac   /* output arrays */
599269291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
59936ecaa68aSToby Isaac 
5994d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5995d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5996d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
599769291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
599869291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5999d3d1a6afSToby Isaac   if (numFields) {
6000d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6001d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6002d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
60034b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6004d3d1a6afSToby Isaac 
60054b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
60064b2f2278SToby Isaac       if (!bSecDof) {
60074b2f2278SToby Isaac         continue;
60084b2f2278SToby Isaac       }
6009d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6010d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6011d3d1a6afSToby Isaac       }
6012d3d1a6afSToby Isaac       if (bDof) {
6013d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6014d3d1a6afSToby Isaac 
6015d3d1a6afSToby Isaac         fStart[0] = 0;
6016d3d1a6afSToby Isaac         fEnd[0]   = 0;
6017d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6018d3d1a6afSToby Isaac           PetscInt fDof;
6019d3d1a6afSToby Isaac 
6020d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6021d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6022d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6023d3d1a6afSToby Isaac         }
6024d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
602536fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6026d3d1a6afSToby Isaac 
6027d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6028d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6029d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6030d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6031d3d1a6afSToby Isaac 
6032d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6033d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6034d3d1a6afSToby Isaac         }
6035d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6036d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6037d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6038d3d1a6afSToby Isaac 
6039d3d1a6afSToby 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 */
6040d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6041d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6042302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
604336fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
6044d3d1a6afSToby Isaac         }
6045d3d1a6afSToby Isaac         newP += bDof;
6046d3d1a6afSToby Isaac 
60476ecaa68aSToby Isaac         if (outValues) {
6048d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
6049d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
6050d3d1a6afSToby 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);
6051d3d1a6afSToby Isaac           }
6052d3d1a6afSToby Isaac         }
60536ecaa68aSToby Isaac       }
6054d3d1a6afSToby Isaac       else {
6055d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6056d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6057d3d1a6afSToby Isaac         newP++;
6058d3d1a6afSToby Isaac       }
6059d3d1a6afSToby Isaac     }
6060d3d1a6afSToby Isaac   } else {
6061d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6062d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6063d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
60644b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6065d3d1a6afSToby Isaac 
60664b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
60674b2f2278SToby Isaac       if (!bSecDof) {
60684b2f2278SToby Isaac         continue;
60694b2f2278SToby Isaac       }
6070d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6071d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6072d3d1a6afSToby Isaac       }
6073d3d1a6afSToby Isaac       if (bDof) {
6074d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
6075d3d1a6afSToby Isaac 
6076d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
607736fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
6078d3d1a6afSToby Isaac 
6079d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
6080d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6081d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6082d3d1a6afSToby Isaac 
6083d3d1a6afSToby 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 */
6084d3d1a6afSToby Isaac 
6085d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6086d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6087302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
608836fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
6089d3d1a6afSToby Isaac         }
6090d3d1a6afSToby Isaac         newP += bDof;
6091d3d1a6afSToby Isaac 
6092d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
60936ecaa68aSToby Isaac         if (outValues) {
6094d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
6095d3d1a6afSToby Isaac         }
60966ecaa68aSToby Isaac       }
6097d3d1a6afSToby Isaac       else {
6098d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6099d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6100d3d1a6afSToby Isaac         newP++;
6101d3d1a6afSToby Isaac       }
6102d3d1a6afSToby Isaac     }
6103d3d1a6afSToby Isaac   }
6104d3d1a6afSToby Isaac 
61056ecaa68aSToby Isaac   if (outValues) {
610669291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
6107580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
6108d3d1a6afSToby Isaac     /* multiply constraints on the right */
6109d3d1a6afSToby Isaac     if (numFields) {
6110d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
6111d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
6112d3d1a6afSToby Isaac 
6113d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6114d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
6115d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6116d3d1a6afSToby Isaac           PetscInt c, r, k;
6117d3d1a6afSToby Isaac           PetscInt dof;
6118d3d1a6afSToby Isaac 
6119d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
61204b2f2278SToby Isaac           if (!dof) {
61214b2f2278SToby Isaac             continue;
61224b2f2278SToby Isaac           }
6123d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6124d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6125d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
6126d3d1a6afSToby Isaac 
6127d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6128d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
6129d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
61304acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6131d3d1a6afSToby Isaac                 }
6132d3d1a6afSToby Isaac               }
6133d3d1a6afSToby Isaac             }
6134d3d1a6afSToby Isaac           }
6135d3d1a6afSToby Isaac           else {
6136d3d1a6afSToby Isaac             /* copy this column as is */
6137d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6138d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
6139d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6140d3d1a6afSToby Isaac               }
6141d3d1a6afSToby Isaac             }
6142d3d1a6afSToby Isaac           }
6143d3d1a6afSToby Isaac           oldOff += dof;
6144d3d1a6afSToby Isaac         }
6145d3d1a6afSToby Isaac       }
6146d3d1a6afSToby Isaac     }
6147d3d1a6afSToby Isaac     else {
6148d3d1a6afSToby Isaac       PetscInt oldOff = 0;
6149d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6150d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
6151d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
6152d3d1a6afSToby Isaac         PetscInt c, r, k;
6153d3d1a6afSToby Isaac         PetscInt dof;
6154d3d1a6afSToby Isaac 
6155d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
61564b2f2278SToby Isaac         if (!dof) {
61574b2f2278SToby Isaac           continue;
61584b2f2278SToby Isaac         }
6159d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6160d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6161d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
6162d3d1a6afSToby Isaac 
6163d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6164d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
6165d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
6166d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6167d3d1a6afSToby Isaac               }
6168d3d1a6afSToby Isaac             }
6169d3d1a6afSToby Isaac           }
6170d3d1a6afSToby Isaac         }
6171d3d1a6afSToby Isaac         else {
6172d3d1a6afSToby Isaac           /* copy this column as is */
6173d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6174d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
6175d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6176d3d1a6afSToby Isaac             }
6177d3d1a6afSToby Isaac           }
6178d3d1a6afSToby Isaac         }
6179d3d1a6afSToby Isaac         oldOff += dof;
6180d3d1a6afSToby Isaac       }
6181d3d1a6afSToby Isaac     }
6182d3d1a6afSToby Isaac 
61836ecaa68aSToby Isaac     if (multiplyLeft) {
618469291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
6185580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
6186d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
6187d3d1a6afSToby Isaac       if (numFields) {
6188d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6189d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
6190d3d1a6afSToby Isaac 
6191d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
6192d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
6193d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
6194d3d1a6afSToby Isaac             PetscInt c, r, k;
6195d3d1a6afSToby Isaac             PetscInt dof;
6196d3d1a6afSToby Isaac 
6197d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
6198d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6199d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
6200d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
6201d3d1a6afSToby Isaac 
6202d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
6203d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6204d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
6205d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6206d3d1a6afSToby Isaac                   }
6207d3d1a6afSToby Isaac                 }
6208d3d1a6afSToby Isaac               }
6209d3d1a6afSToby Isaac             }
6210d3d1a6afSToby Isaac             else {
6211d3d1a6afSToby Isaac               /* copy this row as is */
6212d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6213d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6214d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6215d3d1a6afSToby Isaac                 }
6216d3d1a6afSToby Isaac               }
6217d3d1a6afSToby Isaac             }
6218d3d1a6afSToby Isaac             oldOff += dof;
6219d3d1a6afSToby Isaac           }
6220d3d1a6afSToby Isaac         }
6221d3d1a6afSToby Isaac       }
6222d3d1a6afSToby Isaac       else {
6223d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6224d3d1a6afSToby Isaac 
6225d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6226d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6227d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6228d3d1a6afSToby Isaac           PetscInt c, r, k;
6229d3d1a6afSToby Isaac           PetscInt dof;
6230d3d1a6afSToby Isaac 
6231d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6232d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6233d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6234d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6235d3d1a6afSToby Isaac 
6236d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6237d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6238d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6239d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6240d3d1a6afSToby Isaac                 }
6241d3d1a6afSToby Isaac               }
6242d3d1a6afSToby Isaac             }
6243d3d1a6afSToby Isaac           }
6244d3d1a6afSToby Isaac           else {
6245d3d1a6afSToby Isaac             /* copy this row as is */
62469fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6247d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6248d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6249d3d1a6afSToby Isaac               }
6250d3d1a6afSToby Isaac             }
6251d3d1a6afSToby Isaac           }
6252d3d1a6afSToby Isaac           oldOff += dof;
6253d3d1a6afSToby Isaac         }
6254d3d1a6afSToby Isaac       }
6255d3d1a6afSToby Isaac 
625669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
62576ecaa68aSToby Isaac     }
62586ecaa68aSToby Isaac     else {
62596ecaa68aSToby Isaac       newValues = tmpValues;
62606ecaa68aSToby Isaac     }
62616ecaa68aSToby Isaac   }
62626ecaa68aSToby Isaac 
6263d3d1a6afSToby Isaac   /* clean up */
626469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
626569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
62666ecaa68aSToby Isaac 
6267d3d1a6afSToby Isaac   if (numFields) {
6268d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
626969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
627069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
627169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6272d3d1a6afSToby Isaac     }
6273d3d1a6afSToby Isaac   }
6274d3d1a6afSToby Isaac   else {
627569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
627669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
627769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6278d3d1a6afSToby Isaac   }
6279d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6280d3d1a6afSToby Isaac 
6281d3d1a6afSToby Isaac   /* output */
62826ecaa68aSToby Isaac   if (outPoints) {
6283d3d1a6afSToby Isaac     *outPoints = newPoints;
62846ecaa68aSToby Isaac   }
62856ecaa68aSToby Isaac   else {
628669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
62876ecaa68aSToby Isaac   }
628831620726SToby Isaac   if (outValues) {
6289d3d1a6afSToby Isaac     *outValues = newValues;
62906ecaa68aSToby Isaac   }
62916ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6292d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6293d3d1a6afSToby Isaac   }
6294d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6295d3d1a6afSToby Isaac }
6296d3d1a6afSToby Isaac 
62974a1e0b3eSMatthew G. Knepley /*@C
629871f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
62997cd05799SMatthew G. Knepley 
63007cd05799SMatthew G. Knepley   Not collective
63017cd05799SMatthew G. Knepley 
63027cd05799SMatthew G. Knepley   Input Parameters:
63037cd05799SMatthew G. Knepley + dm         - The DM
630471f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
630571f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
630671f0bbf9SMatthew G. Knepley . point      - The point defining the closure
630771f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
63087cd05799SMatthew G. Knepley 
630971f0bbf9SMatthew G. Knepley   Output Parameters:
631071f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
631171f0bbf9SMatthew G. Knepley . indices    - The dof indices
631271f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
631371f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
63147cd05799SMatthew G. Knepley 
631536fa2b79SJed Brown   Notes:
631636fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
631736fa2b79SJed Brown 
631836fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
631936fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
632036fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
632136fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
632236fa2b79SJed Brown   indices (with the above semantics) are implied.
63237cd05799SMatthew G. Knepley 
63247cd05799SMatthew G. Knepley   Level: advanced
63257cd05799SMatthew G. Knepley 
632636fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
63274a1e0b3eSMatthew G. Knepley @*/
632871f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
632971f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
63307773e69fSMatthew G. Knepley {
633171f0bbf9SMatthew G. Knepley   /* Closure ordering */
63327773e69fSMatthew G. Knepley   PetscSection        clSection;
63337773e69fSMatthew G. Knepley   IS                  clPoints;
633471f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
633571f0bbf9SMatthew G. Knepley   PetscInt           *points;
633671f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
633771f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
63384acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
633971f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
634071f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
634171f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
634271f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
634371f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
634471f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
634571f0bbf9SMatthew G. Knepley 
634671f0bbf9SMatthew G. Knepley   PetscInt           *idx;
634771f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
634871f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
63497773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
63507773e69fSMatthew G. Knepley 
635171f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
63527773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63537773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
635436fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
635571f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
635671f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
635771f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
635871f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
63597773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
63607773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6361580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
636271f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
636371f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
6364c459fbc1SJed Brown   if (useClPerm) {
6365c459fbc1SJed Brown     PetscInt depth, clsize;
6366c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
6367c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
6368c459fbc1SJed Brown       PetscInt dof;
6369c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6370c459fbc1SJed Brown       clsize += dof;
6371c459fbc1SJed Brown     }
6372c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
6373c459fbc1SJed Brown   }
637471f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
637571f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
63767773e69fSMatthew G. Knepley     PetscInt dof, fdof;
63777773e69fSMatthew G. Knepley 
63787773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
63797773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
63807773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
63817773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
63827773e69fSMatthew G. Knepley     }
638371f0bbf9SMatthew G. Knepley     Ni += dof;
63847773e69fSMatthew G. Knepley   }
63857773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
638671f0bbf9SMatthew G. Knepley   if (Nf && offsets[Nf] != Ni) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Ni);
638771f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
638871f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
638971f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
639071f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
639171f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
639271f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
639371f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
63946ecaa68aSToby Isaac 
639571f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
639671f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
639771f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
639871f0bbf9SMatthew G. Knepley 
639971f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
640071f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
640171f0bbf9SMatthew G. Knepley         if (flip) {
640271f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
640371f0bbf9SMatthew G. Knepley 
640471f0bbf9SMatthew G. Knepley           if (!valCopy) {
640571f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
640671f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
640771f0bbf9SMatthew G. Knepley             *values = valCopy;
640871f0bbf9SMatthew G. Knepley           }
640971f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
641071f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
641171f0bbf9SMatthew G. Knepley 
641271f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
641371f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
641471f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
64156ecaa68aSToby Isaac             }
64166ecaa68aSToby Isaac           }
641771f0bbf9SMatthew G. Knepley         }
641871f0bbf9SMatthew G. Knepley         foffset += fdof;
641971f0bbf9SMatthew G. Knepley       }
642071f0bbf9SMatthew G. Knepley     }
642171f0bbf9SMatthew G. Knepley   }
642271f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
642371f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
642471f0bbf9SMatthew G. Knepley   if (NclC) {
642571f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
642671f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
642771f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
642871f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
642971f0bbf9SMatthew G. Knepley     }
643071f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
643171f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
643271f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
643371f0bbf9SMatthew G. Knepley     }
643471f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
643571f0bbf9SMatthew G. Knepley     Ncl     = NclC;
643671f0bbf9SMatthew G. Knepley     Ni      = NiC;
643771f0bbf9SMatthew G. Knepley     points  = pointsC;
643871f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
643971f0bbf9SMatthew G. Knepley   }
644071f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
644171f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
644271f0bbf9SMatthew G. Knepley   if (Nf) {
644371f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
644471f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
644571f0bbf9SMatthew G. Knepley 
644671f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
644771f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
644871f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
644971f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
645071f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
645171f0bbf9SMatthew G. Knepley 
645271f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
64537773e69fSMatthew G. Knepley       }
64547773e69fSMatthew G. Knepley     } else {
645571f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
645671f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
645771f0bbf9SMatthew G. Knepley 
645871f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
645971f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
646071f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
646171f0bbf9SMatthew G. Knepley          * global section. */
646271f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
646371f0bbf9SMatthew G. Knepley       }
646471f0bbf9SMatthew G. Knepley     }
646571f0bbf9SMatthew G. Knepley   } else {
646671f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
646771f0bbf9SMatthew G. Knepley 
646871f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
646971f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
64704acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
64714acb8e1eSToby Isaac 
647271f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
647371f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
647471f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
647571f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
64767773e69fSMatthew G. Knepley     }
64777773e69fSMatthew G. Knepley   }
647871f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
647971f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
648071f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
648171f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
64824acb8e1eSToby Isaac   }
648371f0bbf9SMatthew G. Knepley   if (NclC) {
648471f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
64857773e69fSMatthew G. Knepley   } else {
648671f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
64877773e69fSMatthew G. Knepley   }
648871f0bbf9SMatthew G. Knepley 
648971f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
649071f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
64917773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
64927773e69fSMatthew G. Knepley }
64937773e69fSMatthew G. Knepley 
64947cd05799SMatthew G. Knepley /*@C
649571f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
64967cd05799SMatthew G. Knepley 
64977cd05799SMatthew G. Knepley   Not collective
64987cd05799SMatthew G. Knepley 
64997cd05799SMatthew G. Knepley   Input Parameters:
65007cd05799SMatthew G. Knepley + dm         - The DM
650171f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
650271f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
650371f0bbf9SMatthew G. Knepley . point      - The point defining the closure
650471f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
650571f0bbf9SMatthew G. Knepley 
650671f0bbf9SMatthew G. Knepley   Output Parameters:
650771f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
650871f0bbf9SMatthew G. Knepley . indices    - The dof indices
650971f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
651071f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
651171f0bbf9SMatthew G. Knepley 
651271f0bbf9SMatthew G. Knepley   Notes:
651371f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
651471f0bbf9SMatthew G. Knepley 
651571f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
651671f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
651771f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
651871f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
651971f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
65207cd05799SMatthew G. Knepley 
65217cd05799SMatthew G. Knepley   Level: advanced
65227cd05799SMatthew G. Knepley 
652371f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
65247cd05799SMatthew G. Knepley @*/
652571f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
652671f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
65277773e69fSMatthew G. Knepley {
65287773e69fSMatthew G. Knepley   PetscErrorCode ierr;
65297773e69fSMatthew G. Knepley 
65307773e69fSMatthew G. Knepley   PetscFunctionBegin;
65317773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65327773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
653369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
65347773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
65357773e69fSMatthew G. Knepley }
65367773e69fSMatthew G. Knepley 
65377f5d1fdeSMatthew G. Knepley /*@C
65387f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
65397f5d1fdeSMatthew G. Knepley 
65407f5d1fdeSMatthew G. Knepley   Not collective
65417f5d1fdeSMatthew G. Knepley 
65427f5d1fdeSMatthew G. Knepley   Input Parameters:
65437f5d1fdeSMatthew G. Knepley + dm - The DM
6544ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6545ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
65467f5d1fdeSMatthew G. Knepley . A - The matrix
6547eaf898f9SPatrick Sanan . point - The point in the DM
65487f5d1fdeSMatthew G. Knepley . values - The array of values
65497f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
65507f5d1fdeSMatthew G. Knepley 
65517f5d1fdeSMatthew G. Knepley   Fortran Notes:
65527f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
65537f5d1fdeSMatthew G. Knepley 
65547f5d1fdeSMatthew G. Knepley   Level: intermediate
65557f5d1fdeSMatthew G. Knepley 
65564a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
65577f5d1fdeSMatthew G. Knepley @*/
65587c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6559552f7358SJed Brown {
6560552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
6561552f7358SJed Brown   PetscInt          *indices;
656271f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
656371f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
6564552f7358SJed Brown   PetscErrorCode     ierr;
6565552f7358SJed Brown 
6566552f7358SJed Brown   PetscFunctionBegin;
6567552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
656892fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
65693dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6570e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
65713dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
65723dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6573552f7358SJed Brown 
657471f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
65750d644c17SKarl Rupp 
6576b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
65774a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6578552f7358SJed Brown   if (ierr) {
6579552f7358SJed Brown     PetscMPIInt    rank;
6580552f7358SJed Brown     PetscErrorCode ierr2;
6581552f7358SJed Brown 
658282f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6583e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6584b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
658571f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
658671f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6587552f7358SJed Brown     CHKERRQ(ierr);
6588552f7358SJed Brown   }
65894a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
65904a1e0b3eSMatthew G. Knepley     PetscInt i;
65914a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
65924a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
65934a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
65944a1e0b3eSMatthew G. Knepley   }
659571f0bbf9SMatthew G. Knepley 
659671f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
659771f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
659871f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
65994acb8e1eSToby Isaac }
660071f0bbf9SMatthew G. Knepley 
66014a1e0b3eSMatthew G. Knepley /*@C
66024a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
66034a1e0b3eSMatthew G. Knepley 
66044a1e0b3eSMatthew G. Knepley   Not collective
66054a1e0b3eSMatthew G. Knepley 
66064a1e0b3eSMatthew G. Knepley   Input Parameters:
66074a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
66084a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
66094a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
66104a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
66114a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
66124a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
66134a1e0b3eSMatthew G. Knepley . A - The matrix
66144a1e0b3eSMatthew G. Knepley . point - The point in the DMs
66154a1e0b3eSMatthew G. Knepley . values - The array of values
66164a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
66174a1e0b3eSMatthew G. Knepley 
66184a1e0b3eSMatthew G. Knepley   Level: intermediate
66194a1e0b3eSMatthew G. Knepley 
66204a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
66214a1e0b3eSMatthew G. Knepley @*/
662271f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
662371f0bbf9SMatthew G. Knepley {
662471f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
662571f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
662671f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
662771f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
662871f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
662971f0bbf9SMatthew G. Knepley 
663071f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
663171f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
663271f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
663371f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
663471f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
663571f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
663671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
663771f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
663871f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
663971f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
664071f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
664171f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
664271f0bbf9SMatthew G. Knepley 
664371f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
664471f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
664571f0bbf9SMatthew G. Knepley 
664671f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
66474a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
664871f0bbf9SMatthew G. Knepley   if (ierr) {
664971f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
665071f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
665171f0bbf9SMatthew G. Knepley 
665271f0bbf9SMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
665371f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
665471f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
665571f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
665671f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
665771f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
665871f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
6659d3d1a6afSToby Isaac   }
666071f0bbf9SMatthew G. Knepley 
666171f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
666271f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
666371f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
6664552f7358SJed Brown   PetscFunctionReturn(0);
6665552f7358SJed Brown }
6666552f7358SJed Brown 
6667de41b84cSMatthew 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)
6668de41b84cSMatthew G. Knepley {
6669de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6670de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6671de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6672de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
667317c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6674de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6675412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
66764ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6677de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6678de41b84cSMatthew G. Knepley 
6679de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6680de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6681de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
668292fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6683de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
668492fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6685de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6686e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6687de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6688e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6689de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6690de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6691de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6692de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6693580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6694580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
6695de41b84cSMatthew G. Knepley   /* Column indices */
6696de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
66974ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6698de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6699de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6700de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6701de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6702de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6703de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6704de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6705de41b84cSMatthew G. Knepley       ++q;
6706de41b84cSMatthew G. Knepley     }
6707de41b84cSMatthew G. Knepley   }
6708de41b84cSMatthew G. Knepley   numCPoints = q;
6709de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6710de41b84cSMatthew G. Knepley     PetscInt fdof;
6711de41b84cSMatthew G. Knepley 
6712de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
67134ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6714de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6715de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6716de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6717de41b84cSMatthew G. Knepley     }
6718de41b84cSMatthew G. Knepley     numCIndices += dof;
6719de41b84cSMatthew G. Knepley   }
6720de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6721de41b84cSMatthew G. Knepley   /* Row indices */
6722412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6723412e9a14SMatthew G. Knepley   {
6724412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6725412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6726412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6727412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6728412e9a14SMatthew G. Knepley   }
672969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6730de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6731de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6732de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6733de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6734de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6735de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6736de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
67374ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
67384ca5e9f5SMatthew G. Knepley         if (!dof) continue;
67394ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
67404ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6741de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6742de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6743de41b84cSMatthew G. Knepley         ++q;
6744de41b84cSMatthew G. Knepley       }
6745de41b84cSMatthew G. Knepley     }
6746de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6747de41b84cSMatthew G. Knepley   }
6748de41b84cSMatthew G. Knepley   numFPoints = q;
6749de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6750de41b84cSMatthew G. Knepley     PetscInt fdof;
6751de41b84cSMatthew G. Knepley 
6752de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
67534ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6754de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6755de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6756de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6757de41b84cSMatthew G. Knepley     }
6758de41b84cSMatthew G. Knepley     numFIndices += dof;
6759de41b84cSMatthew G. Knepley   }
6760de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6761de41b84cSMatthew G. Knepley 
67628ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
67638ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
676469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
676569291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6766de41b84cSMatthew G. Knepley   if (numFields) {
67674acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
67684acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
67694acb8e1eSToby Isaac 
67704acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
67714acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
67724acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6773de41b84cSMatthew G. Knepley     }
67744acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
67754acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
677636fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
67774acb8e1eSToby Isaac     }
67784acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
67794acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
678036fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
67814acb8e1eSToby Isaac     }
67824acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
67834acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
67844acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6785de41b84cSMatthew G. Knepley     }
6786de41b84cSMatthew G. Knepley   } else {
67874acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
67884acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
67894acb8e1eSToby Isaac 
67904acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
67914acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
67924acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
67934acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
67944acb8e1eSToby Isaac 
67954acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
679636fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6797de41b84cSMatthew G. Knepley     }
67984acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
67994acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
68004acb8e1eSToby Isaac 
68014acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
680236fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6803de41b84cSMatthew G. Knepley     }
68044acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
68054acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6806de41b84cSMatthew G. Knepley   }
6807de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
68084acb8e1eSToby Isaac   /* TODO: flips */
6809de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6810de41b84cSMatthew G. Knepley   if (ierr) {
6811de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6812de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6813de41b84cSMatthew G. Knepley 
6814de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6815e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6816de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
681769291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
681869291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6819de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6820de41b84cSMatthew G. Knepley   }
682169291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6822de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
682369291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
682469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6825de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6826de41b84cSMatthew G. Knepley }
6827de41b84cSMatthew G. Knepley 
68287c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
68297c927364SMatthew G. Knepley {
68307c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
68317c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
68327c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
683317c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6834412e9a14SMatthew G. Knepley   DMPolytopeType ct;
68357c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
68367c927364SMatthew G. Knepley   PetscErrorCode ierr;
68377c927364SMatthew G. Knepley 
68387c927364SMatthew G. Knepley   PetscFunctionBegin;
68397c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
68407c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
684192fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
68427c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
684392fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
68447c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6845e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
68467c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6847e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
68487c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
68497c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
68507c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6851580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6852580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
68537c927364SMatthew G. Knepley   /* Column indices */
68547c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
68557c927364SMatthew G. Knepley   maxFPoints = numCPoints;
68567c927364SMatthew G. Knepley   /* Compress out points not in the section */
68577c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
68587c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
68597c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
68607c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
68617c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
68627c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
68637c927364SMatthew G. Knepley       ++q;
68647c927364SMatthew G. Knepley     }
68657c927364SMatthew G. Knepley   }
68667c927364SMatthew G. Knepley   numCPoints = q;
68677c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
68687c927364SMatthew G. Knepley     PetscInt fdof;
68697c927364SMatthew G. Knepley 
68707c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
68717c927364SMatthew G. Knepley     if (!dof) continue;
68727c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
68737c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
68747c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
68757c927364SMatthew G. Knepley     }
68767c927364SMatthew G. Knepley     numCIndices += dof;
68777c927364SMatthew G. Knepley   }
68787c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
68797c927364SMatthew G. Knepley   /* Row indices */
6880412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6881412e9a14SMatthew G. Knepley   {
6882412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6883412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6884412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6885412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6886412e9a14SMatthew G. Knepley   }
688769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
68887c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
68897c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
68907c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
68917c927364SMatthew G. Knepley     /* Compress out points not in the section */
68927c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
68937c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
68947c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
68957c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
68967c927364SMatthew G. Knepley         if (!dof) continue;
68977c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
68987c927364SMatthew G. Knepley         if (s < q) continue;
68997c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
69007c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
69017c927364SMatthew G. Knepley         ++q;
69027c927364SMatthew G. Knepley       }
69037c927364SMatthew G. Knepley     }
69047c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
69057c927364SMatthew G. Knepley   }
69067c927364SMatthew G. Knepley   numFPoints = q;
69077c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
69087c927364SMatthew G. Knepley     PetscInt fdof;
69097c927364SMatthew G. Knepley 
69107c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
69117c927364SMatthew G. Knepley     if (!dof) continue;
69127c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
69137c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
69147c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
69157c927364SMatthew G. Knepley     }
69167c927364SMatthew G. Knepley     numFIndices += dof;
69177c927364SMatthew G. Knepley   }
69187c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
69197c927364SMatthew G. Knepley 
69208ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
69218ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
69227c927364SMatthew G. Knepley   if (numFields) {
69234acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
69244acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
69254acb8e1eSToby Isaac 
69264acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
69274acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
69284acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
69297c927364SMatthew G. Knepley     }
69304acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
69314acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
693236fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
69334acb8e1eSToby Isaac     }
69344acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
69354acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
693636fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
69374acb8e1eSToby Isaac     }
69384acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
69394acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
69404acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
69417c927364SMatthew G. Knepley     }
69427c927364SMatthew G. Knepley   } else {
69434acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
69444acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
69454acb8e1eSToby Isaac 
69464acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
69474acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
69484acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
69494acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
69504acb8e1eSToby Isaac 
69514acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
695236fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
69537c927364SMatthew G. Knepley     }
69544acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
69554acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
69564acb8e1eSToby Isaac 
69574acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
695836fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
69597c927364SMatthew G. Knepley     }
69604acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
69614acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
69627c927364SMatthew G. Knepley   }
696369291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
69647c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
69657c927364SMatthew G. Knepley   PetscFunctionReturn(0);
69667c927364SMatthew G. Knepley }
69677c927364SMatthew G. Knepley 
69687cd05799SMatthew G. Knepley /*@C
69697cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
69707cd05799SMatthew G. Knepley 
69717cd05799SMatthew G. Knepley   Input Parameter:
69727cd05799SMatthew G. Knepley . dm   - The DMPlex object
69737cd05799SMatthew G. Knepley 
69747cd05799SMatthew G. Knepley   Output Parameter:
69757cd05799SMatthew G. Knepley . cellHeight - The height of a cell
69767cd05799SMatthew G. Knepley 
69777cd05799SMatthew G. Knepley   Level: developer
69787cd05799SMatthew G. Knepley 
69797cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
69807cd05799SMatthew G. Knepley @*/
6981552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6982552f7358SJed Brown {
6983552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6984552f7358SJed Brown 
6985552f7358SJed Brown   PetscFunctionBegin;
6986552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6987552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6988552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6989552f7358SJed Brown   PetscFunctionReturn(0);
6990552f7358SJed Brown }
6991552f7358SJed Brown 
69927cd05799SMatthew G. Knepley /*@C
69937cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
69947cd05799SMatthew G. Knepley 
69957cd05799SMatthew G. Knepley   Input Parameters:
69967cd05799SMatthew G. Knepley + dm   - The DMPlex object
69977cd05799SMatthew G. Knepley - cellHeight - The height of a cell
69987cd05799SMatthew G. Knepley 
69997cd05799SMatthew G. Knepley   Level: developer
70007cd05799SMatthew G. Knepley 
70017cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
70027cd05799SMatthew G. Knepley @*/
7003552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7004552f7358SJed Brown {
7005552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7006552f7358SJed Brown 
7007552f7358SJed Brown   PetscFunctionBegin;
7008552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7009552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7010552f7358SJed Brown   PetscFunctionReturn(0);
7011552f7358SJed Brown }
7012552f7358SJed Brown 
7013e6139122SMatthew G. Knepley /*@
7014e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7015e6139122SMatthew G. Knepley 
7016e6139122SMatthew G. Knepley   Input Parameter:
7017e6139122SMatthew G. Knepley . dm - The DMPlex object
7018e6139122SMatthew G. Knepley 
7019e6139122SMatthew G. Knepley   Output Parameters:
70202a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
70212a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7022e6139122SMatthew G. Knepley 
70232a9f31c0SMatthew G. Knepley   Level: advanced
7024e6139122SMatthew G. Knepley 
7025412e9a14SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum()
7026e6139122SMatthew G. Knepley @*/
7027e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7028e6139122SMatthew G. Knepley {
7029412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7030e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7031e6139122SMatthew G. Knepley 
7032e6139122SMatthew G. Knepley   PetscFunctionBegin;
7033e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7034412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
7035412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
7036e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
7037e6139122SMatthew G. Knepley }
7038e6139122SMatthew G. Knepley 
7039552f7358SJed Brown /* We can easily have a form that takes an IS instead */
70409886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7041552f7358SJed Brown {
7042552f7358SJed Brown   PetscSection   section, globalSection;
7043552f7358SJed Brown   PetscInt      *numbers, p;
7044552f7358SJed Brown   PetscErrorCode ierr;
7045552f7358SJed Brown 
7046552f7358SJed Brown   PetscFunctionBegin;
704782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
7048552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
7049552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7050552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
7051552f7358SJed Brown   }
7052552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
705315b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
7054854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
7055552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7056552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
7057ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7058ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
7059552f7358SJed Brown   }
706082f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
7061ef48cebcSMatthew G. Knepley   if (globalSize) {
7062ef48cebcSMatthew G. Knepley     PetscLayout layout;
7063ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
7064ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
7065ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
7066ef48cebcSMatthew G. Knepley   }
7067552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
7068552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
7069552f7358SJed Brown   PetscFunctionReturn(0);
7070552f7358SJed Brown }
7071552f7358SJed Brown 
707281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7073552f7358SJed Brown {
7074412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
7075552f7358SJed Brown   PetscErrorCode ierr;
7076552f7358SJed Brown 
7077552f7358SJed Brown   PetscFunctionBegin;
7078552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7079412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
7080412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
70819886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
708281ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
7083552f7358SJed Brown }
708481ed3555SMatthew G. Knepley 
70858dab3259SMatthew G. Knepley /*@
70867cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
70877cd05799SMatthew G. Knepley 
70887cd05799SMatthew G. Knepley   Input Parameter:
70897cd05799SMatthew G. Knepley . dm   - The DMPlex object
70907cd05799SMatthew G. Knepley 
70917cd05799SMatthew G. Knepley   Output Parameter:
70927cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
70937cd05799SMatthew G. Knepley 
70947cd05799SMatthew G. Knepley   Level: developer
70957cd05799SMatthew G. Knepley 
70967cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
70977cd05799SMatthew G. Knepley @*/
709881ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
709981ed3555SMatthew G. Knepley {
710081ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
710181ed3555SMatthew G. Knepley   PetscErrorCode ierr;
710281ed3555SMatthew G. Knepley 
710381ed3555SMatthew G. Knepley   PetscFunctionBegin;
710481ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
710581ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
7106552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
7107552f7358SJed Brown   PetscFunctionReturn(0);
7108552f7358SJed Brown }
7109552f7358SJed Brown 
711081ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
711181ed3555SMatthew G. Knepley {
7112412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
711381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
711481ed3555SMatthew G. Knepley 
711581ed3555SMatthew G. Knepley   PetscFunctionBegin;
711681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
711781ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
71189886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
711981ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
712081ed3555SMatthew G. Knepley }
712181ed3555SMatthew G. Knepley 
71228dab3259SMatthew G. Knepley /*@
71236aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
71247cd05799SMatthew G. Knepley 
71257cd05799SMatthew G. Knepley   Input Parameter:
71267cd05799SMatthew G. Knepley . dm   - The DMPlex object
71277cd05799SMatthew G. Knepley 
71287cd05799SMatthew G. Knepley   Output Parameter:
71297cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
71307cd05799SMatthew G. Knepley 
71317cd05799SMatthew G. Knepley   Level: developer
71327cd05799SMatthew G. Knepley 
71337cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
71347cd05799SMatthew G. Knepley @*/
7135552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7136552f7358SJed Brown {
7137552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
7138552f7358SJed Brown   PetscErrorCode ierr;
7139552f7358SJed Brown 
7140552f7358SJed Brown   PetscFunctionBegin;
7141552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
714281ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
7143552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
7144552f7358SJed Brown   PetscFunctionReturn(0);
7145552f7358SJed Brown }
7146552f7358SJed Brown 
71478dab3259SMatthew G. Knepley /*@
71487cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
71497cd05799SMatthew G. Knepley 
71507cd05799SMatthew G. Knepley   Input Parameter:
71517cd05799SMatthew G. Knepley . dm   - The DMPlex object
71527cd05799SMatthew G. Knepley 
71537cd05799SMatthew G. Knepley   Output Parameter:
71547cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
71557cd05799SMatthew G. Knepley 
71567cd05799SMatthew G. Knepley   Level: developer
71577cd05799SMatthew G. Knepley 
71587cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
71597cd05799SMatthew G. Knepley @*/
7160ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7161ef48cebcSMatthew G. Knepley {
7162ef48cebcSMatthew G. Knepley   IS             nums[4];
7163862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
7164ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
7165ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
7166ef48cebcSMatthew G. Knepley 
7167ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
7168ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7169ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
71708abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
71718abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
7172862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7173862913ffSStefano Zampini     PetscInt end;
7174862913ffSStefano Zampini 
7175862913ffSStefano Zampini     depths[d] = depth-d;
7176862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
7177862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7178862913ffSStefano Zampini   }
7179862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
7180862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
7181862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7182862913ffSStefano 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]);
7183862913ffSStefano Zampini   }
7184ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
7185ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
7186ef48cebcSMatthew G. Knepley 
7187862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
71889886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
7189ef48cebcSMatthew G. Knepley     shift += gsize;
7190ef48cebcSMatthew G. Knepley   }
7191302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7192ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7193ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7194ef48cebcSMatthew G. Knepley }
7195ef48cebcSMatthew G. Knepley 
719608a22f4bSMatthew G. Knepley 
719708a22f4bSMatthew G. Knepley /*@
719808a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
719908a22f4bSMatthew G. Knepley 
720008a22f4bSMatthew G. Knepley   Input Parameter:
720108a22f4bSMatthew G. Knepley . dm - The DMPlex object
720208a22f4bSMatthew G. Knepley 
720308a22f4bSMatthew G. Knepley   Output Parameter:
720408a22f4bSMatthew G. Knepley . ranks - The rank field
720508a22f4bSMatthew G. Knepley 
720608a22f4bSMatthew G. Knepley   Options Database Keys:
720708a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
720808a22f4bSMatthew G. Knepley 
720908a22f4bSMatthew G. Knepley   Level: intermediate
721008a22f4bSMatthew G. Knepley 
721108a22f4bSMatthew G. Knepley .seealso: DMView()
721208a22f4bSMatthew G. Knepley @*/
721308a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
721408a22f4bSMatthew G. Knepley {
721508a22f4bSMatthew G. Knepley   DM             rdm;
721608a22f4bSMatthew G. Knepley   PetscFE        fe;
721708a22f4bSMatthew G. Knepley   PetscScalar   *r;
721808a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
721908a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
722008a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
722108a22f4bSMatthew G. Knepley 
722208a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7223f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7224f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
722508a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
722608a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
722708a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7228f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
722908a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7230e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
723108a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7232e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
723308a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
723408a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
723508a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
723608a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
723708a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
723808a22f4bSMatthew G. Knepley     PetscScalar *lr;
723908a22f4bSMatthew G. Knepley 
724008a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
724171f09efeSPierre Jolivet     if (lr) *lr = rank;
724208a22f4bSMatthew G. Knepley   }
724308a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
724408a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
724508a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
724608a22f4bSMatthew G. Knepley }
724708a22f4bSMatthew G. Knepley 
7248ca8062c8SMatthew G. Knepley /*@
724918e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
725018e14f0cSMatthew G. Knepley 
725118e14f0cSMatthew G. Knepley   Input Parameters:
725218e14f0cSMatthew G. Knepley + dm    - The DMPlex
725318e14f0cSMatthew G. Knepley - label - The DMLabel
725418e14f0cSMatthew G. Knepley 
725518e14f0cSMatthew G. Knepley   Output Parameter:
725618e14f0cSMatthew G. Knepley . val - The label value field
725718e14f0cSMatthew G. Knepley 
725818e14f0cSMatthew G. Knepley   Options Database Keys:
725918e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
726018e14f0cSMatthew G. Knepley 
726118e14f0cSMatthew G. Knepley   Level: intermediate
726218e14f0cSMatthew G. Knepley 
726318e14f0cSMatthew G. Knepley .seealso: DMView()
726418e14f0cSMatthew G. Knepley @*/
726518e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
726618e14f0cSMatthew G. Knepley {
726718e14f0cSMatthew G. Knepley   DM             rdm;
726818e14f0cSMatthew G. Knepley   PetscFE        fe;
726918e14f0cSMatthew G. Knepley   PetscScalar   *v;
727018e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
727118e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
727218e14f0cSMatthew G. Knepley 
727318e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
727418e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
727518e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
727618e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
727718e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
727818e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
727918e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
728018e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7281e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
728218e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7283e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
728418e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
728518e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7286effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
728718e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
728818e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
728918e14f0cSMatthew G. Knepley     PetscScalar *lv;
729018e14f0cSMatthew G. Knepley     PetscInt     cval;
729118e14f0cSMatthew G. Knepley 
729218e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
729318e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
729418e14f0cSMatthew G. Knepley     *lv = cval;
729518e14f0cSMatthew G. Knepley   }
729618e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
729718e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
729818e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
729918e14f0cSMatthew G. Knepley }
730018e14f0cSMatthew G. Knepley 
730118e14f0cSMatthew G. Knepley /*@
7302ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7303ca8062c8SMatthew G. Knepley 
730469916449SMatthew G. Knepley   Input Parameter:
730569916449SMatthew G. Knepley . dm - The DMPlex object
7306ca8062c8SMatthew G. Knepley 
730795eb5ee5SVaclav Hapla   Notes:
730895eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
730995eb5ee5SVaclav Hapla 
731095eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7311ca8062c8SMatthew G. Knepley 
7312ca8062c8SMatthew G. Knepley   Level: developer
7313ca8062c8SMatthew G. Knepley 
731495eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7315ca8062c8SMatthew G. Knepley @*/
7316ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7317ca8062c8SMatthew G. Knepley {
7318ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7319ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7320ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
732157beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
732257beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7323ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7324ca8062c8SMatthew G. Knepley 
7325ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7326ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7327412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7328ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7329ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7330ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7331ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7332ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7333ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7334ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7335ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
733642e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
733742e66dfaSMatthew G. Knepley       PetscInt  d;
733842e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
733942e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
734042e66dfaSMatthew G. Knepley       }
7341ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7342ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7343ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7344ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7345ca8062c8SMatthew G. Knepley       }
734642e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
73478ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7348ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
73498ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7350ca8062c8SMatthew G. Knepley         }
7351302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
73528ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7353ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
73548ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7355ca8062c8SMatthew G. Knepley         }
7356302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
73578ccfff9cSToby 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]);
73588ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7359ca8062c8SMatthew G. Knepley       }
736042e66dfaSMatthew G. Knepley     }
736157beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
736257beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7363ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7364ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7365ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7366ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7367ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7368ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
736957beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
737057beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7371ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7372ca8062c8SMatthew G. Knepley       }
7373ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
73748ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7375ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
73768ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7377ca8062c8SMatthew G. Knepley         }
7378302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
73798ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7380ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
73818ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7382ca8062c8SMatthew G. Knepley         }
7383302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
73848ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7385ca8062c8SMatthew G. Knepley       }
7386ca8062c8SMatthew G. Knepley     }
7387ca8062c8SMatthew G. Knepley   }
738857beb4faSStefano Zampini   if (storagecheck) {
7389ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7390ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
73918ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
739257beb4faSStefano Zampini   }
7393ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7394ca8062c8SMatthew G. Knepley }
7395ca8062c8SMatthew G. Knepley 
7396412e9a14SMatthew G. Knepley /*
7397412e9a14SMatthew G. Knepley   For submeshes with cohesive cells (see DMPlexConstructCohesiveCells()), we allow a special case where some of the boundary of a face (edges and vertices) are not duplicated. We call these special boundary points "unsplit", since the same edge or vertex appears in both copies of the face. These unsplit points throw off our counting, so we have to explicitly account for them here.
7398412e9a14SMatthew G. Knepley */
7399412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7400412e9a14SMatthew G. Knepley {
7401412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7402412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7403412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7404412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7405412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7406412e9a14SMatthew G. Knepley 
7407412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7408412e9a14SMatthew G. Knepley   *unsplit = 0;
7409412e9a14SMatthew G. Knepley   switch (ct) {
7410412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7411412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7412412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7413412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7414412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7415412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7416412e9a14SMatthew G. Knepley       }
7417412e9a14SMatthew G. Knepley       break;
7418412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7419412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7420412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7421412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7422412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7423412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7424412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7425412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7426412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7427412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7428412e9a14SMatthew G. Knepley             PetscInt p;
7429412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7430412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7431412e9a14SMatthew G. Knepley           }
7432412e9a14SMatthew G. Knepley         }
7433412e9a14SMatthew G. Knepley       }
7434412e9a14SMatthew G. Knepley       break;
7435412e9a14SMatthew G. Knepley     default: break;
7436412e9a14SMatthew G. Knepley   }
7437412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7438412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7439412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7440412e9a14SMatthew G. Knepley   }
7441412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7442412e9a14SMatthew G. Knepley }
7443412e9a14SMatthew G. Knepley 
7444ca8062c8SMatthew G. Knepley /*@
7445ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7446ca8062c8SMatthew G. Knepley 
7447ca8062c8SMatthew G. Knepley   Input Parameters:
7448ca8062c8SMatthew G. Knepley + dm - The DMPlex object
744958723a97SMatthew G. Knepley - cellHeight - Normally 0
7450ca8062c8SMatthew G. Knepley 
745195eb5ee5SVaclav Hapla   Notes:
745295eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
745325c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7454ca8062c8SMatthew G. Knepley 
745595eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
745695eb5ee5SVaclav Hapla 
7457ca8062c8SMatthew G. Knepley   Level: developer
7458ca8062c8SMatthew G. Knepley 
745995eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7460ca8062c8SMatthew G. Knepley @*/
746125c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7462ca8062c8SMatthew G. Knepley {
7463412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7464412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7465412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7466ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7467ca8062c8SMatthew G. Knepley 
7468ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7469ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7470412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
747125c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
747258723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7473412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7474412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7475412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
747658723a97SMatthew G. Knepley 
7477412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7478412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7479412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7480412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7481412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7482412e9a14SMatthew G. Knepley       if (coneSize != DMPolytopeTypeGetConeSize(ct)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has cone size %D != %D", c, coneSize, DMPolytopeTypeGetConeSize(ct));
7483412e9a14SMatthew G. Knepley     }
748458723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
748558723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
748658723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7487412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
748858723a97SMatthew G. Knepley     }
748958723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7490412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7491412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7492412e9a14SMatthew G. Knepley       PetscInt unsplit;
749342363296SMatthew G. Knepley 
7494412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7495412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
749642363296SMatthew G. Knepley     }
7497412e9a14SMatthew G. Knepley     if (Nv != DMPolytopeTypeGetNumVertices(ct)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D vertices != %D", c, Nv, DMPolytopeTypeGetNumVertices(ct));
749842363296SMatthew G. Knepley   }
7499ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7500ca8062c8SMatthew G. Knepley }
75019bf0dad6SMatthew G. Knepley 
75029bf0dad6SMatthew G. Knepley /*@
75039bf0dad6SMatthew 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
75049bf0dad6SMatthew G. Knepley 
7505899ea2b8SJacob Faibussowitsch   Not Collective
7506899ea2b8SJacob Faibussowitsch 
75079bf0dad6SMatthew G. Knepley   Input Parameters:
75089bf0dad6SMatthew G. Knepley + dm - The DMPlex object
75099bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
75109bf0dad6SMatthew G. Knepley 
751145da879fSVaclav Hapla   Notes:
751245da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
751345da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
751445da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
751545da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
75169bf0dad6SMatthew G. Knepley 
751795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
751895eb5ee5SVaclav Hapla 
75199bf0dad6SMatthew G. Knepley   Level: developer
75209bf0dad6SMatthew G. Knepley 
752195eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
75229bf0dad6SMatthew G. Knepley @*/
752325c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
75249bf0dad6SMatthew G. Knepley {
7525ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
75269bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7527899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
75289bf0dad6SMatthew G. Knepley 
75299bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
75309bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7531899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
753245da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
753345da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7534899ea2b8SJacob Faibussowitsch     PetscMPIInt	rank;
7535899ea2b8SJacob Faibussowitsch     MPI_Comm	comm;
7536899ea2b8SJacob Faibussowitsch 
7537899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7538899ea2b8SJacob Faibussowitsch     ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
753945da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7540899ea2b8SJacob Faibussowitsch   }
7541899ea2b8SJacob Faibussowitsch 
7542c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7543ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
75449bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7545ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
75463554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
75473554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7548412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7549412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7550ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7551412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7552412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
75539bf0dad6SMatthew G. Knepley 
7554ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7555412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7556412e9a14SMatthew G. Knepley       if (unsplit) continue;
75579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
75589bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
75599bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
75609bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
75619bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
75629bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
75639bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
75649bf0dad6SMatthew G. Knepley       }
7565412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
75668ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
75679bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
75689bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
75699bf0dad6SMatthew G. Knepley 
75709bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
75719bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
75729bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
75739bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
75749bf0dad6SMatthew G. Knepley         }
7575412e9a14SMatthew G. Knepley         if (fnumCorners != faceSizes[f]) 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, faceSizes[f]);
75769bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7577412e9a14SMatthew G. Knepley           if (fclosure[v] != faces[fOff+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[fOff+v]);
75789bf0dad6SMatthew G. Knepley         }
75799bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7580412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
75819bf0dad6SMatthew G. Knepley       }
7582412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
75839bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
75849bf0dad6SMatthew G. Knepley     }
75853554e41dSMatthew G. Knepley   }
7586552f7358SJed Brown   PetscFunctionReturn(0);
7587552f7358SJed Brown }
75883913d7c8SMatthew G. Knepley 
7589bb6a34a8SMatthew G. Knepley /*@
7590bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7591bb6a34a8SMatthew G. Knepley 
7592bb6a34a8SMatthew G. Knepley   Input Parameter:
7593bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7594bb6a34a8SMatthew G. Knepley 
759595eb5ee5SVaclav Hapla   Notes:
759695eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
759795eb5ee5SVaclav Hapla 
759895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7599bb6a34a8SMatthew G. Knepley 
7600bb6a34a8SMatthew G. Knepley   Level: developer
7601bb6a34a8SMatthew G. Knepley 
760295eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7603bb6a34a8SMatthew G. Knepley @*/
7604bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7605bb6a34a8SMatthew G. Knepley {
7606bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7607bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7608412e9a14SMatthew G. Knepley   PetscBool      periodic;
7609412e9a14SMatthew G. Knepley   PetscInt       dim, depth, d, cStart, cEnd, c;
7610bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7611bb6a34a8SMatthew G. Knepley 
7612bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7613bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7614bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7615412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7616bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7617bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7618412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7619412e9a14SMatthew G. Knepley     DMPolytopeType ct;
7620412e9a14SMatthew G. Knepley     PetscInt       unsplit;
7621412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
7622412e9a14SMatthew G. Knepley 
7623412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7624412e9a14SMatthew G. Knepley     switch (ct) {
7625412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7626412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7627412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7628412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
7629412e9a14SMatthew G. Knepley       default: break;
7630412e9a14SMatthew G. Knepley     }
7631412e9a14SMatthew G. Knepley     switch (ct) {
7632412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
7633412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7634412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7635412e9a14SMatthew G. Knepley         continue;
7636412e9a14SMatthew G. Knepley       default: break;
7637412e9a14SMatthew G. Knepley     }
7638412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7639412e9a14SMatthew G. Knepley     if (unsplit) continue;
7640bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7641412e9a14SMatthew G. Knepley     if (detJ < -PETSC_SMALL || (detJ <= 0.0 && !ignoreZeroVol)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
7642bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7643412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
7644bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7645412e9a14SMatthew G. Knepley       if (vol < -PETSC_SMALL || (vol <= 0.0 && !ignoreZeroVol)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
7646bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7647bb6a34a8SMatthew G. Knepley     }
7648bb6a34a8SMatthew G. Knepley   }
7649bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7650bb6a34a8SMatthew G. Knepley }
7651bb6a34a8SMatthew G. Knepley 
765203da9461SVaclav Hapla /*@
7653e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
765403da9461SVaclav Hapla 
765503da9461SVaclav Hapla   Input Parameters:
765603da9461SVaclav Hapla . dm - The DMPlex object
765703da9461SVaclav Hapla 
7658e83a0d2dSVaclav Hapla   Notes:
7659e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
76608918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
766103da9461SVaclav Hapla 
766295eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
766395eb5ee5SVaclav Hapla 
766403da9461SVaclav Hapla   Level: developer
766503da9461SVaclav Hapla 
766695eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
766703da9461SVaclav Hapla @*/
766803da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
766903da9461SVaclav Hapla {
7670f0cfc026SVaclav Hapla   PetscSF         pointSF;
7671f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7672f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
7673f0cfc026SVaclav Hapla   PetscBool       distributed;
767403da9461SVaclav Hapla   PetscErrorCode  ierr;
767503da9461SVaclav Hapla 
767603da9461SVaclav Hapla   PetscFunctionBegin;
767703da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7678f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7679f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7680f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7681f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7682f0cfc026SVaclav Hapla   if (overlap) {
76838918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
76848918e3e2SVaclav Hapla     PetscFunctionReturn(0);
76858918e3e2SVaclav Hapla   }
7686f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7687f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7688f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7689f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
7690f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
769103da9461SVaclav Hapla 
7692ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7693f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7694f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7695f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7696f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
7697f5869d18SMatthew G. Knepley 
7698f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
769903da9461SVaclav Hapla   }
7700ece87651SVaclav Hapla 
7701f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
7702f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7703f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
7704f5869d18SMatthew G. Knepley     const PetscInt *cone;
7705f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
7706f5869d18SMatthew G. Knepley 
7707f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
7708f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
7709f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
7710f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
7711f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
7712f5869d18SMatthew G. Knepley         if (idx < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D but not %D from its cone", point, cone[c]);
7713f5869d18SMatthew G. Knepley       }
7714f5869d18SMatthew G. Knepley     }
7715ece87651SVaclav Hapla   }
771603da9461SVaclav Hapla   PetscFunctionReturn(0);
771703da9461SVaclav Hapla }
771803da9461SVaclav Hapla 
7719068a5610SStefano Zampini typedef struct cell_stats
7720068a5610SStefano Zampini {
7721068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7722068a5610SStefano Zampini   PetscInt  count;
7723068a5610SStefano Zampini } cell_stats_t;
7724068a5610SStefano Zampini 
772525befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7726068a5610SStefano Zampini {
7727068a5610SStefano Zampini   PetscInt i, N = *len;
7728068a5610SStefano Zampini 
7729068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7730068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7731068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7732068a5610SStefano Zampini 
7733068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7734068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7735068a5610SStefano Zampini     B->sum += A->sum;
7736068a5610SStefano Zampini     B->squaresum += A->squaresum;
7737068a5610SStefano Zampini     B->count += A->count;
7738068a5610SStefano Zampini   }
7739068a5610SStefano Zampini }
7740068a5610SStefano Zampini 
7741068a5610SStefano Zampini /*@
774243fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
7743068a5610SStefano Zampini 
77448261a58bSMatthew G. Knepley   Collective on dm
77458261a58bSMatthew G. Knepley 
7746068a5610SStefano Zampini   Input Parameters:
7747068a5610SStefano Zampini + dm        - The DMPlex object
774843fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
774943fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
7750068a5610SStefano Zampini 
775195eb5ee5SVaclav Hapla   Notes:
775295eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
775395eb5ee5SVaclav Hapla 
775495eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7755068a5610SStefano Zampini 
7756068a5610SStefano Zampini   Level: developer
7757068a5610SStefano Zampini 
7758f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
7759068a5610SStefano Zampini @*/
776043fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7761068a5610SStefano Zampini {
7762068a5610SStefano Zampini   DM             dmCoarse;
776343fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
776443fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
776543fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
776643fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
7767412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
776843fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
7769068a5610SStefano Zampini   PetscErrorCode ierr;
7770068a5610SStefano Zampini 
7771068a5610SStefano Zampini   PetscFunctionBegin;
7772068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7773068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7774068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7775068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7776068a5610SStefano Zampini   stats.count = 0;
7777068a5610SStefano Zampini 
777843fa8764SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
777943fa8764SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
778043fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
778143fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
7782412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
778343fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
7784412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
7785068a5610SStefano Zampini     PetscInt  i;
7786068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7787068a5610SStefano Zampini 
7788068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7789068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
779043fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
7791068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7792068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7793068a5610SStefano Zampini     }
7794068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7795068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7796068a5610SStefano Zampini 
7797068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7798068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7799068a5610SStefano Zampini     stats.sum       += cond;
7800068a5610SStefano Zampini     stats.squaresum += cond2;
7801068a5610SStefano Zampini     stats.count++;
78028261a58bSMatthew G. Knepley     if (output && cond > limit) {
780343fa8764SMatthew G. Knepley       PetscSection coordSection;
780443fa8764SMatthew G. Knepley       Vec          coordsLocal;
780543fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
780643fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
780743fa8764SMatthew G. Knepley 
780843fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
780943fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
781043fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
7811087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
781243fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
781343fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
781443fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
781543fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
781648afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
781743fa8764SMatthew G. Knepley         }
781843fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
781943fa8764SMatthew G. Knepley       }
782043fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
782143fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
782243fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
782343fa8764SMatthew G. Knepley 
782443fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
782543fa8764SMatthew G. Knepley           PetscReal len;
782643fa8764SMatthew G. Knepley 
782743fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
7828087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
782943fa8764SMatthew G. Knepley         }
783043fa8764SMatthew G. Knepley       }
783143fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
783243fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
783343fa8764SMatthew G. Knepley     }
7834068a5610SStefano Zampini   }
78358261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
7836068a5610SStefano Zampini 
7837068a5610SStefano Zampini   if (size > 1) {
7838068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7839068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7840068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7841068a5610SStefano Zampini     MPI_Op        statReduce;
7842068a5610SStefano Zampini 
7843068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
7844068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
7845068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
7846068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
7847068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
7848068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
7849068a5610SStefano Zampini   } else {
7850580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
7851068a5610SStefano Zampini   }
7852068a5610SStefano Zampini   if (!rank) {
7853068a5610SStefano Zampini     count = globalStats.count;
7854068a5610SStefano Zampini     min   = globalStats.min;
7855068a5610SStefano Zampini     max   = globalStats.max;
7856068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7857068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7858068a5610SStefano Zampini   }
7859068a5610SStefano Zampini 
7860068a5610SStefano Zampini   if (output) {
7861068a5610SStefano 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);
7862068a5610SStefano Zampini   }
7863068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7864068a5610SStefano Zampini 
7865068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7866068a5610SStefano Zampini   if (dmCoarse) {
7867068a5610SStefano Zampini     PetscBool isplex;
7868068a5610SStefano Zampini 
7869068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7870068a5610SStefano Zampini     if (isplex) {
787143fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
7872068a5610SStefano Zampini     }
7873068a5610SStefano Zampini   }
7874068a5610SStefano Zampini   PetscFunctionReturn(0);
7875068a5610SStefano Zampini }
7876068a5610SStefano Zampini 
7877f108dbd7SJacob Faibussowitsch /*@
7878f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
7879f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
7880f108dbd7SJacob Faibussowitsch 
7881f108dbd7SJacob Faibussowitsch   Collective
7882f108dbd7SJacob Faibussowitsch 
7883f108dbd7SJacob Faibussowitsch   Input Parameters:
7884f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
7885f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
7886f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
7887f108dbd7SJacob Faibussowitsch 
7888f108dbd7SJacob Faibussowitsch   Output Parameters:
7889f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
7890f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
7891f108dbd7SJacob Faibussowitsch 
7892f108dbd7SJacob Faibussowitsch   Options Database Keys:
7893f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
7894f108dbd7SJacob Faibussowitsch supported.
7895f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
7896f108dbd7SJacob Faibussowitsch 
7897f108dbd7SJacob Faibussowitsch   Notes:
7898f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
7899f108dbd7SJacob Faibussowitsch 
7900f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
7901f108dbd7SJacob Faibussowitsch 
7902f108dbd7SJacob Faibussowitsch   Where A_i is the i'th face-normal vector, f_i is the vector from the cell centroid to the i'th face centroid, and c_i
7903f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
7904f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
7905f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
7906f108dbd7SJacob Faibussowitsch 
7907f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
7908f108dbd7SJacob Faibussowitsch 
7909f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
7910f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
7911f108dbd7SJacob Faibussowitsch 
7912f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
7913f108dbd7SJacob Faibussowitsch 
7914f108dbd7SJacob Faibussowitsch   Level: intermediate
7915f108dbd7SJacob Faibussowitsch 
7916f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
7917f108dbd7SJacob Faibussowitsch @*/
7918f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
7919f108dbd7SJacob Faibussowitsch {
7920f108dbd7SJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell;
7921f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
7922f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
7923f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
7924f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
7925f108dbd7SJacob Faibussowitsch   IS                      glob;
7926f108dbd7SJacob Faibussowitsch   DMPlexInterpolatedFlag  interpFlag;
7927f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
7928f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
7929f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
7930f108dbd7SJacob Faibussowitsch 
7931f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
7932f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7933f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
7934f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7935f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
7936f108dbd7SJacob Faibussowitsch   if (nc < 2) {
7937f108dbd7SJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
7938f108dbd7SJacob Faibussowitsch   }
7939f108dbd7SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
7940f108dbd7SJacob Faibussowitsch   if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
7941f108dbd7SJacob Faibussowitsch     PetscMPIInt  rank;
7942f108dbd7SJacob Faibussowitsch 
7943f108dbd7SJacob Faibussowitsch     ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
7944f108dbd7SJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
7945f108dbd7SJacob Faibussowitsch   }
7946f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
7947f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
7948f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
7949f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
7950f108dbd7SJacob Faibussowitsch   } else {
7951f108dbd7SJacob Faibussowitsch     *OrthQualLabel = NULL;
7952f108dbd7SJacob Faibussowitsch   }
7953f108dbd7SJacob Faibussowitsch 
7954f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7955f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7956f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
7957f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
7958f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
7959f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
7960f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
7961f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
7962f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
7963f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
7964f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
7965f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
7966f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
7967f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
7968f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
7969f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
7970f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
7971f108dbd7SJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cell++) {
7972f108dbd7SJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, nf, adjSize = PETSC_DETERMINE, ix = cell-cStart;
7973f108dbd7SJacob Faibussowitsch     const PetscInt     *cone;
7974f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
7975f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
7976f108dbd7SJacob Faibussowitsch     PetscReal          minvalc = 1.0, minvalf = 1.0, OQ;
7977f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
7978f108dbd7SJacob Faibussowitsch 
7979f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
7980f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
7981f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
7982f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
7983f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetConeSize(dm, cell, &nf);CHKERRQ(ierr);
7984f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
7985f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
7986f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
7987f108dbd7SJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneigh++) {
7988f108dbd7SJacob Faibussowitsch       PetscInt         numcovpts, i, neigh = adj[cellneigh];
7989f108dbd7SJacob Faibussowitsch       const PetscInt   *covpts;
7990f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
7991f108dbd7SJacob Faibussowitsch       PetscReal        *ci, *fi, *Ai;
7992f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
7993f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
7994f108dbd7SJacob Faibussowitsch 
7995f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
7996f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
7997f108dbd7SJacob Faibussowitsch       ierr = PetscMalloc3(nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
7998f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
7999f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
8000f108dbd7SJacob Faibussowitsch       ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8001f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
8002f108dbd7SJacob Faibussowitsch       ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8003f108dbd7SJacob Faibussowitsch 
8004f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
8005f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8006f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8007f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
8008f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
8009f108dbd7SJacob Faibussowitsch         normci += PetscPowScalar(ci[i], 2);
8010f108dbd7SJacob Faibussowitsch         normfi += PetscPowScalar(fi[i], 2);
8011f108dbd7SJacob Faibussowitsch         normai += PetscPowScalar(Ai[i], 2);
8012f108dbd7SJacob Faibussowitsch       }
8013f108dbd7SJacob Faibussowitsch       normci = PetscSqrtScalar(normci);
8014f108dbd7SJacob Faibussowitsch       normfi = PetscSqrtScalar(normfi);
8015f108dbd7SJacob Faibussowitsch       normai = PetscSqrtScalar(normai);
8016f108dbd7SJacob Faibussowitsch 
8017f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
8018f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8019f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
8020f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
8021f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
8022f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
8023f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8024f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8025f108dbd7SJacob Faibussowitsch       }
8026f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8027f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
8028f108dbd7SJacob Faibussowitsch       }
8029f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8030f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
8031f108dbd7SJacob Faibussowitsch       }
8032f108dbd7SJacob Faibussowitsch       cellneighiter++;
8033f108dbd7SJacob Faibussowitsch       ierr = PetscFree3(ci, fi, Ai);CHKERRQ(ierr);
8034f108dbd7SJacob Faibussowitsch     }
8035f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
8036f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
8037f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
8038f108dbd7SJacob Faibussowitsch     OQ = PetscMin(minvalf, minvalc);
8039f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
8040f108dbd7SJacob Faibussowitsch       if (OQ <= atol) {
8041f108dbd7SJacob Faibussowitsch         ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);
8042f108dbd7SJacob Faibussowitsch       }
8043f108dbd7SJacob Faibussowitsch     }
8044f108dbd7SJacob Faibussowitsch     ierr = VecSetValuesLocal(*OrthQual, 1, (const PetscInt *) &ix, (const PetscScalar *) &OQ, INSERT_VALUES);CHKERRQ(ierr);
8045f108dbd7SJacob Faibussowitsch   }
8046f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
8047f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
8048f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8049f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8050f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
8051f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8052f108dbd7SJacob Faibussowitsch     if (vwr) {
8053f108dbd7SJacob Faibussowitsch       ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);
8054f108dbd7SJacob Faibussowitsch     }
8055f108dbd7SJacob Faibussowitsch   }
8056f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
8057f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
8058f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
8059f108dbd7SJacob Faibussowitsch }
8060f108dbd7SJacob Faibussowitsch 
8061bceba477SMatthew G. Knepley /* Pointwise interpolation
8062bceba477SMatthew G. Knepley      Just code FEM for now
8063bceba477SMatthew G. Knepley      u^f = I u^c
80644ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
80654ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
80664ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
8067bceba477SMatthew G. Knepley */
8068bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8069bceba477SMatthew G. Knepley {
8070bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
8071bceba477SMatthew G. Knepley   PetscInt       m, n;
8072a063dac3SMatthew G. Knepley   void          *ctx;
807368132eb9SMatthew G. Knepley   DM             cdm;
8074cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
8075bceba477SMatthew G. Knepley   PetscErrorCode ierr;
8076bceba477SMatthew G. Knepley 
8077bceba477SMatthew G. Knepley   PetscFunctionBegin;
8078e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8079bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8080e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8081bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
808268132eb9SMatthew G. Knepley 
8083fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
8084bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
8085bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8086fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
8087a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
808868132eb9SMatthew G. Knepley 
8089a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
809068132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8091cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
809268132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
809368132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
80944db47ee9SStefano Zampini   if (scaling) {
80955d1c2e58SMatthew G. Knepley     /* Use naive scaling */
80965d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
80974db47ee9SStefano Zampini   }
8098a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
8099a063dac3SMatthew G. Knepley }
8100bceba477SMatthew G. Knepley 
81016dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8102a063dac3SMatthew G. Knepley {
810390748bafSMatthew G. Knepley   PetscErrorCode ierr;
81046dbf9973SLawrence Mitchell   VecScatter     ctx;
810590748bafSMatthew G. Knepley 
8106a063dac3SMatthew G. Knepley   PetscFunctionBegin;
81076dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
81086dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
81096dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
8110bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
8111bceba477SMatthew G. Knepley }
8112bceba477SMatthew G. Knepley 
8113bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8114bd041c0cSMatthew G. Knepley {
8115bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
8116bd041c0cSMatthew G. Knepley   PetscInt       m, n;
8117bd041c0cSMatthew G. Knepley   void          *ctx;
8118bd041c0cSMatthew G. Knepley   DM             cdm;
8119bd041c0cSMatthew G. Knepley   PetscBool      regular;
8120bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
8121bd041c0cSMatthew G. Knepley 
8122bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
8123e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8124bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8125e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8126bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
8127bd041c0cSMatthew G. Knepley 
8128bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
8129bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8130bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
8131bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
8132bd041c0cSMatthew G. Knepley 
8133bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
8134bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8135bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8136bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8137bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
8138bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
8139bd041c0cSMatthew G. Knepley }
8140bd041c0cSMatthew G. Knepley 
81410aef6b92SMatthew G. Knepley /*@
81420aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
81430aef6b92SMatthew G. Knepley 
81440aef6b92SMatthew G. Knepley   Input Parameter:
81450aef6b92SMatthew G. Knepley . dm - The DMPlex object
81460aef6b92SMatthew G. Knepley 
81470aef6b92SMatthew G. Knepley   Output Parameter:
81480aef6b92SMatthew G. Knepley . regular - The flag
81490aef6b92SMatthew G. Knepley 
81500aef6b92SMatthew G. Knepley   Level: intermediate
81510aef6b92SMatthew G. Knepley 
81520aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
81530aef6b92SMatthew G. Knepley @*/
81540aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
81550aef6b92SMatthew G. Knepley {
81560aef6b92SMatthew G. Knepley   PetscFunctionBegin;
81570aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81580aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
81590aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
81600aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
81610aef6b92SMatthew G. Knepley }
81620aef6b92SMatthew G. Knepley 
81630aef6b92SMatthew G. Knepley /*@
81640aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
81650aef6b92SMatthew G. Knepley 
81660aef6b92SMatthew G. Knepley   Input Parameters:
81670aef6b92SMatthew G. Knepley + dm - The DMPlex object
81680aef6b92SMatthew G. Knepley - regular - The flag
81690aef6b92SMatthew G. Knepley 
81700aef6b92SMatthew G. Knepley   Level: intermediate
81710aef6b92SMatthew G. Knepley 
81720aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
81730aef6b92SMatthew G. Knepley @*/
81740aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
81750aef6b92SMatthew G. Knepley {
81760aef6b92SMatthew G. Knepley   PetscFunctionBegin;
81770aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81780aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
81790aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
81800aef6b92SMatthew G. Knepley }
81810aef6b92SMatthew G. Knepley 
8182412e9a14SMatthew G. Knepley /*@
8183412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
8184412e9a14SMatthew G. Knepley 
8185412e9a14SMatthew G. Knepley   Input Parameter:
8186412e9a14SMatthew G. Knepley . dm - The DMPlex object
8187412e9a14SMatthew G. Knepley 
8188412e9a14SMatthew G. Knepley   Output Parameter:
8189412e9a14SMatthew G. Knepley . cr - The strategy number
8190412e9a14SMatthew G. Knepley 
8191412e9a14SMatthew G. Knepley   Level: intermediate
8192412e9a14SMatthew G. Knepley 
8193412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8194412e9a14SMatthew G. Knepley @*/
8195412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8196412e9a14SMatthew G. Knepley {
8197412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8198412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8199412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
8200412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8201412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8202412e9a14SMatthew G. Knepley }
8203412e9a14SMatthew G. Knepley 
8204412e9a14SMatthew G. Knepley /*@
8205412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
8206412e9a14SMatthew G. Knepley 
8207412e9a14SMatthew G. Knepley   Input Parameters:
8208412e9a14SMatthew G. Knepley + dm - The DMPlex object
8209412e9a14SMatthew G. Knepley - cr - The strategy number
8210412e9a14SMatthew G. Knepley 
8211412e9a14SMatthew G. Knepley   Level: intermediate
8212412e9a14SMatthew G. Knepley 
8213412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8214412e9a14SMatthew G. Knepley @*/
8215412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8216412e9a14SMatthew G. Knepley {
8217412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8218412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8219412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
8220412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8221412e9a14SMatthew G. Knepley }
8222412e9a14SMatthew G. Knepley 
8223f7c74593SToby Isaac /* anchors */
8224a68b90caSToby Isaac /*@
8225f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
8226f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
8227a68b90caSToby Isaac 
8228e228b242SToby Isaac   not collective
8229a68b90caSToby Isaac 
8230a68b90caSToby Isaac   Input Parameters:
8231a68b90caSToby Isaac . dm - The DMPlex object
8232a68b90caSToby Isaac 
8233a68b90caSToby Isaac   Output Parameters:
8234a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
8235a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
8236a68b90caSToby Isaac 
8237a68b90caSToby Isaac 
8238a68b90caSToby Isaac   Level: intermediate
8239a68b90caSToby Isaac 
8240f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8241a68b90caSToby Isaac @*/
8242a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8243a68b90caSToby Isaac {
8244a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
824541e6d900SToby Isaac   PetscErrorCode ierr;
8246a68b90caSToby Isaac 
8247a68b90caSToby Isaac   PetscFunctionBegin;
8248a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
824941e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
8250a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
8251a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
8252a68b90caSToby Isaac   PetscFunctionReturn(0);
8253a68b90caSToby Isaac }
8254a68b90caSToby Isaac 
8255a68b90caSToby Isaac /*@
8256f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
8257f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
8258a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
8259a68b90caSToby Isaac 
8260a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
8261f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
8262a68b90caSToby Isaac 
8263e228b242SToby Isaac   collective on dm
8264a68b90caSToby Isaac 
8265a68b90caSToby Isaac   Input Parameters:
8266a68b90caSToby Isaac + dm - The DMPlex object
8267e228b242SToby 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).
8268e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
8269a68b90caSToby Isaac 
8270a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
8271a68b90caSToby Isaac 
8272a68b90caSToby Isaac   Level: intermediate
8273a68b90caSToby Isaac 
8274f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8275a68b90caSToby Isaac @*/
8276a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8277a68b90caSToby Isaac {
8278a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8279e228b242SToby Isaac   PetscMPIInt    result;
8280a68b90caSToby Isaac   PetscErrorCode ierr;
8281a68b90caSToby Isaac 
8282a68b90caSToby Isaac   PetscFunctionBegin;
8283a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8284e228b242SToby Isaac   if (anchorSection) {
8285e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
8286e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
8287f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8288e228b242SToby Isaac   }
8289e228b242SToby Isaac   if (anchorIS) {
8290e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
8291e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
8292f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8293e228b242SToby Isaac   }
8294a68b90caSToby Isaac 
8295a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
8296a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
8297a68b90caSToby Isaac   plex->anchorSection = anchorSection;
8298a68b90caSToby Isaac 
8299a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
8300a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
8301a68b90caSToby Isaac   plex->anchorIS = anchorIS;
8302a68b90caSToby Isaac 
8303cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8304a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
8305a68b90caSToby Isaac     const PetscInt *anchors;
8306a68b90caSToby Isaac 
8307a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
8308a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
8309a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
8310a68b90caSToby Isaac     for (a = 0; a < size; a++) {
8311a68b90caSToby Isaac       PetscInt p;
8312a68b90caSToby Isaac 
8313a68b90caSToby Isaac       p = anchors[a];
8314a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
8315a68b90caSToby Isaac         PetscInt dof;
8316a68b90caSToby Isaac 
8317a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8318a68b90caSToby Isaac         if (dof) {
8319a68b90caSToby Isaac           PetscErrorCode ierr2;
8320a68b90caSToby Isaac 
8321a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
83228ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8323a68b90caSToby Isaac         }
8324a68b90caSToby Isaac       }
8325a68b90caSToby Isaac     }
8326a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
8327a68b90caSToby Isaac   }
8328f7c74593SToby Isaac   /* reset the generic constraints */
8329f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
8330a68b90caSToby Isaac   PetscFunctionReturn(0);
8331a68b90caSToby Isaac }
8332a68b90caSToby Isaac 
8333f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8334a68b90caSToby Isaac {
8335f7c74593SToby Isaac   PetscSection anchorSection;
83366995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
8337a68b90caSToby Isaac   PetscErrorCode ierr;
8338a68b90caSToby Isaac 
8339a68b90caSToby Isaac   PetscFunctionBegin;
8340a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8341a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
8342e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
8343a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
83446995de1eSToby Isaac   if (numFields) {
8345719ab38cSToby Isaac     PetscInt f;
8346a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
8347719ab38cSToby Isaac 
8348719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
8349719ab38cSToby Isaac       PetscInt numComp;
8350719ab38cSToby Isaac 
8351719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
8352719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
8353719ab38cSToby Isaac     }
83546995de1eSToby Isaac   }
8355a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
83566995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
83576995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
83586995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
83596995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
8360a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
8361a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
8362a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8363a68b90caSToby Isaac     if (dof) {
8364a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
8365a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
8366a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
8367a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
8368a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
8369a68b90caSToby Isaac       }
8370a68b90caSToby Isaac     }
8371a68b90caSToby Isaac   }
8372a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
8373a68b90caSToby Isaac   PetscFunctionReturn(0);
8374a68b90caSToby Isaac }
8375a68b90caSToby Isaac 
8376f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8377a68b90caSToby Isaac {
8378f7c74593SToby Isaac   PetscSection aSec;
83790ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
83800ac89760SToby Isaac   const PetscInt *anchors;
83810ac89760SToby Isaac   PetscInt numFields, f;
838266ad2231SToby Isaac   IS aIS;
83830ac89760SToby Isaac   PetscErrorCode ierr;
83840ac89760SToby Isaac 
83850ac89760SToby Isaac   PetscFunctionBegin;
83860ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83870ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
83880ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
83890ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
83900ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
8391302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
8392a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
839366ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
83946995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
83956995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
83960ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
83970ac89760SToby Isaac   i[0] = 0;
83980ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
83990ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
8400f19733c5SToby Isaac     PetscInt rDof, rOff, r;
8401f19733c5SToby Isaac 
8402f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
8403f19733c5SToby Isaac     if (!rDof) continue;
8404f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
84050ac89760SToby Isaac     if (numFields) {
84060ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
84070ac89760SToby Isaac         annz = 0;
8408f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
8409f19733c5SToby Isaac           a = anchors[rOff + r];
84100ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
84110ac89760SToby Isaac           annz += aDof;
84120ac89760SToby Isaac         }
84130ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
84140ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
84150ac89760SToby Isaac         for (q = 0; q < dof; q++) {
84160ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
84170ac89760SToby Isaac         }
84180ac89760SToby Isaac       }
84190ac89760SToby Isaac     }
84200ac89760SToby Isaac     else {
84210ac89760SToby Isaac       annz = 0;
84220ac89760SToby Isaac       for (q = 0; q < dof; q++) {
84230ac89760SToby Isaac         a = anchors[off + q];
84240ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
84250ac89760SToby Isaac         annz += aDof;
84260ac89760SToby Isaac       }
84270ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
84280ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
84290ac89760SToby Isaac       for (q = 0; q < dof; q++) {
84300ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
84310ac89760SToby Isaac       }
84320ac89760SToby Isaac     }
84330ac89760SToby Isaac   }
84340ac89760SToby Isaac   nnz = i[m];
84350ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
84360ac89760SToby Isaac   offset = 0;
84370ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
84380ac89760SToby Isaac     if (numFields) {
84390ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
84400ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
84410ac89760SToby Isaac         for (q = 0; q < dof; q++) {
84420ac89760SToby Isaac           PetscInt rDof, rOff, r;
84430ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
84440ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
84450ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
84460ac89760SToby Isaac             PetscInt s;
84470ac89760SToby Isaac 
84480ac89760SToby Isaac             a = anchors[rOff + r];
84490ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
84500ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
84510ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
84520ac89760SToby Isaac               j[offset++] = aOff + s;
84530ac89760SToby Isaac             }
84540ac89760SToby Isaac           }
84550ac89760SToby Isaac         }
84560ac89760SToby Isaac       }
84570ac89760SToby Isaac     }
84580ac89760SToby Isaac     else {
84590ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
84600ac89760SToby Isaac       for (q = 0; q < dof; q++) {
84610ac89760SToby Isaac         PetscInt rDof, rOff, r;
84620ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
84630ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
84640ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
84650ac89760SToby Isaac           PetscInt s;
84660ac89760SToby Isaac 
84670ac89760SToby Isaac           a = anchors[rOff + r];
84680ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
84690ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
84700ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
84710ac89760SToby Isaac             j[offset++] = aOff + s;
84720ac89760SToby Isaac           }
84730ac89760SToby Isaac         }
84740ac89760SToby Isaac       }
84750ac89760SToby Isaac     }
84760ac89760SToby Isaac   }
84770ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
847825570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
847925570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
848066ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
84810ac89760SToby Isaac   PetscFunctionReturn(0);
84820ac89760SToby Isaac }
84830ac89760SToby Isaac 
848466ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
848566ad2231SToby Isaac {
8486f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8487f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
848866ad2231SToby Isaac   Mat            cMat;
848966ad2231SToby Isaac   PetscErrorCode ierr;
849066ad2231SToby Isaac 
849166ad2231SToby Isaac   PetscFunctionBegin;
849266ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8493a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
849466ad2231SToby Isaac   if (anchorSection) {
849544a7f3ddSMatthew G. Knepley     PetscInt Nf;
8496e228b242SToby Isaac 
849792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
8498f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
8499f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
850044a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
850144a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
850266ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
850366ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
850466ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
850566ad2231SToby Isaac   }
850666ad2231SToby Isaac   PetscFunctionReturn(0);
850766ad2231SToby Isaac }
8508a93c429eSMatthew G. Knepley 
8509a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8510a93c429eSMatthew G. Knepley {
8511a93c429eSMatthew G. Knepley   IS             subis;
8512a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
8513a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
8514a93c429eSMatthew G. Knepley 
8515a93c429eSMatthew G. Knepley   PetscFunctionBegin;
851692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
8517a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8518a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8519a93c429eSMatthew G. Knepley   /* Create subdomain */
8520a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
8521a93c429eSMatthew G. Knepley   /* Create submodel */
852297d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
8523a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
852492fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
8525a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
8526e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
8527a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
8528a93c429eSMatthew G. Knepley   if (is) {
8529a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
8530a93c429eSMatthew G. Knepley     IS              spIS;
8531a93c429eSMatthew G. Knepley     const PetscInt *spmap;
8532a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
8533a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8534a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
8535a93c429eSMatthew G. Knepley 
853697d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
8537a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
8538a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
85396f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
85406f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
8541a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
8542a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8543a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
8544a93c429eSMatthew G. Knepley 
8545a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
8546a93c429eSMatthew G. Knepley       if (gdof > 0) {
8547a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8548a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
8549a93c429eSMatthew G. Knepley 
8550a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
8551a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
8552a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
8553a93c429eSMatthew G. Knepley         }
8554a93c429eSMatthew G. Knepley         subSize += pSubSize;
8555a93c429eSMatthew G. Knepley         if (pSubSize) {
8556a93c429eSMatthew G. Knepley           if (bs < 0) {
8557a93c429eSMatthew G. Knepley             bs = pSubSize;
8558a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
8559a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
8560a93c429eSMatthew G. Knepley             bs = 1;
8561a93c429eSMatthew G. Knepley           }
8562a93c429eSMatthew G. Knepley         }
8563a93c429eSMatthew G. Knepley       }
8564a93c429eSMatthew G. Knepley     }
8565a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
8566a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8567a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
8568a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8569a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
8570a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
8571a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8572a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
8573a93c429eSMatthew G. Knepley 
8574a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
8575a93c429eSMatthew G. Knepley       if (gdof > 0) {
8576a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
8577a93c429eSMatthew G. Knepley 
8578a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
8579a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8580a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
8581a93c429eSMatthew G. Knepley 
8582a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
8583a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
8584a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
8585a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
8586a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
8587a93c429eSMatthew G. Knepley           }
8588a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
8589a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
8590a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8591a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
8592a93c429eSMatthew G. Knepley           }
8593a93c429eSMatthew G. Knepley         }
8594a93c429eSMatthew G. Knepley       }
8595a93c429eSMatthew G. Knepley     }
8596a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
8597a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
8598a93c429eSMatthew G. Knepley     if (bs > 1) {
8599a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
8600a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
8601a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
8602a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
8603a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8604a93c429eSMatthew G. Knepley         }
8605a93c429eSMatthew G. Knepley       }
8606a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
8607a93c429eSMatthew G. Knepley     }
8608a93c429eSMatthew G. Knepley     /* Attach nullspace */
8609a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
8610a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8611a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
8612a93c429eSMatthew G. Knepley     }
8613a93c429eSMatthew G. Knepley     if (f < Nf) {
8614a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
8615a93c429eSMatthew G. Knepley 
8616a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
8617a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
8618a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
8619a93c429eSMatthew G. Knepley     }
8620a93c429eSMatthew G. Knepley   }
8621a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
8622a93c429eSMatthew G. Knepley }
8623c0f0dcc3SMatthew G. Knepley 
8624c0f0dcc3SMatthew G. Knepley /*@
8625c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
8626c0f0dcc3SMatthew G. Knepley 
8627c0f0dcc3SMatthew G. Knepley   Input Parameter:
8628c0f0dcc3SMatthew G. Knepley - dm - The DM
8629c0f0dcc3SMatthew G. Knepley 
8630c0f0dcc3SMatthew G. Knepley   Level: developer
8631c0f0dcc3SMatthew G. Knepley 
8632c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8633c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
8634c0f0dcc3SMatthew G. Knepley 
8635c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
8636c0f0dcc3SMatthew G. Knepley @*/
8637c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8638c0f0dcc3SMatthew G. Knepley {
8639e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
8640c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
8641c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
8642c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
8643c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
8644c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
8645c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
8646c0f0dcc3SMatthew G. Knepley   const char        *name;
8647c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
8648e5ed2c37SJose E. Roman #endif
8649c0f0dcc3SMatthew G. Knepley 
8650c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8651c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8652c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
8653c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
8654c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8655c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
8656c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
8657c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
8658c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
8659c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
8660c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
8661c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
8662c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
8663fae64647SBarry Smith   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "DM (%s) FE Residual Integration: %D integrals %D reps\n  Cell rate: %.2g/s flop rate: %.2g MF/s\n", name ? name : "unknown", N, eventInfo.count, (double) cellRate, (double) (flopRate/1.e6));CHKERRQ(ierr);
8664c0f0dcc3SMatthew G. Knepley #else
8665c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8666c0f0dcc3SMatthew G. Knepley #endif
8667c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8668c0f0dcc3SMatthew G. Knepley }
8669