xref: /petsc/src/dm/impls/plex/plex.c (revision ae65431ddf8a50dca4b6cbc3adbb6e0fa4e4a48f)
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 */
11cadf77a0SMark Adams 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,DMPLEX_LocatePoints;
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 
308065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
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   }
88ffc4695bSBarry Smith   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(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) {
315798534f6SMatthew G. Knepley       PetscObject isZero;
316798534f6SMatthew G. Knepley 
317684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
318684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
319684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
320798534f6SMatthew G. Knepley       ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
321798534f6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
323684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
325ef31f671SMatthew G. Knepley     }
326552f7358SJed Brown     if (isvtk) {
327684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
328b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
329b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
330684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
331b136c2c9SMatthew G. Knepley #else
332b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
333b136c2c9SMatthew G. Knepley #endif
334f13a32a3SMatthew G. Knepley     } else if (isdraw) {
335684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
336684b87d9SLisandro Dalcin     } else if (isglvis) {
337684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
338684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
339684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
340684b87d9SLisandro Dalcin     }
341798534f6SMatthew G. Knepley     if (fem) {
342798534f6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
343798534f6SMatthew G. Knepley       ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
344798534f6SMatthew G. Knepley     }
345552f7358SJed Brown   } else {
346684b87d9SLisandro Dalcin     PetscBool isseq;
347684b87d9SLisandro Dalcin 
348684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
34955f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
35055f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
351552f7358SJed Brown   }
352552f7358SJed Brown   PetscFunctionReturn(0);
353552f7358SJed Brown }
354552f7358SJed Brown 
355552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
356552f7358SJed Brown {
357552f7358SJed Brown   DM             dm;
358684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
359552f7358SJed Brown   PetscErrorCode ierr;
360552f7358SJed Brown 
361552f7358SJed Brown   PetscFunctionBegin;
362552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
36382f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
364552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
36533c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
366e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3678135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
368684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
369552f7358SJed Brown     Vec         locv;
370798534f6SMatthew G. Knepley     PetscObject isZero;
371552f7358SJed Brown     const char *name;
372552f7358SJed Brown 
373c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
374552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
375552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
376552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
377552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
378798534f6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
379798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
380552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
381798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
382c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
383b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
384b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38539d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
386b136c2c9SMatthew G. Knepley #else
387b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
388b136c2c9SMatthew G. Knepley #endif
389552f7358SJed Brown   } else {
390684b87d9SLisandro Dalcin     PetscBool isseq;
391684b87d9SLisandro Dalcin 
392684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
39355f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
39455f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
395552f7358SJed Brown   }
396552f7358SJed Brown   PetscFunctionReturn(0);
397552f7358SJed Brown }
398552f7358SJed Brown 
399d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
400d930f514SMatthew G. Knepley {
401d930f514SMatthew G. Knepley   DM                dm;
402d930f514SMatthew G. Knepley   MPI_Comm          comm;
403d930f514SMatthew G. Knepley   PetscViewerFormat format;
404d930f514SMatthew G. Knepley   Vec               v;
405d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
406d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
407d930f514SMatthew G. Knepley 
408d930f514SMatthew G. Knepley   PetscFunctionBegin;
409d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
410d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4112c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
412d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
413d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
416a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
417a8ad634aSStefano Zampini     /* this need a better fix */
418a8ad634aSStefano Zampini     if (dm->useNatural) {
419a8ad634aSStefano Zampini       if (dm->sfNatural) {
420d930f514SMatthew G. Knepley         const char *vecname;
421d930f514SMatthew G. Knepley         PetscInt    n, nroots;
422d930f514SMatthew G. Knepley 
423ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
424d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         if (n == nroots) {
426d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
430d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
431d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
432d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
433a8ad634aSStefano Zampini     } else v = originalv;
434a8ad634aSStefano Zampini   } else v = originalv;
435a8ad634aSStefano Zampini 
436d930f514SMatthew G. Knepley   if (ishdf5) {
437d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43839d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
439d930f514SMatthew G. Knepley #else
440d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
441d930f514SMatthew G. Knepley #endif
442d930f514SMatthew G. Knepley   } else if (isvtk) {
443d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
444d930f514SMatthew G. Knepley   } else {
445d930f514SMatthew G. Knepley     PetscBool isseq;
446d930f514SMatthew G. Knepley 
447d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
448d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
449d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
450d930f514SMatthew G. Knepley   }
451a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
452d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
453d930f514SMatthew G. Knepley }
454d930f514SMatthew G. Knepley 
4552c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4562c40f234SMatthew G. Knepley {
4572c40f234SMatthew G. Knepley   DM             dm;
4582c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4592c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4602c40f234SMatthew G. Knepley 
4612c40f234SMatthew G. Knepley   PetscFunctionBegin;
4622c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4632c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4642c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4652c40f234SMatthew G. Knepley   if (ishdf5) {
4662c40f234SMatthew G. Knepley     DM          dmBC;
4672c40f234SMatthew G. Knepley     Vec         gv;
4682c40f234SMatthew G. Knepley     const char *name;
4692c40f234SMatthew G. Knepley 
4702c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4782c40f234SMatthew G. Knepley   } else {
4792c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4802c40f234SMatthew G. Knepley   }
4812c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4822c40f234SMatthew G. Knepley }
4832c40f234SMatthew G. Knepley 
4842c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4852c40f234SMatthew G. Knepley {
4862c40f234SMatthew G. Knepley   DM             dm;
4872c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4882c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4892c40f234SMatthew G. Knepley 
4902c40f234SMatthew G. Knepley   PetscFunctionBegin;
4912c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4922c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4932c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4942c40f234SMatthew G. Knepley   if (ishdf5) {
495878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49639d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
497b136c2c9SMatthew G. Knepley #else
498b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
499878b459fSMatthew G. Knepley #endif
5002c40f234SMatthew G. Knepley   } else {
5012c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
502552f7358SJed Brown   }
503552f7358SJed Brown   PetscFunctionReturn(0);
504552f7358SJed Brown }
505552f7358SJed Brown 
506d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
507d930f514SMatthew G. Knepley {
508d930f514SMatthew G. Knepley   DM                dm;
509d930f514SMatthew G. Knepley   PetscViewerFormat format;
510d930f514SMatthew G. Knepley   PetscBool         ishdf5;
511d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
512d930f514SMatthew G. Knepley 
513d930f514SMatthew G. Knepley   PetscFunctionBegin;
514d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
515d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
516d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
518d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
519a8ad634aSStefano Zampini     if (dm->useNatural) {
520d930f514SMatthew G. Knepley       if (dm->sfNatural) {
521d930f514SMatthew G. Knepley         if (ishdf5) {
522d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
523d930f514SMatthew G. Knepley           Vec         v;
524d930f514SMatthew G. Knepley           const char *vecname;
525d930f514SMatthew G. Knepley 
526d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
527d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
52939d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
530d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
532d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
533d930f514SMatthew G. Knepley #else
534d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
535d930f514SMatthew G. Knepley #endif
536d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
537d930f514SMatthew G. Knepley       }
538a8ad634aSStefano Zampini     } else {
539a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
540a8ad634aSStefano Zampini     }
541d930f514SMatthew G. Knepley   }
542d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
543d930f514SMatthew G. Knepley }
544d930f514SMatthew G. Knepley 
5457cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
546731e8ddeSMatthew G. Knepley {
547731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
548731e8ddeSMatthew G. Knepley   Vec                coordinates;
549ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
550731e8ddeSMatthew G. Knepley   const char        *name[4];
551731e8ddeSMatthew G. Knepley   const PetscScalar *a;
552731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
553731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
554731e8ddeSMatthew G. Knepley 
555731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
556731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
557731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
559731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
560ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
561731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
562731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
564731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
565731e8ddeSMatthew G. Knepley   name[1]     = "edge";
566731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
567731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
568731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
569731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
570ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
571731e8ddeSMatthew G. Knepley 
572ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
573ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
575731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
577731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
578731e8ddeSMatthew G. Knepley 
579731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
580731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
581731e8ddeSMatthew G. Knepley       if (!dof) continue;
582731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
584f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
586731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
587731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
588731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
589087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
590731e8ddeSMatthew G. Knepley         }
591731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
592731e8ddeSMatthew G. Knepley       }
593731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
594731e8ddeSMatthew G. Knepley     }
595731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
596731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
597731e8ddeSMatthew G. Knepley   }
598731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
599731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
600731e8ddeSMatthew G. Knepley }
601731e8ddeSMatthew G. Knepley 
6027cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
603552f7358SJed Brown {
604552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
605552f7358SJed Brown   DM                cdm;
606412e9a14SMatthew G. Knepley   DMLabel           markers, celltypes;
607552f7358SJed Brown   PetscSection      coordSection;
608552f7358SJed Brown   Vec               coordinates;
609552f7358SJed Brown   PetscViewerFormat format;
610552f7358SJed Brown   PetscErrorCode    ierr;
611552f7358SJed Brown 
612552f7358SJed Brown   PetscFunctionBegin;
613552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61492fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
615552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
616552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
617552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
618552f7358SJed Brown     const char *name;
619f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
620552f7358SJed Brown     PetscInt    pStart, pEnd, p;
621552f7358SJed Brown     PetscMPIInt rank, size;
622552f7358SJed Brown 
623ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
624ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
625552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
626552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
627552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
628f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
629f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
630f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
631f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
632f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
633e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6344d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
635e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
636552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
637552f7358SJed Brown       PetscInt dof, off, s;
638552f7358SJed Brown 
639552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
640552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
641552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
642e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
643552f7358SJed Brown       }
644552f7358SJed Brown     }
645552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
646e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
647e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
648552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
649552f7358SJed Brown       PetscInt dof, off, c;
650552f7358SJed Brown 
651552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
652552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
653552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
654e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
655552f7358SJed Brown       }
656552f7358SJed Brown     }
657552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6584d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6593d2e540fSStefano Zampini     if (coordSection && coordinates) {
66080180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6613d2e540fSStefano Zampini     }
662c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6637422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
664412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &celltypes);CHKERRQ(ierr);
665412e9a14SMatthew G. Knepley     if (celltypes) {ierr = DMLabelView(celltypes, viewer);CHKERRQ(ierr);}
666552f7358SJed Brown     if (size > 1) {
667552f7358SJed Brown       PetscSF sf;
668552f7358SJed Brown 
669552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
670552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
671552f7358SJed Brown     }
672552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
673552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6740588280cSMatthew G. Knepley     const char  *name, *color;
6750588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6760588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
677fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
678552f7358SJed Brown     PetscReal    scale         = 2.0;
67978081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6800588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6810588280cSMatthew G. Knepley     double       tcoords[3];
682552f7358SJed Brown     PetscScalar *coords;
6830588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
684552f7358SJed Brown     PetscMPIInt  rank, size;
6850588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
686fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
687fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
688fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
689552f7358SJed Brown 
6900588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
691552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
692c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6930588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6940588280cSMatthew G. Knepley     numColors  = 10;
6950588280cSMatthew G. Knepley     numLColors = 10;
6960588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
697c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
69878081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
699c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
700c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
7010588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
702c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
7030588280cSMatthew G. Knepley     if (!useColors) {
7040588280cSMatthew G. Knepley       numColors = 3;
7050588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
7060588280cSMatthew G. Knepley     }
707c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
7080588280cSMatthew G. Knepley     if (!useColors) {
7090588280cSMatthew G. Knepley       numLColors = 4;
7100588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7110588280cSMatthew G. Knepley     }
712589a23caSBarry Smith     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);CHKERRQ(ierr);
713202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
714202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
715202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
716202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
717fe1cc32dSStefano Zampini 
718fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
719fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
720fe1cc32dSStefano Zampini     if (lflg) {
721fe1cc32dSStefano Zampini       DMLabel lbl;
722fe1cc32dSStefano Zampini 
723fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
724fe1cc32dSStefano Zampini       if (lbl) {
725fe1cc32dSStefano Zampini         PetscInt val, defval;
726fe1cc32dSStefano Zampini 
727fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
728fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
729fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
730fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
731fe1cc32dSStefano Zampini           PetscInt  closureSize;
732fe1cc32dSStefano Zampini 
733fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
734fe1cc32dSStefano Zampini           if (val == defval) continue;
735fe1cc32dSStefano Zampini 
736fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
737fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
738fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
739fe1cc32dSStefano Zampini           }
740fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
741fe1cc32dSStefano Zampini         }
742fe1cc32dSStefano Zampini       }
743fe1cc32dSStefano Zampini     }
744fe1cc32dSStefano Zampini 
745ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
746ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
747552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
748770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7490588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
750552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
751552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
752552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7530588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7540588280cSMatthew G. Knepley     if (size > 1) {
755f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
756770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
757770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
758770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
759770b213bSMatthew G Knepley         } else if (p > 0) {
760770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
761770b213bSMatthew G Knepley         }
762770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
763770b213bSMatthew G Knepley       }
7640588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7650588280cSMatthew G. Knepley     }
766087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
767fe1cc32dSStefano Zampini 
768552f7358SJed Brown     /* Plot vertices */
769552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
770552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7714d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
772552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
773552f7358SJed Brown       PetscInt  off, dof, d;
7740588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
775552f7358SJed Brown 
776fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
777552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
778552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7790588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
780f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7810588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7820588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
783c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7840588280cSMatthew G. Knepley       }
7850588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7860588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
787552f7358SJed Brown       for (d = 0; d < dof; ++d) {
788552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
789087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
790552f7358SJed Brown       }
7910588280cSMatthew G. Knepley       color = colors[rank%numColors];
7920588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7930588280cSMatthew G. Knepley         PetscInt val;
794c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7950588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7960588280cSMatthew G. Knepley       }
7970588280cSMatthew G. Knepley       if (useNumbers) {
798e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7990588280cSMatthew G. Knepley       } else {
800e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
8010588280cSMatthew G. Knepley       }
802552f7358SJed Brown     }
803552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
804552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
805846a3e8bSMatthew G. Knepley     /* Plot cells */
806846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
80778081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
808846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
809846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
810846a3e8bSMatthew G. Knepley         const PetscInt *cone;
811846a3e8bSMatthew G. Knepley 
812fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
813846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
814846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
815846a3e8bSMatthew G. Knepley           PetscInt val;
816846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
817846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
818846a3e8bSMatthew G. Knepley         }
819846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
820846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
821846a3e8bSMatthew G. Knepley       }
822846a3e8bSMatthew G. Knepley     } else {
823846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
824846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
825846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
826846a3e8bSMatthew G. Knepley 
827fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
828846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
829846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
830846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
831846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
832846a3e8bSMatthew G. Knepley 
833846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
834846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
835846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
836846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
837846a3e8bSMatthew G. Knepley         }
838846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
839846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
840846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
841846a3e8bSMatthew G. Knepley       }
842846a3e8bSMatthew G. Knepley     }
843846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
844846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
845846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
846846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
847846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
848846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
849846a3e8bSMatthew G. Knepley 
850fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
851846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
852846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
853846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
854846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
855846a3e8bSMatthew G. Knepley         PetscInt       off;
856846a3e8bSMatthew G. Knepley 
857846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
858846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
859846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
860846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
861846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
862846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
863846a3e8bSMatthew G. Knepley         }
864846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
865846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
866846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
867846a3e8bSMatthew G. Knepley         ++n;
868846a3e8bSMatthew G. Knepley       }
869846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
870846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
871846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
872846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
873087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
874846a3e8bSMatthew G. Knepley       }
875846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
876846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
877846a3e8bSMatthew G. Knepley         PetscInt val;
878846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
879846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
880846a3e8bSMatthew G. Knepley       }
881846a3e8bSMatthew G. Knepley       if (useNumbers) {
882846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
883846a3e8bSMatthew G. Knepley       } else {
884846a3e8bSMatthew 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);
885846a3e8bSMatthew G. Knepley       }
886846a3e8bSMatthew G. Knepley     }
887846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
888552f7358SJed Brown     /* Plot edges */
889202fd40aSStefano Zampini     if (plotEdges) {
890552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
891552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
892552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
893552f7358SJed Brown         const PetscInt *cone;
894552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
895552f7358SJed Brown 
896fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
897552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
898f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
899552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
900552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
901552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
902552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
903552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
904552f7358SJed Brown         for (d = 0; d < dof; ++d) {
905202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
906c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
907552f7358SJed Brown         }
9080588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
9090588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
9100588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9110588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
912f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9130588280cSMatthew G. Knepley         }
9140588280cSMatthew G. Knepley         color = colors[rank%numColors];
9150588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9160588280cSMatthew G. Knepley           PetscInt val;
917c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9180750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9190588280cSMatthew G. Knepley         }
920e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
921552f7358SJed Brown       }
922552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
923552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
924552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9250588280cSMatthew G. Knepley     }
926552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9274d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9280588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
929770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9300588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9310588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9320588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9330588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
934fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9350f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9360f7d6e4aSStefano Zampini     Vec                    cown,acown;
9370f7d6e4aSStefano Zampini     VecScatter             sct;
9380f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9390f7d6e4aSStefano Zampini     IS                     gid,acis;
9400f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9410f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9420f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9430f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9440f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9450f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9460f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9470f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
9480f7d6e4aSStefano Zampini 
9490f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
950ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
951b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
952ffc4695bSBarry Smith     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRMPI(ierr);
9530f7d6e4aSStefano Zampini #endif
9540f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
955ffc4695bSBarry Smith       ierr = MPI_Comm_group(comm,&ggroup);CHKERRMPI(ierr);
956ffc4695bSBarry Smith       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRMPI(ierr);
9570f7d6e4aSStefano Zampini       d1   = 0;
958ffc4695bSBarry Smith       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRMPI(ierr);
9590f7d6e4aSStefano Zampini       nid  = d2;
960ffc4695bSBarry Smith       ierr = MPI_Group_free(&ggroup);CHKERRMPI(ierr);
961ffc4695bSBarry Smith       ierr = MPI_Group_free(&ngroup);CHKERRMPI(ierr);
962ffc4695bSBarry Smith       ierr = MPI_Comm_free(&ncomm);CHKERRMPI(ierr);
9630f7d6e4aSStefano Zampini     } else nid = 0.0;
9640f7d6e4aSStefano Zampini 
9650f7d6e4aSStefano Zampini     /* Get connectivity */
9660f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
9670f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
9680f7d6e4aSStefano Zampini 
9690f7d6e4aSStefano Zampini     /* filter overlapped local cells */
9700f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
9710f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
9720f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
9730f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
9740f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
9750f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
9760f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
9770f7d6e4aSStefano Zampini     }
9780f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
9790f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
9800f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9810f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
9820f7d6e4aSStefano Zampini 
9830f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
9840f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
9850f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
9860f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
9870f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
9880f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
9890f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
9900f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
9910f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9920f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9930f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
9940f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
9950f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
9960f7d6e4aSStefano Zampini 
9970f7d6e4aSStefano Zampini     /* compute edgeCut */
9980f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
9990f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
10000f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
10010f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
10020f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
10030f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
10040f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
10050f7d6e4aSStefano Zampini       PetscInt totl;
10060f7d6e4aSStefano Zampini 
10070f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
10080f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
10090f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
10100f7d6e4aSStefano Zampini         if (work[i] < 0) {
10110f7d6e4aSStefano Zampini           ect  += 1;
10120f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10130f7d6e4aSStefano Zampini         }
10140f7d6e4aSStefano Zampini       }
10150f7d6e4aSStefano Zampini     }
10160f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10170f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10180f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10190f7d6e4aSStefano Zampini     lm[1] = -numVertices;
10200f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRQ(ierr);
10210f7d6e4aSStefano 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);
10220f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10230f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10240f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
10250f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRQ(ierr);
10260f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1027b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
10280f7d6e4aSStefano 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);
10290f7d6e4aSStefano Zampini #else
10300f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10310f7d6e4aSStefano Zampini #endif
10320f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10330f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10340f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10350f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1036552f7358SJed Brown   } else {
1037412e9a14SMatthew G. Knepley     const char    *name;
1038d80ece95SMatthew G. Knepley     PetscInt      *sizes, *hybsizes, *ghostsizes;
1039412e9a14SMatthew G. Knepley     PetscInt       locDepth, depth, cellHeight, dim, d;
1040d80ece95SMatthew G. Knepley     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1041a57dd577SMatthew G Knepley     PetscInt       numLabels, l;
1042412e9a14SMatthew G. Knepley     DMPolytopeType ct0;
1043412e9a14SMatthew G. Knepley     MPI_Comm       comm;
1044412e9a14SMatthew G. Knepley     PetscMPIInt    size, rank;
1045552f7358SJed Brown 
104682f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
1047ffc4695bSBarry Smith     ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
1048ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
1049c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1050f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10515f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1052f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1053f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1054f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1055552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1056b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
1057d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1058d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1059d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1060412e9a14SMatthew G. Knepley     for (d = 0; d <= depth; d++) {
1061412e9a14SMatthew G. Knepley       PetscInt Nc[2] = {0, 0}, ict;
1062412e9a14SMatthew G. Knepley 
1063552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1064412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, pStart, &ct0);CHKERRQ(ierr);
1065412e9a14SMatthew G. Knepley       ict  = ct0;
1066ffc4695bSBarry Smith       ierr = MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1067412e9a14SMatthew G. Knepley       ct0  = (DMPolytopeType) ict;
1068412e9a14SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1069412e9a14SMatthew G. Knepley         DMPolytopeType ct;
1070412e9a14SMatthew G. Knepley 
1071412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
1072412e9a14SMatthew G. Knepley         if (ct == ct0) ++Nc[0];
1073412e9a14SMatthew G. Knepley         else           ++Nc[1];
1074412e9a14SMatthew G. Knepley       }
1075ffc4695bSBarry Smith       ierr = MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1076ffc4695bSBarry Smith       ierr = MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1077ffc4695bSBarry Smith       if (d == depth) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);}
1078412e9a14SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1079834065abSMatthew G. Knepley       for (p = 0; p < size; ++p) {
1080cbb7f117SMark Adams         if (!rank) {
1081412e9a14SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);CHKERRQ(ierr);
1082412e9a14SMatthew G. Knepley           if (hybsizes[p]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);CHKERRQ(ierr);}
1083412e9a14SMatthew G. Knepley           if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1084834065abSMatthew G. Knepley         }
1085cbb7f117SMark Adams       }
1086552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1087552f7358SJed Brown     }
1088d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
1089c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1090a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1091a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1092a57dd577SMatthew G Knepley       DMLabel         label;
1093a57dd577SMatthew G Knepley       const char     *name;
1094a57dd577SMatthew G Knepley       IS              valueIS;
1095a57dd577SMatthew G Knepley       const PetscInt *values;
1096a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1097a57dd577SMatthew G Knepley 
1098c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1099c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1100a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1101d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1102a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1103a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1104120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1105a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1106a57dd577SMatthew G Knepley         PetscInt size;
1107a57dd577SMatthew G Knepley 
1108a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1109a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1110d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1111a57dd577SMatthew G Knepley       }
1112a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1113120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1114a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11154d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1116a57dd577SMatthew G Knepley     }
111734aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
111834aa8a36SMatthew G. Knepley     if (dm->Nf) {
111934aa8a36SMatthew G. Knepley       PetscInt f;
112034aa8a36SMatthew G. Knepley 
112134aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
112234aa8a36SMatthew G. Knepley         const char *name;
112334aa8a36SMatthew G. Knepley 
112434aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
112534aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
112634aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
112734aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
112834aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
112934aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1130ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
113134aa8a36SMatthew G. Knepley         } else {
113234aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
113334aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
113434aa8a36SMatthew G. Knepley         }
113534aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
113634aa8a36SMatthew G. Knepley       }
113734aa8a36SMatthew G. Knepley     }
1138a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11398e7ff633SMatthew G. Knepley     if (cdm) {
11408e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11418e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11428e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11438e7ff633SMatthew G. Knepley     }
1144552f7358SJed Brown   }
1145552f7358SJed Brown   PetscFunctionReturn(0);
1146552f7358SJed Brown }
1147552f7358SJed Brown 
1148e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1149e5c487bfSMatthew G. Knepley {
1150e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1151e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1152e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1153e5c487bfSMatthew G. Knepley 
1154e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1155ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1156e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1157e5c487bfSMatthew G. Knepley   switch (ct) {
1158e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1159e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1160e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1161e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1162e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1163e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1164e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1165e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1166e5c487bfSMatthew G. Knepley     break;
1167e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
1168e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1169e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1170e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1171e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1172e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1173e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1174e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1175e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1176e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1177e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1178e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1179e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1180e5c487bfSMatthew G. Knepley     break;
1181e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1182e5c487bfSMatthew G. Knepley   }
1183e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1184e5c487bfSMatthew G. Knepley }
1185e5c487bfSMatthew G. Knepley 
1186e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1187e5c487bfSMatthew G. Knepley {
1188e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1189e5c487bfSMatthew G. Knepley   PetscReal      centroid[2] = {0., 0.};
1190e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1191e5c487bfSMatthew G. Knepley   PetscInt       fillColor, v, e, d;
1192e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1193e5c487bfSMatthew G. Knepley 
1194e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1195ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1196e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1197e5c487bfSMatthew G. Knepley   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1198e5c487bfSMatthew G. Knepley   switch (ct) {
1199e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1200e5c487bfSMatthew G. Knepley     {
1201e5c487bfSMatthew G. Knepley       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
1202e5c487bfSMatthew G. Knepley 
1203e5c487bfSMatthew G. Knepley       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1204e5c487bfSMatthew G. Knepley       for (e = 0; e < 3; ++e) {
1205e5c487bfSMatthew G. Knepley         refCoords[0] = refVertices[e*2+0];
1206e5c487bfSMatthew G. Knepley         refCoords[1] = refVertices[e*2+1];
1207e5c487bfSMatthew G. Knepley         for (d = 1; d <= edgeDiv; ++d) {
1208e5c487bfSMatthew G. Knepley           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1209e5c487bfSMatthew G. Knepley           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1210e5c487bfSMatthew G. Knepley         }
1211e5c487bfSMatthew G. Knepley         ierr = DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);CHKERRQ(ierr);
1212e5c487bfSMatthew G. Knepley         for (d = 0; d < edgeDiv; ++d) {
1213e5c487bfSMatthew 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);
1214e5c487bfSMatthew 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);
1215e5c487bfSMatthew G. Knepley         }
1216e5c487bfSMatthew G. Knepley       }
1217e5c487bfSMatthew G. Knepley     }
1218e5c487bfSMatthew G. Knepley     break;
1219e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1220e5c487bfSMatthew G. Knepley   }
1221e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1222e5c487bfSMatthew G. Knepley }
1223e5c487bfSMatthew G. Knepley 
12247cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1225e412dcbdSMatthew G. Knepley {
1226e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1227e412dcbdSMatthew G. Knepley   DM                 cdm;
1228e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1229e412dcbdSMatthew G. Knepley   Vec                coordinates;
1230e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
123129494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1232e5c487bfSMatthew G. Knepley   PetscReal         *refCoords, *edgeCoords;
1233e5c487bfSMatthew G. Knepley   PetscBool          isnull, drawAffine = PETSC_TRUE;
1234e5c487bfSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1235e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1236e412dcbdSMatthew G. Knepley 
1237e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1238e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1239e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1240e5c487bfSMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);CHKERRQ(ierr);
1241e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);CHKERRQ(ierr);}
1242e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
124392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1244e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1245e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1246e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1247e412dcbdSMatthew G. Knepley 
1248e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1249e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1250e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1251e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1252e412dcbdSMatthew G. Knepley 
1253e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1254e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1255e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
12560c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
12570c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1258e412dcbdSMatthew G. Knepley   }
1259e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
126029494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
126129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
126229494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1263e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1264e412dcbdSMatthew G. Knepley 
1265cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1266cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1267ba2698f1SMatthew G. Knepley     PetscInt     numCoords;
1268cf3064d3SMatthew G. Knepley 
1269e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);CHKERRQ(ierr);
1270e5c487bfSMatthew G. Knepley     if (drawAffine) {
1271e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCell(dm, draw, c, coords);CHKERRQ(ierr);
1272e5c487bfSMatthew G. Knepley     } else {
1273e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);CHKERRQ(ierr);
1274cf3064d3SMatthew G. Knepley     }
1275cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1276cf3064d3SMatthew G. Knepley   }
1277e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscFree2(refCoords, edgeCoords);CHKERRQ(ierr);}
1278e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1279e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1280e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1281e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1282e412dcbdSMatthew G. Knepley }
1283e412dcbdSMatthew G. Knepley 
12841e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
12851e50132fSMatthew G. Knepley #include <exodusII.h>
12861e50132fSMatthew G. Knepley #endif
12871e50132fSMatthew G. Knepley 
1288552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1289552f7358SJed Brown {
12901e50132fSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1291002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1292552f7358SJed Brown   PetscErrorCode ierr;
1293552f7358SJed Brown 
1294552f7358SJed Brown   PetscFunctionBegin;
1295552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1296552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1297552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);CHKERRQ(ierr);
1298fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
1299c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
1300e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
13018135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
13021e50132fSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);CHKERRQ(ierr);
1303552f7358SJed Brown   if (iascii) {
13048135c375SStefano Zampini     PetscViewerFormat format;
13058135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13068135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
13078135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13088135c375SStefano Zampini     } else {
1309552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
13108135c375SStefano Zampini     }
1311c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1312c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
131339d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1314c6ccd67eSMatthew G. Knepley #else
1315c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1316552f7358SJed Brown #endif
1317e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1318fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1319e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1320e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
13218135c375SStefano Zampini   } else if (isglvis) {
13228135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13231e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
13241e50132fSMatthew G. Knepley   } else if (isexodus) {
13251e50132fSMatthew G. Knepley     int exoid;
13261e50132fSMatthew G. Knepley     PetscInt cStart, cEnd, c;
13271e50132fSMatthew G. Knepley 
13281e50132fSMatthew G. Knepley     ierr = DMCreateLabel(dm, "Cell Sets");CHKERRQ(ierr);
13291e50132fSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
13301e50132fSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {ierr = DMSetLabelValue(dm, "Cell Sets", c, 1);CHKERRQ(ierr);}
13311e50132fSMatthew G. Knepley     ierr = PetscViewerExodusIIGetId(viewer, &exoid);CHKERRQ(ierr);
13321e50132fSMatthew G. Knepley     ierr = DMPlexView_ExodusII_Internal(dm, exoid, 1);CHKERRQ(ierr);
13331e50132fSMatthew G. Knepley #endif
133462201deeSVaclav Hapla   } else {
1335a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1336fcf6c8fdSToby Isaac   }
1337cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1338cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1339cb3ba0daSMatthew G. Knepley   if (flg) {
1340cb3ba0daSMatthew G. Knepley     Vec ranks;
1341cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1342cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1343cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1344cb3ba0daSMatthew G. Knepley   }
1345002a2709SMatthew G. Knepley   /* Optionally view a label */
1346589a23caSBarry Smith   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);CHKERRQ(ierr);
1347002a2709SMatthew G. Knepley   if (flg) {
1348002a2709SMatthew G. Knepley     DMLabel label;
1349002a2709SMatthew G. Knepley     Vec     val;
1350002a2709SMatthew G. Knepley 
1351002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1352002a2709SMatthew 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);
1353002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1354002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1355002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1356002a2709SMatthew G. Knepley   }
1357552f7358SJed Brown   PetscFunctionReturn(0);
1358552f7358SJed Brown }
1359552f7358SJed Brown 
13602c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13612c40f234SMatthew G. Knepley {
1362d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13632c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13642c40f234SMatthew G. Knepley 
13652c40f234SMatthew G. Knepley   PetscFunctionBegin;
13662c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13672c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13682c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1369d4f5a9a0SVaclav Hapla   if (ishdf5) {
13702c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13719c48423bSVaclav Hapla     PetscViewerFormat format;
13729c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13739c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13749c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1375509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
137639d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1377509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1378b458e8f1SJose E. Roman     PetscFunctionReturn(0);
13792c40f234SMatthew G. Knepley #else
13802c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1381552f7358SJed Brown #endif
1382b458e8f1SJose E. Roman   } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1383552f7358SJed Brown }
1384552f7358SJed Brown 
1385552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1386552f7358SJed Brown {
1387552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1388552f7358SJed Brown   PetscErrorCode ierr;
1389552f7358SJed Brown 
1390552f7358SJed Brown   PetscFunctionBegin;
13918135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13928135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
139328d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
13941eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);CHKERRQ(ierr);
13950d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1396552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1397552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1398552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1399552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1400be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1401552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1402552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1403d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1404d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
140577623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1406a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
140797d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
1408552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1409552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1410a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1411a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1412d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1413d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1414d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1415d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1416d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1417d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1418fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
14190a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
1420552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1421552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1422552f7358SJed Brown   PetscFunctionReturn(0);
1423552f7358SJed Brown }
1424552f7358SJed Brown 
1425b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1426552f7358SJed Brown {
14278d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1428acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1429552f7358SJed Brown   PetscInt               localSize;
1430837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1431552f7358SJed Brown   PetscErrorCode         ierr;
1432b412c318SBarry Smith   MatType                mtype;
14331428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1434552f7358SJed Brown 
1435552f7358SJed Brown   PetscFunctionBegin;
1436607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1437b412c318SBarry Smith   mtype = dm->mattype;
1438e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1439552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1440552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
144182f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1442552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1443552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1444552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1445acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1446acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1447552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1448552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1449552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1450552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1451552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1452552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1453552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1454837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1455552f7358SJed Brown   if (!isShell) {
1456be36d101SStefano Zampini     PetscSection subSection;
1457837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
14580be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1459fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1460552f7358SJed Brown 
146100140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1462be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1463be36d101SStefano Zampini       PetscSection section;
1464be36d101SStefano Zampini       PetscInt     size;
1465be36d101SStefano Zampini 
146692fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1467be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1468be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1469be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1470be36d101SStefano Zampini     } else {
147100140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1472be36d101SStefano Zampini     }
1473552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1474be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1475a9d99c84SMatthew G. Knepley       PetscInt bdof;
1476a9d99c84SMatthew G. Knepley 
1477552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1478fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
14791d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
14801d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
14811d17a0a3SMatthew G. Knepley       if (dof) {
14821d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1483be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1484be36d101SStefano Zampini       }
1485be36d101SStefano Zampini       if (isMatIS) {
1486be36d101SStefano Zampini         PetscInt loff,c,off;
1487be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1488be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1489be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1490552f7358SJed Brown       }
14912a28c762SMatthew G Knepley     }
14922a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14930be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14940be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14950be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14960be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14976fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
14986fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
14994fa26246SMatthew G. Knepley       PetscInt l;
15006fd5c86aSStefano Zampini 
15016fd5c86aSStefano Zampini       lsize = lsize/bs;
15026fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
15034fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1504be36d101SStefano Zampini     }
1505be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1506be36d101SStefano Zampini     if (isMatIS) {
1507be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1508be36d101SStefano Zampini     }
15091795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
15108d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1511552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1512552f7358SJed Brown   }
1513b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1514552f7358SJed Brown   PetscFunctionReturn(0);
1515552f7358SJed Brown }
1516552f7358SJed Brown 
15177cd05799SMatthew G. Knepley /*@
1518a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1519be36d101SStefano Zampini 
1520be36d101SStefano Zampini   Not collective
1521be36d101SStefano Zampini 
1522be36d101SStefano Zampini   Input Parameter:
1523be36d101SStefano Zampini . mesh - The DMPlex
1524be36d101SStefano Zampini 
1525be36d101SStefano Zampini   Output Parameters:
1526be36d101SStefano Zampini . subsection - The subdomain section
1527be36d101SStefano Zampini 
1528be36d101SStefano Zampini   Level: developer
1529be36d101SStefano Zampini 
1530be36d101SStefano Zampini .seealso:
15317cd05799SMatthew G. Knepley @*/
1532be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1533be36d101SStefano Zampini {
1534be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1535be36d101SStefano Zampini   PetscErrorCode ierr;
1536be36d101SStefano Zampini 
1537be36d101SStefano Zampini   PetscFunctionBegin;
1538be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1539be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1540be36d101SStefano Zampini     PetscSection section;
1541be36d101SStefano Zampini     PetscSF      sf;
1542be36d101SStefano Zampini 
1543be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
154492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1545be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1546be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1547be36d101SStefano Zampini   }
1548be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1549be36d101SStefano Zampini   PetscFunctionReturn(0);
1550be36d101SStefano Zampini }
1551be36d101SStefano Zampini 
1552552f7358SJed Brown /*@
1553552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1554552f7358SJed Brown 
1555552f7358SJed Brown   Not collective
1556552f7358SJed Brown 
1557552f7358SJed Brown   Input Parameter:
1558552f7358SJed Brown . mesh - The DMPlex
1559552f7358SJed Brown 
1560552f7358SJed Brown   Output Parameters:
1561552f7358SJed Brown + pStart - The first mesh point
1562552f7358SJed Brown - pEnd   - The upper bound for mesh points
1563552f7358SJed Brown 
1564552f7358SJed Brown   Level: beginner
1565552f7358SJed Brown 
1566552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1567552f7358SJed Brown @*/
1568552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1569552f7358SJed Brown {
1570552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1571552f7358SJed Brown   PetscErrorCode ierr;
1572552f7358SJed Brown 
1573552f7358SJed Brown   PetscFunctionBegin;
1574552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1575552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1576552f7358SJed Brown   PetscFunctionReturn(0);
1577552f7358SJed Brown }
1578552f7358SJed Brown 
1579552f7358SJed Brown /*@
1580552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1581552f7358SJed Brown 
1582552f7358SJed Brown   Not collective
1583552f7358SJed Brown 
1584552f7358SJed Brown   Input Parameters:
1585552f7358SJed Brown + mesh - The DMPlex
1586552f7358SJed Brown . pStart - The first mesh point
1587552f7358SJed Brown - pEnd   - The upper bound for mesh points
1588552f7358SJed Brown 
1589552f7358SJed Brown   Output Parameters:
1590552f7358SJed Brown 
1591552f7358SJed Brown   Level: beginner
1592552f7358SJed Brown 
1593552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1594552f7358SJed Brown @*/
1595552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1596552f7358SJed Brown {
1597552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1598552f7358SJed Brown   PetscErrorCode ierr;
1599552f7358SJed Brown 
1600552f7358SJed Brown   PetscFunctionBegin;
1601552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1602552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1603552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1604552f7358SJed Brown   PetscFunctionReturn(0);
1605552f7358SJed Brown }
1606552f7358SJed Brown 
1607552f7358SJed Brown /*@
1608eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1609552f7358SJed Brown 
1610552f7358SJed Brown   Not collective
1611552f7358SJed Brown 
1612552f7358SJed Brown   Input Parameters:
1613552f7358SJed Brown + mesh - The DMPlex
1614eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1615552f7358SJed Brown 
1616552f7358SJed Brown   Output Parameter:
1617552f7358SJed Brown . size - The cone size for point p
1618552f7358SJed Brown 
1619552f7358SJed Brown   Level: beginner
1620552f7358SJed Brown 
1621552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1622552f7358SJed Brown @*/
1623552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1624552f7358SJed Brown {
1625552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1626552f7358SJed Brown   PetscErrorCode ierr;
1627552f7358SJed Brown 
1628552f7358SJed Brown   PetscFunctionBegin;
1629552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1630552f7358SJed Brown   PetscValidPointer(size, 3);
1631552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1632552f7358SJed Brown   PetscFunctionReturn(0);
1633552f7358SJed Brown }
1634552f7358SJed Brown 
1635552f7358SJed Brown /*@
1636eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1637552f7358SJed Brown 
1638552f7358SJed Brown   Not collective
1639552f7358SJed Brown 
1640552f7358SJed Brown   Input Parameters:
1641552f7358SJed Brown + mesh - The DMPlex
1642eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1643552f7358SJed Brown - size - The cone size for point p
1644552f7358SJed Brown 
1645552f7358SJed Brown   Output Parameter:
1646552f7358SJed Brown 
1647552f7358SJed Brown   Note:
1648552f7358SJed Brown   This should be called after DMPlexSetChart().
1649552f7358SJed Brown 
1650552f7358SJed Brown   Level: beginner
1651552f7358SJed Brown 
1652552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1653552f7358SJed Brown @*/
1654552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1655552f7358SJed Brown {
1656552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1657552f7358SJed Brown   PetscErrorCode ierr;
1658552f7358SJed Brown 
1659552f7358SJed Brown   PetscFunctionBegin;
1660552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1661552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
16620d644c17SKarl Rupp 
1663552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1664552f7358SJed Brown   PetscFunctionReturn(0);
1665552f7358SJed Brown }
1666552f7358SJed Brown 
1667f5a469b9SMatthew G. Knepley /*@
1668eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1669f5a469b9SMatthew G. Knepley 
1670f5a469b9SMatthew G. Knepley   Not collective
1671f5a469b9SMatthew G. Knepley 
1672f5a469b9SMatthew G. Knepley   Input Parameters:
1673f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1674eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1675f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1676f5a469b9SMatthew G. Knepley 
1677f5a469b9SMatthew G. Knepley   Output Parameter:
1678f5a469b9SMatthew G. Knepley 
1679f5a469b9SMatthew G. Knepley   Note:
1680f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1681f5a469b9SMatthew G. Knepley 
1682f5a469b9SMatthew G. Knepley   Level: beginner
1683f5a469b9SMatthew G. Knepley 
1684f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1685f5a469b9SMatthew G. Knepley @*/
1686f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1687f5a469b9SMatthew G. Knepley {
1688f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1689f5a469b9SMatthew G. Knepley   PetscInt       csize;
1690f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1691f5a469b9SMatthew G. Knepley 
1692f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1693f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1694f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1695f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1696f5a469b9SMatthew G. Knepley 
1697f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1698f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1699f5a469b9SMatthew G. Knepley }
1700f5a469b9SMatthew G. Knepley 
1701552f7358SJed Brown /*@C
1702eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1703552f7358SJed Brown 
1704552f7358SJed Brown   Not collective
1705552f7358SJed Brown 
1706552f7358SJed Brown   Input Parameters:
1707833c876bSVaclav Hapla + dm - The DMPlex
1708eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1709552f7358SJed Brown 
1710552f7358SJed Brown   Output Parameter:
1711552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1712552f7358SJed Brown 
1713552f7358SJed Brown   Level: beginner
1714552f7358SJed Brown 
17153813dfbdSMatthew G Knepley   Fortran Notes:
17163813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17173813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1718922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1719922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
17203813dfbdSMatthew G Knepley 
1721e45f02b0SMatthew G. Knepley .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1722552f7358SJed Brown @*/
1723552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1724552f7358SJed Brown {
1725552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1726552f7358SJed Brown   PetscInt       off;
1727552f7358SJed Brown   PetscErrorCode ierr;
1728552f7358SJed Brown 
1729552f7358SJed Brown   PetscFunctionBegin;
1730552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1731552f7358SJed Brown   PetscValidPointer(cone, 3);
1732552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1733552f7358SJed Brown   *cone = &mesh->cones[off];
1734552f7358SJed Brown   PetscFunctionReturn(0);
1735552f7358SJed Brown }
1736552f7358SJed Brown 
17370ce7577fSVaclav Hapla /*@C
17380ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
17390ce7577fSVaclav Hapla 
17400ce7577fSVaclav Hapla   Not collective
17410ce7577fSVaclav Hapla 
17420ce7577fSVaclav Hapla   Input Parameters:
17430ce7577fSVaclav Hapla + dm - The DMPlex
17440ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
17450ce7577fSVaclav Hapla 
17460ce7577fSVaclav Hapla   Output Parameter:
17470ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
17480ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
17490ce7577fSVaclav Hapla 
17500ce7577fSVaclav Hapla   Level: intermediate
17510ce7577fSVaclav Hapla 
1752d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
17530ce7577fSVaclav Hapla @*/
17540ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
17550ce7577fSVaclav Hapla {
17560ce7577fSVaclav Hapla   PetscSection        cs, newcs;
17570ce7577fSVaclav Hapla   PetscInt            *cones;
17580ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
17590ce7577fSVaclav Hapla   PetscInt            n;
17600ce7577fSVaclav Hapla   PetscErrorCode      ierr;
17610ce7577fSVaclav Hapla 
17620ce7577fSVaclav Hapla   PetscFunctionBegin;
17630ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
17640ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
17650ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
17660ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
17670ce7577fSVaclav Hapla   if (pCones) {
17680ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
17690ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
17700ce7577fSVaclav Hapla   }
17710ce7577fSVaclav Hapla   PetscFunctionReturn(0);
17720ce7577fSVaclav Hapla }
17730ce7577fSVaclav Hapla 
1774af9eab45SVaclav Hapla /*@
1775af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1776d4636a37SVaclav Hapla 
1777d4636a37SVaclav Hapla   Not collective
1778d4636a37SVaclav Hapla 
1779d4636a37SVaclav Hapla   Input Parameters:
1780d4636a37SVaclav Hapla + dm - The DMPlex
1781af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1782d4636a37SVaclav Hapla 
1783d4636a37SVaclav Hapla   Output Parameter:
1784af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1785d4636a37SVaclav Hapla 
1786d4636a37SVaclav Hapla   Level: advanced
1787d4636a37SVaclav Hapla 
1788af9eab45SVaclav Hapla   Notes:
1789af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1790af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1791af9eab45SVaclav Hapla 
1792af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1793d4636a37SVaclav Hapla @*/
1794af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1795d4636a37SVaclav Hapla {
1796af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1797af9eab45SVaclav Hapla   PetscInt            depth;
1798d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1799d4636a37SVaclav Hapla 
1800d4636a37SVaclav Hapla   PetscFunctionBegin;
1801af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1802af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1803af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1804af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1805af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1806af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1807af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1808af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1809af9eab45SVaclav Hapla }
1810af9eab45SVaclav Hapla 
1811af9eab45SVaclav Hapla /*@
1812af9eab45SVaclav 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).
1813af9eab45SVaclav Hapla 
1814af9eab45SVaclav Hapla   Not collective
1815af9eab45SVaclav Hapla 
1816af9eab45SVaclav Hapla   Input Parameters:
1817af9eab45SVaclav Hapla + dm - The DMPlex
1818af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1819af9eab45SVaclav Hapla 
1820af9eab45SVaclav Hapla   Output Parameter:
1821af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1822af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1823af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1824af9eab45SVaclav Hapla 
1825af9eab45SVaclav Hapla   Level: advanced
1826af9eab45SVaclav Hapla 
1827af9eab45SVaclav Hapla   Notes:
1828af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1829af9eab45SVaclav Hapla 
1830af9eab45SVaclav 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.
1831af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1832af9eab45SVaclav Hapla 
1833af9eab45SVaclav 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:
1834af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1835af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1836af9eab45SVaclav Hapla 
1837af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1838af9eab45SVaclav Hapla @*/
1839af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1840af9eab45SVaclav Hapla {
1841af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1842af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1843af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1844af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1845af9eab45SVaclav Hapla   PetscSection        *sections_;
1846af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1847af9eab45SVaclav Hapla 
1848af9eab45SVaclav Hapla   PetscFunctionBegin;
1849af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1850af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1851af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1852af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1853af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1854af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1855af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1856af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1857af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1858af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1859af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1860af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1861af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1862af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1863af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1864af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
1865af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
1866af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
1867af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
1868af9eab45SVaclav Hapla       } else {
1869af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
1870af9eab45SVaclav Hapla       }
1871af9eab45SVaclav Hapla     }
1872af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
1873af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
1874af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
1875af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1876af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
1877af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
1878af9eab45SVaclav Hapla       if (cn > 1) {
1879af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
1880af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
1881af9eab45SVaclav Hapla       } else {
1882af9eab45SVaclav Hapla         newarr[co] = arr[i];
1883af9eab45SVaclav Hapla       }
1884af9eab45SVaclav Hapla     }
1885af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
1886af9eab45SVaclav Hapla     arr = newarr;
1887af9eab45SVaclav Hapla     n = newn;
1888af9eab45SVaclav Hapla   }
1889ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
1890af9eab45SVaclav Hapla   *depth = depth_;
1891af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
1892af9eab45SVaclav Hapla   else {
1893af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
1894af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
1895af9eab45SVaclav Hapla   }
1896af9eab45SVaclav Hapla   if (sections) *sections = sections_;
1897af9eab45SVaclav Hapla   else {
1898af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
1899af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
1900af9eab45SVaclav Hapla   }
1901af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1902af9eab45SVaclav Hapla }
1903af9eab45SVaclav Hapla 
1904af9eab45SVaclav Hapla /*@
1905af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
1906af9eab45SVaclav Hapla 
1907af9eab45SVaclav Hapla   Not collective
1908af9eab45SVaclav Hapla 
1909af9eab45SVaclav Hapla   Input Parameters:
1910af9eab45SVaclav Hapla + dm - The DMPlex
1911af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1912af9eab45SVaclav Hapla 
1913af9eab45SVaclav Hapla   Output Parameter:
1914af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1915af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
1916af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1917af9eab45SVaclav Hapla 
1918af9eab45SVaclav Hapla   Level: advanced
1919af9eab45SVaclav Hapla 
1920af9eab45SVaclav Hapla   Notes:
1921af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
1922af9eab45SVaclav Hapla 
1923af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1924af9eab45SVaclav Hapla @*/
1925af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1926af9eab45SVaclav Hapla {
1927af9eab45SVaclav Hapla   PetscInt            d, depth_;
1928af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1929af9eab45SVaclav Hapla 
1930af9eab45SVaclav Hapla   PetscFunctionBegin;
1931af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1932af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1933af9eab45SVaclav Hapla   if (depth) *depth = 0;
1934af9eab45SVaclav Hapla   if (expandedPoints) {
1935af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
1936af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
1937af9eab45SVaclav Hapla   }
1938af9eab45SVaclav Hapla   if (sections)  {
1939af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
1940af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
1941af9eab45SVaclav Hapla   }
1942d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1943d4636a37SVaclav Hapla }
1944d4636a37SVaclav Hapla 
1945552f7358SJed Brown /*@
194692371b87SBarry 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
1947552f7358SJed Brown 
1948552f7358SJed Brown   Not collective
1949552f7358SJed Brown 
1950552f7358SJed Brown   Input Parameters:
1951552f7358SJed Brown + mesh - The DMPlex
1952eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1953552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1954552f7358SJed Brown 
1955552f7358SJed Brown   Output Parameter:
1956552f7358SJed Brown 
1957552f7358SJed Brown   Note:
1958552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1959552f7358SJed Brown 
196092371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
196192371b87SBarry Smith 
1962552f7358SJed Brown   Level: beginner
1963552f7358SJed Brown 
196492371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1965552f7358SJed Brown @*/
1966552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1967552f7358SJed Brown {
1968552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1969552f7358SJed Brown   PetscInt       pStart, pEnd;
1970552f7358SJed Brown   PetscInt       dof, off, c;
1971552f7358SJed Brown   PetscErrorCode ierr;
1972552f7358SJed Brown 
1973552f7358SJed Brown   PetscFunctionBegin;
1974552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1975552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1976552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1977552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1978552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
197982f516ccSBarry 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);
1980552f7358SJed Brown   for (c = 0; c < dof; ++c) {
198182f516ccSBarry 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);
1982552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1983552f7358SJed Brown   }
1984552f7358SJed Brown   PetscFunctionReturn(0);
1985552f7358SJed Brown }
1986552f7358SJed Brown 
1987552f7358SJed Brown /*@C
1988eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1989552f7358SJed Brown 
1990552f7358SJed Brown   Not collective
1991552f7358SJed Brown 
1992552f7358SJed Brown   Input Parameters:
1993552f7358SJed Brown + mesh - The DMPlex
1994eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1995552f7358SJed Brown 
1996552f7358SJed Brown   Output Parameter:
1997552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1998552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1999552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2000552f7358SJed Brown                     the index of the cone point on which to start.
2001552f7358SJed Brown 
2002552f7358SJed Brown   Level: beginner
2003552f7358SJed Brown 
20043813dfbdSMatthew G Knepley   Fortran Notes:
20053813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20063813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20073b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2008922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
20093813dfbdSMatthew G Knepley 
2010552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2011552f7358SJed Brown @*/
2012552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2013552f7358SJed Brown {
2014552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2015552f7358SJed Brown   PetscInt       off;
2016552f7358SJed Brown   PetscErrorCode ierr;
2017552f7358SJed Brown 
2018552f7358SJed Brown   PetscFunctionBegin;
2019552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
202076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2021552f7358SJed Brown     PetscInt dof;
2022552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2023552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2024552f7358SJed Brown   }
2025552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
20260d644c17SKarl Rupp 
2027552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2028552f7358SJed Brown   PetscFunctionReturn(0);
2029552f7358SJed Brown }
2030552f7358SJed Brown 
2031552f7358SJed Brown /*@
2032eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2033552f7358SJed Brown 
2034552f7358SJed Brown   Not collective
2035552f7358SJed Brown 
2036552f7358SJed Brown   Input Parameters:
2037552f7358SJed Brown + mesh - The DMPlex
2038eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2039552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2040552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2041552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2042552f7358SJed Brown                     the index of the cone point on which to start.
2043552f7358SJed Brown 
2044552f7358SJed Brown   Output Parameter:
2045552f7358SJed Brown 
2046552f7358SJed Brown   Note:
2047552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2048552f7358SJed Brown 
2049552f7358SJed Brown   Level: beginner
2050552f7358SJed Brown 
2051552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2052552f7358SJed Brown @*/
2053552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2054552f7358SJed Brown {
2055552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2056552f7358SJed Brown   PetscInt       pStart, pEnd;
2057552f7358SJed Brown   PetscInt       dof, off, c;
2058552f7358SJed Brown   PetscErrorCode ierr;
2059552f7358SJed Brown 
2060552f7358SJed Brown   PetscFunctionBegin;
2061552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2062552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2063552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2064552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2065552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
206682f516ccSBarry 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);
2067552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2068552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2069552f7358SJed Brown 
2070552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
207182f516ccSBarry 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);
2072552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2073552f7358SJed Brown   }
2074552f7358SJed Brown   PetscFunctionReturn(0);
2075552f7358SJed Brown }
2076552f7358SJed Brown 
20777cd05799SMatthew G. Knepley /*@
2078eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
20797cd05799SMatthew G. Knepley 
20807cd05799SMatthew G. Knepley   Not collective
20817cd05799SMatthew G. Knepley 
20827cd05799SMatthew G. Knepley   Input Parameters:
20837cd05799SMatthew G. Knepley + mesh - The DMPlex
2084eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20857cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20867cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
20877cd05799SMatthew G. Knepley 
20887cd05799SMatthew G. Knepley   Level: beginner
20897cd05799SMatthew G. Knepley 
20907cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20917cd05799SMatthew G. Knepley @*/
2092552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2093552f7358SJed Brown {
2094552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2095552f7358SJed Brown   PetscInt       pStart, pEnd;
2096552f7358SJed Brown   PetscInt       dof, off;
2097552f7358SJed Brown   PetscErrorCode ierr;
2098552f7358SJed Brown 
2099552f7358SJed Brown   PetscFunctionBegin;
2100552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2101552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
210282f516ccSBarry 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);
210382f516ccSBarry 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);
210477c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
210577c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
210677c88f5bSMatthew 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);
2107552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2108552f7358SJed Brown   PetscFunctionReturn(0);
2109552f7358SJed Brown }
2110552f7358SJed Brown 
21117cd05799SMatthew G. Knepley /*@
2112eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
21137cd05799SMatthew G. Knepley 
21147cd05799SMatthew G. Knepley   Not collective
21157cd05799SMatthew G. Knepley 
21167cd05799SMatthew G. Knepley   Input Parameters:
21177cd05799SMatthew G. Knepley + mesh - The DMPlex
2118eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
21197cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
21207cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
21217cd05799SMatthew G. Knepley 
21227cd05799SMatthew G. Knepley   Level: beginner
21237cd05799SMatthew G. Knepley 
21247cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
21257cd05799SMatthew G. Knepley @*/
212677c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
212777c88f5bSMatthew G Knepley {
212877c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
212977c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
213077c88f5bSMatthew G Knepley   PetscInt       dof, off;
213177c88f5bSMatthew G Knepley   PetscErrorCode ierr;
213277c88f5bSMatthew G Knepley 
213377c88f5bSMatthew G Knepley   PetscFunctionBegin;
213477c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
213577c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
213677c88f5bSMatthew 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);
213777c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
213877c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
213977c88f5bSMatthew 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);
214077c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
214177c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
214277c88f5bSMatthew G Knepley }
214377c88f5bSMatthew G Knepley 
2144552f7358SJed Brown /*@
2145eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2146552f7358SJed Brown 
2147552f7358SJed Brown   Not collective
2148552f7358SJed Brown 
2149552f7358SJed Brown   Input Parameters:
2150552f7358SJed Brown + mesh - The DMPlex
2151eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2152552f7358SJed Brown 
2153552f7358SJed Brown   Output Parameter:
2154552f7358SJed Brown . size - The support size for point p
2155552f7358SJed Brown 
2156552f7358SJed Brown   Level: beginner
2157552f7358SJed Brown 
2158552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2159552f7358SJed Brown @*/
2160552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2161552f7358SJed Brown {
2162552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2163552f7358SJed Brown   PetscErrorCode ierr;
2164552f7358SJed Brown 
2165552f7358SJed Brown   PetscFunctionBegin;
2166552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2167552f7358SJed Brown   PetscValidPointer(size, 3);
2168552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2169552f7358SJed Brown   PetscFunctionReturn(0);
2170552f7358SJed Brown }
2171552f7358SJed Brown 
2172552f7358SJed Brown /*@
2173eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2174552f7358SJed Brown 
2175552f7358SJed Brown   Not collective
2176552f7358SJed Brown 
2177552f7358SJed Brown   Input Parameters:
2178552f7358SJed Brown + mesh - The DMPlex
2179eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2180552f7358SJed Brown - size - The support size for point p
2181552f7358SJed Brown 
2182552f7358SJed Brown   Output Parameter:
2183552f7358SJed Brown 
2184552f7358SJed Brown   Note:
2185552f7358SJed Brown   This should be called after DMPlexSetChart().
2186552f7358SJed Brown 
2187552f7358SJed Brown   Level: beginner
2188552f7358SJed Brown 
2189552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2190552f7358SJed Brown @*/
2191552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2192552f7358SJed Brown {
2193552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2194552f7358SJed Brown   PetscErrorCode ierr;
2195552f7358SJed Brown 
2196552f7358SJed Brown   PetscFunctionBegin;
2197552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2198552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
21990d644c17SKarl Rupp 
2200552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2201552f7358SJed Brown   PetscFunctionReturn(0);
2202552f7358SJed Brown }
2203552f7358SJed Brown 
2204552f7358SJed Brown /*@C
2205eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2206552f7358SJed Brown 
2207552f7358SJed Brown   Not collective
2208552f7358SJed Brown 
2209552f7358SJed Brown   Input Parameters:
2210552f7358SJed Brown + mesh - The DMPlex
2211eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2212552f7358SJed Brown 
2213552f7358SJed Brown   Output Parameter:
2214552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2215552f7358SJed Brown 
2216552f7358SJed Brown   Level: beginner
2217552f7358SJed Brown 
22183813dfbdSMatthew G Knepley   Fortran Notes:
22193813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22203813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22213b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2222922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
22233813dfbdSMatthew G Knepley 
2224e45f02b0SMatthew G. Knepley .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
2225552f7358SJed Brown @*/
2226552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2227552f7358SJed Brown {
2228552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2229552f7358SJed Brown   PetscInt       off;
2230552f7358SJed Brown   PetscErrorCode ierr;
2231552f7358SJed Brown 
2232552f7358SJed Brown   PetscFunctionBegin;
2233552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2234552f7358SJed Brown   PetscValidPointer(support, 3);
2235552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2236552f7358SJed Brown   *support = &mesh->supports[off];
2237552f7358SJed Brown   PetscFunctionReturn(0);
2238552f7358SJed Brown }
2239552f7358SJed Brown 
2240552f7358SJed Brown /*@
224192371b87SBarry 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
2242552f7358SJed Brown 
2243552f7358SJed Brown   Not collective
2244552f7358SJed Brown 
2245552f7358SJed Brown   Input Parameters:
2246552f7358SJed Brown + mesh - The DMPlex
2247eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
224892371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2249552f7358SJed Brown 
2250552f7358SJed Brown   Output Parameter:
2251552f7358SJed Brown 
2252552f7358SJed Brown   Note:
2253552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2254552f7358SJed Brown 
2255552f7358SJed Brown   Level: beginner
2256552f7358SJed Brown 
225792371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2258552f7358SJed Brown @*/
2259552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2260552f7358SJed Brown {
2261552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2262552f7358SJed Brown   PetscInt       pStart, pEnd;
2263552f7358SJed Brown   PetscInt       dof, off, c;
2264552f7358SJed Brown   PetscErrorCode ierr;
2265552f7358SJed Brown 
2266552f7358SJed Brown   PetscFunctionBegin;
2267552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2268552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2269552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2270552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2271552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
227282f516ccSBarry 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);
2273552f7358SJed Brown   for (c = 0; c < dof; ++c) {
227482f516ccSBarry 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);
2275552f7358SJed Brown     mesh->supports[off+c] = support[c];
2276552f7358SJed Brown   }
2277552f7358SJed Brown   PetscFunctionReturn(0);
2278552f7358SJed Brown }
2279552f7358SJed Brown 
22807cd05799SMatthew G. Knepley /*@
2281eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
22827cd05799SMatthew G. Knepley 
22837cd05799SMatthew G. Knepley   Not collective
22847cd05799SMatthew G. Knepley 
22857cd05799SMatthew G. Knepley   Input Parameters:
22867cd05799SMatthew G. Knepley + mesh - The DMPlex
2287eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22887cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
22897cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
22907cd05799SMatthew G. Knepley 
22917cd05799SMatthew G. Knepley   Level: beginner
22927cd05799SMatthew G. Knepley 
22937cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22947cd05799SMatthew G. Knepley @*/
2295552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2296552f7358SJed Brown {
2297552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2298552f7358SJed Brown   PetscInt       pStart, pEnd;
2299552f7358SJed Brown   PetscInt       dof, off;
2300552f7358SJed Brown   PetscErrorCode ierr;
2301552f7358SJed Brown 
2302552f7358SJed Brown   PetscFunctionBegin;
2303552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2304552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2305552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2306552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
230782f516ccSBarry 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);
230882f516ccSBarry 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);
230982f516ccSBarry 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);
2310552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2311552f7358SJed Brown   PetscFunctionReturn(0);
2312552f7358SJed Brown }
2313552f7358SJed Brown 
2314552f7358SJed Brown /*@C
2315eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2316552f7358SJed Brown 
2317552f7358SJed Brown   Not collective
2318552f7358SJed Brown 
2319552f7358SJed Brown   Input Parameters:
2320552f7358SJed Brown + mesh - The DMPlex
2321eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2322552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
23230298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2324552f7358SJed Brown 
2325552f7358SJed Brown   Output Parameters:
2326552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2327552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2328552f7358SJed Brown 
2329552f7358SJed Brown   Note:
23300298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2331552f7358SJed Brown 
23323813dfbdSMatthew G Knepley   Fortran Notes:
23333813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23343813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23353813dfbdSMatthew G Knepley 
23363813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23373813dfbdSMatthew G Knepley 
2338552f7358SJed Brown   Level: beginner
2339552f7358SJed Brown 
2340552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2341552f7358SJed Brown @*/
2342552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2343552f7358SJed Brown {
2344552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2345552f7358SJed Brown   PetscInt       *closure, *fifo;
23460298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2347552f7358SJed Brown   PetscInt        tmpSize, t;
2348552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2349552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2350552f7358SJed Brown   PetscErrorCode  ierr;
2351552f7358SJed Brown 
2352552f7358SJed Brown   PetscFunctionBegin;
2353552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2354552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2355552f7358SJed Brown   /* This is only 1-level */
2356552f7358SJed Brown   if (useCone) {
2357552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2358552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2359552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2360552f7358SJed Brown   } else {
2361552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2362552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2363552f7358SJed Brown   }
2364bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2365bfbcdd7aSMatthew G. Knepley     if (*points) {
2366bfbcdd7aSMatthew G. Knepley       closure = *points;
2367bfbcdd7aSMatthew G. Knepley     } else {
2368bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
236969291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2370bfbcdd7aSMatthew G. Knepley     }
2371bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2372bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2373bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2374bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2375bfbcdd7aSMatthew G. Knepley     }
2376bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2377bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2378bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2379bfbcdd7aSMatthew G. Knepley   }
238024c766afSToby Isaac   {
238124c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
238224c766afSToby Isaac 
238324c766afSToby Isaac     c = mesh->maxConeSize;
238424c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
238524c766afSToby Isaac     s = mesh->maxSupportSize;
238624c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
238724c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
238824c766afSToby Isaac   }
238969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2390bfbcdd7aSMatthew G. Knepley   if (*points) {
2391bfbcdd7aSMatthew G. Knepley     closure = *points;
2392bfbcdd7aSMatthew G. Knepley   } else {
239369291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2394bfbcdd7aSMatthew G. Knepley   }
2395bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2396552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2397552f7358SJed Brown     const PetscInt cp = tmp[t];
2398552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2399552f7358SJed Brown 
2400552f7358SJed Brown     closure[closureSize]   = cp;
2401552f7358SJed Brown     closure[closureSize+1] = co;
2402552f7358SJed Brown     fifo[fifoSize]         = cp;
2403552f7358SJed Brown     fifo[fifoSize+1]       = co;
2404552f7358SJed Brown   }
2405bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2406552f7358SJed Brown   while (fifoSize - fifoStart) {
2407552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2408552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2409552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2410552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2411552f7358SJed Brown 
2412552f7358SJed Brown     if (useCone) {
2413552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2414552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2415552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2416552f7358SJed Brown     } else {
2417552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2418552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
24190298fd71SBarry Smith       tmpO = NULL;
2420552f7358SJed Brown     }
2421552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2422552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2423552f7358SJed Brown       const PetscInt cp = tmp[i];
24242e1b13c2SMatthew 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. */
24252e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
24262e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
24272e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2428552f7358SJed Brown       PetscInt       c;
2429552f7358SJed Brown 
24302e1b13c2SMatthew G. Knepley       if (rev) {
24312e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
24322e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
24332e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
24342e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
24352e1b13c2SMatthew G. Knepley       }
2436552f7358SJed Brown       /* Check for duplicate */
2437552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2438552f7358SJed Brown         if (closure[c] == cp) break;
2439552f7358SJed Brown       }
2440552f7358SJed Brown       if (c == closureSize) {
2441552f7358SJed Brown         closure[closureSize]   = cp;
2442552f7358SJed Brown         closure[closureSize+1] = co;
2443552f7358SJed Brown         fifo[fifoSize]         = cp;
2444552f7358SJed Brown         fifo[fifoSize+1]       = co;
2445552f7358SJed Brown         closureSize           += 2;
2446552f7358SJed Brown         fifoSize              += 2;
2447552f7358SJed Brown       }
2448552f7358SJed Brown     }
2449552f7358SJed Brown     fifoStart += 2;
2450552f7358SJed Brown   }
2451552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2452552f7358SJed Brown   if (points)    *points    = closure;
245369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2454552f7358SJed Brown   PetscFunctionReturn(0);
2455552f7358SJed Brown }
2456552f7358SJed Brown 
24579bf0dad6SMatthew G. Knepley /*@C
2458eaf898f9SPatrick 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
24599bf0dad6SMatthew G. Knepley 
24609bf0dad6SMatthew G. Knepley   Not collective
24619bf0dad6SMatthew G. Knepley 
24629bf0dad6SMatthew G. Knepley   Input Parameters:
24639bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2464eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24659bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
24669bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24679bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
24689bf0dad6SMatthew G. Knepley 
24699bf0dad6SMatthew G. Knepley   Output Parameters:
24709bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
24719bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
24729bf0dad6SMatthew G. Knepley 
24739bf0dad6SMatthew G. Knepley   Note:
24749bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
24759bf0dad6SMatthew G. Knepley 
24769bf0dad6SMatthew G. Knepley   Fortran Notes:
24779bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24789bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
24799bf0dad6SMatthew G. Knepley 
24809bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24819bf0dad6SMatthew G. Knepley 
24829bf0dad6SMatthew G. Knepley   Level: beginner
24839bf0dad6SMatthew G. Knepley 
24849bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
24859bf0dad6SMatthew G. Knepley @*/
24869bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
24879bf0dad6SMatthew G. Knepley {
24889bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
24899bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
24909bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
24919bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
24929bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
24939bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
24949bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
24959bf0dad6SMatthew G. Knepley 
24969bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
24979bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24989bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24999bf0dad6SMatthew G. Knepley   /* This is only 1-level */
25009bf0dad6SMatthew G. Knepley   if (useCone) {
25019bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
25029bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
25039bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
25049bf0dad6SMatthew G. Knepley   } else {
25059bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
25069bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
25079bf0dad6SMatthew G. Knepley   }
25089bf0dad6SMatthew G. Knepley   if (depth == 1) {
25099bf0dad6SMatthew G. Knepley     if (*points) {
25109bf0dad6SMatthew G. Knepley       closure = *points;
25119bf0dad6SMatthew G. Knepley     } else {
25129bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
251369291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
25149bf0dad6SMatthew G. Knepley     }
25159bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
25169bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
25179bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
25189bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
25199bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
25209bf0dad6SMatthew G. Knepley     }
25219bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
25229bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
25239bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
25249bf0dad6SMatthew G. Knepley   }
252524c766afSToby Isaac   {
252624c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
252724c766afSToby Isaac 
252824c766afSToby Isaac     c = mesh->maxConeSize;
252924c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
253024c766afSToby Isaac     s = mesh->maxSupportSize;
253124c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
253224c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
253324c766afSToby Isaac   }
253469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25359bf0dad6SMatthew G. Knepley   if (*points) {
25369bf0dad6SMatthew G. Knepley     closure = *points;
25379bf0dad6SMatthew G. Knepley   } else {
253869291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
25399bf0dad6SMatthew G. Knepley   }
25409bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
25419bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
25429bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
25439bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
25449bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
25459bf0dad6SMatthew G. Knepley 
25469bf0dad6SMatthew G. Knepley     if (ornt < 0) {
25479bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
25489bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
254986b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
25509bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25519bf0dad6SMatthew G. Knepley     }
25529bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
25539bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
25549bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
25559bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
25569bf0dad6SMatthew G. Knepley   }
25579bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
25589bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
25599bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
25609bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
25619bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
25629bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
25639bf0dad6SMatthew G. Knepley 
25649bf0dad6SMatthew G. Knepley     if (useCone) {
25659bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
25669bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
25679bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
25689bf0dad6SMatthew G. Knepley     } else {
25699bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
25709bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25719bf0dad6SMatthew G. Knepley       tmpO = NULL;
25729bf0dad6SMatthew G. Knepley     }
25739bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
25749bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
25759bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
25769bf0dad6SMatthew 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. */
25779bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25789bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25799bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
25809bf0dad6SMatthew G. Knepley       PetscInt       c;
25819bf0dad6SMatthew G. Knepley 
25829bf0dad6SMatthew G. Knepley       if (rev) {
25839bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
25849bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25859bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25869bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25879bf0dad6SMatthew G. Knepley       }
25889bf0dad6SMatthew G. Knepley       /* Check for duplicate */
25899bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
25909bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
25919bf0dad6SMatthew G. Knepley       }
25929bf0dad6SMatthew G. Knepley       if (c == closureSize) {
25939bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
25949bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
25959bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
25969bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
25979bf0dad6SMatthew G. Knepley         closureSize           += 2;
25989bf0dad6SMatthew G. Knepley         fifoSize              += 2;
25999bf0dad6SMatthew G. Knepley       }
26009bf0dad6SMatthew G. Knepley     }
26019bf0dad6SMatthew G. Knepley     fifoStart += 2;
26029bf0dad6SMatthew G. Knepley   }
26039bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
26049bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
260569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
26069bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
26079bf0dad6SMatthew G. Knepley }
26089bf0dad6SMatthew G. Knepley 
2609552f7358SJed Brown /*@C
2610eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2611552f7358SJed Brown 
2612552f7358SJed Brown   Not collective
2613552f7358SJed Brown 
2614552f7358SJed Brown   Input Parameters:
2615552f7358SJed Brown + mesh - The DMPlex
2616eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2617552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2618e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2619e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2620552f7358SJed Brown 
2621552f7358SJed Brown   Note:
26220298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2623552f7358SJed Brown 
26243813dfbdSMatthew G Knepley   Fortran Notes:
26253813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26263813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26273813dfbdSMatthew G Knepley 
26283813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26293813dfbdSMatthew G Knepley 
2630552f7358SJed Brown   Level: beginner
2631552f7358SJed Brown 
2632552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2633552f7358SJed Brown @*/
2634552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2635552f7358SJed Brown {
2636552f7358SJed Brown   PetscErrorCode ierr;
2637552f7358SJed Brown 
2638552f7358SJed Brown   PetscFunctionBegin;
2639552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2640e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2641e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
264269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
26434ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2644552f7358SJed Brown   PetscFunctionReturn(0);
2645552f7358SJed Brown }
2646552f7358SJed Brown 
2647552f7358SJed Brown /*@
2648eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2649552f7358SJed Brown 
2650552f7358SJed Brown   Not collective
2651552f7358SJed Brown 
2652552f7358SJed Brown   Input Parameter:
2653552f7358SJed Brown . mesh - The DMPlex
2654552f7358SJed Brown 
2655552f7358SJed Brown   Output Parameters:
2656552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2657552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2658552f7358SJed Brown 
2659552f7358SJed Brown   Level: beginner
2660552f7358SJed Brown 
2661552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2662552f7358SJed Brown @*/
2663552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2664552f7358SJed Brown {
2665552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2666552f7358SJed Brown 
2667552f7358SJed Brown   PetscFunctionBegin;
2668552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2669552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2670552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2671552f7358SJed Brown   PetscFunctionReturn(0);
2672552f7358SJed Brown }
2673552f7358SJed Brown 
2674552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2675552f7358SJed Brown {
2676552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2677552f7358SJed Brown   PetscInt       size;
2678552f7358SJed Brown   PetscErrorCode ierr;
2679552f7358SJed Brown 
2680552f7358SJed Brown   PetscFunctionBegin;
2681552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2682552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2683552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
26841795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
26851795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2686ef1259faSMatthew G. Knepley   ierr = PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));CHKERRQ(ierr);
2687552f7358SJed Brown   if (mesh->maxSupportSize) {
2688552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2689552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
26901795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2691ef1259faSMatthew G. Knepley     ierr = PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));CHKERRQ(ierr);
2692552f7358SJed Brown   }
2693552f7358SJed Brown   PetscFunctionReturn(0);
2694552f7358SJed Brown }
2695552f7358SJed Brown 
2696276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2697552f7358SJed Brown {
2698552f7358SJed Brown   PetscErrorCode ierr;
2699552f7358SJed Brown 
2700552f7358SJed Brown   PetscFunctionBegin;
27014d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2702792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2703c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2704736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2705f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2706f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2707f94b4a02SBlaise Bourdin 
27083dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
27093dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2710c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
2711f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2712f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2713f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2714f94b4a02SBlaise Bourdin 
2715f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2716c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2717f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2718f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2719f94b4a02SBlaise Bourdin   }
2720552f7358SJed Brown   PetscFunctionReturn(0);
2721552f7358SJed Brown }
2722552f7358SJed Brown 
27232adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
27242adcc780SMatthew G. Knepley {
27252adcc780SMatthew G. Knepley   PetscErrorCode ierr;
27263dcd263cSBlaise Bourdin   PetscInt       i = 0;
27272adcc780SMatthew G. Knepley 
27282adcc780SMatthew G. Knepley   PetscFunctionBegin;
2729435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2730792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2731c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
27323dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
27333dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
27343dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
27353dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
27363dcd263cSBlaise Bourdin 
27373dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
27383dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2739c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
274092fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
27413dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
27423dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
27433dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
27443dcd263cSBlaise Bourdin 
27453dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2746c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
27473dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
27483dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
27493dcd263cSBlaise Bourdin       break;
27503dcd263cSBlaise Bourdin     }
27513dcd263cSBlaise Bourdin   }
27522adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
27532adcc780SMatthew G. Knepley }
27542adcc780SMatthew G. Knepley 
2755552f7358SJed Brown /*@
2756eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2757552f7358SJed Brown 
2758552f7358SJed Brown   Not collective
2759552f7358SJed Brown 
2760552f7358SJed Brown   Input Parameter:
2761552f7358SJed Brown . mesh - The DMPlex
2762552f7358SJed Brown 
2763552f7358SJed Brown   Output Parameter:
2764552f7358SJed Brown 
2765552f7358SJed Brown   Note:
2766552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2767552f7358SJed Brown 
2768552f7358SJed Brown   Level: beginner
2769552f7358SJed Brown 
2770552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2771552f7358SJed Brown @*/
2772552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2773552f7358SJed Brown {
2774552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2775552f7358SJed Brown   PetscInt      *offsets;
2776552f7358SJed Brown   PetscInt       supportSize;
2777552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2778552f7358SJed Brown   PetscErrorCode ierr;
2779552f7358SJed Brown 
2780552f7358SJed Brown   PetscFunctionBegin;
2781552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
278282f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
278330b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2784552f7358SJed Brown   /* Calculate support sizes */
2785552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2786552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2787552f7358SJed Brown     PetscInt dof, off, c;
2788552f7358SJed Brown 
2789552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2790552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2791552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2792552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2793552f7358SJed Brown     }
2794552f7358SJed Brown   }
2795552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2796552f7358SJed Brown     PetscInt dof;
2797552f7358SJed Brown 
2798552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
27990d644c17SKarl Rupp 
2800552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2801552f7358SJed Brown   }
2802552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2803552f7358SJed Brown   /* Calculate supports */
2804552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
28051795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
28061795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2807552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2808552f7358SJed Brown     PetscInt dof, off, c;
2809552f7358SJed Brown 
2810552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2811552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2812552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2813552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2814552f7358SJed Brown       PetscInt       offS;
2815552f7358SJed Brown 
2816552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
28170d644c17SKarl Rupp 
2818552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2819552f7358SJed Brown       ++offsets[q];
2820552f7358SJed Brown     }
2821552f7358SJed Brown   }
2822552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
282330b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2824552f7358SJed Brown   PetscFunctionReturn(0);
2825552f7358SJed Brown }
2826552f7358SJed Brown 
2827277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2828277ea44aSLisandro Dalcin {
2829277ea44aSLisandro Dalcin   IS             stratumIS;
2830277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2831277ea44aSLisandro Dalcin 
2832277ea44aSLisandro Dalcin   PetscFunctionBegin;
2833277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
283476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2835277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2836277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2837277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2838277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2839277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2840277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2841277ea44aSLisandro Dalcin     }
2842277ea44aSLisandro 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);
2843277ea44aSLisandro Dalcin   }
2844277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2845277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2846277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2847277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2848277ea44aSLisandro Dalcin }
2849277ea44aSLisandro Dalcin 
2850552f7358SJed Brown /*@
2851a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
28526dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2853552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2854552f7358SJed Brown   the DAG.
2855552f7358SJed Brown 
2856bf4602e4SToby Isaac   Collective on dm
2857552f7358SJed Brown 
2858552f7358SJed Brown   Input Parameter:
2859552f7358SJed Brown . mesh - The DMPlex
2860552f7358SJed Brown 
2861552f7358SJed Brown   Output Parameter:
2862552f7358SJed Brown 
2863552f7358SJed Brown   Notes:
2864b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
2865b1bb481bSMatthew 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
2866b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2867c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2868150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2869552f7358SJed Brown 
2870b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
2871b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
2872b1bb481bSMatthew 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
2873b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
2874b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
2875b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
2876b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
2877b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
2878b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
2879b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
2880b1bb481bSMatthew Knepley 
2881150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2882552f7358SJed Brown 
2883552f7358SJed Brown   Level: beginner
2884552f7358SJed Brown 
2885ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
2886552f7358SJed Brown @*/
2887552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2888552f7358SJed Brown {
2889df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2890aa50250dSMatthew G. Knepley   DMLabel        label;
2891552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2892552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2893552f7358SJed Brown   PetscErrorCode ierr;
2894552f7358SJed Brown 
2895552f7358SJed Brown   PetscFunctionBegin;
2896552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2897552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2898277ea44aSLisandro Dalcin 
2899277ea44aSLisandro Dalcin   /* Create depth label */
2900aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2901c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2902aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2903277ea44aSLisandro Dalcin 
2904277ea44aSLisandro Dalcin   {
2905552f7358SJed Brown     /* Initialize roots and count leaves */
2906277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2907277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2908552f7358SJed Brown     PetscInt coneSize, supportSize;
2909552f7358SJed Brown 
2910277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2911552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2912552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2913552f7358SJed Brown       if (!coneSize && supportSize) {
2914277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2915277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2916552f7358SJed Brown         ++numRoots;
2917552f7358SJed Brown       } else if (!supportSize && coneSize) {
2918552f7358SJed Brown         ++numLeaves;
2919552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2920552f7358SJed Brown         /* Isolated points */
2921277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2922277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2923552f7358SJed Brown       }
2924552f7358SJed Brown     }
2925277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2926277ea44aSLisandro Dalcin   }
2927277ea44aSLisandro Dalcin 
2928552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2929277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2930277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2931552f7358SJed Brown     PetscInt coneSize, supportSize;
2932552f7358SJed Brown 
2933277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2934552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2935552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2936552f7358SJed Brown       if (!supportSize && coneSize) {
2937277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2938277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2939552f7358SJed Brown       }
2940552f7358SJed Brown     }
2941277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2942552f7358SJed Brown   } else {
2943277ea44aSLisandro Dalcin     PetscInt level = 0;
2944277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2945552f7358SJed Brown 
2946277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2947277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2948277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2949277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
295074ef644bSMatthew G. Knepley 
2951277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
295274ef644bSMatthew G. Knepley         const PetscInt *support;
295374ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
295474ef644bSMatthew G. Knepley 
2955277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2956277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
295774ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2958277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2959277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2960552f7358SJed Brown         }
2961552f7358SJed Brown       }
2962277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2963277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2964277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
296574ef644bSMatthew G. Knepley     }
296674ef644bSMatthew G. Knepley   }
2967bf4602e4SToby Isaac   { /* just in case there is an empty process */
2968bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2969bf4602e4SToby Isaac 
2970bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
2971ffc4695bSBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
2972bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2973367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2974bf4602e4SToby Isaac     }
2975bf4602e4SToby Isaac   }
2976d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
2977552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2978552f7358SJed Brown   PetscFunctionReturn(0);
2979552f7358SJed Brown }
2980552f7358SJed Brown 
2981412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
2982ba2698f1SMatthew G. Knepley {
2983412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
2984412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
2985ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
2986ba2698f1SMatthew G. Knepley 
2987412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
2988ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2989ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2990ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2991ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
2992ba2698f1SMatthew G. Knepley   if (depth <= 1) {
2993ba2698f1SMatthew G. Knepley     switch (pdepth) {
2994ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
2995ba2698f1SMatthew G. Knepley       case 1:
2996ba2698f1SMatthew G. Knepley         switch (coneSize) {
2997ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
2998ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
2999ba2698f1SMatthew G. Knepley           case 4:
3000ba2698f1SMatthew G. Knepley           switch (dim) {
3001ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3002ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3003ba2698f1SMatthew G. Knepley             default: break;
3004ba2698f1SMatthew G. Knepley           }
3005ba2698f1SMatthew G. Knepley           break;
3006da9060c4SMatthew G. Knepley         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3007ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3008ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3009ba2698f1SMatthew G. Knepley         default: break;
3010ba2698f1SMatthew G. Knepley       }
3011ba2698f1SMatthew G. Knepley     }
3012ba2698f1SMatthew G. Knepley   } else {
3013ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
3014ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
3015ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
3016ba2698f1SMatthew G. Knepley       switch (dim) {
3017ba2698f1SMatthew G. Knepley         case 1:
3018ba2698f1SMatthew G. Knepley           switch (coneSize) {
3019ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3020ba2698f1SMatthew G. Knepley             default: break;
3021ba2698f1SMatthew G. Knepley           }
3022ba2698f1SMatthew G. Knepley           break;
3023ba2698f1SMatthew G. Knepley         case 2:
3024ba2698f1SMatthew G. Knepley           switch (coneSize) {
3025ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3026ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3027ba2698f1SMatthew G. Knepley             default: break;
3028ba2698f1SMatthew G. Knepley           }
3029ba2698f1SMatthew G. Knepley           break;
3030ba2698f1SMatthew G. Knepley         case 3:
3031ba2698f1SMatthew G. Knepley           switch (coneSize) {
3032ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3033da9060c4SMatthew G. Knepley             case 5:
3034da9060c4SMatthew G. Knepley             {
3035da9060c4SMatthew G. Knepley               const PetscInt *cone;
3036da9060c4SMatthew G. Knepley               PetscInt        faceConeSize;
3037da9060c4SMatthew G. Knepley 
3038da9060c4SMatthew G. Knepley               ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
3039da9060c4SMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[0], &faceConeSize);CHKERRQ(ierr);
3040da9060c4SMatthew G. Knepley               switch (faceConeSize) {
3041da9060c4SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3042da9060c4SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
3043da9060c4SMatthew G. Knepley               }
3044da9060c4SMatthew G. Knepley             }
3045da9060c4SMatthew G. Knepley             break;
3046ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3047ba2698f1SMatthew G. Knepley             default: break;
3048ba2698f1SMatthew G. Knepley           }
3049ba2698f1SMatthew G. Knepley           break;
3050ba2698f1SMatthew G. Knepley         default: break;
3051ba2698f1SMatthew G. Knepley       }
3052ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
3053ba2698f1SMatthew G. Knepley       switch (coneSize) {
3054ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3055ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3056ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3057ba2698f1SMatthew G. Knepley         default: break;
3058ba2698f1SMatthew G. Knepley       }
3059ba2698f1SMatthew G. Knepley     }
3060ba2698f1SMatthew G. Knepley   }
3061412e9a14SMatthew G. Knepley   *pt = ct;
3062412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3063ba2698f1SMatthew G. Knepley }
3064412e9a14SMatthew G. Knepley 
3065412e9a14SMatthew G. Knepley /*@
3066412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3067412e9a14SMatthew G. Knepley 
3068412e9a14SMatthew G. Knepley   Collective on dm
3069412e9a14SMatthew G. Knepley 
3070412e9a14SMatthew G. Knepley   Input Parameter:
3071412e9a14SMatthew G. Knepley . mesh - The DMPlex
3072412e9a14SMatthew G. Knepley 
3073412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3074412e9a14SMatthew G. Knepley 
3075412e9a14SMatthew G. Knepley   Level: developer
3076412e9a14SMatthew G. Knepley 
3077412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3078412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3079412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3080412e9a14SMatthew G. Knepley 
3081412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3082412e9a14SMatthew G. Knepley @*/
3083412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3084412e9a14SMatthew G. Knepley {
3085412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3086412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3087412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3088412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3089412e9a14SMatthew G. Knepley 
3090412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3091412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3092412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3093412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3094412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3095412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3096412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3097327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3098412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3099412e9a14SMatthew G. Knepley 
3100412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3101412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3102412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3103412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3104412e9a14SMatthew G. Knepley   }
3105412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3106412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3107ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3108ba2698f1SMatthew G. Knepley }
3109ba2698f1SMatthew G. Knepley 
3110552f7358SJed Brown /*@C
3111552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3112552f7358SJed Brown 
3113552f7358SJed Brown   Not Collective
3114552f7358SJed Brown 
3115552f7358SJed Brown   Input Parameters:
3116552f7358SJed Brown + dm - The DMPlex object
3117552f7358SJed Brown . numPoints - The number of input points for the join
3118552f7358SJed Brown - points - The input points
3119552f7358SJed Brown 
3120552f7358SJed Brown   Output Parameters:
3121552f7358SJed Brown + numCoveredPoints - The number of points in the join
3122552f7358SJed Brown - coveredPoints - The points in the join
3123552f7358SJed Brown 
3124552f7358SJed Brown   Level: intermediate
3125552f7358SJed Brown 
3126552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3127552f7358SJed Brown 
31283813dfbdSMatthew G Knepley   Fortran Notes:
31293813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31303813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31313813dfbdSMatthew G Knepley 
31323813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31333813dfbdSMatthew G Knepley 
3134552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3135552f7358SJed Brown @*/
3136552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3137552f7358SJed Brown {
3138552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3139552f7358SJed Brown   PetscInt      *join[2];
3140552f7358SJed Brown   PetscInt       joinSize, i = 0;
3141552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3142552f7358SJed Brown   PetscErrorCode ierr;
3143552f7358SJed Brown 
3144552f7358SJed Brown   PetscFunctionBegin;
3145552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
314648bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
314748bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
314848bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
314969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
315069291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3151552f7358SJed Brown   /* Copy in support of first point */
3152552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3153552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3154552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3155552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3156552f7358SJed Brown   }
3157552f7358SJed Brown   /* Check each successive support */
3158552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3159552f7358SJed Brown     PetscInt newJoinSize = 0;
3160552f7358SJed Brown 
3161552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3162552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3163552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3164552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3165552f7358SJed Brown 
3166552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3167552f7358SJed Brown         if (point == join[i][m]) {
3168552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3169552f7358SJed Brown           break;
3170552f7358SJed Brown         }
3171552f7358SJed Brown       }
3172552f7358SJed Brown     }
3173552f7358SJed Brown     joinSize = newJoinSize;
3174552f7358SJed Brown     i        = 1-i;
3175552f7358SJed Brown   }
3176552f7358SJed Brown   *numCoveredPoints = joinSize;
3177552f7358SJed Brown   *coveredPoints    = join[i];
317869291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3179552f7358SJed Brown   PetscFunctionReturn(0);
3180552f7358SJed Brown }
3181552f7358SJed Brown 
3182552f7358SJed Brown /*@C
3183552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3184552f7358SJed Brown 
3185552f7358SJed Brown   Not Collective
3186552f7358SJed Brown 
3187552f7358SJed Brown   Input Parameters:
3188552f7358SJed Brown + dm - The DMPlex object
3189552f7358SJed Brown . numPoints - The number of input points for the join
3190552f7358SJed Brown - points - The input points
3191552f7358SJed Brown 
3192552f7358SJed Brown   Output Parameters:
3193552f7358SJed Brown + numCoveredPoints - The number of points in the join
3194552f7358SJed Brown - coveredPoints - The points in the join
3195552f7358SJed Brown 
31963813dfbdSMatthew G Knepley   Fortran Notes:
31973813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31983813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31993813dfbdSMatthew G Knepley 
32003813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32013813dfbdSMatthew G Knepley 
3202552f7358SJed Brown   Level: intermediate
3203552f7358SJed Brown 
3204552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3205552f7358SJed Brown @*/
3206552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3207552f7358SJed Brown {
3208552f7358SJed Brown   PetscErrorCode ierr;
3209552f7358SJed Brown 
3210552f7358SJed Brown   PetscFunctionBegin;
3211552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3212d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3213d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3214d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
321569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3216d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3217552f7358SJed Brown   PetscFunctionReturn(0);
3218552f7358SJed Brown }
3219552f7358SJed Brown 
3220552f7358SJed Brown /*@C
3221552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3222552f7358SJed Brown 
3223552f7358SJed Brown   Not Collective
3224552f7358SJed Brown 
3225552f7358SJed Brown   Input Parameters:
3226552f7358SJed Brown + dm - The DMPlex object
3227552f7358SJed Brown . numPoints - The number of input points for the join
3228552f7358SJed Brown - points - The input points
3229552f7358SJed Brown 
3230552f7358SJed Brown   Output Parameters:
3231552f7358SJed Brown + numCoveredPoints - The number of points in the join
3232552f7358SJed Brown - coveredPoints - The points in the join
3233552f7358SJed Brown 
32343813dfbdSMatthew G Knepley   Fortran Notes:
32353813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32363813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32373813dfbdSMatthew G Knepley 
32383813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32393813dfbdSMatthew G Knepley 
3240552f7358SJed Brown   Level: intermediate
3241552f7358SJed Brown 
3242552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3243552f7358SJed Brown @*/
3244552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3245552f7358SJed Brown {
3246552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3247552f7358SJed Brown   PetscInt      *offsets, **closures;
3248552f7358SJed Brown   PetscInt      *join[2];
3249552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
325024c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3251552f7358SJed Brown   PetscErrorCode ierr;
3252552f7358SJed Brown 
3253552f7358SJed Brown   PetscFunctionBegin;
3254552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
325548bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
325648bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
325748bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3258552f7358SJed Brown 
3259552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
32601795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
326169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
326224c766afSToby Isaac   ms      = mesh->maxSupportSize;
326324c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
326469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
326569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3266552f7358SJed Brown 
3267552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3268552f7358SJed Brown     PetscInt closureSize;
3269552f7358SJed Brown 
3270552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
32710d644c17SKarl Rupp 
3272552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3273552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3274552f7358SJed Brown       PetscInt pStart, pEnd, i;
3275552f7358SJed Brown 
3276552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3277552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3278552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3279552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3280552f7358SJed Brown           break;
3281552f7358SJed Brown         }
3282552f7358SJed Brown       }
3283552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3284552f7358SJed Brown     }
328582f516ccSBarry 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);
3286552f7358SJed Brown   }
3287552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3288552f7358SJed Brown     PetscInt dof;
3289552f7358SJed Brown 
3290552f7358SJed Brown     /* Copy in support of first point */
3291552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3292552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3293552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3294552f7358SJed Brown     }
3295552f7358SJed Brown     /* Check each successive cone */
3296552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3297552f7358SJed Brown       PetscInt newJoinSize = 0;
3298552f7358SJed Brown 
3299552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3300552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3301552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3302552f7358SJed Brown 
3303552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3304552f7358SJed Brown           if (point == join[i][m]) {
3305552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3306552f7358SJed Brown             break;
3307552f7358SJed Brown           }
3308552f7358SJed Brown         }
3309552f7358SJed Brown       }
3310552f7358SJed Brown       joinSize = newJoinSize;
3311552f7358SJed Brown       i        = 1-i;
3312552f7358SJed Brown     }
3313552f7358SJed Brown     if (joinSize) break;
3314552f7358SJed Brown   }
3315552f7358SJed Brown   *numCoveredPoints = joinSize;
3316552f7358SJed Brown   *coveredPoints    = join[i];
3317552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
33180298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3319552f7358SJed Brown   }
3320552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
332169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
332269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3323552f7358SJed Brown   PetscFunctionReturn(0);
3324552f7358SJed Brown }
3325552f7358SJed Brown 
3326552f7358SJed Brown /*@C
3327552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3328552f7358SJed Brown 
3329552f7358SJed Brown   Not Collective
3330552f7358SJed Brown 
3331552f7358SJed Brown   Input Parameters:
3332552f7358SJed Brown + dm - The DMPlex object
3333552f7358SJed Brown . numPoints - The number of input points for the meet
3334552f7358SJed Brown - points - The input points
3335552f7358SJed Brown 
3336552f7358SJed Brown   Output Parameters:
3337552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3338552f7358SJed Brown - coveredPoints - The points in the meet
3339552f7358SJed Brown 
3340552f7358SJed Brown   Level: intermediate
3341552f7358SJed Brown 
3342552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3343552f7358SJed Brown 
33443813dfbdSMatthew G Knepley   Fortran Notes:
33453813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33463813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33473813dfbdSMatthew G Knepley 
33483813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33493813dfbdSMatthew G Knepley 
3350552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3351552f7358SJed Brown @*/
3352552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3353552f7358SJed Brown {
3354552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3355552f7358SJed Brown   PetscInt      *meet[2];
3356552f7358SJed Brown   PetscInt       meetSize, i = 0;
3357552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3358552f7358SJed Brown   PetscErrorCode ierr;
3359552f7358SJed Brown 
3360552f7358SJed Brown   PetscFunctionBegin;
3361552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3362552f7358SJed Brown   PetscValidPointer(points, 2);
3363552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3364552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
336569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
336669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3367552f7358SJed Brown   /* Copy in cone of first point */
3368552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3369552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3370552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3371552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3372552f7358SJed Brown   }
3373552f7358SJed Brown   /* Check each successive cone */
3374552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3375552f7358SJed Brown     PetscInt newMeetSize = 0;
3376552f7358SJed Brown 
3377552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3378552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3379552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3380552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3381552f7358SJed Brown 
3382552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3383552f7358SJed Brown         if (point == meet[i][m]) {
3384552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3385552f7358SJed Brown           break;
3386552f7358SJed Brown         }
3387552f7358SJed Brown       }
3388552f7358SJed Brown     }
3389552f7358SJed Brown     meetSize = newMeetSize;
3390552f7358SJed Brown     i        = 1-i;
3391552f7358SJed Brown   }
3392552f7358SJed Brown   *numCoveringPoints = meetSize;
3393552f7358SJed Brown   *coveringPoints    = meet[i];
339469291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3395552f7358SJed Brown   PetscFunctionReturn(0);
3396552f7358SJed Brown }
3397552f7358SJed Brown 
3398552f7358SJed Brown /*@C
3399552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3400552f7358SJed Brown 
3401552f7358SJed Brown   Not Collective
3402552f7358SJed Brown 
3403552f7358SJed Brown   Input Parameters:
3404552f7358SJed Brown + dm - The DMPlex object
3405552f7358SJed Brown . numPoints - The number of input points for the meet
3406552f7358SJed Brown - points - The input points
3407552f7358SJed Brown 
3408552f7358SJed Brown   Output Parameters:
3409552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3410552f7358SJed Brown - coveredPoints - The points in the meet
3411552f7358SJed Brown 
3412552f7358SJed Brown   Level: intermediate
3413552f7358SJed Brown 
34143813dfbdSMatthew G Knepley   Fortran Notes:
34153813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34163813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34173813dfbdSMatthew G Knepley 
34183813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34193813dfbdSMatthew G Knepley 
3420552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3421552f7358SJed Brown @*/
3422552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3423552f7358SJed Brown {
3424552f7358SJed Brown   PetscErrorCode ierr;
3425552f7358SJed Brown 
3426552f7358SJed Brown   PetscFunctionBegin;
3427552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3428d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3429d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3430d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
343169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3432d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3433552f7358SJed Brown   PetscFunctionReturn(0);
3434552f7358SJed Brown }
3435552f7358SJed Brown 
3436552f7358SJed Brown /*@C
3437552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3438552f7358SJed Brown 
3439552f7358SJed Brown   Not Collective
3440552f7358SJed Brown 
3441552f7358SJed Brown   Input Parameters:
3442552f7358SJed Brown + dm - The DMPlex object
3443552f7358SJed Brown . numPoints - The number of input points for the meet
3444552f7358SJed Brown - points - The input points
3445552f7358SJed Brown 
3446552f7358SJed Brown   Output Parameters:
3447552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3448552f7358SJed Brown - coveredPoints - The points in the meet
3449552f7358SJed Brown 
3450552f7358SJed Brown   Level: intermediate
3451552f7358SJed Brown 
34523813dfbdSMatthew G Knepley   Fortran Notes:
34533813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34543813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34553813dfbdSMatthew G Knepley 
34563813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34573813dfbdSMatthew G Knepley 
3458552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3459552f7358SJed Brown @*/
3460552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3461552f7358SJed Brown {
3462552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3463552f7358SJed Brown   PetscInt      *offsets, **closures;
3464552f7358SJed Brown   PetscInt      *meet[2];
3465552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
346624c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3467552f7358SJed Brown   PetscErrorCode ierr;
3468552f7358SJed Brown 
3469552f7358SJed Brown   PetscFunctionBegin;
3470552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3471552f7358SJed Brown   PetscValidPointer(points, 2);
3472552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3473552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3474552f7358SJed Brown 
3475552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3476785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
347769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
347824c766afSToby Isaac   mc      = mesh->maxConeSize;
347924c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
348069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
348169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3482552f7358SJed Brown 
3483552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3484552f7358SJed Brown     PetscInt closureSize;
3485552f7358SJed Brown 
3486552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
34870d644c17SKarl Rupp 
3488552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3489552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3490552f7358SJed Brown       PetscInt pStart, pEnd, i;
3491552f7358SJed Brown 
3492552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3493552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3494552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3495552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3496552f7358SJed Brown           break;
3497552f7358SJed Brown         }
3498552f7358SJed Brown       }
3499552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3500552f7358SJed Brown     }
350182f516ccSBarry 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);
3502552f7358SJed Brown   }
3503552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3504552f7358SJed Brown     PetscInt dof;
3505552f7358SJed Brown 
3506552f7358SJed Brown     /* Copy in cone of first point */
3507552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3508552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3509552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3510552f7358SJed Brown     }
3511552f7358SJed Brown     /* Check each successive cone */
3512552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3513552f7358SJed Brown       PetscInt newMeetSize = 0;
3514552f7358SJed Brown 
3515552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3516552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3517552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3518552f7358SJed Brown 
3519552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3520552f7358SJed Brown           if (point == meet[i][m]) {
3521552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3522552f7358SJed Brown             break;
3523552f7358SJed Brown           }
3524552f7358SJed Brown         }
3525552f7358SJed Brown       }
3526552f7358SJed Brown       meetSize = newMeetSize;
3527552f7358SJed Brown       i        = 1-i;
3528552f7358SJed Brown     }
3529552f7358SJed Brown     if (meetSize) break;
3530552f7358SJed Brown   }
3531552f7358SJed Brown   *numCoveredPoints = meetSize;
3532552f7358SJed Brown   *coveredPoints    = meet[i];
3533552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
35340298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3535552f7358SJed Brown   }
3536552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
353769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
353869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3539552f7358SJed Brown   PetscFunctionReturn(0);
3540552f7358SJed Brown }
3541552f7358SJed Brown 
35424e3744c5SMatthew G. Knepley /*@C
35434e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
35444e3744c5SMatthew G. Knepley 
35454e3744c5SMatthew G. Knepley   Not Collective
35464e3744c5SMatthew G. Knepley 
35474e3744c5SMatthew G. Knepley   Input Parameters:
35484e3744c5SMatthew G. Knepley + dmA - A DMPlex object
35494e3744c5SMatthew G. Knepley - dmB - A DMPlex object
35504e3744c5SMatthew G. Knepley 
35514e3744c5SMatthew G. Knepley   Output Parameters:
35524e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
35534e3744c5SMatthew G. Knepley 
35544e3744c5SMatthew G. Knepley   Level: intermediate
35554e3744c5SMatthew G. Knepley 
35564e3744c5SMatthew G. Knepley   Notes:
35574e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
35584e3744c5SMatthew G. Knepley 
35594e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
35604e3744c5SMatthew G. Knepley @*/
35614e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
35624e3744c5SMatthew G. Knepley {
35634e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
35644e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
35654e3744c5SMatthew G. Knepley 
35664e3744c5SMatthew G. Knepley   PetscFunctionBegin;
35674e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
35684e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
35694e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
35704e3744c5SMatthew G. Knepley 
35714e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
35724e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
35734e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
35744e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
35754e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
35764e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
35774e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
35784e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35794e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
35804e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
35814e3744c5SMatthew G. Knepley 
35824e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
35834e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
35844e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
35854e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
35864e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
35874e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
35884e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
35894e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
35904e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
35914e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
35924e3744c5SMatthew G. Knepley     }
35934e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
35944e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
35954e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
35964e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
35974e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
35984e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
35994e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
36004e3744c5SMatthew G. Knepley     }
36014e3744c5SMatthew G. Knepley   }
36024e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
36034e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
36044e3744c5SMatthew G. Knepley }
36054e3744c5SMatthew G. Knepley 
36067cd05799SMatthew G. Knepley /*@C
36077cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
36087cd05799SMatthew G. Knepley 
36097cd05799SMatthew G. Knepley   Not Collective
36107cd05799SMatthew G. Knepley 
36117cd05799SMatthew G. Knepley   Input Parameters:
36127cd05799SMatthew G. Knepley + dm         - The DMPlex
36137cd05799SMatthew G. Knepley . cellDim    - The cell dimension
36147cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
36157cd05799SMatthew G. Knepley 
36167cd05799SMatthew G. Knepley   Output Parameters:
36177cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
36187cd05799SMatthew G. Knepley 
36197cd05799SMatthew G. Knepley   Level: developer
36207cd05799SMatthew G. Knepley 
36217cd05799SMatthew G. Knepley   Notes:
36227cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
36237cd05799SMatthew G. Knepley 
36247cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
36257cd05799SMatthew G. Knepley @*/
362618ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3627a6dfd86eSKarl Rupp {
362882f516ccSBarry Smith   MPI_Comm       comm;
3629552f7358SJed Brown   PetscErrorCode ierr;
3630552f7358SJed Brown 
3631552f7358SJed Brown   PetscFunctionBegin;
363282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3633552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3634552f7358SJed Brown   switch (cellDim) {
3635552f7358SJed Brown   case 0:
3636552f7358SJed Brown     *numFaceVertices = 0;
3637552f7358SJed Brown     break;
3638552f7358SJed Brown   case 1:
3639552f7358SJed Brown     *numFaceVertices = 1;
3640552f7358SJed Brown     break;
3641552f7358SJed Brown   case 2:
3642552f7358SJed Brown     switch (numCorners) {
364319436ca2SJed Brown     case 3: /* triangle */
364419436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3645552f7358SJed Brown       break;
364619436ca2SJed Brown     case 4: /* quadrilateral */
364719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3648552f7358SJed Brown       break;
364919436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
365019436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3651552f7358SJed Brown       break;
365219436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
365319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3654552f7358SJed Brown       break;
3655552f7358SJed Brown     default:
3656f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3657552f7358SJed Brown     }
3658552f7358SJed Brown     break;
3659552f7358SJed Brown   case 3:
3660552f7358SJed Brown     switch (numCorners) {
366119436ca2SJed Brown     case 4: /* tetradehdron */
366219436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3663552f7358SJed Brown       break;
366419436ca2SJed Brown     case 6: /* tet cohesive cells */
366519436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3666552f7358SJed Brown       break;
366719436ca2SJed Brown     case 8: /* hexahedron */
366819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3669552f7358SJed Brown       break;
367019436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
367119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3672552f7358SJed Brown       break;
367319436ca2SJed Brown     case 10: /* quadratic tetrahedron */
367419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3675552f7358SJed Brown       break;
367619436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
367719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3678552f7358SJed Brown       break;
367919436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
368019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3681552f7358SJed Brown       break;
368219436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
368319436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3684552f7358SJed Brown       break;
3685552f7358SJed Brown     default:
3686f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3687552f7358SJed Brown     }
3688552f7358SJed Brown     break;
3689552f7358SJed Brown   default:
3690f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3691552f7358SJed Brown   }
3692552f7358SJed Brown   PetscFunctionReturn(0);
3693552f7358SJed Brown }
3694552f7358SJed Brown 
3695552f7358SJed Brown /*@
3696aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3697552f7358SJed Brown 
3698552f7358SJed Brown   Not Collective
3699552f7358SJed Brown 
3700aa50250dSMatthew G. Knepley   Input Parameter:
3701552f7358SJed Brown . dm    - The DMPlex object
3702552f7358SJed Brown 
3703aa50250dSMatthew G. Knepley   Output Parameter:
3704aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3705552f7358SJed Brown 
3706552f7358SJed Brown   Level: developer
3707552f7358SJed Brown 
3708dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
3709aa50250dSMatthew G. Knepley @*/
3710aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3711aa50250dSMatthew G. Knepley {
3712aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3713aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3714aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3715c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3716aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3717aa50250dSMatthew G. Knepley }
3718aa50250dSMatthew G. Knepley 
3719aa50250dSMatthew G. Knepley /*@
3720aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3721aa50250dSMatthew G. Knepley 
3722aa50250dSMatthew G. Knepley   Not Collective
3723aa50250dSMatthew G. Knepley 
3724aa50250dSMatthew G. Knepley   Input Parameter:
3725aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3726aa50250dSMatthew G. Knepley 
3727aa50250dSMatthew G. Knepley   Output Parameter:
3728aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3729aa50250dSMatthew G. Knepley 
3730aa50250dSMatthew G. Knepley   Level: developer
3731552f7358SJed Brown 
3732b1bb481bSMatthew Knepley   Notes:
3733b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3734dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
3735dc287ab2SVaclav Hapla   An empty mesh gives -1.
3736b1bb481bSMatthew Knepley 
3737dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
3738552f7358SJed Brown @*/
3739552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3740552f7358SJed Brown {
3741aa50250dSMatthew G. Knepley   DMLabel        label;
3742aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3743552f7358SJed Brown   PetscErrorCode ierr;
3744552f7358SJed Brown 
3745552f7358SJed Brown   PetscFunctionBegin;
3746552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3747552f7358SJed Brown   PetscValidPointer(depth, 2);
3748aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3749aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3750552f7358SJed Brown   *depth = d-1;
3751552f7358SJed Brown   PetscFunctionReturn(0);
3752552f7358SJed Brown }
3753552f7358SJed Brown 
3754552f7358SJed Brown /*@
3755552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3756552f7358SJed Brown 
3757552f7358SJed Brown   Not Collective
3758552f7358SJed Brown 
3759552f7358SJed Brown   Input Parameters:
3760552f7358SJed Brown + dm           - The DMPlex object
3761552f7358SJed Brown - stratumValue - The requested depth
3762552f7358SJed Brown 
3763552f7358SJed Brown   Output Parameters:
3764552f7358SJed Brown + start - The first point at this depth
3765552f7358SJed Brown - end   - One beyond the last point at this depth
3766552f7358SJed Brown 
3767647867b2SJed Brown   Notes:
3768647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3769647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3770647867b2SJed Brown   higher dimension, e.g., "edges".
3771647867b2SJed Brown 
3772552f7358SJed Brown   Level: developer
3773552f7358SJed Brown 
3774dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
3775552f7358SJed Brown @*/
37760adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37770adebc6cSBarry Smith {
3778aa50250dSMatthew G. Knepley   DMLabel        label;
377963d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3780552f7358SJed Brown   PetscErrorCode ierr;
3781552f7358SJed Brown 
3782552f7358SJed Brown   PetscFunctionBegin;
3783552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
378563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3786552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
37870d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
378863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
378963d1a920SMatthew G. Knepley     if (start) *start = pStart;
379063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
379163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3792552f7358SJed Brown   }
3793aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
379463d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
379563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3796552f7358SJed Brown   PetscFunctionReturn(0);
3797552f7358SJed Brown }
3798552f7358SJed Brown 
3799552f7358SJed Brown /*@
3800552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3801552f7358SJed Brown 
3802552f7358SJed Brown   Not Collective
3803552f7358SJed Brown 
3804552f7358SJed Brown   Input Parameters:
3805552f7358SJed Brown + dm           - The DMPlex object
3806552f7358SJed Brown - stratumValue - The requested height
3807552f7358SJed Brown 
3808552f7358SJed Brown   Output Parameters:
3809552f7358SJed Brown + start - The first point at this height
3810552f7358SJed Brown - end   - One beyond the last point at this height
3811552f7358SJed Brown 
3812647867b2SJed Brown   Notes:
3813647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3814647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3815647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3816647867b2SJed Brown 
3817552f7358SJed Brown   Level: developer
3818552f7358SJed Brown 
38193dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
3820552f7358SJed Brown @*/
38210adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
38220adebc6cSBarry Smith {
3823aa50250dSMatthew G. Knepley   DMLabel        label;
382463d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3825552f7358SJed Brown   PetscErrorCode ierr;
3826552f7358SJed Brown 
3827552f7358SJed Brown   PetscFunctionBegin;
3828552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382963d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
383063d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3831552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
38320d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
383363d1a920SMatthew G. Knepley   if (stratumValue < 0) {
383463d1a920SMatthew G. Knepley     if (start) *start = pStart;
383563d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
383663d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3837552f7358SJed Brown   }
3838aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
383913903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
384063d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
384163d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3842552f7358SJed Brown   PetscFunctionReturn(0);
3843552f7358SJed Brown }
3844552f7358SJed Brown 
3845ba2698f1SMatthew G. Knepley /*@
3846ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
3847ba2698f1SMatthew G. Knepley 
3848ba2698f1SMatthew G. Knepley   Not Collective
3849ba2698f1SMatthew G. Knepley 
3850ba2698f1SMatthew G. Knepley   Input Parameter:
3851ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
3852ba2698f1SMatthew G. Knepley - point - The point
3853ba2698f1SMatthew G. Knepley 
3854ba2698f1SMatthew G. Knepley   Output Parameter:
3855ba2698f1SMatthew G. Knepley . depth - The depth of the point
3856ba2698f1SMatthew G. Knepley 
3857ba2698f1SMatthew G. Knepley   Level: intermediate
3858ba2698f1SMatthew G. Knepley 
38593dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
3860ba2698f1SMatthew G. Knepley @*/
3861ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
3862ba2698f1SMatthew G. Knepley {
3863ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3864ba2698f1SMatthew G. Knepley 
3865ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3866ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386740a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
3868ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
3869ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3870ba2698f1SMatthew G. Knepley }
3871ba2698f1SMatthew G. Knepley 
3872ba2698f1SMatthew G. Knepley /*@
38730c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
38740c0a32dcSVaclav Hapla 
38750c0a32dcSVaclav Hapla   Not Collective
38760c0a32dcSVaclav Hapla 
38770c0a32dcSVaclav Hapla   Input Parameter:
38780c0a32dcSVaclav Hapla + dm    - The DMPlex object
38790c0a32dcSVaclav Hapla - point - The point
38800c0a32dcSVaclav Hapla 
38810c0a32dcSVaclav Hapla   Output Parameter:
38820c0a32dcSVaclav Hapla . height - The height of the point
38830c0a32dcSVaclav Hapla 
38840c0a32dcSVaclav Hapla   Level: intermediate
38850c0a32dcSVaclav Hapla 
38863dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
38870c0a32dcSVaclav Hapla @*/
38880c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
38890c0a32dcSVaclav Hapla {
38900c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
38910c0a32dcSVaclav Hapla   PetscErrorCode ierr;
38920c0a32dcSVaclav Hapla 
38930c0a32dcSVaclav Hapla   PetscFunctionBegin;
38940c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38950c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
38960c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
38970c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
38980c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
38990c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
39000c0a32dcSVaclav Hapla }
39010c0a32dcSVaclav Hapla 
39020c0a32dcSVaclav Hapla /*@
3903ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
3904ba2698f1SMatthew G. Knepley 
3905ba2698f1SMatthew G. Knepley   Not Collective
3906ba2698f1SMatthew G. Knepley 
3907ba2698f1SMatthew G. Knepley   Input Parameter:
3908ba2698f1SMatthew G. Knepley . dm - The DMPlex object
3909ba2698f1SMatthew G. Knepley 
3910ba2698f1SMatthew G. Knepley   Output Parameter:
3911ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
3912ba2698f1SMatthew G. Knepley 
3913412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
3914412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
3915412e9a14SMatthew G. Knepley 
3916ba2698f1SMatthew G. Knepley   Level: developer
3917ba2698f1SMatthew G. Knepley 
3918dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
3919ba2698f1SMatthew G. Knepley @*/
3920ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
3921ba2698f1SMatthew G. Knepley {
3922ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3923ba2698f1SMatthew G. Knepley 
3924ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3925ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3926ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
3927ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
3928ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
3929ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3930ba2698f1SMatthew G. Knepley }
3931ba2698f1SMatthew G. Knepley 
3932ba2698f1SMatthew G. Knepley /*@
3933ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
3934ba2698f1SMatthew G. Knepley 
3935ba2698f1SMatthew G. Knepley   Not Collective
3936ba2698f1SMatthew G. Knepley 
3937ba2698f1SMatthew G. Knepley   Input Parameter:
3938ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
3939ba2698f1SMatthew G. Knepley - cell - The cell
3940ba2698f1SMatthew G. Knepley 
3941ba2698f1SMatthew G. Knepley   Output Parameter:
3942ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
3943ba2698f1SMatthew G. Knepley 
3944ba2698f1SMatthew G. Knepley   Level: intermediate
3945ba2698f1SMatthew G. Knepley 
3946ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3947ba2698f1SMatthew G. Knepley @*/
3948ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
3949ba2698f1SMatthew G. Knepley {
3950ba2698f1SMatthew G. Knepley   DMLabel        label;
3951ba2698f1SMatthew G. Knepley   PetscInt       ct;
3952ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3953ba2698f1SMatthew G. Knepley 
3954ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3955ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3956ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
3957ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3958ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
3959ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
3960ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3961ba2698f1SMatthew G. Knepley }
3962ba2698f1SMatthew G. Knepley 
3963412e9a14SMatthew G. Knepley /*@
3964412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
3965412e9a14SMatthew G. Knepley 
3966412e9a14SMatthew G. Knepley   Not Collective
3967412e9a14SMatthew G. Knepley 
3968412e9a14SMatthew G. Knepley   Input Parameters:
3969412e9a14SMatthew G. Knepley + dm   - The DMPlex object
3970412e9a14SMatthew G. Knepley . cell - The cell
3971412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
3972412e9a14SMatthew G. Knepley 
3973412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
3974412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
3975412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
3976412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
3977412e9a14SMatthew G. Knepley 
3978412e9a14SMatthew G. Knepley   Level: advanced
3979412e9a14SMatthew G. Knepley 
3980412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
3981412e9a14SMatthew G. Knepley @*/
3982412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
3983412e9a14SMatthew G. Knepley {
3984412e9a14SMatthew G. Knepley   DMLabel        label;
3985412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3986412e9a14SMatthew G. Knepley 
3987412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3988412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3989412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3990412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
3991412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3992412e9a14SMatthew G. Knepley }
3993412e9a14SMatthew G. Knepley 
39940adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
39950adebc6cSBarry Smith {
3996efe440bfSMatthew G. Knepley   PetscSection   section, s;
3997efe440bfSMatthew G. Knepley   Mat            m;
39983e922f36SToby Isaac   PetscInt       maxHeight;
3999552f7358SJed Brown   PetscErrorCode ierr;
4000552f7358SJed Brown 
4001552f7358SJed Brown   PetscFunctionBegin;
400238221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
40033e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
40043e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
400582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
400692fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
40071d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4008efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
4009efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
4010efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
4011efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
4012efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
40138f4c458bSMatthew G. Knepley 
40148f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
40158f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
4016552f7358SJed Brown   PetscFunctionReturn(0);
4017552f7358SJed Brown }
4018552f7358SJed Brown 
4019f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4020f19dbd58SToby Isaac {
4021f19dbd58SToby Isaac   Vec            coordsLocal;
4022f19dbd58SToby Isaac   DM             coordsDM;
4023f19dbd58SToby Isaac   PetscErrorCode ierr;
4024f19dbd58SToby Isaac 
4025f19dbd58SToby Isaac   PetscFunctionBegin;
4026f19dbd58SToby Isaac   *field = NULL;
4027f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
4028f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
4029f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
4030f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
4031f19dbd58SToby Isaac   }
4032f19dbd58SToby Isaac   PetscFunctionReturn(0);
4033f19dbd58SToby Isaac }
4034f19dbd58SToby Isaac 
40357cd05799SMatthew G. Knepley /*@C
40367cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone 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: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
40497cd05799SMatthew G. Knepley @*/
40500adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
40510adebc6cSBarry Smith {
4052552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4053552f7358SJed Brown 
4054552f7358SJed Brown   PetscFunctionBegin;
4055552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4056552f7358SJed Brown   if (section) *section = mesh->coneSection;
4057552f7358SJed Brown   PetscFunctionReturn(0);
4058552f7358SJed Brown }
4059552f7358SJed Brown 
40607cd05799SMatthew G. Knepley /*@C
40617cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support 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 . section - The PetscSection object
40707cd05799SMatthew G. Knepley 
40717cd05799SMatthew G. Knepley   Level: developer
40727cd05799SMatthew G. Knepley 
40737cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40747cd05799SMatthew G. Knepley @*/
40758cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
40768cb4d582SMatthew G. Knepley {
40778cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
40788cb4d582SMatthew G. Knepley 
40798cb4d582SMatthew G. Knepley   PetscFunctionBegin;
40808cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40818cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
40828cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
40838cb4d582SMatthew G. Knepley }
40848cb4d582SMatthew G. Knepley 
40857cd05799SMatthew G. Knepley /*@C
40867cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone 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 . cones - The cone 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 DMPlexGetCones(DM dm, PetscInt *cones[])
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 (cones) *cones = mesh->cones;
4107552f7358SJed Brown   PetscFunctionReturn(0);
4108552f7358SJed Brown }
4109552f7358SJed Brown 
41107cd05799SMatthew G. Knepley /*@C
41117cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
41127cd05799SMatthew G. Knepley 
41137cd05799SMatthew G. Knepley   Not Collective
41147cd05799SMatthew G. Knepley 
41157cd05799SMatthew G. Knepley   Input Parameters:
41167cd05799SMatthew G. Knepley . dm        - The DMPlex object
41177cd05799SMatthew G. Knepley 
41187cd05799SMatthew G. Knepley   Output Parameter:
41197cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
41207cd05799SMatthew G. Knepley 
41217cd05799SMatthew G. Knepley   Level: developer
41227cd05799SMatthew G. Knepley 
41237cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
41247cd05799SMatthew G. Knepley @*/
4125a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4126a6dfd86eSKarl Rupp {
4127552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4128552f7358SJed Brown 
4129552f7358SJed Brown   PetscFunctionBegin;
4130552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4131552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4132552f7358SJed Brown   PetscFunctionReturn(0);
4133552f7358SJed Brown }
4134552f7358SJed Brown 
4135552f7358SJed Brown /******************************** FEM Support **********************************/
4136552f7358SJed Brown 
41379e8305c2SJed Brown /*
41389e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
41399e8305c2SJed Brown  representing a line in the section.
41409e8305c2SJed Brown */
41419e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
41429e8305c2SJed Brown {
41439e8305c2SJed Brown   PetscErrorCode ierr;
41449e8305c2SJed Brown 
41459e8305c2SJed Brown   PetscFunctionBeginHot;
41469e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4147a433471fSStefano Zampini   if (line < 0) {
4148a433471fSStefano Zampini     *k = 0;
4149a433471fSStefano Zampini     *Nc = 0;
4150a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
41519e8305c2SJed Brown     *k = 1;
41529e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
41539e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
41549e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
41559e8305c2SJed Brown     *k = *k / *Nc + 1;
41569e8305c2SJed Brown   }
41579e8305c2SJed Brown   PetscFunctionReturn(0);
41589e8305c2SJed Brown }
41599e8305c2SJed Brown 
4160a4355906SMatthew Knepley /*@
4161bc1eb3faSJed Brown 
4162bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4163bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
41641bb6d2a8SBarry Smith   section provided (or the section of the DM).
4165a4355906SMatthew Knepley 
4166a4355906SMatthew Knepley   Input Parameters:
4167a4355906SMatthew Knepley + dm      - The DM
4168a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4169a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4170a4355906SMatthew Knepley 
4171a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4172a4355906SMatthew Knepley   degree of the basis.
4173a4355906SMatthew Knepley 
4174bc1eb3faSJed Brown   Example:
4175bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4176bc1eb3faSJed Brown .vb
4177bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4178bc1eb3faSJed Brown 
4179bc1eb3faSJed Brown   v4 -- e6 -- v3
4180bc1eb3faSJed Brown   |           |
4181bc1eb3faSJed Brown   e7    c0    e8
4182bc1eb3faSJed Brown   |           |
4183bc1eb3faSJed Brown   v1 -- e5 -- v2
4184bc1eb3faSJed Brown .ve
4185bc1eb3faSJed Brown 
4186bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4187bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4188bc1eb3faSJed Brown .vb
4189bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4190bc1eb3faSJed Brown     v1 -> [4]
4191bc1eb3faSJed Brown     ...
4192bc1eb3faSJed Brown     e5 -> [8, 9]
4193bc1eb3faSJed Brown .ve
4194bc1eb3faSJed Brown 
4195bc1eb3faSJed Brown   which corresponds to the dofs
4196bc1eb3faSJed Brown .vb
4197bc1eb3faSJed Brown     6   10  11  7
4198bc1eb3faSJed Brown     13  2   3   15
4199bc1eb3faSJed Brown     12  0   1   14
4200bc1eb3faSJed Brown     4   8   9   5
4201bc1eb3faSJed Brown .ve
4202bc1eb3faSJed Brown 
4203bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4204bc1eb3faSJed Brown .vb
4205bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4206bc1eb3faSJed Brown .ve
4207bc1eb3faSJed Brown 
4208bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4209bc1eb3faSJed Brown .vb
4210bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4211bc1eb3faSJed Brown .ve
4212bc1eb3faSJed Brown 
4213a4355906SMatthew Knepley   Level: developer
4214a4355906SMatthew Knepley 
42159df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4216a4355906SMatthew Knepley @*/
4217bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
42183194fc30SMatthew G. Knepley {
42197391a63aSMatthew G. Knepley   DMLabel        label;
4220bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
42219e8305c2SJed Brown   PetscBool      vertexchart;
42223194fc30SMatthew G. Knepley   PetscErrorCode ierr;
42233194fc30SMatthew G. Knepley 
42243194fc30SMatthew G. Knepley   PetscFunctionBegin;
42253194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4226a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4227a433471fSStefano Zampini   if (point < 0) {
4228a433471fSStefano Zampini     PetscInt sStart,sEnd;
4229a433471fSStefano Zampini 
4230a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4231a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4232a433471fSStefano Zampini   }
42337391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4234a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4235a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
42367391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
42377391a63aSMatthew G. Knepley   else if  (depth == dim) {
42387391a63aSMatthew G. Knepley     const PetscInt *cone;
42397391a63aSMatthew G. Knepley 
42407391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4241d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4242d4e6627bSStefano Zampini     else if (dim == 3) {
4243d4e6627bSStefano Zampini       const PetscInt *cone2;
4244d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4245d4e6627bSStefano Zampini       eStart = cone2[0];
4246d4e6627bSStefano 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);
4247a433471fSStefano 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);
42489e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
42499e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
42509e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
42519e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
42529e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
42539e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
42549e8305c2SJed Brown   }
42553194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4256bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
4257bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4258bb197d40SJed Brown     PetscInt *perm;
4259bb197d40SJed Brown 
42603194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
42619e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4262bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
42633194fc30SMatthew G. Knepley     }
42643194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
42653194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4266bb197d40SJed Brown       switch (d) {
4267babf31e0SJed Brown       case 1:
42689e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4269babf31e0SJed Brown         /*
4270babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4271babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
4272babf31e0SJed Brown          */
4273babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4274babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4275babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4276babf31e0SJed Brown         foffset = offset;
4277babf31e0SJed Brown         break;
427889eabcffSMatthew G. Knepley       case 2:
42793194fc30SMatthew 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} */
42809e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
42813194fc30SMatthew G. Knepley         /* The SEM order is
42823194fc30SMatthew G. Knepley 
42833194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
428489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
42853194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
42863194fc30SMatthew G. Knepley          */
42873194fc30SMatthew G. Knepley         {
42883194fc30SMatthew G. Knepley           const PetscInt of   = 0;
42893194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
42903194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
42913194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
42923194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
42933194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
42943194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
42953194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
42963194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
42973194fc30SMatthew G. Knepley           PetscInt       o;
42983194fc30SMatthew G. Knepley 
42993194fc30SMatthew G. Knepley           /* bottom */
43003194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
43013194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
43023194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
43033194fc30SMatthew G. Knepley           /* middle */
43043194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
43053194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
43063194fc30SMatthew 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;
43073194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
43083194fc30SMatthew G. Knepley           }
43093194fc30SMatthew G. Knepley           /* top */
43103194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
43113194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
43123194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
43133194fc30SMatthew G. Knepley           foffset = offset;
43143194fc30SMatthew G. Knepley         }
431589eabcffSMatthew G. Knepley         break;
431689eabcffSMatthew G. Knepley       case 3:
431789eabcffSMatthew G. Knepley         /* The original hex closure is
431889eabcffSMatthew G. Knepley 
431989eabcffSMatthew G. Knepley          {c,
432089eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
432189eabcffSMatthew 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,
432289eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
432389eabcffSMatthew G. Knepley          */
43249e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
432589eabcffSMatthew G. Knepley         /* The SEM order is
432689eabcffSMatthew G. Knepley          Bottom Slice
432789eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
432889eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
432989eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
433089eabcffSMatthew G. Knepley 
433189eabcffSMatthew G. Knepley          Middle Slice (j)
433289eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
433389eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
433489eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
433589eabcffSMatthew G. Knepley 
433689eabcffSMatthew G. Knepley          Top Slice
433789eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
433889eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
433989eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
434089eabcffSMatthew G. Knepley          */
434189eabcffSMatthew G. Knepley         {
434289eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
434389eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
434489eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
434589eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
434689eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
434789eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
434889eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
434989eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
435089eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
435189eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
435289eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
435389eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
435489eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
435589eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
435689eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
435789eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
435889eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
435989eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
436089eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
436189eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
436289eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
436389eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
436489eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
436589eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
436689eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
436789eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
436889eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
436989eabcffSMatthew G. Knepley           PetscInt       o, n;
437089eabcffSMatthew G. Knepley 
437189eabcffSMatthew G. Knepley           /* Bottom Slice */
437289eabcffSMatthew G. Knepley           /*   bottom */
437389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
437489eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
437589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
437689eabcffSMatthew G. Knepley           /*   middle */
437789eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
437889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4379316b7f87SMax 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;}
438089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
43813194fc30SMatthew G. Knepley           }
438289eabcffSMatthew G. Knepley           /*   top */
438389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
438489eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
438589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
438689eabcffSMatthew G. Knepley 
438789eabcffSMatthew G. Knepley           /* Middle Slice */
438889eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
438989eabcffSMatthew G. Knepley             /*   bottom */
439089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
439189eabcffSMatthew 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;
439289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
439389eabcffSMatthew G. Knepley             /*   middle */
439489eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
439589eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
439689eabcffSMatthew 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;
439789eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
439889eabcffSMatthew G. Knepley             }
439989eabcffSMatthew G. Knepley             /*   top */
440089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
440189eabcffSMatthew 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;
440289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
440389eabcffSMatthew G. Knepley           }
440489eabcffSMatthew G. Knepley 
440589eabcffSMatthew G. Knepley           /* Top Slice */
440689eabcffSMatthew G. Knepley           /*   bottom */
440789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
440889eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
440989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
441089eabcffSMatthew G. Knepley           /*   middle */
441189eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
441289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
441389eabcffSMatthew 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;
441489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
441589eabcffSMatthew G. Knepley           }
441689eabcffSMatthew G. Knepley           /*   top */
441789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
441889eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
441989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
442089eabcffSMatthew G. Knepley 
442189eabcffSMatthew G. Knepley           foffset = offset;
442289eabcffSMatthew G. Knepley         }
442389eabcffSMatthew G. Knepley         break;
4424bb197d40SJed Brown       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
442589eabcffSMatthew G. Knepley       }
442689eabcffSMatthew G. Knepley     }
442789eabcffSMatthew G. Knepley     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
44283194fc30SMatthew G. Knepley     /* Check permutation */
44293194fc30SMatthew G. Knepley     {
44303194fc30SMatthew G. Knepley       PetscInt *check;
44313194fc30SMatthew G. Knepley 
44323194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
44333194fc30SMatthew 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]);}
44343194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
44353194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
44363194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
44373194fc30SMatthew G. Knepley     }
4438bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
4439bb197d40SJed Brown   }
44403194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
44413194fc30SMatthew G. Knepley }
44423194fc30SMatthew G. Knepley 
4443e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4444e071409bSToby Isaac {
4445e071409bSToby Isaac   PetscDS        prob;
4446e071409bSToby Isaac   PetscInt       depth, Nf, h;
4447e071409bSToby Isaac   DMLabel        label;
4448e071409bSToby Isaac   PetscErrorCode ierr;
4449e071409bSToby Isaac 
4450e071409bSToby Isaac   PetscFunctionBeginHot;
4451e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4452e071409bSToby Isaac   Nf      = prob->Nf;
4453e071409bSToby Isaac   label   = dm->depthLabel;
4454e071409bSToby Isaac   *dspace = NULL;
4455e071409bSToby Isaac   if (field < Nf) {
4456e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4457e071409bSToby Isaac 
4458e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4459e071409bSToby Isaac       PetscDualSpace dsp;
4460e071409bSToby Isaac 
4461e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4462e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4463e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4464e071409bSToby Isaac       h    = depth - 1 - h;
4465e071409bSToby Isaac       if (h) {
4466e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4467e071409bSToby Isaac       } else {
4468e071409bSToby Isaac         *dspace = dsp;
4469e071409bSToby Isaac       }
4470e071409bSToby Isaac     }
4471e071409bSToby Isaac   }
4472e071409bSToby Isaac   PetscFunctionReturn(0);
4473e071409bSToby Isaac }
4474e071409bSToby Isaac 
4475e071409bSToby Isaac 
44761a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4477a6dfd86eSKarl Rupp {
4478552f7358SJed Brown   PetscScalar    *array, *vArray;
4479d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
44801a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4481552f7358SJed Brown   PetscErrorCode  ierr;
4482552f7358SJed Brown 
44831b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
44842a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
44855a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
44865a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
44875a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
44883f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
44899df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
44909df71ca4SMatthew G. Knepley       PetscInt dof;
4491d9917b9dSMatthew G. Knepley 
44929df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
44939df71ca4SMatthew G. Knepley       size += dof;
44949df71ca4SMatthew G. Knepley     }
44959df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
44969df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
44972a3aaacfSMatthew G. Knepley       PetscInt       dof;
44985a1bb5cfSMatthew G. Knepley 
44995a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
45002a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
45015a1bb5cfSMatthew G. Knepley       size += dof;
45025a1bb5cfSMatthew G. Knepley     }
45033f7cbbe7SMatthew G. Knepley     if (!values) {
45043f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
45053f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
45063f7cbbe7SMatthew G. Knepley     }
450769291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4508982e9ed1SMatthew G. Knepley   } else {
4509982e9ed1SMatthew G. Knepley     array = *values;
4510982e9ed1SMatthew G. Knepley   }
45119df71ca4SMatthew G. Knepley   size = 0;
45125a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
45139df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
45149df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
45159df71ca4SMatthew G. Knepley     PetscScalar *varr;
4516d9917b9dSMatthew G. Knepley 
45179df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
45189df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
45199df71ca4SMatthew G. Knepley     varr = &vArray[off];
45201a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
45211a271a75SMatthew G. Knepley       array[offset] = varr[d];
45229df71ca4SMatthew G. Knepley     }
45239df71ca4SMatthew G. Knepley     size += dof;
45249df71ca4SMatthew G. Knepley   }
45259df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
45269df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
45279df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
45285a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
45295a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
45305a1bb5cfSMatthew G. Knepley 
453152ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
45325a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
45335a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
45345a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
45355a1bb5cfSMatthew G. Knepley     if (o >= 0) {
45361a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
45371a271a75SMatthew G. Knepley         array[offset] = varr[d];
45385a1bb5cfSMatthew G. Knepley       }
45395a1bb5cfSMatthew G. Knepley     } else {
45401a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
45411a271a75SMatthew G. Knepley         array[offset] = varr[d];
45425a1bb5cfSMatthew G. Knepley       }
45435a1bb5cfSMatthew G. Knepley     }
45449df71ca4SMatthew G. Knepley     size += dof;
45455a1bb5cfSMatthew G. Knepley   }
45465a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
45479df71ca4SMatthew G. Knepley   if (!*values) {
45485a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
45495a1bb5cfSMatthew G. Knepley     *values = array;
45509df71ca4SMatthew G. Knepley   } else {
45518ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
45528c312ff3SMatthew G. Knepley     *csize = size;
45539df71ca4SMatthew G. Knepley   }
45545a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
45555a1bb5cfSMatthew G. Knepley }
4556d9917b9dSMatthew G. Knepley 
455727f02ce8SMatthew G. Knepley /* Compress out points not in the section */
455827f02ce8SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
455927f02ce8SMatthew G. Knepley {
456027f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
456127f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
456227f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
456327f02ce8SMatthew G. Knepley 
456427f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
456527f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
456627f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
456727f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
456827f02ce8SMatthew G. Knepley       points[q*2]   = r;
456927f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
457027f02ce8SMatthew G. Knepley       ++q;
457127f02ce8SMatthew G. Knepley     }
457227f02ce8SMatthew G. Knepley   }
457327f02ce8SMatthew G. Knepley   *numPoints = q;
457427f02ce8SMatthew G. Knepley   return 0;
457527f02ce8SMatthew G. Knepley }
457627f02ce8SMatthew G. Knepley 
457727f02ce8SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
457827f02ce8SMatthew G. Knepley {
457927f02ce8SMatthew G. Knepley   const PetscInt *cone, *ornt;
458027f02ce8SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
458127f02ce8SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
458227f02ce8SMatthew G. Knepley   PetscErrorCode  ierr;
458327f02ce8SMatthew G. Knepley 
458427f02ce8SMatthew G. Knepley   PetscFunctionBeginHot;
458527f02ce8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
458627f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
458727f02ce8SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
458827f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
458927f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
459027f02ce8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, np*2, MPIU_INT, &pts);CHKERRQ(ierr);
459127f02ce8SMatthew G. Knepley   c    = 0;
459227f02ce8SMatthew G. Knepley   pts[c*2+0] = point;
459327f02ce8SMatthew G. Knepley   pts[c*2+1] = 0;
459427f02ce8SMatthew G. Knepley   ++c;
459527f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
459627f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
459727f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
459827f02ce8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
459927f02ce8SMatthew G. Knepley   if (dim >= 2) {
460027f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
460127f02ce8SMatthew G. Knepley   }
460227f02ce8SMatthew G. Knepley   if (dim >= 3) {
460327f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
460427f02ce8SMatthew G. Knepley       const PetscInt  fpoint = cone[d];
460527f02ce8SMatthew G. Knepley       const PetscInt *fcone;
460627f02ce8SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
460727f02ce8SMatthew G. Knepley 
460827f02ce8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
460927f02ce8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
461027f02ce8SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
461127f02ce8SMatthew G. Knepley         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
461227f02ce8SMatthew G. Knepley         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
461327f02ce8SMatthew G. Knepley       }
461427f02ce8SMatthew G. Knepley     }
461527f02ce8SMatthew G. Knepley   }
461627f02ce8SMatthew G. Knepley   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
461727f02ce8SMatthew G. Knepley   *numPoints = np;
461827f02ce8SMatthew G. Knepley   *points    = pts;
461927f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
462027f02ce8SMatthew G. Knepley }
462127f02ce8SMatthew G. Knepley 
462297529cf3SJed Brown /* Compressed closure does not apply closure permutation */
46231dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4624923c78e0SToby Isaac {
462527f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
4626923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4627923c78e0SToby Isaac   PetscErrorCode ierr;
4628923c78e0SToby Isaac 
4629923c78e0SToby Isaac   PetscFunctionBeginHot;
4630923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
463127f02ce8SMatthew G. Knepley   if (*clPoints) {
4632923c78e0SToby Isaac     PetscInt dof, off;
4633923c78e0SToby Isaac 
4634923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4635923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4636923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4637923c78e0SToby Isaac     np   = dof/2;
4638923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
463927f02ce8SMatthew G. Knepley   } else {
4640665f567fSMatthew G. Knepley     DMPolytopeType ct;
4641665f567fSMatthew G. Knepley 
464227f02ce8SMatthew G. Knepley     /* Do not make the label if it does not exist */
464327f02ce8SMatthew G. Knepley     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
464427f02ce8SMatthew G. Knepley     else                    {ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);}
464527f02ce8SMatthew G. Knepley     switch (ct) {
464627f02ce8SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
464727f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);CHKERRQ(ierr);
464827f02ce8SMatthew G. Knepley         break;
464927f02ce8SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
465027f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);CHKERRQ(ierr);
465127f02ce8SMatthew G. Knepley         break;
465227f02ce8SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
465327f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);CHKERRQ(ierr);
465427f02ce8SMatthew G. Knepley         break;
465527f02ce8SMatthew G. Knepley       default:
465627f02ce8SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
465727f02ce8SMatthew G. Knepley     }
465827f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
4659923c78e0SToby Isaac   }
4660923c78e0SToby Isaac   *numPoints = np;
4661923c78e0SToby Isaac   *points    = pts;
4662923c78e0SToby Isaac   *clp       = cla;
4663923c78e0SToby Isaac   PetscFunctionReturn(0);
4664923c78e0SToby Isaac }
4665923c78e0SToby Isaac 
46661dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4667923c78e0SToby Isaac {
4668923c78e0SToby Isaac   PetscErrorCode ierr;
4669923c78e0SToby Isaac 
4670923c78e0SToby Isaac   PetscFunctionBeginHot;
4671923c78e0SToby Isaac   if (!*clPoints) {
4672923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4673923c78e0SToby Isaac   } else {
4674923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4675923c78e0SToby Isaac   }
4676923c78e0SToby Isaac   *numPoints = 0;
4677923c78e0SToby Isaac   *points    = NULL;
4678923c78e0SToby Isaac   *clSec     = NULL;
4679923c78e0SToby Isaac   *clPoints  = NULL;
4680923c78e0SToby Isaac   *clp       = NULL;
4681923c78e0SToby Isaac   PetscFunctionReturn(0);
4682923c78e0SToby Isaac }
4683923c78e0SToby Isaac 
468497e99dd9SToby 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[])
46851a271a75SMatthew G. Knepley {
46861a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
468797e99dd9SToby Isaac   const PetscInt    **perms = NULL;
468897e99dd9SToby Isaac   const PetscScalar **flips = NULL;
46891a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
46901a271a75SMatthew G. Knepley 
46911a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4692fe02ba77SJed Brown   *size = 0;
469397e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
469497e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
469597e99dd9SToby Isaac     const PetscInt    point = points[2*p];
469697e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
469797e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
46981a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
46991a271a75SMatthew G. Knepley     const PetscScalar *varr;
47001a271a75SMatthew G. Knepley 
47011a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
47021a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
47031a271a75SMatthew G. Knepley     varr = &vArray[off];
470497e99dd9SToby Isaac     if (clperm) {
470597e99dd9SToby Isaac       if (perm) {
470697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
47071a271a75SMatthew G. Knepley       } else {
470897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
470997e99dd9SToby Isaac       }
471097e99dd9SToby Isaac       if (flip) {
471197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
471297e99dd9SToby Isaac       }
471397e99dd9SToby Isaac     } else {
471497e99dd9SToby Isaac       if (perm) {
471597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
471697e99dd9SToby Isaac       } else {
471797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
471897e99dd9SToby Isaac       }
471997e99dd9SToby Isaac       if (flip) {
472097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
47211a271a75SMatthew G. Knepley       }
47221a271a75SMatthew G. Knepley     }
472397e99dd9SToby Isaac     offset += dof;
472497e99dd9SToby Isaac   }
472597e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
47261a271a75SMatthew G. Knepley   *size = offset;
47271a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
47281a271a75SMatthew G. Knepley }
47291a271a75SMatthew G. Knepley 
473097e99dd9SToby 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[])
47311a271a75SMatthew G. Knepley {
47321a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
47331a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
47341a271a75SMatthew G. Knepley 
47351a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4736fe02ba77SJed Brown   *size = 0;
47371a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
473897e99dd9SToby Isaac     PetscInt          p;
473997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
474097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
47411a271a75SMatthew G. Knepley 
474297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
474397e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
474497e99dd9SToby Isaac       const PetscInt    point = points[2*p];
474597e99dd9SToby Isaac       PetscInt          fdof, foff, b;
47461a271a75SMatthew G. Knepley       const PetscScalar *varr;
474797e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
474897e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
47491a271a75SMatthew G. Knepley 
47501a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
47511a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
47521a271a75SMatthew G. Knepley       varr = &vArray[foff];
475397e99dd9SToby Isaac       if (clperm) {
475497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
475597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
475697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
47571a271a75SMatthew G. Knepley       } else {
475897e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
475997e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
476097e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
47611a271a75SMatthew G. Knepley       }
476297e99dd9SToby Isaac       offset += fdof;
47631a271a75SMatthew G. Knepley     }
476497e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
47651a271a75SMatthew G. Knepley   }
47661a271a75SMatthew G. Knepley   *size = offset;
47671a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
47681a271a75SMatthew G. Knepley }
47691a271a75SMatthew G. Knepley 
4770552f7358SJed Brown /*@C
4771552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4772552f7358SJed Brown 
4773552f7358SJed Brown   Not collective
4774552f7358SJed Brown 
4775552f7358SJed Brown   Input Parameters:
4776552f7358SJed Brown + dm - The DM
4777552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4778552f7358SJed Brown . v - The local vector
477922c1ee49SMatthew G. Knepley . point - The point in the DM
478022c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
478122c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4782552f7358SJed Brown 
4783552f7358SJed Brown   Output Parameters:
478422c1ee49SMatthew G. Knepley + csize - The number of values in the closure
478522c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
478622c1ee49SMatthew G. Knepley 
478722c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
478822c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
478922c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
479022c1ee49SMatthew G. Knepley $
479122c1ee49SMatthew G. Knepley $ A typical use could be
479222c1ee49SMatthew G. Knepley $
479322c1ee49SMatthew G. Knepley $  values = NULL;
479422c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
479522c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
479622c1ee49SMatthew G. Knepley $    <Compute on closure>
479722c1ee49SMatthew G. Knepley $  }
479822c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
479922c1ee49SMatthew G. Knepley $
480022c1ee49SMatthew G. Knepley $ or
480122c1ee49SMatthew G. Knepley $
480222c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
480322c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
480422c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
480522c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
480622c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
480722c1ee49SMatthew G. Knepley $      <Compute on closure>
480822c1ee49SMatthew G. Knepley $    }
480922c1ee49SMatthew G. Knepley $  }
481022c1ee49SMatthew G. Knepley $  PetscFree(values);
4811552f7358SJed Brown 
4812552f7358SJed Brown   Fortran Notes:
4813552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4814552f7358SJed Brown   include petsc.h90 in your code.
4815552f7358SJed Brown 
4816552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4817552f7358SJed Brown 
4818552f7358SJed Brown   Level: intermediate
4819552f7358SJed Brown 
4820552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4821552f7358SJed Brown @*/
4822552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4823552f7358SJed Brown {
4824552f7358SJed Brown   PetscSection       clSection;
4825d9917b9dSMatthew G. Knepley   IS                 clPoints;
4826552f7358SJed Brown   PetscInt          *points = NULL;
4827c459fbc1SJed Brown   const PetscInt    *clp, *perm;
4828c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
4829552f7358SJed Brown   PetscErrorCode     ierr;
4830552f7358SJed Brown 
4831d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4832552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483392fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
48341a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
48351a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4836552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4837552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4838552f7358SJed Brown   if (depth == 1 && numFields < 2) {
48391a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4840552f7358SJed Brown     PetscFunctionReturn(0);
4841552f7358SJed Brown   }
48421a271a75SMatthew G. Knepley   /* Get points */
4843923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4844c459fbc1SJed Brown   /* Get sizes */
4845c459fbc1SJed Brown   asize = 0;
4846c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
4847c459fbc1SJed Brown     PetscInt dof;
4848552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
48491a271a75SMatthew G. Knepley     asize += dof;
4850552f7358SJed Brown   }
4851c459fbc1SJed Brown   if (values) {
4852c459fbc1SJed Brown     const PetscScalar *vArray;
4853c459fbc1SJed Brown     PetscInt          size;
4854c459fbc1SJed Brown 
4855c459fbc1SJed Brown     if (*values) {
4856c459fbc1SJed 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);
4857c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
4858c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
48598a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
48601a271a75SMatthew G. Knepley     /* Get values */
4861c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
4862c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
4863c459fbc1SJed Brown     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
48641a271a75SMatthew G. Knepley     /* Cleanup array */
48658a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4866d0f6b257SMatthew G. Knepley   }
4867c459fbc1SJed Brown   if (csize) *csize = asize;
4868c459fbc1SJed Brown   /* Cleanup points */
4869c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4870552f7358SJed Brown   PetscFunctionReturn(0);
4871552f7358SJed Brown }
4872552f7358SJed Brown 
4873e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
4874e5c487bfSMatthew G. Knepley {
4875e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
4876e5c487bfSMatthew G. Knepley   PetscSection       clSection;
4877e5c487bfSMatthew G. Knepley   IS                 clPoints;
4878e5c487bfSMatthew G. Knepley   PetscScalar       *array;
4879e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
4880e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
4881c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
4882c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
4883e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
4884e5c487bfSMatthew G. Knepley 
4885e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
4886e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4887e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
4888e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4889e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4890e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
4891e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
4892e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4893e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
4894e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4895e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
4896e5c487bfSMatthew G. Knepley   }
4897e5c487bfSMatthew G. Knepley   /* Get points */
4898e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4899c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
4900c459fbc1SJed Brown     PetscInt dof;
4901c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
4902c459fbc1SJed Brown     clsize += dof;
4903c459fbc1SJed Brown   }
4904c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
4905e5c487bfSMatthew G. Knepley   /* Filter points */
4906e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
4907e5c487bfSMatthew G. Knepley     PetscInt dep;
4908e5c487bfSMatthew G. Knepley 
4909e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
4910e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
4911e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
4912e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
4913e5c487bfSMatthew G. Knepley     ++Np;
4914e5c487bfSMatthew G. Knepley   }
4915e5c487bfSMatthew G. Knepley   /* Get array */
4916e5c487bfSMatthew G. Knepley   if (!values || !*values) {
4917e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
4918e5c487bfSMatthew G. Knepley 
4919e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
4920e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
4921e5c487bfSMatthew G. Knepley       asize += dof;
4922e5c487bfSMatthew G. Knepley     }
4923e5c487bfSMatthew G. Knepley     if (!values) {
4924e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4925e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
4926e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
4927e5c487bfSMatthew G. Knepley     }
4928e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4929e5c487bfSMatthew G. Knepley   } else {
4930e5c487bfSMatthew G. Knepley     array = *values;
4931e5c487bfSMatthew G. Knepley   }
4932e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
4933e5c487bfSMatthew G. Knepley   /* Get values */
4934e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
4935e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
4936e5c487bfSMatthew G. Knepley   /* Cleanup points */
4937e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4938e5c487bfSMatthew G. Knepley   /* Cleanup array */
4939e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4940e5c487bfSMatthew G. Knepley   if (!*values) {
4941e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
4942e5c487bfSMatthew G. Knepley     *values = array;
4943e5c487bfSMatthew G. Knepley   } else {
4944e5c487bfSMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4945e5c487bfSMatthew G. Knepley     *csize = size;
4946e5c487bfSMatthew G. Knepley   }
4947e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
4948e5c487bfSMatthew G. Knepley }
4949e5c487bfSMatthew G. Knepley 
4950552f7358SJed Brown /*@C
4951552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4952552f7358SJed Brown 
4953552f7358SJed Brown   Not collective
4954552f7358SJed Brown 
4955552f7358SJed Brown   Input Parameters:
4956552f7358SJed Brown + dm - The DM
49570298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4958552f7358SJed Brown . v - The local vector
4959eaf898f9SPatrick Sanan . point - The point in the DM
49600298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4961552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4962552f7358SJed Brown 
496322c1ee49SMatthew 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()
496422c1ee49SMatthew G. Knepley 
49653813dfbdSMatthew G Knepley   Fortran Notes:
49663813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
49673813dfbdSMatthew G Knepley   include petsc.h90 in your code.
49683813dfbdSMatthew G Knepley 
49693813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
49703813dfbdSMatthew G Knepley 
4971552f7358SJed Brown   Level: intermediate
4972552f7358SJed Brown 
4973552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4974552f7358SJed Brown @*/
49757c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4976a6dfd86eSKarl Rupp {
4977552f7358SJed Brown   PetscInt       size = 0;
4978552f7358SJed Brown   PetscErrorCode ierr;
4979552f7358SJed Brown 
4980552f7358SJed Brown   PetscFunctionBegin;
4981552f7358SJed Brown   /* Should work without recalculating size */
498269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4983c9fdaa05SMatthew G. Knepley   *values = NULL;
4984552f7358SJed Brown   PetscFunctionReturn(0);
4985552f7358SJed Brown }
4986552f7358SJed Brown 
4987552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4988552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4989552f7358SJed Brown 
499097e99dd9SToby 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[])
4991552f7358SJed Brown {
4992552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4993552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4994552f7358SJed Brown   PetscScalar    *a;
4995552f7358SJed Brown   PetscInt        off, cind = 0, k;
4996552f7358SJed Brown   PetscErrorCode  ierr;
4997552f7358SJed Brown 
4998552f7358SJed Brown   PetscFunctionBegin;
4999552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5000552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5001552f7358SJed Brown   a    = &array[off];
5002552f7358SJed Brown   if (!cdof || setBC) {
500397e99dd9SToby Isaac     if (clperm) {
500497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
500597e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5006552f7358SJed Brown     } else {
500797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
500897e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5009552f7358SJed Brown     }
5010552f7358SJed Brown   } else {
5011552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
501297e99dd9SToby Isaac     if (clperm) {
501397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
5014552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
501597e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5016552f7358SJed Brown         }
5017552f7358SJed Brown       } else {
5018552f7358SJed Brown         for (k = 0; k < dof; ++k) {
5019552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
502097e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
502197e99dd9SToby Isaac         }
502297e99dd9SToby Isaac       }
502397e99dd9SToby Isaac     } else {
502497e99dd9SToby Isaac       if (perm) {
502597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
502697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
502797e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
502897e99dd9SToby Isaac         }
502997e99dd9SToby Isaac       } else {
503097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
503197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
503297e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
503397e99dd9SToby Isaac         }
5034552f7358SJed Brown       }
5035552f7358SJed Brown     }
5036552f7358SJed Brown   }
5037552f7358SJed Brown   PetscFunctionReturn(0);
5038552f7358SJed Brown }
5039552f7358SJed Brown 
504097e99dd9SToby 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[])
5041a5e93ea8SMatthew G. Knepley {
5042a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5043a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5044a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5045a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5046a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5047a5e93ea8SMatthew G. Knepley 
5048a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5049a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5050a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5051a5e93ea8SMatthew G. Knepley   a    = &array[off];
5052a5e93ea8SMatthew G. Knepley   if (cdof) {
5053a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
505497e99dd9SToby Isaac     if (clperm) {
505597e99dd9SToby Isaac       if (perm) {
5056a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5057a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
505897e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
505997e99dd9SToby Isaac             cind++;
5060a5e93ea8SMatthew G. Knepley           }
5061a5e93ea8SMatthew G. Knepley         }
5062a5e93ea8SMatthew G. Knepley       } else {
5063a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5064a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
506597e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
506697e99dd9SToby Isaac             cind++;
506797e99dd9SToby Isaac           }
506897e99dd9SToby Isaac         }
506997e99dd9SToby Isaac       }
507097e99dd9SToby Isaac     } else {
507197e99dd9SToby Isaac       if (perm) {
507297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
507397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
507497e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
507597e99dd9SToby Isaac             cind++;
507697e99dd9SToby Isaac           }
507797e99dd9SToby Isaac         }
507897e99dd9SToby Isaac       } else {
507997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
508097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
508197e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
508297e99dd9SToby Isaac             cind++;
508397e99dd9SToby Isaac           }
5084a5e93ea8SMatthew G. Knepley         }
5085a5e93ea8SMatthew G. Knepley       }
5086a5e93ea8SMatthew G. Knepley     }
5087a5e93ea8SMatthew G. Knepley   }
5088a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5089a5e93ea8SMatthew G. Knepley }
5090a5e93ea8SMatthew G. Knepley 
509197e99dd9SToby 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[])
5092a6dfd86eSKarl Rupp {
5093552f7358SJed Brown   PetscScalar    *a;
50941a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
50951a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
509697e99dd9SToby Isaac   PetscInt        cind = 0, b;
5097552f7358SJed Brown   PetscErrorCode  ierr;
5098552f7358SJed Brown 
5099552f7358SJed Brown   PetscFunctionBegin;
5100552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5101552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
51021a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
51031a271a75SMatthew G. Knepley   a    = &array[foff];
5104552f7358SJed Brown   if (!fcdof || setBC) {
510597e99dd9SToby Isaac     if (clperm) {
510697e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
510797e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5108552f7358SJed Brown     } else {
510997e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
511097e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5111552f7358SJed Brown     }
5112552f7358SJed Brown   } else {
5113552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
511497e99dd9SToby Isaac     if (clperm) {
511597e99dd9SToby Isaac       if (perm) {
511697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
511797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
511897e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5119552f7358SJed Brown         }
5120552f7358SJed Brown       } else {
512197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
512297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
512397e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
512497e99dd9SToby Isaac         }
512597e99dd9SToby Isaac       }
512697e99dd9SToby Isaac     } else {
512797e99dd9SToby Isaac       if (perm) {
512897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
512997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
513097e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
513197e99dd9SToby Isaac         }
513297e99dd9SToby Isaac       } else {
513397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
513497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
513597e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5136552f7358SJed Brown         }
5137552f7358SJed Brown       }
5138552f7358SJed Brown     }
5139552f7358SJed Brown   }
51401a271a75SMatthew G. Knepley   *offset += fdof;
5141552f7358SJed Brown   PetscFunctionReturn(0);
5142552f7358SJed Brown }
5143552f7358SJed Brown 
5144ba322698SMatthew 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[])
5145a5e93ea8SMatthew G. Knepley {
5146a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
51471a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
51481a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
51495da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
5150ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
5151a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5152a5e93ea8SMatthew G. Knepley 
5153a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
51545da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
5155a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5156a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
51571a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
51581a271a75SMatthew G. Knepley   a    = &array[foff];
5159a5e93ea8SMatthew G. Knepley   if (fcdof) {
5160ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
5161a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
516297e99dd9SToby Isaac     if (clperm) {
516397e99dd9SToby Isaac       if (perm) {
5164ba322698SMatthew G. Knepley         if (comps) {
5165ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5166ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
51675da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5168ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5169ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5170ba322698SMatthew G. Knepley           }
5171ba322698SMatthew G. Knepley         } else {
517297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
517397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
517497e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5175a5e93ea8SMatthew G. Knepley               ++cind;
5176a5e93ea8SMatthew G. Knepley             }
5177a5e93ea8SMatthew G. Knepley           }
5178ba322698SMatthew G. Knepley         }
5179ba322698SMatthew G. Knepley       } else {
5180ba322698SMatthew G. Knepley         if (comps) {
5181ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5182ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
51835da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5184ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5185ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5186ba322698SMatthew G. Knepley           }
5187a5e93ea8SMatthew G. Knepley         } else {
518897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
518997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
519097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
519197e99dd9SToby Isaac               ++cind;
519297e99dd9SToby Isaac             }
519397e99dd9SToby Isaac           }
519497e99dd9SToby Isaac         }
5195ba322698SMatthew G. Knepley       }
519697e99dd9SToby Isaac     } else {
519797e99dd9SToby Isaac       if (perm) {
5198ba322698SMatthew G. Knepley         if (comps) {
5199ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5200ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
52015da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5202ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5203ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5204ba322698SMatthew G. Knepley           }
5205ba322698SMatthew G. Knepley         } else {
520697e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
520797e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
520897e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
520997e99dd9SToby Isaac               ++cind;
521097e99dd9SToby Isaac             }
521197e99dd9SToby Isaac           }
5212ba322698SMatthew G. Knepley         }
5213ba322698SMatthew G. Knepley       } else {
5214ba322698SMatthew G. Knepley         if (comps) {
5215ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5216ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
52175da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5218ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5219ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5220ba322698SMatthew G. Knepley           }
522197e99dd9SToby Isaac         } else {
522297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
522397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
522497e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5225a5e93ea8SMatthew G. Knepley               ++cind;
5226a5e93ea8SMatthew G. Knepley             }
5227a5e93ea8SMatthew G. Knepley           }
5228a5e93ea8SMatthew G. Knepley         }
5229a5e93ea8SMatthew G. Knepley       }
5230a5e93ea8SMatthew G. Knepley     }
5231ba322698SMatthew G. Knepley   }
52321a271a75SMatthew G. Knepley   *offset += fdof;
5233a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5234a5e93ea8SMatthew G. Knepley }
5235a5e93ea8SMatthew G. Knepley 
52368f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5237a6dfd86eSKarl Rupp {
5238552f7358SJed Brown   PetscScalar    *array;
52391b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
52401b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5241552f7358SJed Brown   PetscErrorCode  ierr;
5242552f7358SJed Brown 
52431b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5244b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5245b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5246b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5247b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5248b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5249b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5250b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5251b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5252b6ebb6e6SMatthew G. Knepley 
5253b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5254b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5255b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5256b6ebb6e6SMatthew G. Knepley     {
5257b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5258b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5259b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5260b6ebb6e6SMatthew G. Knepley 
5261b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5262b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5263b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5264b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5265b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5266b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5267b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5268b6ebb6e6SMatthew G. Knepley           }
5269b6ebb6e6SMatthew G. Knepley         } else {
5270b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5271b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5272b6ebb6e6SMatthew G. Knepley           }
5273b6ebb6e6SMatthew G. Knepley         }
5274b6ebb6e6SMatthew G. Knepley       } else {
5275b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5276b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5277b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5278b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5279b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5280b6ebb6e6SMatthew G. Knepley           }
5281b6ebb6e6SMatthew G. Knepley         } else {
5282b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5283b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5284b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5285b6ebb6e6SMatthew G. Knepley           }
5286b6ebb6e6SMatthew G. Knepley         }
5287b6ebb6e6SMatthew G. Knepley       }
5288b6ebb6e6SMatthew G. Knepley     }
5289b6ebb6e6SMatthew G. Knepley   }
5290b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5291b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5292b6ebb6e6SMatthew G. Knepley }
52931b406b76SMatthew G. Knepley 
52941b406b76SMatthew G. Knepley /*@C
52951b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
52961b406b76SMatthew G. Knepley 
52971b406b76SMatthew G. Knepley   Not collective
52981b406b76SMatthew G. Knepley 
52991b406b76SMatthew G. Knepley   Input Parameters:
53001b406b76SMatthew G. Knepley + dm - The DM
53011b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53021b406b76SMatthew G. Knepley . v - The local vector
5303eaf898f9SPatrick Sanan . point - The point in the DM
53041b406b76SMatthew G. Knepley . values - The array of values
530522c1ee49SMatthew 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,
530622c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
53071b406b76SMatthew G. Knepley 
53081b406b76SMatthew G. Knepley   Fortran Notes:
53091b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
53101b406b76SMatthew G. Knepley 
53111b406b76SMatthew G. Knepley   Level: intermediate
53121b406b76SMatthew G. Knepley 
53131b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
53141b406b76SMatthew G. Knepley @*/
53151b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
53161b406b76SMatthew G. Knepley {
53171b406b76SMatthew G. Knepley   PetscSection    clSection;
53181b406b76SMatthew G. Knepley   IS              clPoints;
53191b406b76SMatthew G. Knepley   PetscScalar    *array;
53201b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
532127f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
5322c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
53231b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
53241b406b76SMatthew G. Knepley 
53251a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
53261b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
532792fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
53281a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
53291a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
53301b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
53311b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
53321b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
53338f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
53341b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
53351b406b76SMatthew G. Knepley   }
53361a271a75SMatthew G. Knepley   /* Get points */
5337923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5338c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
5339c459fbc1SJed Brown     PetscInt dof;
5340c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5341c459fbc1SJed Brown     clsize += dof;
5342c459fbc1SJed Brown   }
5343c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
53441a271a75SMatthew G. Knepley   /* Get array */
5345552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
53461a271a75SMatthew G. Knepley   /* Get values */
5347ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
534897e99dd9SToby Isaac     PetscInt offset = 0, f;
5349552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
535097e99dd9SToby Isaac       const PetscInt    **perms = NULL;
535197e99dd9SToby Isaac       const PetscScalar **flips = NULL;
535297e99dd9SToby Isaac 
535397e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5354552f7358SJed Brown       switch (mode) {
5355552f7358SJed Brown       case INSERT_VALUES:
535697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
535797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
535897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
535997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
536097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5361552f7358SJed Brown         } break;
5362552f7358SJed Brown       case INSERT_ALL_VALUES:
536397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
536497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
536597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
536697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
536797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5368552f7358SJed Brown         } break;
5369a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
537097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
537197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
537297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
537397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5374ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5375a5e93ea8SMatthew G. Knepley         } break;
5376552f7358SJed Brown       case ADD_VALUES:
537797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
537897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
537997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
538097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
538197e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5382552f7358SJed Brown         } break;
5383552f7358SJed Brown       case ADD_ALL_VALUES:
538497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
538597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
538697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
538797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
538897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5389552f7358SJed Brown         } break;
5390304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
539197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
539297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
539397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
539497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5395ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5396304ab55fSMatthew G. Knepley         } break;
5397552f7358SJed Brown       default:
5398f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5399552f7358SJed Brown       }
540097e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
54011a271a75SMatthew G. Knepley     }
5402552f7358SJed Brown   } else {
54031a271a75SMatthew G. Knepley     PetscInt dof, off;
540497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
540597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
54061a271a75SMatthew G. Knepley 
540797e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5408552f7358SJed Brown     switch (mode) {
5409552f7358SJed Brown     case INSERT_VALUES:
541097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
541197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
541297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
541397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
541497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
541597e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5416552f7358SJed Brown       } break;
5417552f7358SJed Brown     case INSERT_ALL_VALUES:
541897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
541997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
542097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
542197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
542297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
542397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5424552f7358SJed Brown       } break;
5425a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
542697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
542797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
542897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
542997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
543097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
543197e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5432a5e93ea8SMatthew G. Knepley       } break;
5433552f7358SJed Brown     case ADD_VALUES:
543497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
543597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
543697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
543797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
543897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
543997e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5440552f7358SJed Brown       } break;
5441552f7358SJed Brown     case ADD_ALL_VALUES:
544297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
544397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
544497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
544597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
544697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
544797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5448552f7358SJed Brown       } break;
5449304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
545097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
545197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
545297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
545397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
545497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
545597e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5456304ab55fSMatthew G. Knepley       } break;
5457552f7358SJed Brown     default:
5458f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5459552f7358SJed Brown     }
546097e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5461552f7358SJed Brown   }
54621a271a75SMatthew G. Knepley   /* Cleanup points */
5463923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54641a271a75SMatthew G. Knepley   /* Cleanup array */
5465552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5466552f7358SJed Brown   PetscFunctionReturn(0);
5467552f7358SJed Brown }
5468552f7358SJed Brown 
54695f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
54705f790a90SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
54715f790a90SMatthew G. Knepley {
54725f790a90SMatthew G. Knepley   PetscFunctionBegin;
54735f790a90SMatthew G. Knepley   if (label) {
54745f790a90SMatthew G. Knepley     PetscInt       val, fdof;
54755f790a90SMatthew G. Knepley     PetscErrorCode ierr;
54765f790a90SMatthew G. Knepley 
54775f790a90SMatthew G. Knepley     /* There is a problem with this:
54785f790a90SMatthew 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
54795f790a90SMatthew 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.
54805f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
54815f790a90SMatthew G. Knepley     */
54825f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
54835f790a90SMatthew G. Knepley     if (val < 0) {
54845f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
54855f790a90SMatthew G. Knepley       *offset += fdof;
54865f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
54875f790a90SMatthew G. Knepley     }
54885f790a90SMatthew G. Knepley   }
54895f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
54905f790a90SMatthew G. Knepley }
54915f790a90SMatthew G. Knepley 
549297529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
54935f790a90SMatthew 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)
5494e07394fbSMatthew G. Knepley {
5495e07394fbSMatthew G. Knepley   PetscSection      clSection;
5496e07394fbSMatthew G. Knepley   IS                clPoints;
5497e07394fbSMatthew G. Knepley   PetscScalar       *array;
5498e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
549997529cf3SJed Brown   const PetscInt    *clp;
5500e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
550197e99dd9SToby Isaac   PetscInt          offset = 0, f;
5502e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5503e07394fbSMatthew G. Knepley 
5504e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5505e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
550692fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5507e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5508e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5509e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5510e07394fbSMatthew G. Knepley   /* Get points */
5511923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5512e07394fbSMatthew G. Knepley   /* Get array */
5513e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5514e07394fbSMatthew G. Knepley   /* Get values */
5515e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
551697e99dd9SToby Isaac     const PetscInt    **perms = NULL;
551797e99dd9SToby Isaac     const PetscScalar **flips = NULL;
551897e99dd9SToby Isaac 
5519e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5520e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5521e07394fbSMatthew G. Knepley         PetscInt fdof;
5522e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5523e07394fbSMatthew G. Knepley         offset += fdof;
5524e07394fbSMatthew G. Knepley       }
5525e07394fbSMatthew G. Knepley       continue;
5526e07394fbSMatthew G. Knepley     }
552797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5528e07394fbSMatthew G. Knepley     switch (mode) {
5529e07394fbSMatthew G. Knepley     case INSERT_VALUES:
553097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
553197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
553297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
553397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55345f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
553597529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5536e07394fbSMatthew G. Knepley       } break;
5537e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
553897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
553997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
554097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
554197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55425f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
554397529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5544e07394fbSMatthew G. Knepley       } break;
5545e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
554697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
554797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
554897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
554997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55505f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
555197529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5552e07394fbSMatthew G. Knepley       } break;
5553e07394fbSMatthew G. Knepley     case ADD_VALUES:
555497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
555597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
555697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
555797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55585f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
555997529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5560e07394fbSMatthew G. Knepley       } break;
5561e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
556297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
556397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
556497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
556597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
55665f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
556797529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5568e07394fbSMatthew G. Knepley       } break;
5569e07394fbSMatthew G. Knepley     default:
5570f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5571e07394fbSMatthew G. Knepley     }
557297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5573e07394fbSMatthew G. Knepley   }
5574e07394fbSMatthew G. Knepley   /* Cleanup points */
5575923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5576e07394fbSMatthew G. Knepley   /* Cleanup array */
5577e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5578e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5579e07394fbSMatthew G. Knepley }
5580e07394fbSMatthew G. Knepley 
55817cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5582552f7358SJed Brown {
5583552f7358SJed Brown   PetscMPIInt    rank;
5584552f7358SJed Brown   PetscInt       i, j;
5585552f7358SJed Brown   PetscErrorCode ierr;
5586552f7358SJed Brown 
5587552f7358SJed Brown   PetscFunctionBegin;
5588ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr);
5589eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5590e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5591e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5592b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5593557cf195SMatthew G. Knepley   if (!values) PetscFunctionReturn(0);
5594b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5595e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5596b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5597519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
55987eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5599552f7358SJed Brown #else
5600b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5601552f7358SJed Brown #endif
5602552f7358SJed Brown     }
560377a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5604552f7358SJed Brown   }
5605552f7358SJed Brown   PetscFunctionReturn(0);
5606552f7358SJed Brown }
5607552f7358SJed Brown 
560805586334SMatthew G. Knepley /*
560905586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
561005586334SMatthew G. Knepley 
561105586334SMatthew G. Knepley   Input Parameters:
561205586334SMatthew G. Knepley + section - The section for this data layout
561336fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
561405586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
561505586334SMatthew G. Knepley . off     - The global offset of this point
561605586334SMatthew G. Knepley . loff    - The local offset of each field
561705586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
561805586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
561905586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
562005586334SMatthew G. Knepley 
562105586334SMatthew G. Knepley   Output Parameter:
562205586334SMatthew G. Knepley . indices - Indices for dofs on this point
562305586334SMatthew G. Knepley 
562405586334SMatthew G. Knepley   Level: developer
562505586334SMatthew G. Knepley 
562605586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
562705586334SMatthew G. Knepley */
562836fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5629a6dfd86eSKarl Rupp {
5630e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5631552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5632552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5633552f7358SJed Brown   PetscInt        cind = 0, k;
5634552f7358SJed Brown   PetscErrorCode  ierr;
5635552f7358SJed Brown 
5636552f7358SJed Brown   PetscFunctionBegin;
563736fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5638552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5639552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5640552f7358SJed Brown   if (!cdof || setBC) {
564105586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
564205586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
564305586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
564405586334SMatthew G. Knepley 
564505586334SMatthew G. Knepley       indices[ind] = off + k;
5646552f7358SJed Brown     }
5647552f7358SJed Brown   } else {
5648552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
56494acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
565005586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
565105586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
565205586334SMatthew G. Knepley 
56534acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
56544acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
565505586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
56564acb8e1eSToby Isaac         ++cind;
56574acb8e1eSToby Isaac       } else {
565836fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
5659552f7358SJed Brown       }
5660552f7358SJed Brown     }
5661552f7358SJed Brown   }
5662e6ccafaeSMatthew G Knepley   *loff += dof;
5663552f7358SJed Brown   PetscFunctionReturn(0);
5664552f7358SJed Brown }
5665552f7358SJed Brown 
56667e29afd2SMatthew G. Knepley /*
566736fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
56687e29afd2SMatthew G. Knepley 
566936fa2b79SJed Brown  Input Parameters:
567036fa2b79SJed Brown + section - a section (global or local)
567136fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
567236fa2b79SJed Brown . point - point within section
567336fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
567436fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
567536fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
567636fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
567736fa2b79SJed Brown . permsoff - offset
567836fa2b79SJed Brown - indperm - index permutation
567936fa2b79SJed Brown 
568036fa2b79SJed Brown  Output Parameter:
568136fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
568236fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
568336fa2b79SJed Brown 
568436fa2b79SJed Brown  Notes:
568536fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
568636fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
568736fa2b79SJed Brown  in the local vector.
568836fa2b79SJed Brown 
568936fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
569036fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
569136fa2b79SJed Brown 
569236fa2b79SJed Brown  Developer Note:
569336fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
569436fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
569536fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
569636fa2b79SJed Brown 
569736fa2b79SJed Brown  Example:
569836fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
569936fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
570036fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
570136fa2b79SJed 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.
570236fa2b79SJed Brown 
570336fa2b79SJed Brown  Level: developer
57047e29afd2SMatthew G. Knepley */
570536fa2b79SJed 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[])
5706a6dfd86eSKarl Rupp {
5707552f7358SJed Brown   PetscInt       numFields, foff, f;
5708552f7358SJed Brown   PetscErrorCode ierr;
5709552f7358SJed Brown 
5710552f7358SJed Brown   PetscFunctionBegin;
571136fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5712552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5713552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
57144acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5715552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
57164acb8e1eSToby Isaac     PetscInt        cind = 0, b;
57174acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5718552f7358SJed Brown 
5719552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5720552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5721552f7358SJed Brown     if (!cfdof || setBC) {
572205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
572305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
572405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
572505586334SMatthew G. Knepley 
572605586334SMatthew G. Knepley         indices[ind] = off+foff+b;
572705586334SMatthew G. Knepley       }
5728552f7358SJed Brown     } else {
5729552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
573005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
573105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
573205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
573305586334SMatthew G. Knepley 
57344acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
573505586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5736552f7358SJed Brown           ++cind;
5737552f7358SJed Brown         } else {
573836fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
5739552f7358SJed Brown         }
5740552f7358SJed Brown       }
5741552f7358SJed Brown     }
574236fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
5743552f7358SJed Brown     foffs[f] += fdof;
5744552f7358SJed Brown   }
5745552f7358SJed Brown   PetscFunctionReturn(0);
5746552f7358SJed Brown }
5747552f7358SJed Brown 
57487e29afd2SMatthew G. Knepley /*
57497e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
57507e29afd2SMatthew G. Knepley 
57517e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
5752645102dcSJed Brown 
5753645102dcSJed Brown  Notes:
5754645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
5755645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
57567e29afd2SMatthew G. Knepley */
5757645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
57587e29afd2SMatthew G. Knepley {
57597e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
57607e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
57617e29afd2SMatthew G. Knepley 
57627e29afd2SMatthew G. Knepley   PetscFunctionBegin;
57637e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
57647e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
57657e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
57667e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
57677e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
57687e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
57697e29afd2SMatthew G. Knepley 
57707e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
57717e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
57727e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
5773645102dcSJed Brown     if (!cfdof) {
577405586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
577505586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
577605586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
577705586334SMatthew G. Knepley 
577805586334SMatthew G. Knepley         indices[ind] = foff+b;
577905586334SMatthew G. Knepley       }
57807e29afd2SMatthew G. Knepley     } else {
57817e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
578205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
578305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
578405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
578505586334SMatthew G. Knepley 
57867e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
578705586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
57887e29afd2SMatthew G. Knepley           ++cind;
57897e29afd2SMatthew G. Knepley         } else {
579005586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
57917e29afd2SMatthew G. Knepley         }
57927e29afd2SMatthew G. Knepley       }
57937e29afd2SMatthew G. Knepley     }
57947e29afd2SMatthew G. Knepley     foffs[f] += fdof;
57957e29afd2SMatthew G. Knepley   }
57967e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
57977e29afd2SMatthew G. Knepley }
57987e29afd2SMatthew G. Knepley 
57994acb8e1eSToby 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)
5800d3d1a6afSToby Isaac {
5801d3d1a6afSToby Isaac   Mat             cMat;
5802d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5803d3d1a6afSToby Isaac   IS              aIS;
5804d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5805d3d1a6afSToby Isaac   const PetscInt  *anchors;
5806e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5807d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5808d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5809d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5810d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5811d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5812d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5813d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
58146ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5815d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5816d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5817d3d1a6afSToby Isaac 
5818d3d1a6afSToby Isaac   PetscFunctionBegin;
5819d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5820d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5821d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5822d3d1a6afSToby Isaac 
5823a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5824d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5825d3d1a6afSToby Isaac   if (aSec) {
5826580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5827d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5828d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5829d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5830d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5831d3d1a6afSToby Isaac      * into the global matrix anyway) */
5832d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5833d3d1a6afSToby Isaac       PetscInt b    = points[p];
58344b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5835d3d1a6afSToby Isaac 
58364b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
58374b2f2278SToby Isaac       if (!bSecDof) {
58384b2f2278SToby Isaac         continue;
58394b2f2278SToby Isaac       }
5840d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5841d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5842d3d1a6afSToby Isaac       }
5843d3d1a6afSToby Isaac       if (bDof) {
5844d3d1a6afSToby Isaac         /* this point is constrained */
5845d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5846d3d1a6afSToby Isaac         PetscInt bOff, q;
5847d3d1a6afSToby Isaac 
5848d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5849d3d1a6afSToby Isaac         newNumPoints  += bDof;
5850d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5851d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5852d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5853d3d1a6afSToby Isaac           PetscInt aDof;
5854d3d1a6afSToby Isaac 
5855d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5856d3d1a6afSToby Isaac           newNumIndices += aDof;
5857d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5858d3d1a6afSToby Isaac             PetscInt fDof;
5859d3d1a6afSToby Isaac 
5860d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5861d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5862d3d1a6afSToby Isaac           }
5863d3d1a6afSToby Isaac         }
5864d3d1a6afSToby Isaac       }
5865d3d1a6afSToby Isaac       else {
5866d3d1a6afSToby Isaac         /* this point is not constrained */
5867d3d1a6afSToby Isaac         newNumPoints++;
58684b2f2278SToby Isaac         newNumIndices += bSecDof;
5869d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5870d3d1a6afSToby Isaac           PetscInt fDof;
5871d3d1a6afSToby Isaac 
5872d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5873d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5874d3d1a6afSToby Isaac         }
5875d3d1a6afSToby Isaac       }
5876d3d1a6afSToby Isaac     }
5877d3d1a6afSToby Isaac   }
5878d3d1a6afSToby Isaac   if (!anyConstrained) {
587972b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
588072b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
588172b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
588272b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
588372b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5884d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5885d3d1a6afSToby Isaac   }
5886d3d1a6afSToby Isaac 
58876ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
58886ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
58896ecaa68aSToby Isaac 
5890f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5891d3d1a6afSToby Isaac 
58926ecaa68aSToby Isaac   if (!outPoints && !outValues) {
58936ecaa68aSToby Isaac     if (offsets) {
58946ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
58956ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
58966ecaa68aSToby Isaac       }
58976ecaa68aSToby Isaac     }
58986ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
58996ecaa68aSToby Isaac     PetscFunctionReturn(0);
59006ecaa68aSToby Isaac   }
59016ecaa68aSToby Isaac 
5902f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5903d3d1a6afSToby Isaac 
5904f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5905d3d1a6afSToby Isaac 
5906d3d1a6afSToby Isaac   /* workspaces */
5907d3d1a6afSToby Isaac   if (numFields) {
5908d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
590969291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
591069291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5911d3d1a6afSToby Isaac     }
5912d3d1a6afSToby Isaac   }
5913d3d1a6afSToby Isaac   else {
591469291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
591569291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5916d3d1a6afSToby Isaac   }
5917d3d1a6afSToby Isaac 
5918d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5919d3d1a6afSToby Isaac   if (numFields) {
59204b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
59214b2f2278SToby Isaac 
5922d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5923d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
59244b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5925d3d1a6afSToby Isaac 
59264b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
59274b2f2278SToby Isaac       if (!bSecDof) {
59284b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
59294b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
59304b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
59314b2f2278SToby Isaac         }
59324b2f2278SToby Isaac         continue;
59334b2f2278SToby Isaac       }
5934d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5935d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5936d3d1a6afSToby Isaac       }
5937d3d1a6afSToby Isaac       if (bDof) {
5938d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5939d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5940d3d1a6afSToby Isaac 
5941d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5942d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5943d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5944d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5945d3d1a6afSToby Isaac             PetscInt aFDof;
5946d3d1a6afSToby Isaac 
5947d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5948d3d1a6afSToby Isaac             allFDof += aFDof;
5949d3d1a6afSToby Isaac           }
5950d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5951d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5952d3d1a6afSToby Isaac         }
5953d3d1a6afSToby Isaac       }
5954d3d1a6afSToby Isaac       else {
5955d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5956d3d1a6afSToby Isaac           PetscInt fDof;
5957d3d1a6afSToby Isaac 
5958d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5959d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5960d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5961d3d1a6afSToby Isaac         }
5962d3d1a6afSToby Isaac       }
5963d3d1a6afSToby Isaac     }
59644b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
59654b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
59664b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5967d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5968d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5969d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5970d3d1a6afSToby Isaac       }
597119f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
597219f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
597369291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5974d3d1a6afSToby Isaac     }
5975d3d1a6afSToby Isaac   }
5976d3d1a6afSToby Isaac   else {
5977d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5978d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
59794b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5980d3d1a6afSToby Isaac 
59814b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
59824b2f2278SToby Isaac       if (!bSecDof) {
59834b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
59844b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
59854b2f2278SToby Isaac         continue;
59864b2f2278SToby Isaac       }
5987d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5988d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5989d3d1a6afSToby Isaac       }
5990d3d1a6afSToby Isaac       if (bDof) {
59914b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5992d3d1a6afSToby Isaac 
5993d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5994d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5995d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5996d3d1a6afSToby Isaac 
5997d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5998d3d1a6afSToby Isaac           allDof += aDof;
5999d3d1a6afSToby Isaac         }
6000d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
60014b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
6002d3d1a6afSToby Isaac       }
6003d3d1a6afSToby Isaac       else {
60044b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
6005d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
6006d3d1a6afSToby Isaac       }
6007d3d1a6afSToby Isaac     }
6008d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
6009d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
6010d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6011d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6012d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6013d3d1a6afSToby Isaac     }
601469291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
6015d3d1a6afSToby Isaac   }
6016d3d1a6afSToby Isaac 
60176ecaa68aSToby Isaac   /* output arrays */
601869291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
60196ecaa68aSToby Isaac 
6020d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
6021d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
6022d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
602369291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
602469291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
6025d3d1a6afSToby Isaac   if (numFields) {
6026d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6027d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6028d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
60294b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6030d3d1a6afSToby Isaac 
60314b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
60324b2f2278SToby Isaac       if (!bSecDof) {
60334b2f2278SToby Isaac         continue;
60344b2f2278SToby Isaac       }
6035d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6036d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6037d3d1a6afSToby Isaac       }
6038d3d1a6afSToby Isaac       if (bDof) {
6039d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6040d3d1a6afSToby Isaac 
6041d3d1a6afSToby Isaac         fStart[0] = 0;
6042d3d1a6afSToby Isaac         fEnd[0]   = 0;
6043d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6044d3d1a6afSToby Isaac           PetscInt fDof;
6045d3d1a6afSToby Isaac 
6046d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6047d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6048d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6049d3d1a6afSToby Isaac         }
6050d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
605136fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6052d3d1a6afSToby Isaac 
6053d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6054d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6055d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6056d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6057d3d1a6afSToby Isaac 
6058d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6059d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6060d3d1a6afSToby Isaac         }
6061d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6062d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6063d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6064d3d1a6afSToby Isaac 
6065d3d1a6afSToby 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 */
6066d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6067d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6068302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
606936fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
6070d3d1a6afSToby Isaac         }
6071d3d1a6afSToby Isaac         newP += bDof;
6072d3d1a6afSToby Isaac 
60736ecaa68aSToby Isaac         if (outValues) {
6074d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
6075d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
6076d3d1a6afSToby 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);
6077d3d1a6afSToby Isaac           }
6078d3d1a6afSToby Isaac         }
60796ecaa68aSToby Isaac       }
6080d3d1a6afSToby Isaac       else {
6081d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6082d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6083d3d1a6afSToby Isaac         newP++;
6084d3d1a6afSToby Isaac       }
6085d3d1a6afSToby Isaac     }
6086d3d1a6afSToby Isaac   } else {
6087d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6088d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6089d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
60904b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6091d3d1a6afSToby Isaac 
60924b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
60934b2f2278SToby Isaac       if (!bSecDof) {
60944b2f2278SToby Isaac         continue;
60954b2f2278SToby Isaac       }
6096d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6097d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6098d3d1a6afSToby Isaac       }
6099d3d1a6afSToby Isaac       if (bDof) {
6100d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
6101d3d1a6afSToby Isaac 
6102d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
610336fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
6104d3d1a6afSToby Isaac 
6105d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
6106d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6107d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6108d3d1a6afSToby Isaac 
6109d3d1a6afSToby 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 */
6110d3d1a6afSToby Isaac 
6111d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6112d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6113302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
611436fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
6115d3d1a6afSToby Isaac         }
6116d3d1a6afSToby Isaac         newP += bDof;
6117d3d1a6afSToby Isaac 
6118d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
61196ecaa68aSToby Isaac         if (outValues) {
6120d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
6121d3d1a6afSToby Isaac         }
61226ecaa68aSToby Isaac       }
6123d3d1a6afSToby Isaac       else {
6124d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6125d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6126d3d1a6afSToby Isaac         newP++;
6127d3d1a6afSToby Isaac       }
6128d3d1a6afSToby Isaac     }
6129d3d1a6afSToby Isaac   }
6130d3d1a6afSToby Isaac 
61316ecaa68aSToby Isaac   if (outValues) {
613269291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
6133580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
6134d3d1a6afSToby Isaac     /* multiply constraints on the right */
6135d3d1a6afSToby Isaac     if (numFields) {
6136d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
6137d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
6138d3d1a6afSToby Isaac 
6139d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6140d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
6141d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6142d3d1a6afSToby Isaac           PetscInt c, r, k;
6143d3d1a6afSToby Isaac           PetscInt dof;
6144d3d1a6afSToby Isaac 
6145d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
61464b2f2278SToby Isaac           if (!dof) {
61474b2f2278SToby Isaac             continue;
61484b2f2278SToby Isaac           }
6149d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6150d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6151d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
6152d3d1a6afSToby Isaac 
6153d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6154d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
6155d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
61564acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6157d3d1a6afSToby Isaac                 }
6158d3d1a6afSToby Isaac               }
6159d3d1a6afSToby Isaac             }
6160d3d1a6afSToby Isaac           }
6161d3d1a6afSToby Isaac           else {
6162d3d1a6afSToby Isaac             /* copy this column as is */
6163d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6164d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
6165d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6166d3d1a6afSToby Isaac               }
6167d3d1a6afSToby Isaac             }
6168d3d1a6afSToby Isaac           }
6169d3d1a6afSToby Isaac           oldOff += dof;
6170d3d1a6afSToby Isaac         }
6171d3d1a6afSToby Isaac       }
6172d3d1a6afSToby Isaac     }
6173d3d1a6afSToby Isaac     else {
6174d3d1a6afSToby Isaac       PetscInt oldOff = 0;
6175d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6176d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
6177d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
6178d3d1a6afSToby Isaac         PetscInt c, r, k;
6179d3d1a6afSToby Isaac         PetscInt dof;
6180d3d1a6afSToby Isaac 
6181d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
61824b2f2278SToby Isaac         if (!dof) {
61834b2f2278SToby Isaac           continue;
61844b2f2278SToby Isaac         }
6185d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6186d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6187d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
6188d3d1a6afSToby Isaac 
6189d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6190d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
6191d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
6192d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6193d3d1a6afSToby Isaac               }
6194d3d1a6afSToby Isaac             }
6195d3d1a6afSToby Isaac           }
6196d3d1a6afSToby Isaac         }
6197d3d1a6afSToby Isaac         else {
6198d3d1a6afSToby Isaac           /* copy this column as is */
6199d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6200d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
6201d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6202d3d1a6afSToby Isaac             }
6203d3d1a6afSToby Isaac           }
6204d3d1a6afSToby Isaac         }
6205d3d1a6afSToby Isaac         oldOff += dof;
6206d3d1a6afSToby Isaac       }
6207d3d1a6afSToby Isaac     }
6208d3d1a6afSToby Isaac 
62096ecaa68aSToby Isaac     if (multiplyLeft) {
621069291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
6211580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
6212d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
6213d3d1a6afSToby Isaac       if (numFields) {
6214d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6215d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
6216d3d1a6afSToby Isaac 
6217d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
6218d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
6219d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
6220d3d1a6afSToby Isaac             PetscInt c, r, k;
6221d3d1a6afSToby Isaac             PetscInt dof;
6222d3d1a6afSToby Isaac 
6223d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
6224d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6225d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
6226d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
6227d3d1a6afSToby Isaac 
6228d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
6229d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6230d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
6231d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6232d3d1a6afSToby Isaac                   }
6233d3d1a6afSToby Isaac                 }
6234d3d1a6afSToby Isaac               }
6235d3d1a6afSToby Isaac             }
6236d3d1a6afSToby Isaac             else {
6237d3d1a6afSToby Isaac               /* copy this row as is */
6238d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6239d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6240d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6241d3d1a6afSToby Isaac                 }
6242d3d1a6afSToby Isaac               }
6243d3d1a6afSToby Isaac             }
6244d3d1a6afSToby Isaac             oldOff += dof;
6245d3d1a6afSToby Isaac           }
6246d3d1a6afSToby Isaac         }
6247d3d1a6afSToby Isaac       }
6248d3d1a6afSToby Isaac       else {
6249d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6250d3d1a6afSToby Isaac 
6251d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6252d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6253d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6254d3d1a6afSToby Isaac           PetscInt c, r, k;
6255d3d1a6afSToby Isaac           PetscInt dof;
6256d3d1a6afSToby Isaac 
6257d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6258d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6259d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6260d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6261d3d1a6afSToby Isaac 
6262d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6263d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6264d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6265d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6266d3d1a6afSToby Isaac                 }
6267d3d1a6afSToby Isaac               }
6268d3d1a6afSToby Isaac             }
6269d3d1a6afSToby Isaac           }
6270d3d1a6afSToby Isaac           else {
6271d3d1a6afSToby Isaac             /* copy this row as is */
62729fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6273d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6274d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6275d3d1a6afSToby Isaac               }
6276d3d1a6afSToby Isaac             }
6277d3d1a6afSToby Isaac           }
6278d3d1a6afSToby Isaac           oldOff += dof;
6279d3d1a6afSToby Isaac         }
6280d3d1a6afSToby Isaac       }
6281d3d1a6afSToby Isaac 
628269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
62836ecaa68aSToby Isaac     }
62846ecaa68aSToby Isaac     else {
62856ecaa68aSToby Isaac       newValues = tmpValues;
62866ecaa68aSToby Isaac     }
62876ecaa68aSToby Isaac   }
62886ecaa68aSToby Isaac 
6289d3d1a6afSToby Isaac   /* clean up */
629069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
629169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
62926ecaa68aSToby Isaac 
6293d3d1a6afSToby Isaac   if (numFields) {
6294d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
629569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
629669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
629769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6298d3d1a6afSToby Isaac     }
6299d3d1a6afSToby Isaac   }
6300d3d1a6afSToby Isaac   else {
630169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
630269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
630369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6304d3d1a6afSToby Isaac   }
6305d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6306d3d1a6afSToby Isaac 
6307d3d1a6afSToby Isaac   /* output */
63086ecaa68aSToby Isaac   if (outPoints) {
6309d3d1a6afSToby Isaac     *outPoints = newPoints;
63106ecaa68aSToby Isaac   }
63116ecaa68aSToby Isaac   else {
631269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
63136ecaa68aSToby Isaac   }
631431620726SToby Isaac   if (outValues) {
6315d3d1a6afSToby Isaac     *outValues = newValues;
63166ecaa68aSToby Isaac   }
63176ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6318d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6319d3d1a6afSToby Isaac   }
6320d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6321d3d1a6afSToby Isaac }
6322d3d1a6afSToby Isaac 
63234a1e0b3eSMatthew G. Knepley /*@C
632471f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
63257cd05799SMatthew G. Knepley 
63267cd05799SMatthew G. Knepley   Not collective
63277cd05799SMatthew G. Knepley 
63287cd05799SMatthew G. Knepley   Input Parameters:
63297cd05799SMatthew G. Knepley + dm         - The DM
633071f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
633171f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
633271f0bbf9SMatthew G. Knepley . point      - The point defining the closure
633371f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
63347cd05799SMatthew G. Knepley 
633571f0bbf9SMatthew G. Knepley   Output Parameters:
633671f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
633771f0bbf9SMatthew G. Knepley . indices    - The dof indices
633871f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
633971f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
63407cd05799SMatthew G. Knepley 
634136fa2b79SJed Brown   Notes:
634236fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
634336fa2b79SJed Brown 
634436fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
634536fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
634636fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
634736fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
634836fa2b79SJed Brown   indices (with the above semantics) are implied.
63497cd05799SMatthew G. Knepley 
63507cd05799SMatthew G. Knepley   Level: advanced
63517cd05799SMatthew G. Knepley 
635236fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
63534a1e0b3eSMatthew G. Knepley @*/
635471f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
635571f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
63567773e69fSMatthew G. Knepley {
635771f0bbf9SMatthew G. Knepley   /* Closure ordering */
63587773e69fSMatthew G. Knepley   PetscSection        clSection;
63597773e69fSMatthew G. Knepley   IS                  clPoints;
636071f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
636171f0bbf9SMatthew G. Knepley   PetscInt           *points;
636271f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
636371f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
63644acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
636571f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
636671f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
636771f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
636871f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
636971f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
637071f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
637171f0bbf9SMatthew G. Knepley 
637271f0bbf9SMatthew G. Knepley   PetscInt           *idx;
637371f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
637471f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
63757773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
63767773e69fSMatthew G. Knepley 
637771f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
63787773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63797773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
638036fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
638171f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
638271f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
638371f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
638471f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
63857773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
63867773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6387580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
638871f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
638971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
6390c459fbc1SJed Brown   if (useClPerm) {
6391c459fbc1SJed Brown     PetscInt depth, clsize;
6392c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
6393c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
6394c459fbc1SJed Brown       PetscInt dof;
6395c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6396c459fbc1SJed Brown       clsize += dof;
6397c459fbc1SJed Brown     }
6398c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
6399c459fbc1SJed Brown   }
640071f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
640171f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
64027773e69fSMatthew G. Knepley     PetscInt dof, fdof;
64037773e69fSMatthew G. Knepley 
64047773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
64057773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
64067773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
64077773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
64087773e69fSMatthew G. Knepley     }
640971f0bbf9SMatthew G. Knepley     Ni += dof;
64107773e69fSMatthew G. Knepley   }
64117773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
641271f0bbf9SMatthew 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);
641371f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
641471f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
641571f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
641671f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
641771f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
641871f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
641971f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
64206ecaa68aSToby Isaac 
642171f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
642271f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
642371f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
642471f0bbf9SMatthew G. Knepley 
642571f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
642671f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
642771f0bbf9SMatthew G. Knepley         if (flip) {
642871f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
642971f0bbf9SMatthew G. Knepley 
643071f0bbf9SMatthew G. Knepley           if (!valCopy) {
643171f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
643271f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
643371f0bbf9SMatthew G. Knepley             *values = valCopy;
643471f0bbf9SMatthew G. Knepley           }
643571f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
643671f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
643771f0bbf9SMatthew G. Knepley 
643871f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
643971f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
644071f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
64416ecaa68aSToby Isaac             }
64426ecaa68aSToby Isaac           }
644371f0bbf9SMatthew G. Knepley         }
644471f0bbf9SMatthew G. Knepley         foffset += fdof;
644571f0bbf9SMatthew G. Knepley       }
644671f0bbf9SMatthew G. Knepley     }
644771f0bbf9SMatthew G. Knepley   }
644871f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
644971f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
645071f0bbf9SMatthew G. Knepley   if (NclC) {
645171f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
645271f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
645371f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
645471f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
645571f0bbf9SMatthew G. Knepley     }
645671f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
645771f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
645871f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
645971f0bbf9SMatthew G. Knepley     }
646071f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
646171f0bbf9SMatthew G. Knepley     Ncl     = NclC;
646271f0bbf9SMatthew G. Knepley     Ni      = NiC;
646371f0bbf9SMatthew G. Knepley     points  = pointsC;
646471f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
646571f0bbf9SMatthew G. Knepley   }
646671f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
646771f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
646871f0bbf9SMatthew G. Knepley   if (Nf) {
646971f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
647071f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
647171f0bbf9SMatthew G. Knepley 
647271f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
647371f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
647471f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
647571f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
647671f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
647771f0bbf9SMatthew G. Knepley 
647871f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
64797773e69fSMatthew G. Knepley       }
64807773e69fSMatthew G. Knepley     } else {
648171f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
648271f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
648371f0bbf9SMatthew G. Knepley 
648471f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
648571f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
648671f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
648771f0bbf9SMatthew G. Knepley          * global section. */
648871f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
648971f0bbf9SMatthew G. Knepley       }
649071f0bbf9SMatthew G. Knepley     }
649171f0bbf9SMatthew G. Knepley   } else {
649271f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
649371f0bbf9SMatthew G. Knepley 
649471f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
649571f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
64964acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
64974acb8e1eSToby Isaac 
649871f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
649971f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
650071f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
650171f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
65027773e69fSMatthew G. Knepley     }
65037773e69fSMatthew G. Knepley   }
650471f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
650571f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
650671f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
650771f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
65084acb8e1eSToby Isaac   }
650971f0bbf9SMatthew G. Knepley   if (NclC) {
651071f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
65117773e69fSMatthew G. Knepley   } else {
651271f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
65137773e69fSMatthew G. Knepley   }
651471f0bbf9SMatthew G. Knepley 
651571f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
651671f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
65177773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
65187773e69fSMatthew G. Knepley }
65197773e69fSMatthew G. Knepley 
65207cd05799SMatthew G. Knepley /*@C
652171f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
65227cd05799SMatthew G. Knepley 
65237cd05799SMatthew G. Knepley   Not collective
65247cd05799SMatthew G. Knepley 
65257cd05799SMatthew G. Knepley   Input Parameters:
65267cd05799SMatthew G. Knepley + dm         - The DM
652771f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
652871f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
652971f0bbf9SMatthew G. Knepley . point      - The point defining the closure
653071f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
653171f0bbf9SMatthew G. Knepley 
653271f0bbf9SMatthew G. Knepley   Output Parameters:
653371f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
653471f0bbf9SMatthew G. Knepley . indices    - The dof indices
653571f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
653671f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
653771f0bbf9SMatthew G. Knepley 
653871f0bbf9SMatthew G. Knepley   Notes:
653971f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
654071f0bbf9SMatthew G. Knepley 
654171f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
654271f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
654371f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
654471f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
654571f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
65467cd05799SMatthew G. Knepley 
65477cd05799SMatthew G. Knepley   Level: advanced
65487cd05799SMatthew G. Knepley 
654971f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
65507cd05799SMatthew G. Knepley @*/
655171f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
655271f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
65537773e69fSMatthew G. Knepley {
65547773e69fSMatthew G. Knepley   PetscErrorCode ierr;
65557773e69fSMatthew G. Knepley 
65567773e69fSMatthew G. Knepley   PetscFunctionBegin;
65577773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65587773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
655969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
65607773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
65617773e69fSMatthew G. Knepley }
65627773e69fSMatthew G. Knepley 
65637f5d1fdeSMatthew G. Knepley /*@C
65647f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
65657f5d1fdeSMatthew G. Knepley 
65667f5d1fdeSMatthew G. Knepley   Not collective
65677f5d1fdeSMatthew G. Knepley 
65687f5d1fdeSMatthew G. Knepley   Input Parameters:
65697f5d1fdeSMatthew G. Knepley + dm - The DM
6570ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6571ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
65727f5d1fdeSMatthew G. Knepley . A - The matrix
6573eaf898f9SPatrick Sanan . point - The point in the DM
65747f5d1fdeSMatthew G. Knepley . values - The array of values
65757f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
65767f5d1fdeSMatthew G. Knepley 
65777f5d1fdeSMatthew G. Knepley   Fortran Notes:
65787f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
65797f5d1fdeSMatthew G. Knepley 
65807f5d1fdeSMatthew G. Knepley   Level: intermediate
65817f5d1fdeSMatthew G. Knepley 
65824a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
65837f5d1fdeSMatthew G. Knepley @*/
65847c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6585552f7358SJed Brown {
6586552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
6587552f7358SJed Brown   PetscInt          *indices;
658871f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
658971f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
6590552f7358SJed Brown   PetscErrorCode     ierr;
6591552f7358SJed Brown 
6592552f7358SJed Brown   PetscFunctionBegin;
6593552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
659492fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
65953dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6596e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
65973dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
65983dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6599552f7358SJed Brown 
660071f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
66010d644c17SKarl Rupp 
6602b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
66034a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6604552f7358SJed Brown   if (ierr) {
6605552f7358SJed Brown     PetscMPIInt    rank;
6606552f7358SJed Brown     PetscErrorCode ierr2;
6607552f7358SJed Brown 
660855b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6609e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6610b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
661171f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
661271f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6613552f7358SJed Brown     CHKERRQ(ierr);
6614552f7358SJed Brown   }
66154a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
66164a1e0b3eSMatthew G. Knepley     PetscInt i;
66174a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
66184a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
66194a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
66204a1e0b3eSMatthew G. Knepley   }
662171f0bbf9SMatthew G. Knepley 
662271f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
662371f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
662471f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
66254acb8e1eSToby Isaac }
662671f0bbf9SMatthew G. Knepley 
66274a1e0b3eSMatthew G. Knepley /*@C
66284a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
66294a1e0b3eSMatthew G. Knepley 
66304a1e0b3eSMatthew G. Knepley   Not collective
66314a1e0b3eSMatthew G. Knepley 
66324a1e0b3eSMatthew G. Knepley   Input Parameters:
66334a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
66344a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
66354a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
66364a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
66374a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
66384a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
66394a1e0b3eSMatthew G. Knepley . A - The matrix
66404a1e0b3eSMatthew G. Knepley . point - The point in the DMs
66414a1e0b3eSMatthew G. Knepley . values - The array of values
66424a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
66434a1e0b3eSMatthew G. Knepley 
66444a1e0b3eSMatthew G. Knepley   Level: intermediate
66454a1e0b3eSMatthew G. Knepley 
66464a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
66474a1e0b3eSMatthew G. Knepley @*/
664871f0bbf9SMatthew 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)
664971f0bbf9SMatthew G. Knepley {
665071f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
665171f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
665271f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
665371f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
665471f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
665571f0bbf9SMatthew G. Knepley 
665671f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
665771f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
665871f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
665971f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
666071f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
666171f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
666271f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
666371f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
666471f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
666571f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
666671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
666771f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
666871f0bbf9SMatthew G. Knepley 
666971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
667071f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
667171f0bbf9SMatthew G. Knepley 
667271f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
66734a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
667471f0bbf9SMatthew G. Knepley   if (ierr) {
667571f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
667671f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
667771f0bbf9SMatthew G. Knepley 
667855b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
667971f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
668071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
668171f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
668271f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
668371f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
668471f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
6685d3d1a6afSToby Isaac   }
668671f0bbf9SMatthew G. Knepley 
668771f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
668871f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
668971f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
6690552f7358SJed Brown   PetscFunctionReturn(0);
6691552f7358SJed Brown }
6692552f7358SJed Brown 
6693de41b84cSMatthew 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)
6694de41b84cSMatthew G. Knepley {
6695de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6696de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6697de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6698de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
669917c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6700de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6701412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
67024ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6703de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6704de41b84cSMatthew G. Knepley 
6705de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6706de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6707de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
670892fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6709de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
671092fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6711de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6712e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6713de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6714e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6715de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6716de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6717de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6718de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6719580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6720580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
6721de41b84cSMatthew G. Knepley   /* Column indices */
6722de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
67234ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6724de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6725de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6726de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6727de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6728de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6729de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6730de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6731de41b84cSMatthew G. Knepley       ++q;
6732de41b84cSMatthew G. Knepley     }
6733de41b84cSMatthew G. Knepley   }
6734de41b84cSMatthew G. Knepley   numCPoints = q;
6735de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6736de41b84cSMatthew G. Knepley     PetscInt fdof;
6737de41b84cSMatthew G. Knepley 
6738de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
67394ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6740de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6741de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6742de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6743de41b84cSMatthew G. Knepley     }
6744de41b84cSMatthew G. Knepley     numCIndices += dof;
6745de41b84cSMatthew G. Knepley   }
6746de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6747de41b84cSMatthew G. Knepley   /* Row indices */
6748412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6749412e9a14SMatthew G. Knepley   {
6750412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6751412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6752412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6753412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6754412e9a14SMatthew G. Knepley   }
675569291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6756de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6757de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6758de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6759de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6760de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6761de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6762de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
67634ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
67644ca5e9f5SMatthew G. Knepley         if (!dof) continue;
67654ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
67664ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6767de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6768de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6769de41b84cSMatthew G. Knepley         ++q;
6770de41b84cSMatthew G. Knepley       }
6771de41b84cSMatthew G. Knepley     }
6772de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6773de41b84cSMatthew G. Knepley   }
6774de41b84cSMatthew G. Knepley   numFPoints = q;
6775de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6776de41b84cSMatthew G. Knepley     PetscInt fdof;
6777de41b84cSMatthew G. Knepley 
6778de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
67794ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6780de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6781de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6782de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6783de41b84cSMatthew G. Knepley     }
6784de41b84cSMatthew G. Knepley     numFIndices += dof;
6785de41b84cSMatthew G. Knepley   }
6786de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6787de41b84cSMatthew G. Knepley 
67888ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
67898ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
679069291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
679169291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6792de41b84cSMatthew G. Knepley   if (numFields) {
67934acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
67944acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
67954acb8e1eSToby Isaac 
67964acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
67974acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
67984acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6799de41b84cSMatthew G. Knepley     }
68004acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
68014acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
680236fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
68034acb8e1eSToby Isaac     }
68044acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
68054acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
680636fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
68074acb8e1eSToby Isaac     }
68084acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
68094acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
68104acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6811de41b84cSMatthew G. Knepley     }
6812de41b84cSMatthew G. Knepley   } else {
68134acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
68144acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
68154acb8e1eSToby Isaac 
68164acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
68174acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
68184acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
68194acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
68204acb8e1eSToby Isaac 
68214acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
682236fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6823de41b84cSMatthew G. Knepley     }
68244acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
68254acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
68264acb8e1eSToby Isaac 
68274acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
682836fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6829de41b84cSMatthew G. Knepley     }
68304acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
68314acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6832de41b84cSMatthew G. Knepley   }
6833de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
68344acb8e1eSToby Isaac   /* TODO: flips */
6835de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6836de41b84cSMatthew G. Knepley   if (ierr) {
6837de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6838de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6839de41b84cSMatthew G. Knepley 
684055b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6841e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6842de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
684369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
684469291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6845de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6846de41b84cSMatthew G. Knepley   }
684769291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6848de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
684969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
685069291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6851de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6852de41b84cSMatthew G. Knepley }
6853de41b84cSMatthew G. Knepley 
68547c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
68557c927364SMatthew G. Knepley {
68567c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
68577c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
68587c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
685917c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6860412e9a14SMatthew G. Knepley   DMPolytopeType ct;
68617c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
68627c927364SMatthew G. Knepley   PetscErrorCode ierr;
68637c927364SMatthew G. Knepley 
68647c927364SMatthew G. Knepley   PetscFunctionBegin;
68657c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
68667c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
686792fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
68687c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
686992fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
68707c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6871e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
68727c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6873e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
68747c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
68757c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
68767c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6877580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6878580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
68797c927364SMatthew G. Knepley   /* Column indices */
68807c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
68817c927364SMatthew G. Knepley   maxFPoints = numCPoints;
68827c927364SMatthew G. Knepley   /* Compress out points not in the section */
68837c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
68847c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
68857c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
68867c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
68877c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
68887c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
68897c927364SMatthew G. Knepley       ++q;
68907c927364SMatthew G. Knepley     }
68917c927364SMatthew G. Knepley   }
68927c927364SMatthew G. Knepley   numCPoints = q;
68937c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
68947c927364SMatthew G. Knepley     PetscInt fdof;
68957c927364SMatthew G. Knepley 
68967c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
68977c927364SMatthew G. Knepley     if (!dof) continue;
68987c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
68997c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
69007c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
69017c927364SMatthew G. Knepley     }
69027c927364SMatthew G. Knepley     numCIndices += dof;
69037c927364SMatthew G. Knepley   }
69047c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
69057c927364SMatthew G. Knepley   /* Row indices */
6906412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6907412e9a14SMatthew G. Knepley   {
6908412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6909412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6910412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6911412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6912412e9a14SMatthew G. Knepley   }
691369291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
69147c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
69157c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
69167c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
69177c927364SMatthew G. Knepley     /* Compress out points not in the section */
69187c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
69197c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
69207c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
69217c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
69227c927364SMatthew G. Knepley         if (!dof) continue;
69237c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
69247c927364SMatthew G. Knepley         if (s < q) continue;
69257c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
69267c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
69277c927364SMatthew G. Knepley         ++q;
69287c927364SMatthew G. Knepley       }
69297c927364SMatthew G. Knepley     }
69307c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
69317c927364SMatthew G. Knepley   }
69327c927364SMatthew G. Knepley   numFPoints = q;
69337c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
69347c927364SMatthew G. Knepley     PetscInt fdof;
69357c927364SMatthew G. Knepley 
69367c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
69377c927364SMatthew G. Knepley     if (!dof) continue;
69387c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
69397c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
69407c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
69417c927364SMatthew G. Knepley     }
69427c927364SMatthew G. Knepley     numFIndices += dof;
69437c927364SMatthew G. Knepley   }
69447c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
69457c927364SMatthew G. Knepley 
69468ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
69478ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
69487c927364SMatthew G. Knepley   if (numFields) {
69494acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
69504acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
69514acb8e1eSToby Isaac 
69524acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
69534acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
69544acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
69557c927364SMatthew G. Knepley     }
69564acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
69574acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
695836fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
69594acb8e1eSToby Isaac     }
69604acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
69614acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
696236fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
69634acb8e1eSToby Isaac     }
69644acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
69654acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
69664acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
69677c927364SMatthew G. Knepley     }
69687c927364SMatthew G. Knepley   } else {
69694acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
69704acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
69714acb8e1eSToby Isaac 
69724acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
69734acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
69744acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
69754acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
69764acb8e1eSToby Isaac 
69774acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
697836fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
69797c927364SMatthew G. Knepley     }
69804acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
69814acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
69824acb8e1eSToby Isaac 
69834acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
698436fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
69857c927364SMatthew G. Knepley     }
69864acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
69874acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
69887c927364SMatthew G. Knepley   }
698969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
69907c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
69917c927364SMatthew G. Knepley   PetscFunctionReturn(0);
69927c927364SMatthew G. Knepley }
69937c927364SMatthew G. Knepley 
69947cd05799SMatthew G. Knepley /*@C
69957cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
69967cd05799SMatthew G. Knepley 
69977cd05799SMatthew G. Knepley   Input Parameter:
69987cd05799SMatthew G. Knepley . dm   - The DMPlex object
69997cd05799SMatthew G. Knepley 
70007cd05799SMatthew G. Knepley   Output Parameter:
70017cd05799SMatthew G. Knepley . cellHeight - The height of a cell
70027cd05799SMatthew G. Knepley 
70037cd05799SMatthew G. Knepley   Level: developer
70047cd05799SMatthew G. Knepley 
70057cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
70067cd05799SMatthew G. Knepley @*/
7007552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7008552f7358SJed Brown {
7009552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7010552f7358SJed Brown 
7011552f7358SJed Brown   PetscFunctionBegin;
7012552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7013552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
7014552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
7015552f7358SJed Brown   PetscFunctionReturn(0);
7016552f7358SJed Brown }
7017552f7358SJed Brown 
70187cd05799SMatthew G. Knepley /*@C
70197cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
70207cd05799SMatthew G. Knepley 
70217cd05799SMatthew G. Knepley   Input Parameters:
70227cd05799SMatthew G. Knepley + dm   - The DMPlex object
70237cd05799SMatthew G. Knepley - cellHeight - The height of a cell
70247cd05799SMatthew G. Knepley 
70257cd05799SMatthew G. Knepley   Level: developer
70267cd05799SMatthew G. Knepley 
70277cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
70287cd05799SMatthew G. Knepley @*/
7029552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7030552f7358SJed Brown {
7031552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7032552f7358SJed Brown 
7033552f7358SJed Brown   PetscFunctionBegin;
7034552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7035552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7036552f7358SJed Brown   PetscFunctionReturn(0);
7037552f7358SJed Brown }
7038552f7358SJed Brown 
7039e6139122SMatthew G. Knepley /*@
7040e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7041e6139122SMatthew G. Knepley 
7042e6139122SMatthew G. Knepley   Input Parameter:
7043e6139122SMatthew G. Knepley . dm - The DMPlex object
7044e6139122SMatthew G. Knepley 
7045e6139122SMatthew G. Knepley   Output Parameters:
70462a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
70472a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7048e6139122SMatthew G. Knepley 
70492a9f31c0SMatthew G. Knepley   Level: advanced
7050e6139122SMatthew G. Knepley 
70518065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7052e6139122SMatthew G. Knepley @*/
7053e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7054e6139122SMatthew G. Knepley {
7055412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7056e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7057e6139122SMatthew G. Knepley 
7058e6139122SMatthew G. Knepley   PetscFunctionBegin;
7059e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7060412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
7061412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
7062e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
7063e6139122SMatthew G. Knepley }
7064e6139122SMatthew G. Knepley 
7065552f7358SJed Brown /* We can easily have a form that takes an IS instead */
70669886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7067552f7358SJed Brown {
7068552f7358SJed Brown   PetscSection   section, globalSection;
7069552f7358SJed Brown   PetscInt      *numbers, p;
7070552f7358SJed Brown   PetscErrorCode ierr;
7071552f7358SJed Brown 
7072552f7358SJed Brown   PetscFunctionBegin;
707382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
7074552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
7075552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7076552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
7077552f7358SJed Brown   }
7078552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
707915b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
7080854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
7081552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7082552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
7083ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7084ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
7085552f7358SJed Brown   }
708682f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
7087ef48cebcSMatthew G. Knepley   if (globalSize) {
7088ef48cebcSMatthew G. Knepley     PetscLayout layout;
7089ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
7090ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
7091ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
7092ef48cebcSMatthew G. Knepley   }
7093552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
7094552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
7095552f7358SJed Brown   PetscFunctionReturn(0);
7096552f7358SJed Brown }
7097552f7358SJed Brown 
709881ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7099552f7358SJed Brown {
7100412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
7101552f7358SJed Brown   PetscErrorCode ierr;
7102552f7358SJed Brown 
7103552f7358SJed Brown   PetscFunctionBegin;
7104552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7105412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
7106412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
71079886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
710881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
7109552f7358SJed Brown }
711081ed3555SMatthew G. Knepley 
71118dab3259SMatthew G. Knepley /*@
71127cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
71137cd05799SMatthew G. Knepley 
71147cd05799SMatthew G. Knepley   Input Parameter:
71157cd05799SMatthew G. Knepley . dm   - The DMPlex object
71167cd05799SMatthew G. Knepley 
71177cd05799SMatthew G. Knepley   Output Parameter:
71187cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
71197cd05799SMatthew G. Knepley 
71207cd05799SMatthew G. Knepley   Level: developer
71217cd05799SMatthew G. Knepley 
71227cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
71237cd05799SMatthew G. Knepley @*/
712481ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
712581ed3555SMatthew G. Knepley {
712681ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
712781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
712881ed3555SMatthew G. Knepley 
712981ed3555SMatthew G. Knepley   PetscFunctionBegin;
713081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
713181ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
7132552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
7133552f7358SJed Brown   PetscFunctionReturn(0);
7134552f7358SJed Brown }
7135552f7358SJed Brown 
713681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
713781ed3555SMatthew G. Knepley {
7138412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
713981ed3555SMatthew G. Knepley   PetscErrorCode ierr;
714081ed3555SMatthew G. Knepley 
714181ed3555SMatthew G. Knepley   PetscFunctionBegin;
714281ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
714381ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
71449886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
714581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
714681ed3555SMatthew G. Knepley }
714781ed3555SMatthew G. Knepley 
71488dab3259SMatthew G. Knepley /*@
71496aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
71507cd05799SMatthew G. Knepley 
71517cd05799SMatthew G. Knepley   Input Parameter:
71527cd05799SMatthew G. Knepley . dm   - The DMPlex object
71537cd05799SMatthew G. Knepley 
71547cd05799SMatthew G. Knepley   Output Parameter:
71557cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
71567cd05799SMatthew G. Knepley 
71577cd05799SMatthew G. Knepley   Level: developer
71587cd05799SMatthew G. Knepley 
71597cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
71607cd05799SMatthew G. Knepley @*/
7161552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7162552f7358SJed Brown {
7163552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
7164552f7358SJed Brown   PetscErrorCode ierr;
7165552f7358SJed Brown 
7166552f7358SJed Brown   PetscFunctionBegin;
7167552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
716881ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
7169552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
7170552f7358SJed Brown   PetscFunctionReturn(0);
7171552f7358SJed Brown }
7172552f7358SJed Brown 
71738dab3259SMatthew G. Knepley /*@
71747cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
71757cd05799SMatthew G. Knepley 
71767cd05799SMatthew G. Knepley   Input Parameter:
71777cd05799SMatthew G. Knepley . dm   - The DMPlex object
71787cd05799SMatthew G. Knepley 
71797cd05799SMatthew G. Knepley   Output Parameter:
71807cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
71817cd05799SMatthew G. Knepley 
71827cd05799SMatthew G. Knepley   Level: developer
71837cd05799SMatthew G. Knepley 
71847cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
71857cd05799SMatthew G. Knepley @*/
7186ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7187ef48cebcSMatthew G. Knepley {
7188ef48cebcSMatthew G. Knepley   IS             nums[4];
7189862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
7190ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
7191ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
7192ef48cebcSMatthew G. Knepley 
7193ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
7194ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7195ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
71968abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
71978abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
7198862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7199862913ffSStefano Zampini     PetscInt end;
7200862913ffSStefano Zampini 
7201862913ffSStefano Zampini     depths[d] = depth-d;
7202862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
7203862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7204862913ffSStefano Zampini   }
7205862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
7206862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
7207862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7208862913ffSStefano 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]);
7209862913ffSStefano Zampini   }
7210ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
7211ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
7212ef48cebcSMatthew G. Knepley 
7213862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
72149886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
7215ef48cebcSMatthew G. Knepley     shift += gsize;
7216ef48cebcSMatthew G. Knepley   }
7217302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7218ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7219ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7220ef48cebcSMatthew G. Knepley }
7221ef48cebcSMatthew G. Knepley 
722208a22f4bSMatthew G. Knepley 
722308a22f4bSMatthew G. Knepley /*@
722408a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
722508a22f4bSMatthew G. Knepley 
722608a22f4bSMatthew G. Knepley   Input Parameter:
722708a22f4bSMatthew G. Knepley . dm - The DMPlex object
722808a22f4bSMatthew G. Knepley 
722908a22f4bSMatthew G. Knepley   Output Parameter:
723008a22f4bSMatthew G. Knepley . ranks - The rank field
723108a22f4bSMatthew G. Knepley 
723208a22f4bSMatthew G. Knepley   Options Database Keys:
723308a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
723408a22f4bSMatthew G. Knepley 
723508a22f4bSMatthew G. Knepley   Level: intermediate
723608a22f4bSMatthew G. Knepley 
723708a22f4bSMatthew G. Knepley .seealso: DMView()
723808a22f4bSMatthew G. Knepley @*/
723908a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
724008a22f4bSMatthew G. Knepley {
724108a22f4bSMatthew G. Knepley   DM             rdm;
724208a22f4bSMatthew G. Knepley   PetscFE        fe;
724308a22f4bSMatthew G. Knepley   PetscScalar   *r;
724408a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
7245a55f9a55SMatthew G. Knepley   DMPolytopeType ct;
724608a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
7247a55f9a55SMatthew G. Knepley   PetscBool      simplex;
724808a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
724908a22f4bSMatthew G. Knepley 
725008a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7251f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7252f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
7253ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
725408a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
725508a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7256a55f9a55SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7257a55f9a55SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
7258a55f9a55SMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
7259a55f9a55SMatthew G. Knepley   ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
726008a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7261e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
726208a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7263e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
726408a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
726508a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
726608a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
726708a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
726808a22f4bSMatthew G. Knepley     PetscScalar *lr;
726908a22f4bSMatthew G. Knepley 
727008a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
727171f09efeSPierre Jolivet     if (lr) *lr = rank;
727208a22f4bSMatthew G. Knepley   }
727308a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
727408a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
727508a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
727608a22f4bSMatthew G. Knepley }
727708a22f4bSMatthew G. Knepley 
7278ca8062c8SMatthew G. Knepley /*@
727918e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
728018e14f0cSMatthew G. Knepley 
728118e14f0cSMatthew G. Knepley   Input Parameters:
728218e14f0cSMatthew G. Knepley + dm    - The DMPlex
728318e14f0cSMatthew G. Knepley - label - The DMLabel
728418e14f0cSMatthew G. Knepley 
728518e14f0cSMatthew G. Knepley   Output Parameter:
728618e14f0cSMatthew G. Knepley . val - The label value field
728718e14f0cSMatthew G. Knepley 
728818e14f0cSMatthew G. Knepley   Options Database Keys:
728918e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
729018e14f0cSMatthew G. Knepley 
729118e14f0cSMatthew G. Knepley   Level: intermediate
729218e14f0cSMatthew G. Knepley 
729318e14f0cSMatthew G. Knepley .seealso: DMView()
729418e14f0cSMatthew G. Knepley @*/
729518e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
729618e14f0cSMatthew G. Knepley {
729718e14f0cSMatthew G. Knepley   DM             rdm;
729818e14f0cSMatthew G. Knepley   PetscFE        fe;
729918e14f0cSMatthew G. Knepley   PetscScalar   *v;
730018e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
730118e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
730218e14f0cSMatthew G. Knepley 
730318e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
730418e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
730518e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
730618e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
730718e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
730818e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
730918e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
731018e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7311e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
731218e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7313e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
731418e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
731518e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7316effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
731718e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
731818e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
731918e14f0cSMatthew G. Knepley     PetscScalar *lv;
732018e14f0cSMatthew G. Knepley     PetscInt     cval;
732118e14f0cSMatthew G. Knepley 
732218e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
732318e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
732418e14f0cSMatthew G. Knepley     *lv = cval;
732518e14f0cSMatthew G. Knepley   }
732618e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
732718e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
732818e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
732918e14f0cSMatthew G. Knepley }
733018e14f0cSMatthew G. Knepley 
733118e14f0cSMatthew G. Knepley /*@
7332ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7333ca8062c8SMatthew G. Knepley 
733469916449SMatthew G. Knepley   Input Parameter:
733569916449SMatthew G. Knepley . dm - The DMPlex object
7336ca8062c8SMatthew G. Knepley 
733795eb5ee5SVaclav Hapla   Notes:
733895eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
733995eb5ee5SVaclav Hapla 
734095eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7341ca8062c8SMatthew G. Knepley 
7342ca8062c8SMatthew G. Knepley   Level: developer
7343ca8062c8SMatthew G. Knepley 
734495eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7345ca8062c8SMatthew G. Knepley @*/
7346ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7347ca8062c8SMatthew G. Knepley {
7348ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7349ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7350ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
735157beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
735257beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7353ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7354ca8062c8SMatthew G. Knepley 
7355ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7356ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7357412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7358ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7359ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7360ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7361ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7362ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7363ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7364ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7365ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
736642e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
736742e66dfaSMatthew G. Knepley       PetscInt  d;
736842e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
736942e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
737042e66dfaSMatthew G. Knepley       }
7371ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7372ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7373ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7374ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7375ca8062c8SMatthew G. Knepley       }
737642e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
73778ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7378ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
73798ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7380ca8062c8SMatthew G. Knepley         }
7381302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
73828ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7383ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
73848ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7385ca8062c8SMatthew G. Knepley         }
7386302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
73878ccfff9cSToby 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]);
73888ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7389ca8062c8SMatthew G. Knepley       }
739042e66dfaSMatthew G. Knepley     }
739157beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
739257beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7393ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7394ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7395ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7396ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7397ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7398ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
739957beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
740057beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7401ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7402ca8062c8SMatthew G. Knepley       }
7403ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
74048ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7405ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
74068ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7407ca8062c8SMatthew G. Knepley         }
7408302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
74098ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7410ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
74118ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7412ca8062c8SMatthew G. Knepley         }
7413302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
74148ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7415ca8062c8SMatthew G. Knepley       }
7416ca8062c8SMatthew G. Knepley     }
7417ca8062c8SMatthew G. Knepley   }
741857beb4faSStefano Zampini   if (storagecheck) {
7419ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7420ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
74218ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
742257beb4faSStefano Zampini   }
7423ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7424ca8062c8SMatthew G. Knepley }
7425ca8062c8SMatthew G. Knepley 
7426412e9a14SMatthew G. Knepley /*
7427412e9a14SMatthew 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.
7428412e9a14SMatthew G. Knepley */
7429412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7430412e9a14SMatthew G. Knepley {
7431412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7432412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7433412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7434412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7435412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7436412e9a14SMatthew G. Knepley 
7437412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7438412e9a14SMatthew G. Knepley   *unsplit = 0;
7439412e9a14SMatthew G. Knepley   switch (ct) {
7440412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7441412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7442412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7443412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7444412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7445412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7446412e9a14SMatthew G. Knepley       }
7447412e9a14SMatthew G. Knepley       break;
7448412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7449412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7450412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7451412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7452412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7453412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7454412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7455412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7456412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7457412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7458412e9a14SMatthew G. Knepley             PetscInt p;
7459412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7460412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7461412e9a14SMatthew G. Knepley           }
7462412e9a14SMatthew G. Knepley         }
7463412e9a14SMatthew G. Knepley       }
7464412e9a14SMatthew G. Knepley       break;
7465412e9a14SMatthew G. Knepley     default: break;
7466412e9a14SMatthew G. Knepley   }
7467412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7468412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7469412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7470412e9a14SMatthew G. Knepley   }
7471412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7472412e9a14SMatthew G. Knepley }
7473412e9a14SMatthew G. Knepley 
7474ca8062c8SMatthew G. Knepley /*@
7475ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7476ca8062c8SMatthew G. Knepley 
7477ca8062c8SMatthew G. Knepley   Input Parameters:
7478ca8062c8SMatthew G. Knepley + dm - The DMPlex object
747958723a97SMatthew G. Knepley - cellHeight - Normally 0
7480ca8062c8SMatthew G. Knepley 
748195eb5ee5SVaclav Hapla   Notes:
748295eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
748325c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7484ca8062c8SMatthew G. Knepley 
748595eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
748695eb5ee5SVaclav Hapla 
7487ca8062c8SMatthew G. Knepley   Level: developer
7488ca8062c8SMatthew G. Knepley 
748995eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7490ca8062c8SMatthew G. Knepley @*/
749125c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7492ca8062c8SMatthew G. Knepley {
7493412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7494412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7495412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7496ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7497ca8062c8SMatthew G. Knepley 
7498ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7499ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7500412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
750125c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
750258723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7503412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7504412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7505412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
750658723a97SMatthew G. Knepley 
7507412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7508412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7509412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7510412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7511412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7512d4961f80SStefano Zampini       if (coneSize != DMPolytopeTypeGetConeSize(ct)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has cone size %D != %D", c, DMPolytopeTypes[ct], coneSize, DMPolytopeTypeGetConeSize(ct));
7513412e9a14SMatthew G. Knepley     }
751458723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
751558723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
751658723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7517412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
751858723a97SMatthew G. Knepley     }
751958723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7520412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7521412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7522412e9a14SMatthew G. Knepley       PetscInt unsplit;
752342363296SMatthew G. Knepley 
7524412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7525412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
752642363296SMatthew G. Knepley     }
7527d4961f80SStefano Zampini     if (Nv != DMPolytopeTypeGetNumVertices(ct)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has %D vertices != %D", c, DMPolytopeTypes[ct], Nv, DMPolytopeTypeGetNumVertices(ct));
752842363296SMatthew G. Knepley   }
7529ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7530ca8062c8SMatthew G. Knepley }
75319bf0dad6SMatthew G. Knepley 
75329bf0dad6SMatthew G. Knepley /*@
75339bf0dad6SMatthew 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
75349bf0dad6SMatthew G. Knepley 
7535899ea2b8SJacob Faibussowitsch   Not Collective
7536899ea2b8SJacob Faibussowitsch 
75379bf0dad6SMatthew G. Knepley   Input Parameters:
75389bf0dad6SMatthew G. Knepley + dm - The DMPlex object
75399bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
75409bf0dad6SMatthew G. Knepley 
754145da879fSVaclav Hapla   Notes:
754245da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
754345da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
754445da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
754545da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
75469bf0dad6SMatthew G. Knepley 
754795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
754895eb5ee5SVaclav Hapla 
75499bf0dad6SMatthew G. Knepley   Level: developer
75509bf0dad6SMatthew G. Knepley 
755195eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
75529bf0dad6SMatthew G. Knepley @*/
755325c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
75549bf0dad6SMatthew G. Knepley {
7555ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
75569bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7557899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
75589bf0dad6SMatthew G. Knepley 
75599bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
75609bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7561899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
756245da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
756345da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7564899ea2b8SJacob Faibussowitsch     PetscMPIInt rank;
7565899ea2b8SJacob Faibussowitsch     MPI_Comm    comm;
7566899ea2b8SJacob Faibussowitsch 
7567899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7568ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
756945da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7570899ea2b8SJacob Faibussowitsch   }
7571899ea2b8SJacob Faibussowitsch 
7572c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7573ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
75749bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7575ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
75763554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
75773554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7578412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7579412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7580ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7581412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7582412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
75839bf0dad6SMatthew G. Knepley 
7584ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7585412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7586412e9a14SMatthew G. Knepley       if (unsplit) continue;
75879bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
75889bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
75899bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
75909bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
75919bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
75929bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
75939bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
75949bf0dad6SMatthew G. Knepley       }
7595412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
7596d4961f80SStefano Zampini       if (coneSize != numFaces) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has %D faces but should have %D", c, DMPolytopeTypes[ct], coneSize, numFaces);
75979bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
7598d4961f80SStefano Zampini         DMPolytopeType fct;
75999bf0dad6SMatthew G. Knepley         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
76009bf0dad6SMatthew G. Knepley 
7601d4961f80SStefano Zampini         ierr = DMPlexGetCellType(dm, cone[f], &fct);CHKERRQ(ierr);
76029bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
76039bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
76049bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
76059bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
76069bf0dad6SMatthew G. Knepley         }
7607d4961f80SStefano Zampini         if (fnumCorners != faceSizes[f]) SETERRQ7(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %D) of cell %D of type %s has %D vertices but should have %D", cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], fnumCorners, faceSizes[f]);
76089bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7609d4961f80SStefano Zampini           if (fclosure[v] != faces[fOff+v]) SETERRQ8(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %d) of cell %D of type %s vertex %D, %D != %D", cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], v, fclosure[v], faces[fOff+v]);
76109bf0dad6SMatthew G. Knepley         }
76119bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7612412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
76139bf0dad6SMatthew G. Knepley       }
7614412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
76159bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
76169bf0dad6SMatthew G. Knepley     }
76173554e41dSMatthew G. Knepley   }
7618552f7358SJed Brown   PetscFunctionReturn(0);
7619552f7358SJed Brown }
76203913d7c8SMatthew G. Knepley 
7621bb6a34a8SMatthew G. Knepley /*@
7622bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7623bb6a34a8SMatthew G. Knepley 
7624bb6a34a8SMatthew G. Knepley   Input Parameter:
7625bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7626bb6a34a8SMatthew G. Knepley 
762795eb5ee5SVaclav Hapla   Notes:
762895eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
762995eb5ee5SVaclav Hapla 
763095eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7631bb6a34a8SMatthew G. Knepley 
7632bb6a34a8SMatthew G. Knepley   Level: developer
7633bb6a34a8SMatthew G. Knepley 
763495eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7635bb6a34a8SMatthew G. Knepley @*/
7636bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7637bb6a34a8SMatthew G. Knepley {
7638bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7639bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7640412e9a14SMatthew G. Knepley   PetscBool      periodic;
764151a74b61SMatthew G. Knepley   PetscInt       dim, depth, dE, d, cStart, cEnd, c;
7642bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7643bb6a34a8SMatthew G. Knepley 
7644bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7645bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
764651a74b61SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
764751a74b61SMatthew G. Knepley   if (dim != dE) PetscFunctionReturn(0);
7648bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7649412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7650bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7651bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7652412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7653412e9a14SMatthew G. Knepley     DMPolytopeType ct;
7654412e9a14SMatthew G. Knepley     PetscInt       unsplit;
7655412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
7656412e9a14SMatthew G. Knepley 
7657412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7658412e9a14SMatthew G. Knepley     switch (ct) {
7659412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7660412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7661412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7662412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
7663412e9a14SMatthew G. Knepley       default: break;
7664412e9a14SMatthew G. Knepley     }
7665412e9a14SMatthew G. Knepley     switch (ct) {
7666412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
7667412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7668412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7669412e9a14SMatthew G. Knepley         continue;
7670412e9a14SMatthew G. Knepley       default: break;
7671412e9a14SMatthew G. Knepley     }
7672412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7673412e9a14SMatthew G. Knepley     if (unsplit) continue;
7674bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7675d4961f80SStefano Zampini     if (detJ < -PETSC_SMALL || (detJ <= 0.0 && !ignoreZeroVol)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D of type %s is inverted, |J| = %g", c, DMPolytopeTypes[ct], (double) detJ);
7676bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7677412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
7678bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7679d4961f80SStefano Zampini       if (vol < -PETSC_SMALL || (vol <= 0.0 && !ignoreZeroVol)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D of type %s is inverted, vol = %g", c, DMPolytopeTypes[ct], (double) vol);
7680bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7681bb6a34a8SMatthew G. Knepley     }
7682bb6a34a8SMatthew G. Knepley   }
7683bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7684bb6a34a8SMatthew G. Knepley }
7685bb6a34a8SMatthew G. Knepley 
768603da9461SVaclav Hapla /*@
7687e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
768803da9461SVaclav Hapla 
768903da9461SVaclav Hapla   Input Parameters:
769003da9461SVaclav Hapla . dm - The DMPlex object
769103da9461SVaclav Hapla 
7692e83a0d2dSVaclav Hapla   Notes:
7693e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
76948918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
769503da9461SVaclav Hapla 
769695eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
769795eb5ee5SVaclav Hapla 
769803da9461SVaclav Hapla   Level: developer
769903da9461SVaclav Hapla 
770095eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
770103da9461SVaclav Hapla @*/
770203da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
770303da9461SVaclav Hapla {
7704f0cfc026SVaclav Hapla   PetscSF         pointSF;
7705f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7706f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
7707f0cfc026SVaclav Hapla   PetscBool       distributed;
770803da9461SVaclav Hapla   PetscErrorCode  ierr;
770903da9461SVaclav Hapla 
771003da9461SVaclav Hapla   PetscFunctionBegin;
771103da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7712f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7713f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7714f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7715f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7716f0cfc026SVaclav Hapla   if (overlap) {
77178918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
77188918e3e2SVaclav Hapla     PetscFunctionReturn(0);
77198918e3e2SVaclav Hapla   }
7720f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7721f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7722f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7723f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
7724f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
772503da9461SVaclav Hapla 
7726ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7727f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7728f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7729f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7730f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
7731f5869d18SMatthew G. Knepley 
7732f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
773303da9461SVaclav Hapla   }
7734ece87651SVaclav Hapla 
7735f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
7736f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7737f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
7738f5869d18SMatthew G. Knepley     const PetscInt *cone;
7739f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
7740f5869d18SMatthew G. Knepley 
7741f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
7742f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
7743f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
7744f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
7745f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
7746f5869d18SMatthew 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]);
7747f5869d18SMatthew G. Knepley       }
7748f5869d18SMatthew G. Knepley     }
7749ece87651SVaclav Hapla   }
775003da9461SVaclav Hapla   PetscFunctionReturn(0);
775103da9461SVaclav Hapla }
775203da9461SVaclav Hapla 
7753068a5610SStefano Zampini typedef struct cell_stats
7754068a5610SStefano Zampini {
7755068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7756068a5610SStefano Zampini   PetscInt  count;
7757068a5610SStefano Zampini } cell_stats_t;
7758068a5610SStefano Zampini 
775925befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7760068a5610SStefano Zampini {
7761068a5610SStefano Zampini   PetscInt i, N = *len;
7762068a5610SStefano Zampini 
7763068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7764068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7765068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7766068a5610SStefano Zampini 
7767068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7768068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7769068a5610SStefano Zampini     B->sum += A->sum;
7770068a5610SStefano Zampini     B->squaresum += A->squaresum;
7771068a5610SStefano Zampini     B->count += A->count;
7772068a5610SStefano Zampini   }
7773068a5610SStefano Zampini }
7774068a5610SStefano Zampini 
7775068a5610SStefano Zampini /*@
777643fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
7777068a5610SStefano Zampini 
77788261a58bSMatthew G. Knepley   Collective on dm
77798261a58bSMatthew G. Knepley 
7780068a5610SStefano Zampini   Input Parameters:
7781068a5610SStefano Zampini + dm        - The DMPlex object
778243fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
778343fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
7784068a5610SStefano Zampini 
778595eb5ee5SVaclav Hapla   Notes:
778695eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
778795eb5ee5SVaclav Hapla 
778895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7789068a5610SStefano Zampini 
7790068a5610SStefano Zampini   Level: developer
7791068a5610SStefano Zampini 
7792f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
7793068a5610SStefano Zampini @*/
779443fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7795068a5610SStefano Zampini {
7796068a5610SStefano Zampini   DM             dmCoarse;
779743fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
779843fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
779943fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
780043fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
7801412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
780243fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
7803068a5610SStefano Zampini   PetscErrorCode ierr;
7804068a5610SStefano Zampini 
7805068a5610SStefano Zampini   PetscFunctionBegin;
7806068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7807068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7808068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7809068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7810068a5610SStefano Zampini   stats.count = 0;
7811068a5610SStefano Zampini 
7812ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
7813ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
781443fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
781543fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
7816412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
781743fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
7818412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
7819068a5610SStefano Zampini     PetscInt  i;
7820068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7821068a5610SStefano Zampini 
7822068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7823068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
782443fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
7825068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7826068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7827068a5610SStefano Zampini     }
7828068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7829068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7830068a5610SStefano Zampini 
7831068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7832068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7833068a5610SStefano Zampini     stats.sum       += cond;
7834068a5610SStefano Zampini     stats.squaresum += cond2;
7835068a5610SStefano Zampini     stats.count++;
78368261a58bSMatthew G. Knepley     if (output && cond > limit) {
783743fa8764SMatthew G. Knepley       PetscSection coordSection;
783843fa8764SMatthew G. Knepley       Vec          coordsLocal;
783943fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
784043fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
784143fa8764SMatthew G. Knepley 
784243fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
784343fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
784443fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
7845087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
784643fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
784743fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
784843fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
784943fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
785048afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
785143fa8764SMatthew G. Knepley         }
785243fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
785343fa8764SMatthew G. Knepley       }
785443fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
785543fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
785643fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
785743fa8764SMatthew G. Knepley 
785843fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
785943fa8764SMatthew G. Knepley           PetscReal len;
786043fa8764SMatthew G. Knepley 
786143fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
7862087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
786343fa8764SMatthew G. Knepley         }
786443fa8764SMatthew G. Knepley       }
786543fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
786643fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
786743fa8764SMatthew G. Knepley     }
7868068a5610SStefano Zampini   }
78698261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
7870068a5610SStefano Zampini 
7871068a5610SStefano Zampini   if (size > 1) {
7872068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7873068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7874068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7875068a5610SStefano Zampini     MPI_Op        statReduce;
7876068a5610SStefano Zampini 
7877ffc4695bSBarry Smith     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRMPI(ierr);
7878ffc4695bSBarry Smith     ierr = MPI_Type_commit(&statType);CHKERRMPI(ierr);
7879ffc4695bSBarry Smith     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRMPI(ierr);
7880ffc4695bSBarry Smith     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRMPI(ierr);
7881ffc4695bSBarry Smith     ierr = MPI_Op_free(&statReduce);CHKERRMPI(ierr);
7882ffc4695bSBarry Smith     ierr = MPI_Type_free(&statType);CHKERRMPI(ierr);
7883068a5610SStefano Zampini   } else {
7884580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
7885068a5610SStefano Zampini   }
7886068a5610SStefano Zampini   if (!rank) {
7887068a5610SStefano Zampini     count = globalStats.count;
7888068a5610SStefano Zampini     min   = globalStats.min;
7889068a5610SStefano Zampini     max   = globalStats.max;
7890068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7891068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7892068a5610SStefano Zampini   }
7893068a5610SStefano Zampini 
7894068a5610SStefano Zampini   if (output) {
7895068a5610SStefano 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);
7896068a5610SStefano Zampini   }
7897068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7898068a5610SStefano Zampini 
7899068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7900068a5610SStefano Zampini   if (dmCoarse) {
7901068a5610SStefano Zampini     PetscBool isplex;
7902068a5610SStefano Zampini 
7903068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7904068a5610SStefano Zampini     if (isplex) {
790543fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
7906068a5610SStefano Zampini     }
7907068a5610SStefano Zampini   }
7908068a5610SStefano Zampini   PetscFunctionReturn(0);
7909068a5610SStefano Zampini }
7910068a5610SStefano Zampini 
7911f108dbd7SJacob Faibussowitsch /*@
7912f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
7913f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
7914f108dbd7SJacob Faibussowitsch 
7915f108dbd7SJacob Faibussowitsch   Collective
7916f108dbd7SJacob Faibussowitsch 
7917f108dbd7SJacob Faibussowitsch   Input Parameters:
7918f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
7919f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
7920f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
7921f108dbd7SJacob Faibussowitsch 
7922f108dbd7SJacob Faibussowitsch   Output Parameters:
7923f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
7924f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
7925f108dbd7SJacob Faibussowitsch 
7926f108dbd7SJacob Faibussowitsch   Options Database Keys:
7927f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
7928f108dbd7SJacob Faibussowitsch supported.
7929f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
7930f108dbd7SJacob Faibussowitsch 
7931f108dbd7SJacob Faibussowitsch   Notes:
7932f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
7933f108dbd7SJacob Faibussowitsch 
7934f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
7935f108dbd7SJacob Faibussowitsch 
7936f108dbd7SJacob 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
7937f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
7938f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
7939f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
7940f108dbd7SJacob Faibussowitsch 
7941f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
7942f108dbd7SJacob Faibussowitsch 
7943f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
7944f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
7945f108dbd7SJacob Faibussowitsch 
7946f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
7947f108dbd7SJacob Faibussowitsch 
7948f108dbd7SJacob Faibussowitsch   Level: intermediate
7949f108dbd7SJacob Faibussowitsch 
7950f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
7951f108dbd7SJacob Faibussowitsch @*/
7952f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
7953f108dbd7SJacob Faibussowitsch {
7954f108dbd7SJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell;
7955f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
7956f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
7957f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
7958f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
7959f108dbd7SJacob Faibussowitsch   IS                      glob;
7960f108dbd7SJacob Faibussowitsch   DMPlexInterpolatedFlag  interpFlag;
7961f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
7962f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
7963f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
7964f108dbd7SJacob Faibussowitsch 
7965f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
7966f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7967f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
7968f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7969f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
7970f108dbd7SJacob Faibussowitsch   if (nc < 2) {
7971f108dbd7SJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
7972f108dbd7SJacob Faibussowitsch   }
7973f108dbd7SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
7974f108dbd7SJacob Faibussowitsch   if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
7975f108dbd7SJacob Faibussowitsch     PetscMPIInt  rank;
7976f108dbd7SJacob Faibussowitsch 
7977ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
7978f108dbd7SJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
7979f108dbd7SJacob Faibussowitsch   }
7980f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
7981f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
7982f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
7983f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
7984f108dbd7SJacob Faibussowitsch   } else {
7985f108dbd7SJacob Faibussowitsch     *OrthQualLabel = NULL;
7986f108dbd7SJacob Faibussowitsch   }
7987f108dbd7SJacob Faibussowitsch 
7988f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7989f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7990f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
7991f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
7992f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
7993f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
7994f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
7995f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
7996f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
7997f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
7998f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
7999f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
8000f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
8001f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8002f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8003f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
8004f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
8005f108dbd7SJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cell++) {
8006f108dbd7SJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, nf, adjSize = PETSC_DETERMINE, ix = cell-cStart;
8007f108dbd7SJacob Faibussowitsch     const PetscInt     *cone;
8008f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
8009f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
8010f108dbd7SJacob Faibussowitsch     PetscReal          minvalc = 1.0, minvalf = 1.0, OQ;
8011f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
8012f108dbd7SJacob Faibussowitsch 
8013f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
8014f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
8015f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
8016f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
8017f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetConeSize(dm, cell, &nf);CHKERRQ(ierr);
8018f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
8019f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8020f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
8021f108dbd7SJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneigh++) {
8022f108dbd7SJacob Faibussowitsch       PetscInt         numcovpts, i, neigh = adj[cellneigh];
8023f108dbd7SJacob Faibussowitsch       const PetscInt   *covpts;
8024f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
8025f108dbd7SJacob Faibussowitsch       PetscReal        *ci, *fi, *Ai;
8026f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
8027f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
8028f108dbd7SJacob Faibussowitsch 
8029f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
8030f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
8031f108dbd7SJacob Faibussowitsch       ierr = PetscMalloc3(nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
8032f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
8033f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
8034f108dbd7SJacob Faibussowitsch       ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8035f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
8036f108dbd7SJacob Faibussowitsch       ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8037f108dbd7SJacob Faibussowitsch 
8038f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
8039f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8040f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8041f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
8042f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
8043f108dbd7SJacob Faibussowitsch         normci += PetscPowScalar(ci[i], 2);
8044f108dbd7SJacob Faibussowitsch         normfi += PetscPowScalar(fi[i], 2);
8045f108dbd7SJacob Faibussowitsch         normai += PetscPowScalar(Ai[i], 2);
8046f108dbd7SJacob Faibussowitsch       }
8047f108dbd7SJacob Faibussowitsch       normci = PetscSqrtScalar(normci);
8048f108dbd7SJacob Faibussowitsch       normfi = PetscSqrtScalar(normfi);
8049f108dbd7SJacob Faibussowitsch       normai = PetscSqrtScalar(normai);
8050f108dbd7SJacob Faibussowitsch 
8051f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
8052f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8053f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
8054f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
8055f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
8056f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
8057f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8058f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8059f108dbd7SJacob Faibussowitsch       }
8060f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8061f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
8062f108dbd7SJacob Faibussowitsch       }
8063f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8064f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
8065f108dbd7SJacob Faibussowitsch       }
8066f108dbd7SJacob Faibussowitsch       cellneighiter++;
8067f108dbd7SJacob Faibussowitsch       ierr = PetscFree3(ci, fi, Ai);CHKERRQ(ierr);
8068f108dbd7SJacob Faibussowitsch     }
8069f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
8070f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
8071f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
8072f108dbd7SJacob Faibussowitsch     OQ = PetscMin(minvalf, minvalc);
8073f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
8074f108dbd7SJacob Faibussowitsch       if (OQ <= atol) {
8075f108dbd7SJacob Faibussowitsch         ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);
8076f108dbd7SJacob Faibussowitsch       }
8077f108dbd7SJacob Faibussowitsch     }
8078f108dbd7SJacob Faibussowitsch     ierr = VecSetValuesLocal(*OrthQual, 1, (const PetscInt *) &ix, (const PetscScalar *) &OQ, INSERT_VALUES);CHKERRQ(ierr);
8079f108dbd7SJacob Faibussowitsch   }
8080f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
8081f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
8082f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8083f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8084f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
8085f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8086f108dbd7SJacob Faibussowitsch     if (vwr) {
8087f108dbd7SJacob Faibussowitsch       ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);
8088f108dbd7SJacob Faibussowitsch     }
8089f108dbd7SJacob Faibussowitsch   }
8090f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
8091f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
8092f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
8093f108dbd7SJacob Faibussowitsch }
8094f108dbd7SJacob Faibussowitsch 
80951eb70e55SToby Isaac /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
80961eb70e55SToby Isaac  * interpolator construction */
80971eb70e55SToby Isaac static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
80981eb70e55SToby Isaac {
80991eb70e55SToby Isaac   PetscSection   section, newSection, gsection;
81001eb70e55SToby Isaac   PetscSF        sf;
81011eb70e55SToby Isaac   PetscBool      hasConstraints, ghasConstraints;
81021eb70e55SToby Isaac   PetscErrorCode ierr;
81031eb70e55SToby Isaac 
81041eb70e55SToby Isaac   PetscFunctionBegin;
81051eb70e55SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81061eb70e55SToby Isaac   PetscValidPointer(odm,2);
81071eb70e55SToby Isaac   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
81081eb70e55SToby Isaac   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
81091eb70e55SToby Isaac   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
81101eb70e55SToby Isaac   if (!ghasConstraints) {
81111eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
81121eb70e55SToby Isaac     *odm = dm;
81131eb70e55SToby Isaac     PetscFunctionReturn(0);
81141eb70e55SToby Isaac   }
81151eb70e55SToby Isaac   ierr = DMClone(dm, odm);CHKERRQ(ierr);
81161eb70e55SToby Isaac   ierr = DMCopyFields(dm, *odm);CHKERRQ(ierr);
81171eb70e55SToby Isaac   ierr = DMGetLocalSection(*odm, &newSection);CHKERRQ(ierr);
81181eb70e55SToby Isaac   ierr = DMGetPointSF(*odm, &sf);CHKERRQ(ierr);
81191eb70e55SToby Isaac   ierr = PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
81201eb70e55SToby Isaac   ierr = DMSetGlobalSection(*odm, gsection);CHKERRQ(ierr);
81211eb70e55SToby Isaac   ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
81221eb70e55SToby Isaac   PetscFunctionReturn(0);
81231eb70e55SToby Isaac }
81241eb70e55SToby Isaac 
81251eb70e55SToby Isaac static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
81261eb70e55SToby Isaac {
81271eb70e55SToby Isaac   DM             dmco, dmfo;
81281eb70e55SToby Isaac   Mat            interpo;
81291eb70e55SToby Isaac   Vec            rscale;
81301eb70e55SToby Isaac   Vec            cglobalo, clocal;
81311eb70e55SToby Isaac   Vec            fglobal, fglobalo, flocal;
81321eb70e55SToby Isaac   PetscBool      regular;
81331eb70e55SToby Isaac   PetscErrorCode ierr;
81341eb70e55SToby Isaac 
81351eb70e55SToby Isaac   PetscFunctionBegin;
81361eb70e55SToby Isaac   ierr = DMGetFullDM(dmc, &dmco);CHKERRQ(ierr);
81371eb70e55SToby Isaac   ierr = DMGetFullDM(dmf, &dmfo);CHKERRQ(ierr);
81381eb70e55SToby Isaac   ierr = DMSetCoarseDM(dmfo, dmco);CHKERRQ(ierr);
81391eb70e55SToby Isaac   ierr = DMPlexGetRegularRefinement(dmf, &regular);CHKERRQ(ierr);
81401eb70e55SToby Isaac   ierr = DMPlexSetRegularRefinement(dmfo, regular);CHKERRQ(ierr);
81411eb70e55SToby Isaac   ierr = DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);CHKERRQ(ierr);
81421eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmco, &cglobalo);CHKERRQ(ierr);
81431eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmc, &clocal);CHKERRQ(ierr);
81441eb70e55SToby Isaac   ierr = VecSet(cglobalo, 0.);CHKERRQ(ierr);
81451eb70e55SToby Isaac   ierr = VecSet(clocal, 0.);CHKERRQ(ierr);
81461eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmf, &fglobal);CHKERRQ(ierr);
81471eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmfo, &fglobalo);CHKERRQ(ierr);
81481eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmf, &flocal);CHKERRQ(ierr);
81491eb70e55SToby Isaac   ierr = VecSet(fglobal, 0.);CHKERRQ(ierr);
81501eb70e55SToby Isaac   ierr = VecSet(fglobalo, 0.);CHKERRQ(ierr);
81511eb70e55SToby Isaac   ierr = VecSet(flocal, 0.);CHKERRQ(ierr);
81521eb70e55SToby Isaac   ierr = DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);CHKERRQ(ierr);
81531eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
81541eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
81551eb70e55SToby Isaac   ierr = MatMult(interpo, cglobalo, fglobalo);CHKERRQ(ierr);
81561eb70e55SToby Isaac   ierr = DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
81571eb70e55SToby Isaac   ierr = DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
81581eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
81591eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
81601eb70e55SToby Isaac   *shift = fglobal;
81611eb70e55SToby Isaac   ierr = VecDestroy(&flocal);CHKERRQ(ierr);
81621eb70e55SToby Isaac   ierr = VecDestroy(&fglobalo);CHKERRQ(ierr);
81631eb70e55SToby Isaac   ierr = VecDestroy(&clocal);CHKERRQ(ierr);
81641eb70e55SToby Isaac   ierr = VecDestroy(&cglobalo);CHKERRQ(ierr);
81651eb70e55SToby Isaac   ierr = VecDestroy(&rscale);CHKERRQ(ierr);
81661eb70e55SToby Isaac   ierr = MatDestroy(&interpo);CHKERRQ(ierr);
81671eb70e55SToby Isaac   ierr = DMDestroy(&dmfo);CHKERRQ(ierr);
81681eb70e55SToby Isaac   ierr = DMDestroy(&dmco);CHKERRQ(ierr);
81691eb70e55SToby Isaac   PetscFunctionReturn(0);
81701eb70e55SToby Isaac }
81711eb70e55SToby Isaac 
81721eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
81731eb70e55SToby Isaac {
81741eb70e55SToby Isaac   PetscObject    shifto;
81751eb70e55SToby Isaac   Vec            shift;
81761eb70e55SToby Isaac 
81771eb70e55SToby Isaac   PetscErrorCode ierr;
81781eb70e55SToby Isaac 
81791eb70e55SToby Isaac   PetscFunctionBegin;
81801eb70e55SToby Isaac   if (!interp) {
81811eb70e55SToby Isaac     Vec rscale;
81821eb70e55SToby Isaac 
81831eb70e55SToby Isaac     ierr = DMCreateInterpolation(coarse, fine, &interp, &rscale);CHKERRQ(ierr);
81841eb70e55SToby Isaac     ierr = VecDestroy(&rscale);CHKERRQ(ierr);
81851eb70e55SToby Isaac   } else {
81861eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)interp);CHKERRQ(ierr);
81871eb70e55SToby Isaac   }
81881eb70e55SToby Isaac   ierr = PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);CHKERRQ(ierr);
81891eb70e55SToby Isaac   if (!shifto) {
81901eb70e55SToby Isaac     ierr = DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);CHKERRQ(ierr);
81911eb70e55SToby Isaac     ierr = PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);CHKERRQ(ierr);
81921eb70e55SToby Isaac     shifto = (PetscObject) shift;
81931eb70e55SToby Isaac     ierr = VecDestroy(&shift);CHKERRQ(ierr);
81941eb70e55SToby Isaac   }
81951eb70e55SToby Isaac   shift = (Vec) shifto;
81961eb70e55SToby Isaac   ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
81971eb70e55SToby Isaac   ierr = VecAXPY(fineSol, 1.0, shift);CHKERRQ(ierr);
81981eb70e55SToby Isaac   ierr = MatDestroy(&interp);CHKERRQ(ierr);
81991eb70e55SToby Isaac   PetscFunctionReturn(0);
82001eb70e55SToby Isaac }
82011eb70e55SToby Isaac 
8202bceba477SMatthew G. Knepley /* Pointwise interpolation
8203bceba477SMatthew G. Knepley      Just code FEM for now
8204bceba477SMatthew G. Knepley      u^f = I u^c
82054ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
82064ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
82074ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
8208bceba477SMatthew G. Knepley */
8209bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8210bceba477SMatthew G. Knepley {
8211bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
8212bceba477SMatthew G. Knepley   PetscInt       m, n;
8213a063dac3SMatthew G. Knepley   void          *ctx;
821468132eb9SMatthew G. Knepley   DM             cdm;
8215cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
8216bceba477SMatthew G. Knepley   PetscErrorCode ierr;
8217bceba477SMatthew G. Knepley 
8218bceba477SMatthew G. Knepley   PetscFunctionBegin;
8219e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8220bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8221e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8222bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
822368132eb9SMatthew G. Knepley 
8224fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
8225bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
8226bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8227fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
8228a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
822968132eb9SMatthew G. Knepley 
8230a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
823168132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8232cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
823368132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
823468132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
82354db47ee9SStefano Zampini   if (scaling) {
82365d1c2e58SMatthew G. Knepley     /* Use naive scaling */
82375d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
82384db47ee9SStefano Zampini   }
8239a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
8240a063dac3SMatthew G. Knepley }
8241bceba477SMatthew G. Knepley 
82426dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8243a063dac3SMatthew G. Knepley {
824490748bafSMatthew G. Knepley   PetscErrorCode ierr;
82456dbf9973SLawrence Mitchell   VecScatter     ctx;
824690748bafSMatthew G. Knepley 
8247a063dac3SMatthew G. Knepley   PetscFunctionBegin;
82486dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
82496dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
82506dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
8251bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
8252bceba477SMatthew G. Knepley }
8253bceba477SMatthew G. Knepley 
82543e9753d6SMatthew G. Knepley static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
82553e9753d6SMatthew G. Knepley                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
82563e9753d6SMatthew G. Knepley                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
82573e9753d6SMatthew G. Knepley                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
82583e9753d6SMatthew G. Knepley {
82593e9753d6SMatthew G. Knepley   g0[0] = 1.0;
82603e9753d6SMatthew G. Knepley }
82613e9753d6SMatthew G. Knepley 
8262bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8263bd041c0cSMatthew G. Knepley {
8264bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
8265bd041c0cSMatthew G. Knepley   PetscInt       m, n;
8266bd041c0cSMatthew G. Knepley   void          *ctx;
8267bd041c0cSMatthew G. Knepley   DM             cdm;
8268bd041c0cSMatthew G. Knepley   PetscBool      regular;
8269bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
8270bd041c0cSMatthew G. Knepley 
8271bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
82723e9753d6SMatthew G. Knepley   if (dmFine == dmCoarse) {
82733e9753d6SMatthew G. Knepley     DM       dmc;
82743e9753d6SMatthew G. Knepley     PetscDS  ds;
82753e9753d6SMatthew G. Knepley     Vec      u;
82763e9753d6SMatthew G. Knepley     IS       cellIS;
82776528b96dSMatthew G. Knepley     PetscHashFormKey key;
82783e9753d6SMatthew G. Knepley     PetscInt depth;
82793e9753d6SMatthew G. Knepley 
82803e9753d6SMatthew G. Knepley     ierr = DMClone(dmFine, &dmc);CHKERRQ(ierr);
82813e9753d6SMatthew G. Knepley     ierr = DMCopyDisc(dmFine, dmc);CHKERRQ(ierr);
82823e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
82833e9753d6SMatthew G. Knepley     ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
82843e9753d6SMatthew G. Knepley     ierr = DMCreateMatrix(dmc, mass);CHKERRQ(ierr);
82853e9753d6SMatthew G. Knepley     ierr = DMGetGlobalVector(dmc, &u);CHKERRQ(ierr);
82863e9753d6SMatthew G. Knepley     ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
82873e9753d6SMatthew G. Knepley     ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
82883e9753d6SMatthew G. Knepley     ierr = MatZeroEntries(*mass);CHKERRQ(ierr);
82896528b96dSMatthew G. Knepley     key.label = NULL;
82906528b96dSMatthew G. Knepley     key.value = 0;
82916528b96dSMatthew G. Knepley     key.field = 0;
82926528b96dSMatthew G. Knepley     ierr = DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);CHKERRQ(ierr);
82933e9753d6SMatthew G. Knepley     ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
82943e9753d6SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmc, &u);CHKERRQ(ierr);
82953e9753d6SMatthew G. Knepley     ierr = DMDestroy(&dmc);CHKERRQ(ierr);
82963e9753d6SMatthew G. Knepley   } else {
8297e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8298bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8299e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8300bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
8301bd041c0cSMatthew G. Knepley 
8302bd041c0cSMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
8303bd041c0cSMatthew G. Knepley     ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8304bd041c0cSMatthew G. Knepley     ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
8305bd041c0cSMatthew G. Knepley     ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
8306bd041c0cSMatthew G. Knepley 
8307bd041c0cSMatthew G. Knepley     ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
8308bd041c0cSMatthew G. Knepley     ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8309bd041c0cSMatthew G. Knepley     if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8310bd041c0cSMatthew G. Knepley     else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
83113e9753d6SMatthew G. Knepley   }
8312bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
8313bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
8314bd041c0cSMatthew G. Knepley }
8315bd041c0cSMatthew G. Knepley 
83160aef6b92SMatthew G. Knepley /*@
83170aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
83180aef6b92SMatthew G. Knepley 
83190aef6b92SMatthew G. Knepley   Input Parameter:
83200aef6b92SMatthew G. Knepley . dm - The DMPlex object
83210aef6b92SMatthew G. Knepley 
83220aef6b92SMatthew G. Knepley   Output Parameter:
83230aef6b92SMatthew G. Knepley . regular - The flag
83240aef6b92SMatthew G. Knepley 
83250aef6b92SMatthew G. Knepley   Level: intermediate
83260aef6b92SMatthew G. Knepley 
83270aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
83280aef6b92SMatthew G. Knepley @*/
83290aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
83300aef6b92SMatthew G. Knepley {
83310aef6b92SMatthew G. Knepley   PetscFunctionBegin;
83320aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83330aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
83340aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
83350aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
83360aef6b92SMatthew G. Knepley }
83370aef6b92SMatthew G. Knepley 
83380aef6b92SMatthew G. Knepley /*@
83390aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
83400aef6b92SMatthew G. Knepley 
83410aef6b92SMatthew G. Knepley   Input Parameters:
83420aef6b92SMatthew G. Knepley + dm - The DMPlex object
83430aef6b92SMatthew G. Knepley - regular - The flag
83440aef6b92SMatthew G. Knepley 
83450aef6b92SMatthew G. Knepley   Level: intermediate
83460aef6b92SMatthew G. Knepley 
83470aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
83480aef6b92SMatthew G. Knepley @*/
83490aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
83500aef6b92SMatthew G. Knepley {
83510aef6b92SMatthew G. Knepley   PetscFunctionBegin;
83520aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83530aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
83540aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
83550aef6b92SMatthew G. Knepley }
83560aef6b92SMatthew G. Knepley 
8357412e9a14SMatthew G. Knepley /*@
8358412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
8359412e9a14SMatthew G. Knepley 
8360412e9a14SMatthew G. Knepley   Input Parameter:
8361412e9a14SMatthew G. Knepley . dm - The DMPlex object
8362412e9a14SMatthew G. Knepley 
8363412e9a14SMatthew G. Knepley   Output Parameter:
8364412e9a14SMatthew G. Knepley . cr - The strategy number
8365412e9a14SMatthew G. Knepley 
8366412e9a14SMatthew G. Knepley   Level: intermediate
8367412e9a14SMatthew G. Knepley 
8368412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8369412e9a14SMatthew G. Knepley @*/
8370412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8371412e9a14SMatthew G. Knepley {
8372412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8373412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8374412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
8375412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8376412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8377412e9a14SMatthew G. Knepley }
8378412e9a14SMatthew G. Knepley 
8379412e9a14SMatthew G. Knepley /*@
8380412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
8381412e9a14SMatthew G. Knepley 
8382412e9a14SMatthew G. Knepley   Input Parameters:
8383412e9a14SMatthew G. Knepley + dm - The DMPlex object
8384412e9a14SMatthew G. Knepley - cr - The strategy number
8385412e9a14SMatthew G. Knepley 
8386412e9a14SMatthew G. Knepley   Level: intermediate
8387412e9a14SMatthew G. Knepley 
8388412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8389412e9a14SMatthew G. Knepley @*/
8390412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8391412e9a14SMatthew G. Knepley {
8392412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8393412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8394412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
8395412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8396412e9a14SMatthew G. Knepley }
8397412e9a14SMatthew G. Knepley 
8398f7c74593SToby Isaac /* anchors */
8399a68b90caSToby Isaac /*@
8400f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
8401f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
8402a68b90caSToby Isaac 
8403e228b242SToby Isaac   not collective
8404a68b90caSToby Isaac 
8405a68b90caSToby Isaac   Input Parameters:
8406a68b90caSToby Isaac . dm - The DMPlex object
8407a68b90caSToby Isaac 
8408a68b90caSToby Isaac   Output Parameters:
8409a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
8410a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
8411a68b90caSToby Isaac 
8412a68b90caSToby Isaac 
8413a68b90caSToby Isaac   Level: intermediate
8414a68b90caSToby Isaac 
8415f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8416a68b90caSToby Isaac @*/
8417a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8418a68b90caSToby Isaac {
8419a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
842041e6d900SToby Isaac   PetscErrorCode ierr;
8421a68b90caSToby Isaac 
8422a68b90caSToby Isaac   PetscFunctionBegin;
8423a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
842441e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
8425a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
8426a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
8427a68b90caSToby Isaac   PetscFunctionReturn(0);
8428a68b90caSToby Isaac }
8429a68b90caSToby Isaac 
8430a68b90caSToby Isaac /*@
8431f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
8432f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
8433a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
8434a68b90caSToby Isaac 
8435a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
8436f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
8437a68b90caSToby Isaac 
8438e228b242SToby Isaac   collective on dm
8439a68b90caSToby Isaac 
8440a68b90caSToby Isaac   Input Parameters:
8441a68b90caSToby Isaac + dm - The DMPlex object
8442e228b242SToby 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).
8443e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
8444a68b90caSToby Isaac 
8445a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
8446a68b90caSToby Isaac 
8447a68b90caSToby Isaac   Level: intermediate
8448a68b90caSToby Isaac 
8449f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8450a68b90caSToby Isaac @*/
8451a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8452a68b90caSToby Isaac {
8453a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8454e228b242SToby Isaac   PetscMPIInt    result;
8455a68b90caSToby Isaac   PetscErrorCode ierr;
8456a68b90caSToby Isaac 
8457a68b90caSToby Isaac   PetscFunctionBegin;
8458a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8459e228b242SToby Isaac   if (anchorSection) {
8460e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
8461ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRMPI(ierr);
8462f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8463e228b242SToby Isaac   }
8464e228b242SToby Isaac   if (anchorIS) {
8465e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
8466ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRMPI(ierr);
8467f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8468e228b242SToby Isaac   }
8469a68b90caSToby Isaac 
8470a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
8471a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
8472a68b90caSToby Isaac   plex->anchorSection = anchorSection;
8473a68b90caSToby Isaac 
8474a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
8475a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
8476a68b90caSToby Isaac   plex->anchorIS = anchorIS;
8477a68b90caSToby Isaac 
8478cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8479a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
8480a68b90caSToby Isaac     const PetscInt *anchors;
8481a68b90caSToby Isaac 
8482a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
8483a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
8484a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
8485a68b90caSToby Isaac     for (a = 0; a < size; a++) {
8486a68b90caSToby Isaac       PetscInt p;
8487a68b90caSToby Isaac 
8488a68b90caSToby Isaac       p = anchors[a];
8489a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
8490a68b90caSToby Isaac         PetscInt dof;
8491a68b90caSToby Isaac 
8492a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8493a68b90caSToby Isaac         if (dof) {
8494a68b90caSToby Isaac           PetscErrorCode ierr2;
8495a68b90caSToby Isaac 
8496a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
84978ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8498a68b90caSToby Isaac         }
8499a68b90caSToby Isaac       }
8500a68b90caSToby Isaac     }
8501a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
8502a68b90caSToby Isaac   }
8503f7c74593SToby Isaac   /* reset the generic constraints */
8504f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
8505a68b90caSToby Isaac   PetscFunctionReturn(0);
8506a68b90caSToby Isaac }
8507a68b90caSToby Isaac 
8508f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8509a68b90caSToby Isaac {
8510f7c74593SToby Isaac   PetscSection anchorSection;
85116995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
8512a68b90caSToby Isaac   PetscErrorCode ierr;
8513a68b90caSToby Isaac 
8514a68b90caSToby Isaac   PetscFunctionBegin;
8515a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8516a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
8517e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
8518a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
85196995de1eSToby Isaac   if (numFields) {
8520719ab38cSToby Isaac     PetscInt f;
8521a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
8522719ab38cSToby Isaac 
8523719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
8524719ab38cSToby Isaac       PetscInt numComp;
8525719ab38cSToby Isaac 
8526719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
8527719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
8528719ab38cSToby Isaac     }
85296995de1eSToby Isaac   }
8530a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
85316995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
85326995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
85336995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
85346995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
8535a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
8536a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
8537a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8538a68b90caSToby Isaac     if (dof) {
8539a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
8540a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
8541a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
8542a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
8543a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
8544a68b90caSToby Isaac       }
8545a68b90caSToby Isaac     }
8546a68b90caSToby Isaac   }
8547a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
8548*ae65431dSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *cSec, "Constraint Section");CHKERRQ(ierr);
8549a68b90caSToby Isaac   PetscFunctionReturn(0);
8550a68b90caSToby Isaac }
8551a68b90caSToby Isaac 
8552f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8553a68b90caSToby Isaac {
8554f7c74593SToby Isaac   PetscSection   aSec;
8555*ae65431dSMatthew G. Knepley   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
85560ac89760SToby Isaac   const PetscInt *anchors;
85570ac89760SToby Isaac   PetscInt       numFields, f;
855866ad2231SToby Isaac   IS             aIS;
85590ac89760SToby Isaac   PetscErrorCode ierr;
8560e19f7ee6SMark Adams   MatType        mtype;
8561e19f7ee6SMark Adams   PetscBool      iscuda,iskokkos;
85620ac89760SToby Isaac 
85630ac89760SToby Isaac   PetscFunctionBegin;
85640ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85650ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
85660ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
85670ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
85680ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
8569e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);CHKERRQ(ierr);
8570e19f7ee6SMark Adams   if (!iscuda) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);CHKERRQ(ierr); }
8571e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);CHKERRQ(ierr);
8572e19f7ee6SMark Adams   if (!iskokkos) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);CHKERRQ(ierr); }
8573e19f7ee6SMark Adams   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8574e19f7ee6SMark Adams   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8575e19f7ee6SMark Adams   else mtype = MATSEQAIJ;
8576e19f7ee6SMark Adams   ierr = MatSetType(*cMat,mtype);CHKERRQ(ierr);
8577a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
857866ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
85796995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
85806995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
8581*ae65431dSMatthew G. Knepley   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
85820ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
85830ac89760SToby Isaac   i[0] = 0;
85840ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
85850ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
8586f19733c5SToby Isaac     PetscInt rDof, rOff, r;
8587f19733c5SToby Isaac 
8588f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
8589f19733c5SToby Isaac     if (!rDof) continue;
8590f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
85910ac89760SToby Isaac     if (numFields) {
85920ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
85930ac89760SToby Isaac         annz = 0;
8594f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
8595f19733c5SToby Isaac           a = anchors[rOff + r];
8596*ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
85970ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
85980ac89760SToby Isaac           annz += aDof;
85990ac89760SToby Isaac         }
86000ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
86010ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
86020ac89760SToby Isaac         for (q = 0; q < dof; q++) {
86030ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
86040ac89760SToby Isaac         }
86050ac89760SToby Isaac       }
86060ac89760SToby Isaac     }
86070ac89760SToby Isaac     else {
86080ac89760SToby Isaac       annz = 0;
8609326b8f31SMatthew G. Knepley       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
86100ac89760SToby Isaac       for (q = 0; q < dof; q++) {
8611*ae65431dSMatthew G. Knepley         a = anchors[rOff + q];
8612*ae65431dSMatthew G. Knepley         if (a < sStart || a >= sEnd) continue;
86130ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
86140ac89760SToby Isaac         annz += aDof;
86150ac89760SToby Isaac       }
86160ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
86170ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
86180ac89760SToby Isaac       for (q = 0; q < dof; q++) {
86190ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
86200ac89760SToby Isaac       }
86210ac89760SToby Isaac     }
86220ac89760SToby Isaac   }
86230ac89760SToby Isaac   nnz = i[m];
86240ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
86250ac89760SToby Isaac   offset = 0;
86260ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
86270ac89760SToby Isaac     if (numFields) {
86280ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
86290ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
86300ac89760SToby Isaac         for (q = 0; q < dof; q++) {
86310ac89760SToby Isaac           PetscInt rDof, rOff, r;
86320ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
86330ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
86340ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
86350ac89760SToby Isaac             PetscInt s;
86360ac89760SToby Isaac 
86370ac89760SToby Isaac             a = anchors[rOff + r];
8638*ae65431dSMatthew G. Knepley             if (a < sStart || a >= sEnd) continue;
86390ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
86400ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
86410ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
86420ac89760SToby Isaac               j[offset++] = aOff + s;
86430ac89760SToby Isaac             }
86440ac89760SToby Isaac           }
86450ac89760SToby Isaac         }
86460ac89760SToby Isaac       }
86470ac89760SToby Isaac     }
86480ac89760SToby Isaac     else {
86490ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
86500ac89760SToby Isaac       for (q = 0; q < dof; q++) {
86510ac89760SToby Isaac         PetscInt rDof, rOff, r;
86520ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
86530ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
86540ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
86550ac89760SToby Isaac           PetscInt s;
86560ac89760SToby Isaac 
86570ac89760SToby Isaac           a = anchors[rOff + r];
8658*ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
86590ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
86600ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
86610ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
86620ac89760SToby Isaac             j[offset++] = aOff + s;
86630ac89760SToby Isaac           }
86640ac89760SToby Isaac         }
86650ac89760SToby Isaac       }
86660ac89760SToby Isaac     }
86670ac89760SToby Isaac   }
86680ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
866925570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
867025570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
867166ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
86720ac89760SToby Isaac   PetscFunctionReturn(0);
86730ac89760SToby Isaac }
86740ac89760SToby Isaac 
867566ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
867666ad2231SToby Isaac {
8677f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8678f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
867966ad2231SToby Isaac   Mat            cMat;
868066ad2231SToby Isaac   PetscErrorCode ierr;
868166ad2231SToby Isaac 
868266ad2231SToby Isaac   PetscFunctionBegin;
868366ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8684a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
868566ad2231SToby Isaac   if (anchorSection) {
868644a7f3ddSMatthew G. Knepley     PetscInt Nf;
8687e228b242SToby Isaac 
868892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
8689f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
8690f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
869144a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
869244a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
869366ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
869466ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
869566ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
869666ad2231SToby Isaac   }
869766ad2231SToby Isaac   PetscFunctionReturn(0);
869866ad2231SToby Isaac }
8699a93c429eSMatthew G. Knepley 
8700a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8701a93c429eSMatthew G. Knepley {
8702a93c429eSMatthew G. Knepley   IS             subis;
8703a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
8704a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
8705a93c429eSMatthew G. Knepley 
8706a93c429eSMatthew G. Knepley   PetscFunctionBegin;
870792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
8708a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8709a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8710a93c429eSMatthew G. Knepley   /* Create subdomain */
8711a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
8712a93c429eSMatthew G. Knepley   /* Create submodel */
871397d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
8714a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
871592fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
8716a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
8717e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
8718a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
8719a93c429eSMatthew G. Knepley   if (is) {
8720a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
8721a93c429eSMatthew G. Knepley     IS              spIS;
8722a93c429eSMatthew G. Knepley     const PetscInt *spmap;
8723a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
8724a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8725a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
8726a93c429eSMatthew G. Knepley 
872797d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
8728a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
8729a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
87306f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
87316f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
8732a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
8733a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8734a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
8735a93c429eSMatthew G. Knepley 
8736a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
8737a93c429eSMatthew G. Knepley       if (gdof > 0) {
8738a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8739a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
8740a93c429eSMatthew G. Knepley 
8741a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
8742a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
8743a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
8744a93c429eSMatthew G. Knepley         }
8745a93c429eSMatthew G. Knepley         subSize += pSubSize;
8746a93c429eSMatthew G. Knepley         if (pSubSize) {
8747a93c429eSMatthew G. Knepley           if (bs < 0) {
8748a93c429eSMatthew G. Knepley             bs = pSubSize;
8749a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
8750a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
8751a93c429eSMatthew G. Knepley             bs = 1;
8752a93c429eSMatthew G. Knepley           }
8753a93c429eSMatthew G. Knepley         }
8754a93c429eSMatthew G. Knepley       }
8755a93c429eSMatthew G. Knepley     }
8756a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
8757a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8758a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
8759a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8760a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
8761a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
8762a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8763a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
8764a93c429eSMatthew G. Knepley 
8765a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
8766a93c429eSMatthew G. Knepley       if (gdof > 0) {
8767a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
8768a93c429eSMatthew G. Knepley 
8769a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
8770a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8771a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
8772a93c429eSMatthew G. Knepley 
8773a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
8774a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
8775a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
8776a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
8777a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
8778a93c429eSMatthew G. Knepley           }
8779a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
8780a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
8781a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8782a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
8783a93c429eSMatthew G. Knepley           }
8784a93c429eSMatthew G. Knepley         }
8785a93c429eSMatthew G. Knepley       }
8786a93c429eSMatthew G. Knepley     }
8787a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
8788a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
8789a93c429eSMatthew G. Knepley     if (bs > 1) {
8790a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
8791a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
8792a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
8793a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
8794a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8795a93c429eSMatthew G. Knepley         }
8796a93c429eSMatthew G. Knepley       }
8797a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
8798a93c429eSMatthew G. Knepley     }
8799a93c429eSMatthew G. Knepley     /* Attach nullspace */
8800a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
8801a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8802a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
8803a93c429eSMatthew G. Knepley     }
8804a93c429eSMatthew G. Knepley     if (f < Nf) {
8805a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
8806a93c429eSMatthew G. Knepley 
88078cda7954SMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);CHKERRQ(ierr);
8808a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
8809a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
8810a93c429eSMatthew G. Knepley     }
8811a93c429eSMatthew G. Knepley   }
8812a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
8813a93c429eSMatthew G. Knepley }
8814c0f0dcc3SMatthew G. Knepley 
8815c0f0dcc3SMatthew G. Knepley /*@
8816c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
8817c0f0dcc3SMatthew G. Knepley 
8818c0f0dcc3SMatthew G. Knepley   Input Parameter:
8819c0f0dcc3SMatthew G. Knepley - dm - The DM
8820c0f0dcc3SMatthew G. Knepley 
8821c0f0dcc3SMatthew G. Knepley   Level: developer
8822c0f0dcc3SMatthew G. Knepley 
8823c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8824c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
8825c0f0dcc3SMatthew G. Knepley 
8826c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
8827c0f0dcc3SMatthew G. Knepley @*/
8828c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8829c0f0dcc3SMatthew G. Knepley {
8830e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
8831c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
8832c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
8833c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
8834c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
8835c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
8836c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
8837c0f0dcc3SMatthew G. Knepley   const char        *name;
8838c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
8839e5ed2c37SJose E. Roman #endif
8840c0f0dcc3SMatthew G. Knepley 
8841c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8842c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8843c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
8844c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
8845c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8846c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
8847c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
8848c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
8849c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
8850c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
8851c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
8852c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
8853c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
8854fae64647SBarry 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);
8855c0f0dcc3SMatthew G. Knepley #else
8856c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8857c0f0dcc3SMatthew G. Knepley #endif
8858c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8859c0f0dcc3SMatthew G. Knepley }
8860