xref: /petsc/src/dm/impls/plex/plex.c (revision b08ad5debb040888c9dfea8f44cf65b3877fb5b3)
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 {
1066823f3c5SBlaise Bourdin       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output type 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;
3586823f3c5SBlaise Bourdin   PetscBool      isvtk, ishdf5, isdraw, isglvis, isexodusii;
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);
3686823f3c5SBlaise Bourdin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);CHKERRQ(ierr);
369684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
370552f7358SJed Brown     Vec         locv;
371798534f6SMatthew G. Knepley     PetscObject isZero;
372552f7358SJed Brown     const char *name;
373552f7358SJed Brown 
374c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
375552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
376552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
377552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
378552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
379798534f6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
380798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
381552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
382798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
383c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
384b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
385b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
387b136c2c9SMatthew G. Knepley #else
388b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
389b136c2c9SMatthew G. Knepley #endif
3906823f3c5SBlaise Bourdin   } else if (isexodusii) {
3916823f3c5SBlaise Bourdin #if defined(PETSC_HAVE_EXODUSII)
3926823f3c5SBlaise Bourdin     ierr = VecView_PlexExodusII_Internal(v, viewer);CHKERRQ(ierr);
3936823f3c5SBlaise Bourdin #else
3946823f3c5SBlaise Bourdin     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
3956823f3c5SBlaise Bourdin #endif
396552f7358SJed Brown   } else {
397684b87d9SLisandro Dalcin     PetscBool isseq;
398684b87d9SLisandro Dalcin 
399684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
40055f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
40155f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
402552f7358SJed Brown   }
403552f7358SJed Brown   PetscFunctionReturn(0);
404552f7358SJed Brown }
405552f7358SJed Brown 
406d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
407d930f514SMatthew G. Knepley {
408d930f514SMatthew G. Knepley   DM                dm;
409d930f514SMatthew G. Knepley   MPI_Comm          comm;
410d930f514SMatthew G. Knepley   PetscViewerFormat format;
411d930f514SMatthew G. Knepley   Vec               v;
412d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
413d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
414d930f514SMatthew G. Knepley 
415d930f514SMatthew G. Knepley   PetscFunctionBegin;
416d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
417d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4182c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
419d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
421d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
422d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
423a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
424a8ad634aSStefano Zampini     /* this need a better fix */
425a8ad634aSStefano Zampini     if (dm->useNatural) {
426a8ad634aSStefano Zampini       if (dm->sfNatural) {
427d930f514SMatthew G. Knepley         const char *vecname;
428d930f514SMatthew G. Knepley         PetscInt    n, nroots;
429d930f514SMatthew G. Knepley 
430ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
431d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
432d930f514SMatthew G. Knepley         if (n == nroots) {
433d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
434d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
435d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
436d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
437d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
439d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
440a8ad634aSStefano Zampini     } else v = originalv;
441a8ad634aSStefano Zampini   } else v = originalv;
442a8ad634aSStefano Zampini 
443d930f514SMatthew G. Knepley   if (ishdf5) {
444d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
44539d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
446d930f514SMatthew G. Knepley #else
447d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
448d930f514SMatthew G. Knepley #endif
449d930f514SMatthew G. Knepley   } else if (isvtk) {
450d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
451d930f514SMatthew G. Knepley   } else {
452d930f514SMatthew G. Knepley     PetscBool isseq;
453d930f514SMatthew G. Knepley 
454d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
455d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
456d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
457d930f514SMatthew G. Knepley   }
458a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
459d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
460d930f514SMatthew G. Knepley }
461d930f514SMatthew G. Knepley 
4622c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4632c40f234SMatthew G. Knepley {
4642c40f234SMatthew G. Knepley   DM             dm;
4652c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4662c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4672c40f234SMatthew G. Knepley 
4682c40f234SMatthew G. Knepley   PetscFunctionBegin;
4692c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4702c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4712c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley   if (ishdf5) {
4732c40f234SMatthew G. Knepley     DM          dmBC;
4742c40f234SMatthew G. Knepley     Vec         gv;
4752c40f234SMatthew G. Knepley     const char *name;
4762c40f234SMatthew G. Knepley 
4772c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4782c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4802c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4812c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4822c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4832c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4842c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4852c40f234SMatthew G. Knepley   } else {
4862c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4872c40f234SMatthew G. Knepley   }
4882c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4892c40f234SMatthew G. Knepley }
4902c40f234SMatthew G. Knepley 
4912c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4922c40f234SMatthew G. Knepley {
4932c40f234SMatthew G. Knepley   DM             dm;
4946823f3c5SBlaise Bourdin   PetscBool      ishdf5,isexodusii;
4952c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4962c40f234SMatthew G. Knepley 
4972c40f234SMatthew G. Knepley   PetscFunctionBegin;
4982c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4992c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
5002c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
5016823f3c5SBlaise Bourdin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);CHKERRQ(ierr);
5022c40f234SMatthew G. Knepley   if (ishdf5) {
503878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
50439d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
505b136c2c9SMatthew G. Knepley #else
506b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
507878b459fSMatthew G. Knepley #endif
5086823f3c5SBlaise Bourdin   } else if (isexodusii) {
5096823f3c5SBlaise Bourdin #if defined(PETSC_HAVE_EXODUSII)
5106823f3c5SBlaise Bourdin     ierr = VecLoad_PlexExodusII_Internal(v, viewer);CHKERRQ(ierr);
5116823f3c5SBlaise Bourdin #else
5126823f3c5SBlaise Bourdin     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
5136823f3c5SBlaise Bourdin #endif
5142c40f234SMatthew G. Knepley   } else {
5152c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
516552f7358SJed Brown   }
517552f7358SJed Brown   PetscFunctionReturn(0);
518552f7358SJed Brown }
519552f7358SJed Brown 
520d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
521d930f514SMatthew G. Knepley {
522d930f514SMatthew G. Knepley   DM                dm;
523d930f514SMatthew G. Knepley   PetscViewerFormat format;
524d930f514SMatthew G. Knepley   PetscBool         ishdf5;
525d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
526d930f514SMatthew G. Knepley 
527d930f514SMatthew G. Knepley   PetscFunctionBegin;
528d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
530d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
532d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
533a8ad634aSStefano Zampini     if (dm->useNatural) {
534d930f514SMatthew G. Knepley       if (dm->sfNatural) {
535d930f514SMatthew G. Knepley         if (ishdf5) {
536d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
537d930f514SMatthew G. Knepley           Vec         v;
538d930f514SMatthew G. Knepley           const char *vecname;
539d930f514SMatthew G. Knepley 
540d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
541d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
542d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
54339d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
544d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
545d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
546d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
547d930f514SMatthew G. Knepley #else
548d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
549d930f514SMatthew G. Knepley #endif
550d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
551d930f514SMatthew G. Knepley       }
552a8ad634aSStefano Zampini     } else {
553a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
554a8ad634aSStefano Zampini     }
555d930f514SMatthew G. Knepley   }
556d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
557d930f514SMatthew G. Knepley }
558d930f514SMatthew G. Knepley 
5597cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
560731e8ddeSMatthew G. Knepley {
561731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
562731e8ddeSMatthew G. Knepley   Vec                coordinates;
563ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
564731e8ddeSMatthew G. Knepley   const char        *name[4];
565731e8ddeSMatthew G. Knepley   const PetscScalar *a;
566731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
567731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
568731e8ddeSMatthew G. Knepley 
569731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
570731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
571731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
572731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
573731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
574ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
575731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
577731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
578731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
579731e8ddeSMatthew G. Knepley   name[1]     = "edge";
580731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
581731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
582731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
583731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
584ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
585731e8ddeSMatthew G. Knepley 
586ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
587ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
588731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
590731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
591731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
592731e8ddeSMatthew G. Knepley 
593731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
594731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
595731e8ddeSMatthew G. Knepley       if (!dof) continue;
596731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
597731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
598f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
599731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
600731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
601731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
602731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
603087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
604731e8ddeSMatthew G. Knepley         }
605731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
606731e8ddeSMatthew G. Knepley       }
607731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
608731e8ddeSMatthew G. Knepley     }
609731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
610731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
611731e8ddeSMatthew G. Knepley   }
612731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
613731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
614731e8ddeSMatthew G. Knepley }
615731e8ddeSMatthew G. Knepley 
6167cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
617552f7358SJed Brown {
618552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
619552f7358SJed Brown   DM                cdm;
620412e9a14SMatthew G. Knepley   DMLabel           markers, celltypes;
621552f7358SJed Brown   PetscSection      coordSection;
622552f7358SJed Brown   Vec               coordinates;
623552f7358SJed Brown   PetscViewerFormat format;
624552f7358SJed Brown   PetscErrorCode    ierr;
625552f7358SJed Brown 
626552f7358SJed Brown   PetscFunctionBegin;
627552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62892fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
629552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
630552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
631552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
632552f7358SJed Brown     const char *name;
633f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
634552f7358SJed Brown     PetscInt    pStart, pEnd, p;
635552f7358SJed Brown     PetscMPIInt rank, size;
636552f7358SJed Brown 
637ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
638ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
639552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
640552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
641552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
642f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
643f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
644f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
645f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
646f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
647e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6484d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
649e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
650552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
651552f7358SJed Brown       PetscInt dof, off, s;
652552f7358SJed Brown 
653552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
654552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
655552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
656e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
657552f7358SJed Brown       }
658552f7358SJed Brown     }
659552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
660e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
661e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
662552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
663552f7358SJed Brown       PetscInt dof, off, c;
664552f7358SJed Brown 
665552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
666552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
667552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
668e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
669552f7358SJed Brown       }
670552f7358SJed Brown     }
671552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6724d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6733d2e540fSStefano Zampini     if (coordSection && coordinates) {
67480180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6753d2e540fSStefano Zampini     }
676c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6777422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
678412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &celltypes);CHKERRQ(ierr);
679412e9a14SMatthew G. Knepley     if (celltypes) {ierr = DMLabelView(celltypes, viewer);CHKERRQ(ierr);}
680552f7358SJed Brown     if (size > 1) {
681552f7358SJed Brown       PetscSF sf;
682552f7358SJed Brown 
683552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
684552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
685552f7358SJed Brown     }
686552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
687552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6880588280cSMatthew G. Knepley     const char  *name, *color;
6890588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6900588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
691fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
692552f7358SJed Brown     PetscReal    scale         = 2.0;
69378081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6940588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6950588280cSMatthew G. Knepley     double       tcoords[3];
696552f7358SJed Brown     PetscScalar *coords;
6970588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
698552f7358SJed Brown     PetscMPIInt  rank, size;
6990588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
700fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
701fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
702fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
703552f7358SJed Brown 
7040588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
705552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
706c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
7070588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
7080588280cSMatthew G. Knepley     numColors  = 10;
7090588280cSMatthew G. Knepley     numLColors = 10;
7100588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
711c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
71278081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
713c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
714c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
7150588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
716c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
7170588280cSMatthew G. Knepley     if (!useColors) {
7180588280cSMatthew G. Knepley       numColors = 3;
7190588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
7200588280cSMatthew G. Knepley     }
721c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
7220588280cSMatthew G. Knepley     if (!useColors) {
7230588280cSMatthew G. Knepley       numLColors = 4;
7240588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7250588280cSMatthew G. Knepley     }
726589a23caSBarry Smith     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);CHKERRQ(ierr);
727202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
728202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
729202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
730202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
731fe1cc32dSStefano Zampini 
732fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
733fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
734fe1cc32dSStefano Zampini     if (lflg) {
735fe1cc32dSStefano Zampini       DMLabel lbl;
736fe1cc32dSStefano Zampini 
737fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
738fe1cc32dSStefano Zampini       if (lbl) {
739fe1cc32dSStefano Zampini         PetscInt val, defval;
740fe1cc32dSStefano Zampini 
741fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
742fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
743fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
744fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
745fe1cc32dSStefano Zampini           PetscInt  closureSize;
746fe1cc32dSStefano Zampini 
747fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
748fe1cc32dSStefano Zampini           if (val == defval) continue;
749fe1cc32dSStefano Zampini 
750fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
751fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
752fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
753fe1cc32dSStefano Zampini           }
754fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
755fe1cc32dSStefano Zampini         }
756fe1cc32dSStefano Zampini       }
757fe1cc32dSStefano Zampini     }
758fe1cc32dSStefano Zampini 
759ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
760ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
761552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
762770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7630588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
764552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
765552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
766552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7670588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7680588280cSMatthew G. Knepley     if (size > 1) {
769f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
770770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
771770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
772770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
773770b213bSMatthew G Knepley         } else if (p > 0) {
774770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
775770b213bSMatthew G Knepley         }
776770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
777770b213bSMatthew G Knepley       }
7780588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7790588280cSMatthew G. Knepley     }
780087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
781fe1cc32dSStefano Zampini 
782552f7358SJed Brown     /* Plot vertices */
783552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
784552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7854d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
786552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
787552f7358SJed Brown       PetscInt  off, dof, d;
7880588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
789552f7358SJed Brown 
790fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
791552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
792552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7930588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
794f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7950588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7960588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
797c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7980588280cSMatthew G. Knepley       }
7990588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
8000588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
801552f7358SJed Brown       for (d = 0; d < dof; ++d) {
802552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
803087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
804552f7358SJed Brown       }
8050588280cSMatthew G. Knepley       color = colors[rank%numColors];
8060588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
8070588280cSMatthew G. Knepley         PetscInt val;
808c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8090588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
8100588280cSMatthew G. Knepley       }
8110588280cSMatthew G. Knepley       if (useNumbers) {
812e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
8130588280cSMatthew G. Knepley       } else {
814e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
8150588280cSMatthew G. Knepley       }
816552f7358SJed Brown     }
817552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
818552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
819846a3e8bSMatthew G. Knepley     /* Plot cells */
820846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
82178081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
822846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
823846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
824846a3e8bSMatthew G. Knepley         const PetscInt *cone;
825846a3e8bSMatthew G. Knepley 
826fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
827846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
828846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
829846a3e8bSMatthew G. Knepley           PetscInt val;
830846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
831846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
832846a3e8bSMatthew G. Knepley         }
833846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
834846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
835846a3e8bSMatthew G. Knepley       }
836846a3e8bSMatthew G. Knepley     } else {
837846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
838846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
839846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
840846a3e8bSMatthew G. Knepley 
841fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
842846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
843846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
844846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
845846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
846846a3e8bSMatthew G. Knepley 
847846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
848846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
849846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
850846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
851846a3e8bSMatthew G. Knepley         }
852846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
853846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
854846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
855846a3e8bSMatthew G. Knepley       }
856846a3e8bSMatthew G. Knepley     }
857846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
858846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
859846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
860846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
861846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
862846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
863846a3e8bSMatthew G. Knepley 
864fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
865846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
866846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
867846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
868846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
869846a3e8bSMatthew G. Knepley         PetscInt       off;
870846a3e8bSMatthew G. Knepley 
871846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
872846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
873846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
874846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
875846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
876846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
877846a3e8bSMatthew G. Knepley         }
878846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
879846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
880846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
881846a3e8bSMatthew G. Knepley         ++n;
882846a3e8bSMatthew G. Knepley       }
883846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
884846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
885846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
886846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
887087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
888846a3e8bSMatthew G. Knepley       }
889846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
890846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
891846a3e8bSMatthew G. Knepley         PetscInt val;
892846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
893846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
894846a3e8bSMatthew G. Knepley       }
895846a3e8bSMatthew G. Knepley       if (useNumbers) {
896846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
897846a3e8bSMatthew G. Knepley       } else {
898846a3e8bSMatthew 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);
899846a3e8bSMatthew G. Knepley       }
900846a3e8bSMatthew G. Knepley     }
901846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
902552f7358SJed Brown     /* Plot edges */
903202fd40aSStefano Zampini     if (plotEdges) {
904552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
905552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
906552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
907552f7358SJed Brown         const PetscInt *cone;
908552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
909552f7358SJed Brown 
910fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
911552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
912f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
913552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
914552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
915552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
916552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
917552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
918552f7358SJed Brown         for (d = 0; d < dof; ++d) {
919202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
920c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
921552f7358SJed Brown         }
9220588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
9230588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
9240588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9250588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
926f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9270588280cSMatthew G. Knepley         }
9280588280cSMatthew G. Knepley         color = colors[rank%numColors];
9290588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9300588280cSMatthew G. Knepley           PetscInt val;
931c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9320750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9330588280cSMatthew G. Knepley         }
934e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
935552f7358SJed Brown       }
936552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
937552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
938552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9390588280cSMatthew G. Knepley     }
940552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9414d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9420588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
943770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9440588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9450588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9460588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9470588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
948fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9490f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9500f7d6e4aSStefano Zampini     Vec                    cown,acown;
9510f7d6e4aSStefano Zampini     VecScatter             sct;
9520f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9530f7d6e4aSStefano Zampini     IS                     gid,acis;
9540f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9550f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9560f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9570f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9580f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9590f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9600f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9610f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
9620f7d6e4aSStefano Zampini 
9630f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
964ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
965b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
966ffc4695bSBarry Smith     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRMPI(ierr);
9670f7d6e4aSStefano Zampini #endif
9680f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
969ffc4695bSBarry Smith       ierr = MPI_Comm_group(comm,&ggroup);CHKERRMPI(ierr);
970ffc4695bSBarry Smith       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRMPI(ierr);
9710f7d6e4aSStefano Zampini       d1   = 0;
972ffc4695bSBarry Smith       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRMPI(ierr);
9730f7d6e4aSStefano Zampini       nid  = d2;
974ffc4695bSBarry Smith       ierr = MPI_Group_free(&ggroup);CHKERRMPI(ierr);
975ffc4695bSBarry Smith       ierr = MPI_Group_free(&ngroup);CHKERRMPI(ierr);
976ffc4695bSBarry Smith       ierr = MPI_Comm_free(&ncomm);CHKERRMPI(ierr);
9770f7d6e4aSStefano Zampini     } else nid = 0.0;
9780f7d6e4aSStefano Zampini 
9790f7d6e4aSStefano Zampini     /* Get connectivity */
9800f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
9810f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
9820f7d6e4aSStefano Zampini 
9830f7d6e4aSStefano Zampini     /* filter overlapped local cells */
9840f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
9850f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
9860f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
9870f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
9880f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
9890f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
9900f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
9910f7d6e4aSStefano Zampini     }
9920f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
9930f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
9940f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9950f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
9960f7d6e4aSStefano Zampini 
9970f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
9980f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
9990f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
10000f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
10010f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
10020f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
10030f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
10040f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
10050f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10060f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10070f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
10080f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
10090f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
10100f7d6e4aSStefano Zampini 
10110f7d6e4aSStefano Zampini     /* compute edgeCut */
10120f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
10130f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
10140f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
10150f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
10160f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
10170f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
10180f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
10190f7d6e4aSStefano Zampini       PetscInt totl;
10200f7d6e4aSStefano Zampini 
10210f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
10220f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
10230f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
10240f7d6e4aSStefano Zampini         if (work[i] < 0) {
10250f7d6e4aSStefano Zampini           ect  += 1;
10260f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10270f7d6e4aSStefano Zampini         }
10280f7d6e4aSStefano Zampini       }
10290f7d6e4aSStefano Zampini     }
10300f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10310f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10320f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10330f7d6e4aSStefano Zampini     lm[1] = -numVertices;
1034820f2d46SBarry Smith     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRMPI(ierr);
10350f7d6e4aSStefano 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);
10360f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10370f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10380f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1039820f2d46SBarry Smith     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRMPI(ierr);
10400f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1041b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
10420f7d6e4aSStefano 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);
10430f7d6e4aSStefano Zampini #else
10440f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10450f7d6e4aSStefano Zampini #endif
10460f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10470f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10480f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10490f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1050552f7358SJed Brown   } else {
1051412e9a14SMatthew G. Knepley     const char    *name;
1052d80ece95SMatthew G. Knepley     PetscInt      *sizes, *hybsizes, *ghostsizes;
1053412e9a14SMatthew G. Knepley     PetscInt       locDepth, depth, cellHeight, dim, d;
1054d80ece95SMatthew G. Knepley     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1055a57dd577SMatthew G Knepley     PetscInt       numLabels, l;
1056412e9a14SMatthew G. Knepley     DMPolytopeType ct0;
1057412e9a14SMatthew G. Knepley     MPI_Comm       comm;
1058412e9a14SMatthew G. Knepley     PetscMPIInt    size, rank;
1059552f7358SJed Brown 
106082f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
1061ffc4695bSBarry Smith     ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
1062ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
1063c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1064f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10655f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1066f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1067f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1068f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1069552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1070820f2d46SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRMPI(ierr);
1071d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1072d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1073d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1074412e9a14SMatthew G. Knepley     for (d = 0; d <= depth; d++) {
1075412e9a14SMatthew G. Knepley       PetscInt Nc[2] = {0, 0}, ict;
1076412e9a14SMatthew G. Knepley 
1077552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1078412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, pStart, &ct0);CHKERRQ(ierr);
1079412e9a14SMatthew G. Knepley       ict  = ct0;
1080ffc4695bSBarry Smith       ierr = MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1081412e9a14SMatthew G. Knepley       ct0  = (DMPolytopeType) ict;
1082412e9a14SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1083412e9a14SMatthew G. Knepley         DMPolytopeType ct;
1084412e9a14SMatthew G. Knepley 
1085412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
1086412e9a14SMatthew G. Knepley         if (ct == ct0) ++Nc[0];
1087412e9a14SMatthew G. Knepley         else           ++Nc[1];
1088412e9a14SMatthew G. Knepley       }
1089ffc4695bSBarry Smith       ierr = MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1090ffc4695bSBarry Smith       ierr = MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1091ffc4695bSBarry Smith       if (d == depth) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);}
1092412e9a14SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1093834065abSMatthew G. Knepley       for (p = 0; p < size; ++p) {
1094cbb7f117SMark Adams         if (!rank) {
1095412e9a14SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);CHKERRQ(ierr);
1096412e9a14SMatthew G. Knepley           if (hybsizes[p]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);CHKERRQ(ierr);}
1097412e9a14SMatthew G. Knepley           if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1098834065abSMatthew G. Knepley         }
1099cbb7f117SMark Adams       }
1100552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1101552f7358SJed Brown     }
1102d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
1103c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1104a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1105a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1106a57dd577SMatthew G Knepley       DMLabel         label;
1107a57dd577SMatthew G Knepley       const char     *name;
1108a57dd577SMatthew G Knepley       IS              valueIS;
1109a57dd577SMatthew G Knepley       const PetscInt *values;
1110a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1111a57dd577SMatthew G Knepley 
1112c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1113c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1114a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1115d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1116a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1117a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1118120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1119a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1120a57dd577SMatthew G Knepley         PetscInt size;
1121a57dd577SMatthew G Knepley 
1122a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1123a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1124d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1125a57dd577SMatthew G Knepley       }
1126a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1127120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1128a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11294d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1130a57dd577SMatthew G Knepley     }
113134aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
113234aa8a36SMatthew G. Knepley     if (dm->Nf) {
113334aa8a36SMatthew G. Knepley       PetscInt f;
113434aa8a36SMatthew G. Knepley 
113534aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
113634aa8a36SMatthew G. Knepley         const char *name;
113734aa8a36SMatthew G. Knepley 
113834aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
113934aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
114034aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
114134aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
114234aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
114334aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1144ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
114534aa8a36SMatthew G. Knepley         } else {
114634aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
114734aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
114834aa8a36SMatthew G. Knepley         }
114934aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
115034aa8a36SMatthew G. Knepley       }
115134aa8a36SMatthew G. Knepley     }
1152a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11538e7ff633SMatthew G. Knepley     if (cdm) {
11548e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11558e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11568e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11578e7ff633SMatthew G. Knepley     }
1158552f7358SJed Brown   }
1159552f7358SJed Brown   PetscFunctionReturn(0);
1160552f7358SJed Brown }
1161552f7358SJed Brown 
1162e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1163e5c487bfSMatthew G. Knepley {
1164e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1165e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1166e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1167e5c487bfSMatthew G. Knepley 
1168e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1169ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1170e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1171e5c487bfSMatthew G. Knepley   switch (ct) {
1172e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1173e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
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,
1176e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1177e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1178e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1179e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1180e5c487bfSMatthew G. Knepley     break;
1181e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
1182e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1183e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1184e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1185e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1186e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1187e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1188e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1189e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1190e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1191e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1192e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1193e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1194e5c487bfSMatthew G. Knepley     break;
1195e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1196e5c487bfSMatthew G. Knepley   }
1197e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1198e5c487bfSMatthew G. Knepley }
1199e5c487bfSMatthew G. Knepley 
1200e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1201e5c487bfSMatthew G. Knepley {
1202e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1203e5c487bfSMatthew G. Knepley   PetscReal      centroid[2] = {0., 0.};
1204e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1205e5c487bfSMatthew G. Knepley   PetscInt       fillColor, v, e, d;
1206e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1207e5c487bfSMatthew G. Knepley 
1208e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1209ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1210e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1211e5c487bfSMatthew G. Knepley   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1212e5c487bfSMatthew G. Knepley   switch (ct) {
1213e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1214e5c487bfSMatthew G. Knepley     {
1215e5c487bfSMatthew G. Knepley       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
1216e5c487bfSMatthew G. Knepley 
1217e5c487bfSMatthew G. Knepley       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1218e5c487bfSMatthew G. Knepley       for (e = 0; e < 3; ++e) {
1219e5c487bfSMatthew G. Knepley         refCoords[0] = refVertices[e*2+0];
1220e5c487bfSMatthew G. Knepley         refCoords[1] = refVertices[e*2+1];
1221e5c487bfSMatthew G. Knepley         for (d = 1; d <= edgeDiv; ++d) {
1222e5c487bfSMatthew G. Knepley           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1223e5c487bfSMatthew G. Knepley           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1224e5c487bfSMatthew G. Knepley         }
1225e5c487bfSMatthew G. Knepley         ierr = DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);CHKERRQ(ierr);
1226e5c487bfSMatthew G. Knepley         for (d = 0; d < edgeDiv; ++d) {
1227e5c487bfSMatthew 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);
1228e5c487bfSMatthew 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);
1229e5c487bfSMatthew G. Knepley         }
1230e5c487bfSMatthew G. Knepley       }
1231e5c487bfSMatthew G. Knepley     }
1232e5c487bfSMatthew G. Knepley     break;
1233e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1234e5c487bfSMatthew G. Knepley   }
1235e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1236e5c487bfSMatthew G. Knepley }
1237e5c487bfSMatthew G. Knepley 
12387cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1239e412dcbdSMatthew G. Knepley {
1240e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1241e412dcbdSMatthew G. Knepley   DM                 cdm;
1242e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1243e412dcbdSMatthew G. Knepley   Vec                coordinates;
1244e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
124529494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1246e5c487bfSMatthew G. Knepley   PetscReal         *refCoords, *edgeCoords;
1247e5c487bfSMatthew G. Knepley   PetscBool          isnull, drawAffine = PETSC_TRUE;
1248e5c487bfSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1249e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1250e412dcbdSMatthew G. Knepley 
1251e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1252e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1253e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1254e5c487bfSMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);CHKERRQ(ierr);
1255e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);CHKERRQ(ierr);}
1256e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
125792fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1258e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1259e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1260e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1261e412dcbdSMatthew G. Knepley 
1262e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1263e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1264e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1265e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1266e412dcbdSMatthew G. Knepley 
1267e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1268e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1269e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
12700c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
12710c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1272e412dcbdSMatthew G. Knepley   }
1273e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
1274820f2d46SBarry Smith   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1275820f2d46SBarry Smith   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
127629494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1277e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1278e412dcbdSMatthew G. Knepley 
1279cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1280cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1281ba2698f1SMatthew G. Knepley     PetscInt     numCoords;
1282cf3064d3SMatthew G. Knepley 
1283e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);CHKERRQ(ierr);
1284e5c487bfSMatthew G. Knepley     if (drawAffine) {
1285e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCell(dm, draw, c, coords);CHKERRQ(ierr);
1286e5c487bfSMatthew G. Knepley     } else {
1287e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);CHKERRQ(ierr);
1288cf3064d3SMatthew G. Knepley     }
1289cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1290cf3064d3SMatthew G. Knepley   }
1291e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscFree2(refCoords, edgeCoords);CHKERRQ(ierr);}
1292e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1293e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1294e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1295e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1296e412dcbdSMatthew G. Knepley }
1297e412dcbdSMatthew G. Knepley 
12981e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
12991e50132fSMatthew G. Knepley #include <exodusII.h>
13006823f3c5SBlaise Bourdin #include <petscviewerexodusii.h>
13011e50132fSMatthew G. Knepley #endif
13021e50132fSMatthew G. Knepley 
1303552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1304552f7358SJed Brown {
13051e50132fSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1306002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1307552f7358SJed Brown   PetscErrorCode ierr;
1308552f7358SJed Brown 
1309552f7358SJed Brown   PetscFunctionBegin;
1310552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1311552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1312552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);CHKERRQ(ierr);
1313fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
1314c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
1315e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
13168135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
13171e50132fSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);CHKERRQ(ierr);
1318552f7358SJed Brown   if (iascii) {
13198135c375SStefano Zampini     PetscViewerFormat format;
13208135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13218135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
13228135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13238135c375SStefano Zampini     } else {
1324552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
13258135c375SStefano Zampini     }
1326c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1327c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
132839d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1329c6ccd67eSMatthew G. Knepley #else
1330c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1331552f7358SJed Brown #endif
1332e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1333fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1334e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1335e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
13368135c375SStefano Zampini   } else if (isglvis) {
13378135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13381e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
13391e50132fSMatthew G. Knepley   } else if (isexodus) {
13406823f3c5SBlaise Bourdin /*
13416823f3c5SBlaise Bourdin       exodusII requires that all sets be part of exactly one cell set.
13426823f3c5SBlaise Bourdin       If the dm does not have a "Cell Sets" label defined, we create one
13436823f3c5SBlaise Bourdin       with ID 1, containig all cells.
13446823f3c5SBlaise Bourdin       Note that if the Cell Sets label is defined but does not cover all cells,
13456823f3c5SBlaise Bourdin       we may still have a problem. This should probably be checked here or in the viewer;
13466823f3c5SBlaise Bourdin     */
13476823f3c5SBlaise Bourdin     PetscInt numCS;
13486823f3c5SBlaise Bourdin     ierr = DMGetLabelSize(dm,"Cell Sets",&numCS);CHKERRQ(ierr);
13496823f3c5SBlaise Bourdin     if (!numCS){
13501e50132fSMatthew G. Knepley       PetscInt cStart, cEnd, c;
13511e50132fSMatthew G. Knepley       ierr = DMCreateLabel(dm, "Cell Sets");CHKERRQ(ierr);
13521e50132fSMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
13531e50132fSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {ierr = DMSetLabelValue(dm, "Cell Sets", c, 1);CHKERRQ(ierr);}
13546823f3c5SBlaise Bourdin     }
13556823f3c5SBlaise Bourdin     ierr = DMView_PlexExodusII(dm, viewer);CHKERRQ(ierr);
13561e50132fSMatthew G. Knepley #endif
135762201deeSVaclav Hapla   } else {
1358a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1359fcf6c8fdSToby Isaac   }
1360cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1361cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1362cb3ba0daSMatthew G. Knepley   if (flg) {
1363cb3ba0daSMatthew G. Knepley     Vec ranks;
1364cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1365cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1366cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1367cb3ba0daSMatthew G. Knepley   }
1368002a2709SMatthew G. Knepley   /* Optionally view a label */
1369589a23caSBarry Smith   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);CHKERRQ(ierr);
1370002a2709SMatthew G. Knepley   if (flg) {
1371002a2709SMatthew G. Knepley     DMLabel label;
1372002a2709SMatthew G. Knepley     Vec     val;
1373002a2709SMatthew G. Knepley 
1374002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1375002a2709SMatthew 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);
1376002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1377002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1378002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1379002a2709SMatthew G. Knepley   }
1380552f7358SJed Brown   PetscFunctionReturn(0);
1381552f7358SJed Brown }
1382552f7358SJed Brown 
13832c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13842c40f234SMatthew G. Knepley {
1385d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13862c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13872c40f234SMatthew G. Knepley 
13882c40f234SMatthew G. Knepley   PetscFunctionBegin;
13892c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13902c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13912c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1392d4f5a9a0SVaclav Hapla   if (ishdf5) {
13932c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13949c48423bSVaclav Hapla     PetscViewerFormat format;
13959c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13969c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13979c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1398509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
139939d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1400509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1401b458e8f1SJose E. Roman     PetscFunctionReturn(0);
14022c40f234SMatthew G. Knepley #else
14032c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1404552f7358SJed Brown #endif
1405b458e8f1SJose E. Roman   } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1406552f7358SJed Brown }
1407552f7358SJed Brown 
1408ea8e1828Sksagiyam /*@
1409ea8e1828Sksagiyam   DMPlexTopologyLoad - Loads a topology into a DMPlex
1410ea8e1828Sksagiyam 
1411ea8e1828Sksagiyam   Collective on DM
1412ea8e1828Sksagiyam 
1413ea8e1828Sksagiyam   Input Parameters:
1414ea8e1828Sksagiyam + dm     - The DM into which the topology is loaded
1415ea8e1828Sksagiyam - viewer - The PetscViewer for the saved topology
1416ea8e1828Sksagiyam 
1417dec9e869Sksagiyam   Output Parameters:
1418dec9e869Sksagiyam . sf     - The PetscSF that pushes points in [0, N) to the associated points in the loaded plex, where N is the global number of points; NULL if unneeded
1419dec9e869Sksagiyam 
1420ea8e1828Sksagiyam   Level: advanced
1421ea8e1828Sksagiyam 
1422*b08ad5deSksagiyam .seealso: DMLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1423ea8e1828Sksagiyam @*/
1424dec9e869Sksagiyam PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *sf)
1425ea8e1828Sksagiyam {
1426ea8e1828Sksagiyam   PetscBool      ishdf5;
1427ea8e1828Sksagiyam   PetscErrorCode ierr;
1428ea8e1828Sksagiyam 
1429ea8e1828Sksagiyam   PetscFunctionBegin;
1430ea8e1828Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1431ea8e1828Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1432ea8e1828Sksagiyam   PetscValidPointer(sf, 3);
1433ea8e1828Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1434ea8e1828Sksagiyam   if (ishdf5) {
1435ea8e1828Sksagiyam #if defined(PETSC_HAVE_HDF5)
1436ea8e1828Sksagiyam     PetscViewerFormat format;
1437ea8e1828Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1438ea8e1828Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1439dec9e869Sksagiyam       ierr = DMPlexTopologyLoad_HDF5_Internal(dm, viewer, sf);CHKERRQ(ierr);
1440ea8e1828Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1441ea8e1828Sksagiyam #else
1442ea8e1828Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1443ea8e1828Sksagiyam #endif
1444ea8e1828Sksagiyam   }
1445ea8e1828Sksagiyam   PetscFunctionReturn(0);
1446ea8e1828Sksagiyam }
1447ea8e1828Sksagiyam 
14483e701f1cSksagiyam /*@
14493e701f1cSksagiyam   DMPlexCoordinatesLoad - Loads coordinates into a DMPlex
14503e701f1cSksagiyam 
14513e701f1cSksagiyam   Collective on DM
14523e701f1cSksagiyam 
14533e701f1cSksagiyam   Input Parameters:
14543e701f1cSksagiyam + dm     - The DM into which the coordinates are loaded
14553e701f1cSksagiyam - viewer - The PetscViewer for the saved coordinates
14563e701f1cSksagiyam 
14573e701f1cSksagiyam   Level: advanced
14583e701f1cSksagiyam 
1459*b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
14603e701f1cSksagiyam @*/
14613e701f1cSksagiyam PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer)
14623e701f1cSksagiyam {
14633e701f1cSksagiyam   PetscBool      ishdf5;
14643e701f1cSksagiyam   PetscErrorCode ierr;
14653e701f1cSksagiyam 
14663e701f1cSksagiyam   PetscFunctionBegin;
14673e701f1cSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14683e701f1cSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
14693e701f1cSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
14703e701f1cSksagiyam   if (ishdf5) {
14713e701f1cSksagiyam #if defined(PETSC_HAVE_HDF5)
14723e701f1cSksagiyam     PetscViewerFormat format;
14733e701f1cSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
14743e701f1cSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
14753e701f1cSksagiyam       ierr = DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
14763e701f1cSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
14773e701f1cSksagiyam #else
14783e701f1cSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
14793e701f1cSksagiyam #endif
14803e701f1cSksagiyam   }
14813e701f1cSksagiyam   PetscFunctionReturn(0);
14823e701f1cSksagiyam }
14833e701f1cSksagiyam 
1484*b08ad5deSksagiyam /*@
1485*b08ad5deSksagiyam   DMPlexLabelsLoad - Loads labels into a DMPlex
1486*b08ad5deSksagiyam 
1487*b08ad5deSksagiyam   Collective on DM
1488*b08ad5deSksagiyam 
1489*b08ad5deSksagiyam   Input Parameters:
1490*b08ad5deSksagiyam + dm     - The DM into which the labels are loaded
1491*b08ad5deSksagiyam - viewer - The PetscViewer for the saved labels
1492*b08ad5deSksagiyam 
1493*b08ad5deSksagiyam   Level: advanced
1494*b08ad5deSksagiyam 
1495*b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1496*b08ad5deSksagiyam @*/
1497*b08ad5deSksagiyam PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer)
1498*b08ad5deSksagiyam {
1499*b08ad5deSksagiyam   PetscBool      ishdf5;
1500*b08ad5deSksagiyam   PetscErrorCode ierr;
1501*b08ad5deSksagiyam 
1502*b08ad5deSksagiyam   PetscFunctionBegin;
1503*b08ad5deSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1504*b08ad5deSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1505*b08ad5deSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1506*b08ad5deSksagiyam   if (ishdf5) {
1507*b08ad5deSksagiyam #if defined(PETSC_HAVE_HDF5)
1508*b08ad5deSksagiyam     PetscViewerFormat format;
1509*b08ad5deSksagiyam 
1510*b08ad5deSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1511*b08ad5deSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1512*b08ad5deSksagiyam       ierr = DMPlexLabelsLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1513*b08ad5deSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1514*b08ad5deSksagiyam #else
1515*b08ad5deSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1516*b08ad5deSksagiyam #endif
1517*b08ad5deSksagiyam   }
1518*b08ad5deSksagiyam   PetscFunctionReturn(0);
1519*b08ad5deSksagiyam }
1520*b08ad5deSksagiyam 
1521552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1522552f7358SJed Brown {
1523552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1524552f7358SJed Brown   PetscErrorCode ierr;
1525552f7358SJed Brown 
1526552f7358SJed Brown   PetscFunctionBegin;
15278135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
15288135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
152928d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
15301eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);CHKERRQ(ierr);
15310d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1532552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1533552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1534552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1535552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1536be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1537552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1538552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1539d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1540d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
154177623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1542a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
154397d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
1544552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1545552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1546a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1547a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1548d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1549d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1550d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1551d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1552d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1553d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1554fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
15550a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
1556552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1557552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1558552f7358SJed Brown   PetscFunctionReturn(0);
1559552f7358SJed Brown }
1560552f7358SJed Brown 
1561b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1562552f7358SJed Brown {
15638d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1564acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1565552f7358SJed Brown   PetscInt               localSize;
1566837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1567552f7358SJed Brown   PetscErrorCode         ierr;
1568b412c318SBarry Smith   MatType                mtype;
15691428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1570552f7358SJed Brown 
1571552f7358SJed Brown   PetscFunctionBegin;
1572607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1573b412c318SBarry Smith   mtype = dm->mattype;
1574e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1575552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1576552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
157782f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1578552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1579552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1580552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1581acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1582acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1583552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1584552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1585552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1586552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1587552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1588552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1589552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1590837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1591552f7358SJed Brown   if (!isShell) {
1592be36d101SStefano Zampini     PetscSection subSection;
1593837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
15940be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1595fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1596552f7358SJed Brown 
159700140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1598be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1599be36d101SStefano Zampini       PetscSection section;
1600be36d101SStefano Zampini       PetscInt     size;
1601be36d101SStefano Zampini 
160292fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1603be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1604be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1605be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1606be36d101SStefano Zampini     } else {
160700140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1608be36d101SStefano Zampini     }
1609552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1610be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1611a9d99c84SMatthew G. Knepley       PetscInt bdof;
1612a9d99c84SMatthew G. Knepley 
1613552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1614fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
16151d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
16161d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
16171d17a0a3SMatthew G. Knepley       if (dof) {
16181d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1619be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1620be36d101SStefano Zampini       }
1621be36d101SStefano Zampini       if (isMatIS) {
1622be36d101SStefano Zampini         PetscInt loff,c,off;
1623be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1624be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1625be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1626552f7358SJed Brown       }
16272a28c762SMatthew G Knepley     }
16282a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
16290be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
16300be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
16310be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
16320be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
16336fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
16346fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
16354fa26246SMatthew G. Knepley       PetscInt l;
16366fd5c86aSStefano Zampini 
16376fd5c86aSStefano Zampini       lsize = lsize/bs;
16386fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
16394fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1640be36d101SStefano Zampini     }
1641be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1642be36d101SStefano Zampini     if (isMatIS) {
1643be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1644be36d101SStefano Zampini     }
16451795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
16468d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1647552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1648552f7358SJed Brown   }
1649b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1650552f7358SJed Brown   PetscFunctionReturn(0);
1651552f7358SJed Brown }
1652552f7358SJed Brown 
16537cd05799SMatthew G. Knepley /*@
1654a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1655be36d101SStefano Zampini 
1656be36d101SStefano Zampini   Not collective
1657be36d101SStefano Zampini 
1658be36d101SStefano Zampini   Input Parameter:
1659be36d101SStefano Zampini . mesh - The DMPlex
1660be36d101SStefano Zampini 
1661be36d101SStefano Zampini   Output Parameters:
1662be36d101SStefano Zampini . subsection - The subdomain section
1663be36d101SStefano Zampini 
1664be36d101SStefano Zampini   Level: developer
1665be36d101SStefano Zampini 
1666be36d101SStefano Zampini .seealso:
16677cd05799SMatthew G. Knepley @*/
1668be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1669be36d101SStefano Zampini {
1670be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1671be36d101SStefano Zampini   PetscErrorCode ierr;
1672be36d101SStefano Zampini 
1673be36d101SStefano Zampini   PetscFunctionBegin;
1674be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1675be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1676be36d101SStefano Zampini     PetscSection section;
1677be36d101SStefano Zampini     PetscSF      sf;
1678be36d101SStefano Zampini 
1679be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
168092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1681be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1682be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1683be36d101SStefano Zampini   }
1684be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1685be36d101SStefano Zampini   PetscFunctionReturn(0);
1686be36d101SStefano Zampini }
1687be36d101SStefano Zampini 
1688552f7358SJed Brown /*@
1689552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1690552f7358SJed Brown 
1691552f7358SJed Brown   Not collective
1692552f7358SJed Brown 
1693552f7358SJed Brown   Input Parameter:
1694552f7358SJed Brown . mesh - The DMPlex
1695552f7358SJed Brown 
1696552f7358SJed Brown   Output Parameters:
1697552f7358SJed Brown + pStart - The first mesh point
1698552f7358SJed Brown - pEnd   - The upper bound for mesh points
1699552f7358SJed Brown 
1700552f7358SJed Brown   Level: beginner
1701552f7358SJed Brown 
1702552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1703552f7358SJed Brown @*/
1704552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1705552f7358SJed Brown {
1706552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1707552f7358SJed Brown   PetscErrorCode ierr;
1708552f7358SJed Brown 
1709552f7358SJed Brown   PetscFunctionBegin;
1710552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1711552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1712552f7358SJed Brown   PetscFunctionReturn(0);
1713552f7358SJed Brown }
1714552f7358SJed Brown 
1715552f7358SJed Brown /*@
1716552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1717552f7358SJed Brown 
1718552f7358SJed Brown   Not collective
1719552f7358SJed Brown 
1720552f7358SJed Brown   Input Parameters:
1721552f7358SJed Brown + mesh - The DMPlex
1722552f7358SJed Brown . pStart - The first mesh point
1723552f7358SJed Brown - pEnd   - The upper bound for mesh points
1724552f7358SJed Brown 
1725552f7358SJed Brown   Output Parameters:
1726552f7358SJed Brown 
1727552f7358SJed Brown   Level: beginner
1728552f7358SJed Brown 
1729552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1730552f7358SJed Brown @*/
1731552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1732552f7358SJed Brown {
1733552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1734552f7358SJed Brown   PetscErrorCode ierr;
1735552f7358SJed Brown 
1736552f7358SJed Brown   PetscFunctionBegin;
1737552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1738552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1739552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1740552f7358SJed Brown   PetscFunctionReturn(0);
1741552f7358SJed Brown }
1742552f7358SJed Brown 
1743552f7358SJed Brown /*@
1744eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1745552f7358SJed Brown 
1746552f7358SJed Brown   Not collective
1747552f7358SJed Brown 
1748552f7358SJed Brown   Input Parameters:
1749552f7358SJed Brown + mesh - The DMPlex
1750eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1751552f7358SJed Brown 
1752552f7358SJed Brown   Output Parameter:
1753552f7358SJed Brown . size - The cone size for point p
1754552f7358SJed Brown 
1755552f7358SJed Brown   Level: beginner
1756552f7358SJed Brown 
1757552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1758552f7358SJed Brown @*/
1759552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1760552f7358SJed Brown {
1761552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1762552f7358SJed Brown   PetscErrorCode ierr;
1763552f7358SJed Brown 
1764552f7358SJed Brown   PetscFunctionBegin;
1765552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1766552f7358SJed Brown   PetscValidPointer(size, 3);
1767552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1768552f7358SJed Brown   PetscFunctionReturn(0);
1769552f7358SJed Brown }
1770552f7358SJed Brown 
1771552f7358SJed Brown /*@
1772eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1773552f7358SJed Brown 
1774552f7358SJed Brown   Not collective
1775552f7358SJed Brown 
1776552f7358SJed Brown   Input Parameters:
1777552f7358SJed Brown + mesh - The DMPlex
1778eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1779552f7358SJed Brown - size - The cone size for point p
1780552f7358SJed Brown 
1781552f7358SJed Brown   Output Parameter:
1782552f7358SJed Brown 
1783552f7358SJed Brown   Note:
1784552f7358SJed Brown   This should be called after DMPlexSetChart().
1785552f7358SJed Brown 
1786552f7358SJed Brown   Level: beginner
1787552f7358SJed Brown 
1788552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1789552f7358SJed Brown @*/
1790552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1791552f7358SJed Brown {
1792552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1793552f7358SJed Brown   PetscErrorCode ierr;
1794552f7358SJed Brown 
1795552f7358SJed Brown   PetscFunctionBegin;
1796552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1797552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
17980d644c17SKarl Rupp 
1799552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1800552f7358SJed Brown   PetscFunctionReturn(0);
1801552f7358SJed Brown }
1802552f7358SJed Brown 
1803f5a469b9SMatthew G. Knepley /*@
1804eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1805f5a469b9SMatthew G. Knepley 
1806f5a469b9SMatthew G. Knepley   Not collective
1807f5a469b9SMatthew G. Knepley 
1808f5a469b9SMatthew G. Knepley   Input Parameters:
1809f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1810eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1811f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1812f5a469b9SMatthew G. Knepley 
1813f5a469b9SMatthew G. Knepley   Output Parameter:
1814f5a469b9SMatthew G. Knepley 
1815f5a469b9SMatthew G. Knepley   Note:
1816f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1817f5a469b9SMatthew G. Knepley 
1818f5a469b9SMatthew G. Knepley   Level: beginner
1819f5a469b9SMatthew G. Knepley 
1820f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1821f5a469b9SMatthew G. Knepley @*/
1822f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1823f5a469b9SMatthew G. Knepley {
1824f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1825f5a469b9SMatthew G. Knepley   PetscInt       csize;
1826f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1827f5a469b9SMatthew G. Knepley 
1828f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1829f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1830f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1831f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1832f5a469b9SMatthew G. Knepley 
1833f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1834f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1835f5a469b9SMatthew G. Knepley }
1836f5a469b9SMatthew G. Knepley 
1837552f7358SJed Brown /*@C
1838eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1839552f7358SJed Brown 
1840552f7358SJed Brown   Not collective
1841552f7358SJed Brown 
1842552f7358SJed Brown   Input Parameters:
1843833c876bSVaclav Hapla + dm - The DMPlex
1844eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1845552f7358SJed Brown 
1846552f7358SJed Brown   Output Parameter:
1847552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1848552f7358SJed Brown 
1849552f7358SJed Brown   Level: beginner
1850552f7358SJed Brown 
18513813dfbdSMatthew G Knepley   Fortran Notes:
18523813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18533813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1854922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1855922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
18563813dfbdSMatthew G Knepley 
1857e45f02b0SMatthew G. Knepley .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1858552f7358SJed Brown @*/
1859552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1860552f7358SJed Brown {
1861552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1862552f7358SJed Brown   PetscInt       off;
1863552f7358SJed Brown   PetscErrorCode ierr;
1864552f7358SJed Brown 
1865552f7358SJed Brown   PetscFunctionBegin;
1866552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1867552f7358SJed Brown   PetscValidPointer(cone, 3);
1868552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1869552f7358SJed Brown   *cone = &mesh->cones[off];
1870552f7358SJed Brown   PetscFunctionReturn(0);
1871552f7358SJed Brown }
1872552f7358SJed Brown 
18730ce7577fSVaclav Hapla /*@C
18740ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
18750ce7577fSVaclav Hapla 
18760ce7577fSVaclav Hapla   Not collective
18770ce7577fSVaclav Hapla 
18780ce7577fSVaclav Hapla   Input Parameters:
18790ce7577fSVaclav Hapla + dm - The DMPlex
18800ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
18810ce7577fSVaclav Hapla 
18820ce7577fSVaclav Hapla   Output Parameter:
18830ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
18840ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
18850ce7577fSVaclav Hapla 
18860ce7577fSVaclav Hapla   Level: intermediate
18870ce7577fSVaclav Hapla 
1888d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
18890ce7577fSVaclav Hapla @*/
18900ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
18910ce7577fSVaclav Hapla {
18920ce7577fSVaclav Hapla   PetscSection        cs, newcs;
18930ce7577fSVaclav Hapla   PetscInt            *cones;
18940ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
18950ce7577fSVaclav Hapla   PetscInt            n;
18960ce7577fSVaclav Hapla   PetscErrorCode      ierr;
18970ce7577fSVaclav Hapla 
18980ce7577fSVaclav Hapla   PetscFunctionBegin;
18990ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
19000ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
19010ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
19020ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
19030ce7577fSVaclav Hapla   if (pCones) {
19040ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
19050ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
19060ce7577fSVaclav Hapla   }
19070ce7577fSVaclav Hapla   PetscFunctionReturn(0);
19080ce7577fSVaclav Hapla }
19090ce7577fSVaclav Hapla 
1910af9eab45SVaclav Hapla /*@
1911af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1912d4636a37SVaclav Hapla 
1913d4636a37SVaclav Hapla   Not collective
1914d4636a37SVaclav Hapla 
1915d4636a37SVaclav Hapla   Input Parameters:
1916d4636a37SVaclav Hapla + dm - The DMPlex
1917af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1918d4636a37SVaclav Hapla 
1919d4636a37SVaclav Hapla   Output Parameter:
1920af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1921d4636a37SVaclav Hapla 
1922d4636a37SVaclav Hapla   Level: advanced
1923d4636a37SVaclav Hapla 
1924af9eab45SVaclav Hapla   Notes:
1925af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1926af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1927af9eab45SVaclav Hapla 
1928af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1929d4636a37SVaclav Hapla @*/
1930af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1931d4636a37SVaclav Hapla {
1932af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1933af9eab45SVaclav Hapla   PetscInt            depth;
1934d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1935d4636a37SVaclav Hapla 
1936d4636a37SVaclav Hapla   PetscFunctionBegin;
1937af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1938af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1939af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1940af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1941af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1942af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1943af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1944af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1945af9eab45SVaclav Hapla }
1946af9eab45SVaclav Hapla 
1947af9eab45SVaclav Hapla /*@
1948af9eab45SVaclav 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).
1949af9eab45SVaclav Hapla 
1950af9eab45SVaclav Hapla   Not collective
1951af9eab45SVaclav Hapla 
1952af9eab45SVaclav Hapla   Input Parameters:
1953af9eab45SVaclav Hapla + dm - The DMPlex
1954af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1955af9eab45SVaclav Hapla 
1956af9eab45SVaclav Hapla   Output Parameter:
1957af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1958af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1959af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1960af9eab45SVaclav Hapla 
1961af9eab45SVaclav Hapla   Level: advanced
1962af9eab45SVaclav Hapla 
1963af9eab45SVaclav Hapla   Notes:
1964af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1965af9eab45SVaclav Hapla 
1966af9eab45SVaclav 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.
1967af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1968af9eab45SVaclav Hapla 
1969af9eab45SVaclav 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:
1970af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1971af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1972af9eab45SVaclav Hapla 
1973af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1974af9eab45SVaclav Hapla @*/
1975af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1976af9eab45SVaclav Hapla {
1977af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1978af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1979af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1980af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1981af9eab45SVaclav Hapla   PetscSection        *sections_;
1982af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1983af9eab45SVaclav Hapla 
1984af9eab45SVaclav Hapla   PetscFunctionBegin;
1985af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1986af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1987af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1988af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1989af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1990af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1991af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1992af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1993af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1994af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1995af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1996af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1997af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1998af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1999af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2000af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
2001af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
2002af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
2003af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
2004af9eab45SVaclav Hapla       } else {
2005af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
2006af9eab45SVaclav Hapla       }
2007af9eab45SVaclav Hapla     }
2008af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
2009af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
2010af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
2011af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2012af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
2013af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
2014af9eab45SVaclav Hapla       if (cn > 1) {
2015af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
2016af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
2017af9eab45SVaclav Hapla       } else {
2018af9eab45SVaclav Hapla         newarr[co] = arr[i];
2019af9eab45SVaclav Hapla       }
2020af9eab45SVaclav Hapla     }
2021af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
2022af9eab45SVaclav Hapla     arr = newarr;
2023af9eab45SVaclav Hapla     n = newn;
2024af9eab45SVaclav Hapla   }
2025ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
2026af9eab45SVaclav Hapla   *depth = depth_;
2027af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
2028af9eab45SVaclav Hapla   else {
2029af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
2030af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
2031af9eab45SVaclav Hapla   }
2032af9eab45SVaclav Hapla   if (sections) *sections = sections_;
2033af9eab45SVaclav Hapla   else {
2034af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
2035af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
2036af9eab45SVaclav Hapla   }
2037af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2038af9eab45SVaclav Hapla }
2039af9eab45SVaclav Hapla 
2040af9eab45SVaclav Hapla /*@
2041af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
2042af9eab45SVaclav Hapla 
2043af9eab45SVaclav Hapla   Not collective
2044af9eab45SVaclav Hapla 
2045af9eab45SVaclav Hapla   Input Parameters:
2046af9eab45SVaclav Hapla + dm - The DMPlex
2047af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2048af9eab45SVaclav Hapla 
2049af9eab45SVaclav Hapla   Output Parameter:
2050af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2051af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
2052af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2053af9eab45SVaclav Hapla 
2054af9eab45SVaclav Hapla   Level: advanced
2055af9eab45SVaclav Hapla 
2056af9eab45SVaclav Hapla   Notes:
2057af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
2058af9eab45SVaclav Hapla 
2059af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2060af9eab45SVaclav Hapla @*/
2061af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2062af9eab45SVaclav Hapla {
2063af9eab45SVaclav Hapla   PetscInt            d, depth_;
2064af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2065af9eab45SVaclav Hapla 
2066af9eab45SVaclav Hapla   PetscFunctionBegin;
2067af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2068af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
2069af9eab45SVaclav Hapla   if (depth) *depth = 0;
2070af9eab45SVaclav Hapla   if (expandedPoints) {
2071af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
2072af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
2073af9eab45SVaclav Hapla   }
2074af9eab45SVaclav Hapla   if (sections)  {
2075af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
2076af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
2077af9eab45SVaclav Hapla   }
2078d4636a37SVaclav Hapla   PetscFunctionReturn(0);
2079d4636a37SVaclav Hapla }
2080d4636a37SVaclav Hapla 
2081552f7358SJed Brown /*@
208292371b87SBarry 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
2083552f7358SJed Brown 
2084552f7358SJed Brown   Not collective
2085552f7358SJed Brown 
2086552f7358SJed Brown   Input Parameters:
2087552f7358SJed Brown + mesh - The DMPlex
2088eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2089552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
2090552f7358SJed Brown 
2091552f7358SJed Brown   Output Parameter:
2092552f7358SJed Brown 
2093552f7358SJed Brown   Note:
2094552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2095552f7358SJed Brown 
209692371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
209792371b87SBarry Smith 
2098552f7358SJed Brown   Level: beginner
2099552f7358SJed Brown 
210092371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
2101552f7358SJed Brown @*/
2102552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
2103552f7358SJed Brown {
2104552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2105552f7358SJed Brown   PetscInt       pStart, pEnd;
2106552f7358SJed Brown   PetscInt       dof, off, c;
2107552f7358SJed Brown   PetscErrorCode ierr;
2108552f7358SJed Brown 
2109552f7358SJed Brown   PetscFunctionBegin;
2110552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2111552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2112552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2113552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
2114552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
211582f516ccSBarry 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);
2116552f7358SJed Brown   for (c = 0; c < dof; ++c) {
211782f516ccSBarry 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);
2118552f7358SJed Brown     mesh->cones[off+c] = cone[c];
2119552f7358SJed Brown   }
2120552f7358SJed Brown   PetscFunctionReturn(0);
2121552f7358SJed Brown }
2122552f7358SJed Brown 
2123552f7358SJed Brown /*@C
2124eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
2125552f7358SJed Brown 
2126552f7358SJed Brown   Not collective
2127552f7358SJed Brown 
2128552f7358SJed Brown   Input Parameters:
2129552f7358SJed Brown + mesh - The DMPlex
2130eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2131552f7358SJed Brown 
2132552f7358SJed Brown   Output Parameter:
2133552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2134552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2135552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2136552f7358SJed Brown                     the index of the cone point on which to start.
2137552f7358SJed Brown 
2138552f7358SJed Brown   Level: beginner
2139552f7358SJed Brown 
21403813dfbdSMatthew G Knepley   Fortran Notes:
21413813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21423813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21433b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2144922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
21453813dfbdSMatthew G Knepley 
2146552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2147552f7358SJed Brown @*/
2148552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2149552f7358SJed Brown {
2150552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2151552f7358SJed Brown   PetscInt       off;
2152552f7358SJed Brown   PetscErrorCode ierr;
2153552f7358SJed Brown 
2154552f7358SJed Brown   PetscFunctionBegin;
2155552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
215676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2157552f7358SJed Brown     PetscInt dof;
2158552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2159552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2160552f7358SJed Brown   }
2161552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
21620d644c17SKarl Rupp 
2163552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2164552f7358SJed Brown   PetscFunctionReturn(0);
2165552f7358SJed Brown }
2166552f7358SJed Brown 
2167552f7358SJed Brown /*@
2168eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2169552f7358SJed Brown 
2170552f7358SJed Brown   Not collective
2171552f7358SJed Brown 
2172552f7358SJed Brown   Input Parameters:
2173552f7358SJed Brown + mesh - The DMPlex
2174eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2175552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2176552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2177552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2178552f7358SJed Brown                     the index of the cone point on which to start.
2179552f7358SJed Brown 
2180552f7358SJed Brown   Output Parameter:
2181552f7358SJed Brown 
2182552f7358SJed Brown   Note:
2183552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2184552f7358SJed Brown 
2185552f7358SJed Brown   Level: beginner
2186552f7358SJed Brown 
2187552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2188552f7358SJed Brown @*/
2189552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2190552f7358SJed Brown {
2191552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2192552f7358SJed Brown   PetscInt       pStart, pEnd;
2193552f7358SJed Brown   PetscInt       dof, off, c;
2194552f7358SJed Brown   PetscErrorCode ierr;
2195552f7358SJed Brown 
2196552f7358SJed Brown   PetscFunctionBegin;
2197552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2198552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2199552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2200552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2201552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
220282f516ccSBarry 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);
2203552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2204552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2205552f7358SJed Brown 
2206552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
220782f516ccSBarry 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);
2208552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2209552f7358SJed Brown   }
2210552f7358SJed Brown   PetscFunctionReturn(0);
2211552f7358SJed Brown }
2212552f7358SJed Brown 
22137cd05799SMatthew G. Knepley /*@
2214eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
22157cd05799SMatthew G. Knepley 
22167cd05799SMatthew G. Knepley   Not collective
22177cd05799SMatthew G. Knepley 
22187cd05799SMatthew G. Knepley   Input Parameters:
22197cd05799SMatthew G. Knepley + mesh - The DMPlex
2220eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22217cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
22227cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
22237cd05799SMatthew G. Knepley 
22247cd05799SMatthew G. Knepley   Level: beginner
22257cd05799SMatthew G. Knepley 
22267cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22277cd05799SMatthew G. Knepley @*/
2228552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2229552f7358SJed Brown {
2230552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2231552f7358SJed Brown   PetscInt       pStart, pEnd;
2232552f7358SJed Brown   PetscInt       dof, off;
2233552f7358SJed Brown   PetscErrorCode ierr;
2234552f7358SJed Brown 
2235552f7358SJed Brown   PetscFunctionBegin;
2236552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2237552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
223882f516ccSBarry 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);
223982f516ccSBarry 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);
224077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
224177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
224277c88f5bSMatthew 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);
2243552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2244552f7358SJed Brown   PetscFunctionReturn(0);
2245552f7358SJed Brown }
2246552f7358SJed Brown 
22477cd05799SMatthew G. Knepley /*@
2248eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
22497cd05799SMatthew G. Knepley 
22507cd05799SMatthew G. Knepley   Not collective
22517cd05799SMatthew G. Knepley 
22527cd05799SMatthew G. Knepley   Input Parameters:
22537cd05799SMatthew G. Knepley + mesh - The DMPlex
2254eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22557cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
22567cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
22577cd05799SMatthew G. Knepley 
22587cd05799SMatthew G. Knepley   Level: beginner
22597cd05799SMatthew G. Knepley 
22607cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22617cd05799SMatthew G. Knepley @*/
226277c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
226377c88f5bSMatthew G Knepley {
226477c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
226577c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
226677c88f5bSMatthew G Knepley   PetscInt       dof, off;
226777c88f5bSMatthew G Knepley   PetscErrorCode ierr;
226877c88f5bSMatthew G Knepley 
226977c88f5bSMatthew G Knepley   PetscFunctionBegin;
227077c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
227177c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
227277c88f5bSMatthew 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);
227377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
227477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
227577c88f5bSMatthew 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);
227677c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
227777c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
227877c88f5bSMatthew G Knepley }
227977c88f5bSMatthew G Knepley 
2280552f7358SJed Brown /*@
2281eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2282552f7358SJed Brown 
2283552f7358SJed Brown   Not collective
2284552f7358SJed Brown 
2285552f7358SJed Brown   Input Parameters:
2286552f7358SJed Brown + mesh - The DMPlex
2287eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2288552f7358SJed Brown 
2289552f7358SJed Brown   Output Parameter:
2290552f7358SJed Brown . size - The support size for point p
2291552f7358SJed Brown 
2292552f7358SJed Brown   Level: beginner
2293552f7358SJed Brown 
2294552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2295552f7358SJed Brown @*/
2296552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2297552f7358SJed Brown {
2298552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2299552f7358SJed Brown   PetscErrorCode ierr;
2300552f7358SJed Brown 
2301552f7358SJed Brown   PetscFunctionBegin;
2302552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2303552f7358SJed Brown   PetscValidPointer(size, 3);
2304552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2305552f7358SJed Brown   PetscFunctionReturn(0);
2306552f7358SJed Brown }
2307552f7358SJed Brown 
2308552f7358SJed Brown /*@
2309eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2310552f7358SJed Brown 
2311552f7358SJed Brown   Not collective
2312552f7358SJed Brown 
2313552f7358SJed Brown   Input Parameters:
2314552f7358SJed Brown + mesh - The DMPlex
2315eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2316552f7358SJed Brown - size - The support size for point p
2317552f7358SJed Brown 
2318552f7358SJed Brown   Output Parameter:
2319552f7358SJed Brown 
2320552f7358SJed Brown   Note:
2321552f7358SJed Brown   This should be called after DMPlexSetChart().
2322552f7358SJed Brown 
2323552f7358SJed Brown   Level: beginner
2324552f7358SJed Brown 
2325552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2326552f7358SJed Brown @*/
2327552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2328552f7358SJed Brown {
2329552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2330552f7358SJed Brown   PetscErrorCode ierr;
2331552f7358SJed Brown 
2332552f7358SJed Brown   PetscFunctionBegin;
2333552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2334552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
23350d644c17SKarl Rupp 
2336552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2337552f7358SJed Brown   PetscFunctionReturn(0);
2338552f7358SJed Brown }
2339552f7358SJed Brown 
2340552f7358SJed Brown /*@C
2341eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2342552f7358SJed Brown 
2343552f7358SJed Brown   Not collective
2344552f7358SJed Brown 
2345552f7358SJed Brown   Input Parameters:
2346552f7358SJed Brown + mesh - The DMPlex
2347eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2348552f7358SJed Brown 
2349552f7358SJed Brown   Output Parameter:
2350552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2351552f7358SJed Brown 
2352552f7358SJed Brown   Level: beginner
2353552f7358SJed Brown 
23543813dfbdSMatthew G Knepley   Fortran Notes:
23553813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23563813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23573b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2358922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
23593813dfbdSMatthew G Knepley 
2360e45f02b0SMatthew G. Knepley .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
2361552f7358SJed Brown @*/
2362552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2363552f7358SJed Brown {
2364552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2365552f7358SJed Brown   PetscInt       off;
2366552f7358SJed Brown   PetscErrorCode ierr;
2367552f7358SJed Brown 
2368552f7358SJed Brown   PetscFunctionBegin;
2369552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2370552f7358SJed Brown   PetscValidPointer(support, 3);
2371552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2372552f7358SJed Brown   *support = &mesh->supports[off];
2373552f7358SJed Brown   PetscFunctionReturn(0);
2374552f7358SJed Brown }
2375552f7358SJed Brown 
2376552f7358SJed Brown /*@
237792371b87SBarry 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
2378552f7358SJed Brown 
2379552f7358SJed Brown   Not collective
2380552f7358SJed Brown 
2381552f7358SJed Brown   Input Parameters:
2382552f7358SJed Brown + mesh - The DMPlex
2383eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
238492371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2385552f7358SJed Brown 
2386552f7358SJed Brown   Output Parameter:
2387552f7358SJed Brown 
2388552f7358SJed Brown   Note:
2389552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2390552f7358SJed Brown 
2391552f7358SJed Brown   Level: beginner
2392552f7358SJed Brown 
239392371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2394552f7358SJed Brown @*/
2395552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2396552f7358SJed Brown {
2397552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2398552f7358SJed Brown   PetscInt       pStart, pEnd;
2399552f7358SJed Brown   PetscInt       dof, off, c;
2400552f7358SJed Brown   PetscErrorCode ierr;
2401552f7358SJed Brown 
2402552f7358SJed Brown   PetscFunctionBegin;
2403552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2404552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2405552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2406552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2407552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
240882f516ccSBarry 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);
2409552f7358SJed Brown   for (c = 0; c < dof; ++c) {
241082f516ccSBarry 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);
2411552f7358SJed Brown     mesh->supports[off+c] = support[c];
2412552f7358SJed Brown   }
2413552f7358SJed Brown   PetscFunctionReturn(0);
2414552f7358SJed Brown }
2415552f7358SJed Brown 
24167cd05799SMatthew G. Knepley /*@
2417eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
24187cd05799SMatthew G. Knepley 
24197cd05799SMatthew G. Knepley   Not collective
24207cd05799SMatthew G. Knepley 
24217cd05799SMatthew G. Knepley   Input Parameters:
24227cd05799SMatthew G. Knepley + mesh - The DMPlex
2423eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24247cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
24257cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
24267cd05799SMatthew G. Knepley 
24277cd05799SMatthew G. Knepley   Level: beginner
24287cd05799SMatthew G. Knepley 
24297cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
24307cd05799SMatthew G. Knepley @*/
2431552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2432552f7358SJed Brown {
2433552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2434552f7358SJed Brown   PetscInt       pStart, pEnd;
2435552f7358SJed Brown   PetscInt       dof, off;
2436552f7358SJed Brown   PetscErrorCode ierr;
2437552f7358SJed Brown 
2438552f7358SJed Brown   PetscFunctionBegin;
2439552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2440552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2441552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2442552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
244382f516ccSBarry 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);
244482f516ccSBarry 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);
244582f516ccSBarry 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);
2446552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2447552f7358SJed Brown   PetscFunctionReturn(0);
2448552f7358SJed Brown }
2449552f7358SJed Brown 
2450552f7358SJed Brown /*@C
2451eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2452552f7358SJed Brown 
2453552f7358SJed Brown   Not collective
2454552f7358SJed Brown 
2455552f7358SJed Brown   Input Parameters:
2456552f7358SJed Brown + mesh - The DMPlex
2457eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2458552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24590298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2460552f7358SJed Brown 
2461552f7358SJed Brown   Output Parameters:
2462552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2463552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2464552f7358SJed Brown 
2465552f7358SJed Brown   Note:
24660298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2467552f7358SJed Brown 
24683813dfbdSMatthew G Knepley   Fortran Notes:
24693813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24703813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24713813dfbdSMatthew G Knepley 
24723813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24733813dfbdSMatthew G Knepley 
2474552f7358SJed Brown   Level: beginner
2475552f7358SJed Brown 
2476552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2477552f7358SJed Brown @*/
2478552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2479552f7358SJed Brown {
2480552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2481552f7358SJed Brown   PetscInt       *closure, *fifo;
24820298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2483552f7358SJed Brown   PetscInt        tmpSize, t;
2484552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2485552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2486552f7358SJed Brown   PetscErrorCode  ierr;
2487552f7358SJed Brown 
2488552f7358SJed Brown   PetscFunctionBegin;
2489552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2490552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2491552f7358SJed Brown   /* This is only 1-level */
2492552f7358SJed Brown   if (useCone) {
2493552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2494552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2495552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2496552f7358SJed Brown   } else {
2497552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2498552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2499552f7358SJed Brown   }
2500bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2501bfbcdd7aSMatthew G. Knepley     if (*points) {
2502bfbcdd7aSMatthew G. Knepley       closure = *points;
2503bfbcdd7aSMatthew G. Knepley     } else {
2504bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
250569291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2506bfbcdd7aSMatthew G. Knepley     }
2507bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2508bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2509bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2510bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2511bfbcdd7aSMatthew G. Knepley     }
2512bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2513bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2514bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2515bfbcdd7aSMatthew G. Knepley   }
251624c766afSToby Isaac   {
251724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
251824c766afSToby Isaac 
251924c766afSToby Isaac     c = mesh->maxConeSize;
252024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
252124c766afSToby Isaac     s = mesh->maxSupportSize;
252224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
252324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
252424c766afSToby Isaac   }
252569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2526bfbcdd7aSMatthew G. Knepley   if (*points) {
2527bfbcdd7aSMatthew G. Knepley     closure = *points;
2528bfbcdd7aSMatthew G. Knepley   } else {
252969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2530bfbcdd7aSMatthew G. Knepley   }
2531bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2532552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2533552f7358SJed Brown     const PetscInt cp = tmp[t];
2534552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2535552f7358SJed Brown 
2536552f7358SJed Brown     closure[closureSize]   = cp;
2537552f7358SJed Brown     closure[closureSize+1] = co;
2538552f7358SJed Brown     fifo[fifoSize]         = cp;
2539552f7358SJed Brown     fifo[fifoSize+1]       = co;
2540552f7358SJed Brown   }
2541bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2542552f7358SJed Brown   while (fifoSize - fifoStart) {
2543552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2544552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2545552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2546552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2547552f7358SJed Brown 
2548552f7358SJed Brown     if (useCone) {
2549552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2550552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2551552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2552552f7358SJed Brown     } else {
2553552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2554552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25550298fd71SBarry Smith       tmpO = NULL;
2556552f7358SJed Brown     }
2557552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2558552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2559552f7358SJed Brown       const PetscInt cp = tmp[i];
25602e1b13c2SMatthew 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. */
25612e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25622e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25632e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2564552f7358SJed Brown       PetscInt       c;
2565552f7358SJed Brown 
25662e1b13c2SMatthew G. Knepley       if (rev) {
25672e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
25682e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25692e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25702e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25712e1b13c2SMatthew G. Knepley       }
2572552f7358SJed Brown       /* Check for duplicate */
2573552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2574552f7358SJed Brown         if (closure[c] == cp) break;
2575552f7358SJed Brown       }
2576552f7358SJed Brown       if (c == closureSize) {
2577552f7358SJed Brown         closure[closureSize]   = cp;
2578552f7358SJed Brown         closure[closureSize+1] = co;
2579552f7358SJed Brown         fifo[fifoSize]         = cp;
2580552f7358SJed Brown         fifo[fifoSize+1]       = co;
2581552f7358SJed Brown         closureSize           += 2;
2582552f7358SJed Brown         fifoSize              += 2;
2583552f7358SJed Brown       }
2584552f7358SJed Brown     }
2585552f7358SJed Brown     fifoStart += 2;
2586552f7358SJed Brown   }
2587552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2588552f7358SJed Brown   if (points)    *points    = closure;
258969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2590552f7358SJed Brown   PetscFunctionReturn(0);
2591552f7358SJed Brown }
2592552f7358SJed Brown 
25939bf0dad6SMatthew G. Knepley /*@C
2594eaf898f9SPatrick 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
25959bf0dad6SMatthew G. Knepley 
25969bf0dad6SMatthew G. Knepley   Not collective
25979bf0dad6SMatthew G. Knepley 
25989bf0dad6SMatthew G. Knepley   Input Parameters:
25999bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2600eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
26019bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
26029bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
26039bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
26049bf0dad6SMatthew G. Knepley 
26059bf0dad6SMatthew G. Knepley   Output Parameters:
26069bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
26079bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
26089bf0dad6SMatthew G. Knepley 
26099bf0dad6SMatthew G. Knepley   Note:
26109bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
26119bf0dad6SMatthew G. Knepley 
26129bf0dad6SMatthew G. Knepley   Fortran Notes:
26139bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26149bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
26159bf0dad6SMatthew G. Knepley 
26169bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26179bf0dad6SMatthew G. Knepley 
26189bf0dad6SMatthew G. Knepley   Level: beginner
26199bf0dad6SMatthew G. Knepley 
26209bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
26219bf0dad6SMatthew G. Knepley @*/
26229bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
26239bf0dad6SMatthew G. Knepley {
26249bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
26259bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
26269bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
26279bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
26289bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
26299bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
26309bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
26319bf0dad6SMatthew G. Knepley 
26329bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
26339bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26349bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
26359bf0dad6SMatthew G. Knepley   /* This is only 1-level */
26369bf0dad6SMatthew G. Knepley   if (useCone) {
26379bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
26389bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
26399bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
26409bf0dad6SMatthew G. Knepley   } else {
26419bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
26429bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
26439bf0dad6SMatthew G. Knepley   }
26449bf0dad6SMatthew G. Knepley   if (depth == 1) {
26459bf0dad6SMatthew G. Knepley     if (*points) {
26469bf0dad6SMatthew G. Knepley       closure = *points;
26479bf0dad6SMatthew G. Knepley     } else {
26489bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
264969291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
26509bf0dad6SMatthew G. Knepley     }
26519bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
26529bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
26539bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
26549bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
26559bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
26569bf0dad6SMatthew G. Knepley     }
26579bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
26589bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
26599bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
26609bf0dad6SMatthew G. Knepley   }
266124c766afSToby Isaac   {
266224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
266324c766afSToby Isaac 
266424c766afSToby Isaac     c = mesh->maxConeSize;
266524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
266624c766afSToby Isaac     s = mesh->maxSupportSize;
266724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
266824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
266924c766afSToby Isaac   }
267069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
26719bf0dad6SMatthew G. Knepley   if (*points) {
26729bf0dad6SMatthew G. Knepley     closure = *points;
26739bf0dad6SMatthew G. Knepley   } else {
267469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
26759bf0dad6SMatthew G. Knepley   }
26769bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
26779bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
26789bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
26799bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
26809bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
26819bf0dad6SMatthew G. Knepley 
26829bf0dad6SMatthew G. Knepley     if (ornt < 0) {
26839bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
26849bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
268586b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
26869bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
26879bf0dad6SMatthew G. Knepley     }
26889bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
26899bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
26909bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
26919bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
26929bf0dad6SMatthew G. Knepley   }
26939bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
26949bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
26959bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
26969bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
26979bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
26989bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
26999bf0dad6SMatthew G. Knepley 
27009bf0dad6SMatthew G. Knepley     if (useCone) {
27019bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
27029bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
27039bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
27049bf0dad6SMatthew G. Knepley     } else {
27059bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
27069bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
27079bf0dad6SMatthew G. Knepley       tmpO = NULL;
27089bf0dad6SMatthew G. Knepley     }
27099bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
27109bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
27119bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
27129bf0dad6SMatthew 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. */
27139bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
27149bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
27159bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
27169bf0dad6SMatthew G. Knepley       PetscInt       c;
27179bf0dad6SMatthew G. Knepley 
27189bf0dad6SMatthew G. Knepley       if (rev) {
27199bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
27209bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
27219bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
27229bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
27239bf0dad6SMatthew G. Knepley       }
27249bf0dad6SMatthew G. Knepley       /* Check for duplicate */
27259bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
27269bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
27279bf0dad6SMatthew G. Knepley       }
27289bf0dad6SMatthew G. Knepley       if (c == closureSize) {
27299bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
27309bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
27319bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
27329bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
27339bf0dad6SMatthew G. Knepley         closureSize           += 2;
27349bf0dad6SMatthew G. Knepley         fifoSize              += 2;
27359bf0dad6SMatthew G. Knepley       }
27369bf0dad6SMatthew G. Knepley     }
27379bf0dad6SMatthew G. Knepley     fifoStart += 2;
27389bf0dad6SMatthew G. Knepley   }
27399bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
27409bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
274169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
27429bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
27439bf0dad6SMatthew G. Knepley }
27449bf0dad6SMatthew G. Knepley 
2745552f7358SJed Brown /*@C
2746eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2747552f7358SJed Brown 
2748552f7358SJed Brown   Not collective
2749552f7358SJed Brown 
2750552f7358SJed Brown   Input Parameters:
2751552f7358SJed Brown + mesh - The DMPlex
2752eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2753552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2754e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2755e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2756552f7358SJed Brown 
2757552f7358SJed Brown   Note:
27580298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2759552f7358SJed Brown 
27603813dfbdSMatthew G Knepley   Fortran Notes:
27613813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27623813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27633813dfbdSMatthew G Knepley 
27643813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27653813dfbdSMatthew G Knepley 
2766552f7358SJed Brown   Level: beginner
2767552f7358SJed Brown 
2768552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2769552f7358SJed Brown @*/
2770552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2771552f7358SJed Brown {
2772552f7358SJed Brown   PetscErrorCode ierr;
2773552f7358SJed Brown 
2774552f7358SJed Brown   PetscFunctionBegin;
2775552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2776e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2777e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
277869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
27794ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2780552f7358SJed Brown   PetscFunctionReturn(0);
2781552f7358SJed Brown }
2782552f7358SJed Brown 
2783552f7358SJed Brown /*@
2784eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2785552f7358SJed Brown 
2786552f7358SJed Brown   Not collective
2787552f7358SJed Brown 
2788552f7358SJed Brown   Input Parameter:
2789552f7358SJed Brown . mesh - The DMPlex
2790552f7358SJed Brown 
2791552f7358SJed Brown   Output Parameters:
2792552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2793552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2794552f7358SJed Brown 
2795552f7358SJed Brown   Level: beginner
2796552f7358SJed Brown 
2797552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2798552f7358SJed Brown @*/
2799552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2800552f7358SJed Brown {
2801552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2802552f7358SJed Brown 
2803552f7358SJed Brown   PetscFunctionBegin;
2804552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2805552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2806552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2807552f7358SJed Brown   PetscFunctionReturn(0);
2808552f7358SJed Brown }
2809552f7358SJed Brown 
2810552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2811552f7358SJed Brown {
2812552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2813552f7358SJed Brown   PetscInt       size;
2814552f7358SJed Brown   PetscErrorCode ierr;
2815552f7358SJed Brown 
2816552f7358SJed Brown   PetscFunctionBegin;
2817552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2818552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2819552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
28201795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
28211795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2822ef1259faSMatthew G. Knepley   ierr = PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));CHKERRQ(ierr);
2823552f7358SJed Brown   if (mesh->maxSupportSize) {
2824552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2825552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
28261795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2827ef1259faSMatthew G. Knepley     ierr = PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));CHKERRQ(ierr);
2828552f7358SJed Brown   }
2829552f7358SJed Brown   PetscFunctionReturn(0);
2830552f7358SJed Brown }
2831552f7358SJed Brown 
2832276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2833552f7358SJed Brown {
2834552f7358SJed Brown   PetscErrorCode ierr;
2835552f7358SJed Brown 
2836552f7358SJed Brown   PetscFunctionBegin;
28374d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2838792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2839c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2840736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2841f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2842f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2843f94b4a02SBlaise Bourdin 
28443dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
28453dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2846c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
2847f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2848f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2849f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2850f94b4a02SBlaise Bourdin 
2851f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2852c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2853f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2854f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2855f94b4a02SBlaise Bourdin   }
2856552f7358SJed Brown   PetscFunctionReturn(0);
2857552f7358SJed Brown }
2858552f7358SJed Brown 
28592adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
28602adcc780SMatthew G. Knepley {
28612adcc780SMatthew G. Knepley   PetscErrorCode ierr;
28623dcd263cSBlaise Bourdin   PetscInt       i = 0;
28632adcc780SMatthew G. Knepley 
28642adcc780SMatthew G. Knepley   PetscFunctionBegin;
2865435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2866792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2867c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
28683dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
28693dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
28703dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
28713dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
28723dcd263cSBlaise Bourdin 
28733dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
28743dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2875c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
287692fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
28773dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
28783dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
28793dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
28803dcd263cSBlaise Bourdin 
28813dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2882c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
28833dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
28843dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
28853dcd263cSBlaise Bourdin       break;
28863dcd263cSBlaise Bourdin     }
28873dcd263cSBlaise Bourdin   }
28882adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
28892adcc780SMatthew G. Knepley }
28902adcc780SMatthew G. Knepley 
2891552f7358SJed Brown /*@
2892eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2893552f7358SJed Brown 
2894552f7358SJed Brown   Not collective
2895552f7358SJed Brown 
2896552f7358SJed Brown   Input Parameter:
2897552f7358SJed Brown . mesh - The DMPlex
2898552f7358SJed Brown 
2899552f7358SJed Brown   Output Parameter:
2900552f7358SJed Brown 
2901552f7358SJed Brown   Note:
2902552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2903552f7358SJed Brown 
2904552f7358SJed Brown   Level: beginner
2905552f7358SJed Brown 
2906552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2907552f7358SJed Brown @*/
2908552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2909552f7358SJed Brown {
2910552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2911552f7358SJed Brown   PetscInt      *offsets;
2912552f7358SJed Brown   PetscInt       supportSize;
2913552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2914552f7358SJed Brown   PetscErrorCode ierr;
2915552f7358SJed Brown 
2916552f7358SJed Brown   PetscFunctionBegin;
2917552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291882f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
291930b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2920552f7358SJed Brown   /* Calculate support sizes */
2921552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2922552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2923552f7358SJed Brown     PetscInt dof, off, c;
2924552f7358SJed Brown 
2925552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2926552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2927552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2928552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2929552f7358SJed Brown     }
2930552f7358SJed Brown   }
2931552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2932552f7358SJed Brown     PetscInt dof;
2933552f7358SJed Brown 
2934552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
29350d644c17SKarl Rupp 
2936552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2937552f7358SJed Brown   }
2938552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2939552f7358SJed Brown   /* Calculate supports */
2940552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
29411795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
29421795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2943552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2944552f7358SJed Brown     PetscInt dof, off, c;
2945552f7358SJed Brown 
2946552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2947552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2948552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2949552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2950552f7358SJed Brown       PetscInt       offS;
2951552f7358SJed Brown 
2952552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
29530d644c17SKarl Rupp 
2954552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2955552f7358SJed Brown       ++offsets[q];
2956552f7358SJed Brown     }
2957552f7358SJed Brown   }
2958552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
295930b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2960552f7358SJed Brown   PetscFunctionReturn(0);
2961552f7358SJed Brown }
2962552f7358SJed Brown 
2963277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2964277ea44aSLisandro Dalcin {
2965277ea44aSLisandro Dalcin   IS             stratumIS;
2966277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2967277ea44aSLisandro Dalcin 
2968277ea44aSLisandro Dalcin   PetscFunctionBegin;
2969277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
297076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2971277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2972277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2973277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2974277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2975277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2976277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2977277ea44aSLisandro Dalcin     }
2978277ea44aSLisandro 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);
2979277ea44aSLisandro Dalcin   }
2980277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2981277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2982277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2983277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2984277ea44aSLisandro Dalcin }
2985277ea44aSLisandro Dalcin 
2986552f7358SJed Brown /*@
2987a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
29886dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2989552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2990552f7358SJed Brown   the DAG.
2991552f7358SJed Brown 
2992bf4602e4SToby Isaac   Collective on dm
2993552f7358SJed Brown 
2994552f7358SJed Brown   Input Parameter:
2995552f7358SJed Brown . mesh - The DMPlex
2996552f7358SJed Brown 
2997552f7358SJed Brown   Output Parameter:
2998552f7358SJed Brown 
2999552f7358SJed Brown   Notes:
3000b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3001b1bb481bSMatthew 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
3002b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
3003c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
3004150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
3005552f7358SJed Brown 
3006b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3007b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3008b1bb481bSMatthew 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
3009b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
3010b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
3011b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
3012b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
3013b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
3014b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
3015b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
3016b1bb481bSMatthew Knepley 
3017150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
3018552f7358SJed Brown 
3019552f7358SJed Brown   Level: beginner
3020552f7358SJed Brown 
3021ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
3022552f7358SJed Brown @*/
3023552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
3024552f7358SJed Brown {
3025df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3026aa50250dSMatthew G. Knepley   DMLabel        label;
3027552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3028552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
3029552f7358SJed Brown   PetscErrorCode ierr;
3030552f7358SJed Brown 
3031552f7358SJed Brown   PetscFunctionBegin;
3032552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3033552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3034277ea44aSLisandro Dalcin 
3035277ea44aSLisandro Dalcin   /* Create depth label */
3036aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3037c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
3038aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3039277ea44aSLisandro Dalcin 
3040277ea44aSLisandro Dalcin   {
3041552f7358SJed Brown     /* Initialize roots and count leaves */
3042277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3043277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3044552f7358SJed Brown     PetscInt coneSize, supportSize;
3045552f7358SJed Brown 
3046277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3047552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3048552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3049552f7358SJed Brown       if (!coneSize && supportSize) {
3050277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3051277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3052552f7358SJed Brown         ++numRoots;
3053552f7358SJed Brown       } else if (!supportSize && coneSize) {
3054552f7358SJed Brown         ++numLeaves;
3055552f7358SJed Brown       } else if (!supportSize && !coneSize) {
3056552f7358SJed Brown         /* Isolated points */
3057277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3058277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3059552f7358SJed Brown       }
3060552f7358SJed Brown     }
3061277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
3062277ea44aSLisandro Dalcin   }
3063277ea44aSLisandro Dalcin 
3064552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
3065277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3066277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3067552f7358SJed Brown     PetscInt coneSize, supportSize;
3068552f7358SJed Brown 
3069277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3070552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3071552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3072552f7358SJed Brown       if (!supportSize && coneSize) {
3073277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3074277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3075552f7358SJed Brown       }
3076552f7358SJed Brown     }
3077277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
3078552f7358SJed Brown   } else {
3079277ea44aSLisandro Dalcin     PetscInt level = 0;
3080277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
3081552f7358SJed Brown 
3082277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3083277ea44aSLisandro Dalcin     while (qEnd > qStart) {
3084277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
3085277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
308674ef644bSMatthew G. Knepley 
3087277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
308874ef644bSMatthew G. Knepley         const PetscInt *support;
308974ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
309074ef644bSMatthew G. Knepley 
3091277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
3092277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
309374ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3094277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
3095277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
3096552f7358SJed Brown         }
3097552f7358SJed Brown       }
3098277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
3099277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
3100277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
310174ef644bSMatthew G. Knepley     }
310274ef644bSMatthew G. Knepley   }
3103bf4602e4SToby Isaac   { /* just in case there is an empty process */
3104bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
3105bf4602e4SToby Isaac 
3106bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
3107ffc4695bSBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
3108bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
3109367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
3110bf4602e4SToby Isaac     }
3111bf4602e4SToby Isaac   }
3112d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
3113552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3114552f7358SJed Brown   PetscFunctionReturn(0);
3115552f7358SJed Brown }
3116552f7358SJed Brown 
3117412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
3118ba2698f1SMatthew G. Knepley {
3119412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3120412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
3121ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3122ba2698f1SMatthew G. Knepley 
3123412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
3124ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3125ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3126ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3127ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
3128ba2698f1SMatthew G. Knepley   if (depth <= 1) {
3129ba2698f1SMatthew G. Knepley     switch (pdepth) {
3130ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
3131ba2698f1SMatthew G. Knepley       case 1:
3132ba2698f1SMatthew G. Knepley         switch (coneSize) {
3133ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3134ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3135ba2698f1SMatthew G. Knepley           case 4:
3136ba2698f1SMatthew G. Knepley           switch (dim) {
3137ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3138ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3139ba2698f1SMatthew G. Knepley             default: break;
3140ba2698f1SMatthew G. Knepley           }
3141ba2698f1SMatthew G. Knepley           break;
3142da9060c4SMatthew G. Knepley         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3143ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3144ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3145ba2698f1SMatthew G. Knepley         default: break;
3146ba2698f1SMatthew G. Knepley       }
3147ba2698f1SMatthew G. Knepley     }
3148ba2698f1SMatthew G. Knepley   } else {
3149ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
3150ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
3151ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
3152ba2698f1SMatthew G. Knepley       switch (dim) {
3153ba2698f1SMatthew G. Knepley         case 1:
3154ba2698f1SMatthew G. Knepley           switch (coneSize) {
3155ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3156ba2698f1SMatthew G. Knepley             default: break;
3157ba2698f1SMatthew G. Knepley           }
3158ba2698f1SMatthew G. Knepley           break;
3159ba2698f1SMatthew G. Knepley         case 2:
3160ba2698f1SMatthew G. Knepley           switch (coneSize) {
3161ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3162ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3163ba2698f1SMatthew G. Knepley             default: break;
3164ba2698f1SMatthew G. Knepley           }
3165ba2698f1SMatthew G. Knepley           break;
3166ba2698f1SMatthew G. Knepley         case 3:
3167ba2698f1SMatthew G. Knepley           switch (coneSize) {
3168ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3169da9060c4SMatthew G. Knepley             case 5:
3170da9060c4SMatthew G. Knepley             {
3171da9060c4SMatthew G. Knepley               const PetscInt *cone;
3172da9060c4SMatthew G. Knepley               PetscInt        faceConeSize;
3173da9060c4SMatthew G. Knepley 
3174da9060c4SMatthew G. Knepley               ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
3175da9060c4SMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[0], &faceConeSize);CHKERRQ(ierr);
3176da9060c4SMatthew G. Knepley               switch (faceConeSize) {
3177da9060c4SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3178da9060c4SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
3179da9060c4SMatthew G. Knepley               }
3180da9060c4SMatthew G. Knepley             }
3181da9060c4SMatthew G. Knepley             break;
3182ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3183ba2698f1SMatthew G. Knepley             default: break;
3184ba2698f1SMatthew G. Knepley           }
3185ba2698f1SMatthew G. Knepley           break;
3186ba2698f1SMatthew G. Knepley         default: break;
3187ba2698f1SMatthew G. Knepley       }
3188ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
3189ba2698f1SMatthew G. Knepley       switch (coneSize) {
3190ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3191ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3192ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3193ba2698f1SMatthew G. Knepley         default: break;
3194ba2698f1SMatthew G. Knepley       }
3195ba2698f1SMatthew G. Knepley     }
3196ba2698f1SMatthew G. Knepley   }
3197412e9a14SMatthew G. Knepley   *pt = ct;
3198412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3199ba2698f1SMatthew G. Knepley }
3200412e9a14SMatthew G. Knepley 
3201412e9a14SMatthew G. Knepley /*@
3202412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3203412e9a14SMatthew G. Knepley 
3204412e9a14SMatthew G. Knepley   Collective on dm
3205412e9a14SMatthew G. Knepley 
3206412e9a14SMatthew G. Knepley   Input Parameter:
3207412e9a14SMatthew G. Knepley . mesh - The DMPlex
3208412e9a14SMatthew G. Knepley 
3209412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3210412e9a14SMatthew G. Knepley 
3211412e9a14SMatthew G. Knepley   Level: developer
3212412e9a14SMatthew G. Knepley 
3213412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3214412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3215412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3216412e9a14SMatthew G. Knepley 
3217412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3218412e9a14SMatthew G. Knepley @*/
3219412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3220412e9a14SMatthew G. Knepley {
3221412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3222412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3223412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3224412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3225412e9a14SMatthew G. Knepley 
3226412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3227412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3228412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3229412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3230412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3231412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3232412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3233327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3234412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3235412e9a14SMatthew G. Knepley 
3236412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3237412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3238412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3239412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3240412e9a14SMatthew G. Knepley   }
3241412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3242412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3243ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3244ba2698f1SMatthew G. Knepley }
3245ba2698f1SMatthew G. Knepley 
3246552f7358SJed Brown /*@C
3247552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3248552f7358SJed Brown 
3249552f7358SJed Brown   Not Collective
3250552f7358SJed Brown 
3251552f7358SJed Brown   Input Parameters:
3252552f7358SJed Brown + dm - The DMPlex object
3253552f7358SJed Brown . numPoints - The number of input points for the join
3254552f7358SJed Brown - points - The input points
3255552f7358SJed Brown 
3256552f7358SJed Brown   Output Parameters:
3257552f7358SJed Brown + numCoveredPoints - The number of points in the join
3258552f7358SJed Brown - coveredPoints - The points in the join
3259552f7358SJed Brown 
3260552f7358SJed Brown   Level: intermediate
3261552f7358SJed Brown 
3262552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3263552f7358SJed Brown 
32643813dfbdSMatthew G Knepley   Fortran Notes:
32653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32673813dfbdSMatthew G Knepley 
32683813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32693813dfbdSMatthew G Knepley 
3270552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3271552f7358SJed Brown @*/
3272552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3273552f7358SJed Brown {
3274552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3275552f7358SJed Brown   PetscInt      *join[2];
3276552f7358SJed Brown   PetscInt       joinSize, i = 0;
3277552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3278552f7358SJed Brown   PetscErrorCode ierr;
3279552f7358SJed Brown 
3280552f7358SJed Brown   PetscFunctionBegin;
3281552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
328248bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
328348bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
328448bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
328569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
328669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3287552f7358SJed Brown   /* Copy in support of first point */
3288552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3289552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3290552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3291552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3292552f7358SJed Brown   }
3293552f7358SJed Brown   /* Check each successive support */
3294552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3295552f7358SJed Brown     PetscInt newJoinSize = 0;
3296552f7358SJed Brown 
3297552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3298552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3299552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3300552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3301552f7358SJed Brown 
3302552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3303552f7358SJed Brown         if (point == join[i][m]) {
3304552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3305552f7358SJed Brown           break;
3306552f7358SJed Brown         }
3307552f7358SJed Brown       }
3308552f7358SJed Brown     }
3309552f7358SJed Brown     joinSize = newJoinSize;
3310552f7358SJed Brown     i        = 1-i;
3311552f7358SJed Brown   }
3312552f7358SJed Brown   *numCoveredPoints = joinSize;
3313552f7358SJed Brown   *coveredPoints    = join[i];
331469291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3315552f7358SJed Brown   PetscFunctionReturn(0);
3316552f7358SJed Brown }
3317552f7358SJed Brown 
3318552f7358SJed Brown /*@C
3319552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3320552f7358SJed Brown 
3321552f7358SJed Brown   Not Collective
3322552f7358SJed Brown 
3323552f7358SJed Brown   Input Parameters:
3324552f7358SJed Brown + dm - The DMPlex object
3325552f7358SJed Brown . numPoints - The number of input points for the join
3326552f7358SJed Brown - points - The input points
3327552f7358SJed Brown 
3328552f7358SJed Brown   Output Parameters:
3329552f7358SJed Brown + numCoveredPoints - The number of points in the join
3330552f7358SJed Brown - coveredPoints - The points in the join
3331552f7358SJed Brown 
33323813dfbdSMatthew G Knepley   Fortran Notes:
33333813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33343813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33353813dfbdSMatthew G Knepley 
33363813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33373813dfbdSMatthew G Knepley 
3338552f7358SJed Brown   Level: intermediate
3339552f7358SJed Brown 
3340552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3341552f7358SJed Brown @*/
3342552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3343552f7358SJed Brown {
3344552f7358SJed Brown   PetscErrorCode ierr;
3345552f7358SJed Brown 
3346552f7358SJed Brown   PetscFunctionBegin;
3347552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3348d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3349d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3350d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
335169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3352d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3353552f7358SJed Brown   PetscFunctionReturn(0);
3354552f7358SJed Brown }
3355552f7358SJed Brown 
3356552f7358SJed Brown /*@C
3357552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3358552f7358SJed Brown 
3359552f7358SJed Brown   Not Collective
3360552f7358SJed Brown 
3361552f7358SJed Brown   Input Parameters:
3362552f7358SJed Brown + dm - The DMPlex object
3363552f7358SJed Brown . numPoints - The number of input points for the join
3364552f7358SJed Brown - points - The input points
3365552f7358SJed Brown 
3366552f7358SJed Brown   Output Parameters:
3367552f7358SJed Brown + numCoveredPoints - The number of points in the join
3368552f7358SJed Brown - coveredPoints - The points in the join
3369552f7358SJed Brown 
33703813dfbdSMatthew G Knepley   Fortran Notes:
33713813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33723813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33733813dfbdSMatthew G Knepley 
33743813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33753813dfbdSMatthew G Knepley 
3376552f7358SJed Brown   Level: intermediate
3377552f7358SJed Brown 
3378552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3379552f7358SJed Brown @*/
3380552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3381552f7358SJed Brown {
3382552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3383552f7358SJed Brown   PetscInt      *offsets, **closures;
3384552f7358SJed Brown   PetscInt      *join[2];
3385552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
338624c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3387552f7358SJed Brown   PetscErrorCode ierr;
3388552f7358SJed Brown 
3389552f7358SJed Brown   PetscFunctionBegin;
3390552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
339148bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
339248bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
339348bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3394552f7358SJed Brown 
3395552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
33961795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
339769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
339824c766afSToby Isaac   ms      = mesh->maxSupportSize;
339924c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
340069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
340169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3402552f7358SJed Brown 
3403552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3404552f7358SJed Brown     PetscInt closureSize;
3405552f7358SJed Brown 
3406552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
34070d644c17SKarl Rupp 
3408552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3409552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3410552f7358SJed Brown       PetscInt pStart, pEnd, i;
3411552f7358SJed Brown 
3412552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3413552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3414552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3415552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3416552f7358SJed Brown           break;
3417552f7358SJed Brown         }
3418552f7358SJed Brown       }
3419552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3420552f7358SJed Brown     }
342182f516ccSBarry 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);
3422552f7358SJed Brown   }
3423552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3424552f7358SJed Brown     PetscInt dof;
3425552f7358SJed Brown 
3426552f7358SJed Brown     /* Copy in support of first point */
3427552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3428552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3429552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3430552f7358SJed Brown     }
3431552f7358SJed Brown     /* Check each successive cone */
3432552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3433552f7358SJed Brown       PetscInt newJoinSize = 0;
3434552f7358SJed Brown 
3435552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3436552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3437552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3438552f7358SJed Brown 
3439552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3440552f7358SJed Brown           if (point == join[i][m]) {
3441552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3442552f7358SJed Brown             break;
3443552f7358SJed Brown           }
3444552f7358SJed Brown         }
3445552f7358SJed Brown       }
3446552f7358SJed Brown       joinSize = newJoinSize;
3447552f7358SJed Brown       i        = 1-i;
3448552f7358SJed Brown     }
3449552f7358SJed Brown     if (joinSize) break;
3450552f7358SJed Brown   }
3451552f7358SJed Brown   *numCoveredPoints = joinSize;
3452552f7358SJed Brown   *coveredPoints    = join[i];
3453552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
34540298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3455552f7358SJed Brown   }
3456552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
345769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
345869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3459552f7358SJed Brown   PetscFunctionReturn(0);
3460552f7358SJed Brown }
3461552f7358SJed Brown 
3462552f7358SJed Brown /*@C
3463552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3464552f7358SJed Brown 
3465552f7358SJed Brown   Not Collective
3466552f7358SJed Brown 
3467552f7358SJed Brown   Input Parameters:
3468552f7358SJed Brown + dm - The DMPlex object
3469552f7358SJed Brown . numPoints - The number of input points for the meet
3470552f7358SJed Brown - points - The input points
3471552f7358SJed Brown 
3472552f7358SJed Brown   Output Parameters:
3473552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3474552f7358SJed Brown - coveredPoints - The points in the meet
3475552f7358SJed Brown 
3476552f7358SJed Brown   Level: intermediate
3477552f7358SJed Brown 
3478552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3479552f7358SJed Brown 
34803813dfbdSMatthew G Knepley   Fortran Notes:
34813813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34823813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34833813dfbdSMatthew G Knepley 
34843813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34853813dfbdSMatthew G Knepley 
3486552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3487552f7358SJed Brown @*/
3488552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3489552f7358SJed Brown {
3490552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3491552f7358SJed Brown   PetscInt      *meet[2];
3492552f7358SJed Brown   PetscInt       meetSize, i = 0;
3493552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3494552f7358SJed Brown   PetscErrorCode ierr;
3495552f7358SJed Brown 
3496552f7358SJed Brown   PetscFunctionBegin;
3497552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3498552f7358SJed Brown   PetscValidPointer(points, 2);
3499552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3500552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
350169291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
350269291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3503552f7358SJed Brown   /* Copy in cone of first point */
3504552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3505552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3506552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3507552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3508552f7358SJed Brown   }
3509552f7358SJed Brown   /* Check each successive cone */
3510552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3511552f7358SJed Brown     PetscInt newMeetSize = 0;
3512552f7358SJed Brown 
3513552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3514552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3515552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3516552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3517552f7358SJed Brown 
3518552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3519552f7358SJed Brown         if (point == meet[i][m]) {
3520552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3521552f7358SJed Brown           break;
3522552f7358SJed Brown         }
3523552f7358SJed Brown       }
3524552f7358SJed Brown     }
3525552f7358SJed Brown     meetSize = newMeetSize;
3526552f7358SJed Brown     i        = 1-i;
3527552f7358SJed Brown   }
3528552f7358SJed Brown   *numCoveringPoints = meetSize;
3529552f7358SJed Brown   *coveringPoints    = meet[i];
353069291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3531552f7358SJed Brown   PetscFunctionReturn(0);
3532552f7358SJed Brown }
3533552f7358SJed Brown 
3534552f7358SJed Brown /*@C
3535552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3536552f7358SJed Brown 
3537552f7358SJed Brown   Not Collective
3538552f7358SJed Brown 
3539552f7358SJed Brown   Input Parameters:
3540552f7358SJed Brown + dm - The DMPlex object
3541552f7358SJed Brown . numPoints - The number of input points for the meet
3542552f7358SJed Brown - points - The input points
3543552f7358SJed Brown 
3544552f7358SJed Brown   Output Parameters:
3545552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3546552f7358SJed Brown - coveredPoints - The points in the meet
3547552f7358SJed Brown 
3548552f7358SJed Brown   Level: intermediate
3549552f7358SJed Brown 
35503813dfbdSMatthew G Knepley   Fortran Notes:
35513813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35523813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35533813dfbdSMatthew G Knepley 
35543813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35553813dfbdSMatthew G Knepley 
3556552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3557552f7358SJed Brown @*/
3558552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3559552f7358SJed Brown {
3560552f7358SJed Brown   PetscErrorCode ierr;
3561552f7358SJed Brown 
3562552f7358SJed Brown   PetscFunctionBegin;
3563552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3564d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3565d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3566d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
356769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3568d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3569552f7358SJed Brown   PetscFunctionReturn(0);
3570552f7358SJed Brown }
3571552f7358SJed Brown 
3572552f7358SJed Brown /*@C
3573552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3574552f7358SJed Brown 
3575552f7358SJed Brown   Not Collective
3576552f7358SJed Brown 
3577552f7358SJed Brown   Input Parameters:
3578552f7358SJed Brown + dm - The DMPlex object
3579552f7358SJed Brown . numPoints - The number of input points for the meet
3580552f7358SJed Brown - points - The input points
3581552f7358SJed Brown 
3582552f7358SJed Brown   Output Parameters:
3583552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3584552f7358SJed Brown - coveredPoints - The points in the meet
3585552f7358SJed Brown 
3586552f7358SJed Brown   Level: intermediate
3587552f7358SJed Brown 
35883813dfbdSMatthew G Knepley   Fortran Notes:
35893813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35903813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35913813dfbdSMatthew G Knepley 
35923813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35933813dfbdSMatthew G Knepley 
3594552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3595552f7358SJed Brown @*/
3596552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3597552f7358SJed Brown {
3598552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3599552f7358SJed Brown   PetscInt      *offsets, **closures;
3600552f7358SJed Brown   PetscInt      *meet[2];
3601552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
360224c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3603552f7358SJed Brown   PetscErrorCode ierr;
3604552f7358SJed Brown 
3605552f7358SJed Brown   PetscFunctionBegin;
3606552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3607552f7358SJed Brown   PetscValidPointer(points, 2);
3608552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3609552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3610552f7358SJed Brown 
3611552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3612785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
361369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
361424c766afSToby Isaac   mc      = mesh->maxConeSize;
361524c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
361669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
361769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3618552f7358SJed Brown 
3619552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3620552f7358SJed Brown     PetscInt closureSize;
3621552f7358SJed Brown 
3622552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
36230d644c17SKarl Rupp 
3624552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3625552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3626552f7358SJed Brown       PetscInt pStart, pEnd, i;
3627552f7358SJed Brown 
3628552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3629552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3630552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3631552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3632552f7358SJed Brown           break;
3633552f7358SJed Brown         }
3634552f7358SJed Brown       }
3635552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3636552f7358SJed Brown     }
363782f516ccSBarry 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);
3638552f7358SJed Brown   }
3639552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3640552f7358SJed Brown     PetscInt dof;
3641552f7358SJed Brown 
3642552f7358SJed Brown     /* Copy in cone of first point */
3643552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3644552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3645552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3646552f7358SJed Brown     }
3647552f7358SJed Brown     /* Check each successive cone */
3648552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3649552f7358SJed Brown       PetscInt newMeetSize = 0;
3650552f7358SJed Brown 
3651552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3652552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3653552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3654552f7358SJed Brown 
3655552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3656552f7358SJed Brown           if (point == meet[i][m]) {
3657552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3658552f7358SJed Brown             break;
3659552f7358SJed Brown           }
3660552f7358SJed Brown         }
3661552f7358SJed Brown       }
3662552f7358SJed Brown       meetSize = newMeetSize;
3663552f7358SJed Brown       i        = 1-i;
3664552f7358SJed Brown     }
3665552f7358SJed Brown     if (meetSize) break;
3666552f7358SJed Brown   }
3667552f7358SJed Brown   *numCoveredPoints = meetSize;
3668552f7358SJed Brown   *coveredPoints    = meet[i];
3669552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
36700298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3671552f7358SJed Brown   }
3672552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
367369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
367469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3675552f7358SJed Brown   PetscFunctionReturn(0);
3676552f7358SJed Brown }
3677552f7358SJed Brown 
36784e3744c5SMatthew G. Knepley /*@C
36794e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
36804e3744c5SMatthew G. Knepley 
36814e3744c5SMatthew G. Knepley   Not Collective
36824e3744c5SMatthew G. Knepley 
36834e3744c5SMatthew G. Knepley   Input Parameters:
36844e3744c5SMatthew G. Knepley + dmA - A DMPlex object
36854e3744c5SMatthew G. Knepley - dmB - A DMPlex object
36864e3744c5SMatthew G. Knepley 
36874e3744c5SMatthew G. Knepley   Output Parameters:
36884e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
36894e3744c5SMatthew G. Knepley 
36904e3744c5SMatthew G. Knepley   Level: intermediate
36914e3744c5SMatthew G. Knepley 
36924e3744c5SMatthew G. Knepley   Notes:
36934e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
36944e3744c5SMatthew G. Knepley 
36954e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
36964e3744c5SMatthew G. Knepley @*/
36974e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
36984e3744c5SMatthew G. Knepley {
36994e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
37004e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
37014e3744c5SMatthew G. Knepley 
37024e3744c5SMatthew G. Knepley   PetscFunctionBegin;
37034e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
37044e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
37054e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
37064e3744c5SMatthew G. Knepley 
37074e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
37084e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
37094e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
37104e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
37114e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
37124e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
37134e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
37144e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37154e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
37164e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
37174e3744c5SMatthew G. Knepley 
37184e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
37194e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
37204e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
37214e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
37224e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
37234e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
37244e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
37254e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
37264e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
37274e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
37284e3744c5SMatthew G. Knepley     }
37294e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
37304e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
37314e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
37324e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
37334e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
37344e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
37354e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
37364e3744c5SMatthew G. Knepley     }
37374e3744c5SMatthew G. Knepley   }
37384e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
37394e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
37404e3744c5SMatthew G. Knepley }
37414e3744c5SMatthew G. Knepley 
37427cd05799SMatthew G. Knepley /*@C
37437cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
37447cd05799SMatthew G. Knepley 
37457cd05799SMatthew G. Knepley   Not Collective
37467cd05799SMatthew G. Knepley 
37477cd05799SMatthew G. Knepley   Input Parameters:
37487cd05799SMatthew G. Knepley + dm         - The DMPlex
37497cd05799SMatthew G. Knepley . cellDim    - The cell dimension
37507cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
37517cd05799SMatthew G. Knepley 
37527cd05799SMatthew G. Knepley   Output Parameters:
37537cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
37547cd05799SMatthew G. Knepley 
37557cd05799SMatthew G. Knepley   Level: developer
37567cd05799SMatthew G. Knepley 
37577cd05799SMatthew G. Knepley   Notes:
37587cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
37597cd05799SMatthew G. Knepley 
37607cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
37617cd05799SMatthew G. Knepley @*/
376218ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3763a6dfd86eSKarl Rupp {
376482f516ccSBarry Smith   MPI_Comm       comm;
3765552f7358SJed Brown   PetscErrorCode ierr;
3766552f7358SJed Brown 
3767552f7358SJed Brown   PetscFunctionBegin;
376882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3769552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3770552f7358SJed Brown   switch (cellDim) {
3771552f7358SJed Brown   case 0:
3772552f7358SJed Brown     *numFaceVertices = 0;
3773552f7358SJed Brown     break;
3774552f7358SJed Brown   case 1:
3775552f7358SJed Brown     *numFaceVertices = 1;
3776552f7358SJed Brown     break;
3777552f7358SJed Brown   case 2:
3778552f7358SJed Brown     switch (numCorners) {
377919436ca2SJed Brown     case 3: /* triangle */
378019436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3781552f7358SJed Brown       break;
378219436ca2SJed Brown     case 4: /* quadrilateral */
378319436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3784552f7358SJed Brown       break;
378519436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
378619436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3787552f7358SJed Brown       break;
378819436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
378919436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3790552f7358SJed Brown       break;
3791552f7358SJed Brown     default:
3792f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3793552f7358SJed Brown     }
3794552f7358SJed Brown     break;
3795552f7358SJed Brown   case 3:
3796552f7358SJed Brown     switch (numCorners) {
379719436ca2SJed Brown     case 4: /* tetradehdron */
379819436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3799552f7358SJed Brown       break;
380019436ca2SJed Brown     case 6: /* tet cohesive cells */
380119436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3802552f7358SJed Brown       break;
380319436ca2SJed Brown     case 8: /* hexahedron */
380419436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3805552f7358SJed Brown       break;
380619436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
380719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3808552f7358SJed Brown       break;
380919436ca2SJed Brown     case 10: /* quadratic tetrahedron */
381019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3811552f7358SJed Brown       break;
381219436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
381319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3814552f7358SJed Brown       break;
381519436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
381619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3817552f7358SJed Brown       break;
381819436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
381919436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3820552f7358SJed Brown       break;
3821552f7358SJed Brown     default:
3822f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3823552f7358SJed Brown     }
3824552f7358SJed Brown     break;
3825552f7358SJed Brown   default:
3826f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3827552f7358SJed Brown   }
3828552f7358SJed Brown   PetscFunctionReturn(0);
3829552f7358SJed Brown }
3830552f7358SJed Brown 
3831552f7358SJed Brown /*@
3832aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3833552f7358SJed Brown 
3834552f7358SJed Brown   Not Collective
3835552f7358SJed Brown 
3836aa50250dSMatthew G. Knepley   Input Parameter:
3837552f7358SJed Brown . dm    - The DMPlex object
3838552f7358SJed Brown 
3839aa50250dSMatthew G. Knepley   Output Parameter:
3840aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3841552f7358SJed Brown 
3842552f7358SJed Brown   Level: developer
3843552f7358SJed Brown 
3844dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
3845aa50250dSMatthew G. Knepley @*/
3846aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3847aa50250dSMatthew G. Knepley {
3848aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3849aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3850aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3851c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3852aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3853aa50250dSMatthew G. Knepley }
3854aa50250dSMatthew G. Knepley 
3855aa50250dSMatthew G. Knepley /*@
3856aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3857aa50250dSMatthew G. Knepley 
3858aa50250dSMatthew G. Knepley   Not Collective
3859aa50250dSMatthew G. Knepley 
3860aa50250dSMatthew G. Knepley   Input Parameter:
3861aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3862aa50250dSMatthew G. Knepley 
3863aa50250dSMatthew G. Knepley   Output Parameter:
3864aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3865aa50250dSMatthew G. Knepley 
3866aa50250dSMatthew G. Knepley   Level: developer
3867552f7358SJed Brown 
3868b1bb481bSMatthew Knepley   Notes:
3869b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3870dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
3871dc287ab2SVaclav Hapla   An empty mesh gives -1.
3872b1bb481bSMatthew Knepley 
3873dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
3874552f7358SJed Brown @*/
3875552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3876552f7358SJed Brown {
3877aa50250dSMatthew G. Knepley   DMLabel        label;
3878aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3879552f7358SJed Brown   PetscErrorCode ierr;
3880552f7358SJed Brown 
3881552f7358SJed Brown   PetscFunctionBegin;
3882552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3883552f7358SJed Brown   PetscValidPointer(depth, 2);
3884aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3885aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3886552f7358SJed Brown   *depth = d-1;
3887552f7358SJed Brown   PetscFunctionReturn(0);
3888552f7358SJed Brown }
3889552f7358SJed Brown 
3890552f7358SJed Brown /*@
3891552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3892552f7358SJed Brown 
3893552f7358SJed Brown   Not Collective
3894552f7358SJed Brown 
3895552f7358SJed Brown   Input Parameters:
3896552f7358SJed Brown + dm           - The DMPlex object
3897552f7358SJed Brown - stratumValue - The requested depth
3898552f7358SJed Brown 
3899552f7358SJed Brown   Output Parameters:
3900552f7358SJed Brown + start - The first point at this depth
3901552f7358SJed Brown - end   - One beyond the last point at this depth
3902552f7358SJed Brown 
3903647867b2SJed Brown   Notes:
3904647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3905647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3906647867b2SJed Brown   higher dimension, e.g., "edges".
3907647867b2SJed Brown 
3908552f7358SJed Brown   Level: developer
3909552f7358SJed Brown 
3910dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
3911552f7358SJed Brown @*/
39120adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
39130adebc6cSBarry Smith {
3914aa50250dSMatthew G. Knepley   DMLabel        label;
391563d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3916552f7358SJed Brown   PetscErrorCode ierr;
3917552f7358SJed Brown 
3918552f7358SJed Brown   PetscFunctionBegin;
3919552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
392063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
392163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3922552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
39230d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
392463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
392563d1a920SMatthew G. Knepley     if (start) *start = pStart;
392663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
392763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3928552f7358SJed Brown   }
3929aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
393063d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
393163d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3932552f7358SJed Brown   PetscFunctionReturn(0);
3933552f7358SJed Brown }
3934552f7358SJed Brown 
3935552f7358SJed Brown /*@
3936552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3937552f7358SJed Brown 
3938552f7358SJed Brown   Not Collective
3939552f7358SJed Brown 
3940552f7358SJed Brown   Input Parameters:
3941552f7358SJed Brown + dm           - The DMPlex object
3942552f7358SJed Brown - stratumValue - The requested height
3943552f7358SJed Brown 
3944552f7358SJed Brown   Output Parameters:
3945552f7358SJed Brown + start - The first point at this height
3946552f7358SJed Brown - end   - One beyond the last point at this height
3947552f7358SJed Brown 
3948647867b2SJed Brown   Notes:
3949647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3950647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3951647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3952647867b2SJed Brown 
3953552f7358SJed Brown   Level: developer
3954552f7358SJed Brown 
39553dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
3956552f7358SJed Brown @*/
39570adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
39580adebc6cSBarry Smith {
3959aa50250dSMatthew G. Knepley   DMLabel        label;
396063d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3961552f7358SJed Brown   PetscErrorCode ierr;
3962552f7358SJed Brown 
3963552f7358SJed Brown   PetscFunctionBegin;
3964552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
396563d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
396663d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3967552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
39680d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
396963d1a920SMatthew G. Knepley   if (stratumValue < 0) {
397063d1a920SMatthew G. Knepley     if (start) *start = pStart;
397163d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
397263d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3973552f7358SJed Brown   }
3974aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
397513903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
397663d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
397763d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3978552f7358SJed Brown   PetscFunctionReturn(0);
3979552f7358SJed Brown }
3980552f7358SJed Brown 
3981ba2698f1SMatthew G. Knepley /*@
3982ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
3983ba2698f1SMatthew G. Knepley 
3984ba2698f1SMatthew G. Knepley   Not Collective
3985ba2698f1SMatthew G. Knepley 
3986ba2698f1SMatthew G. Knepley   Input Parameter:
3987ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
3988ba2698f1SMatthew G. Knepley - point - The point
3989ba2698f1SMatthew G. Knepley 
3990ba2698f1SMatthew G. Knepley   Output Parameter:
3991ba2698f1SMatthew G. Knepley . depth - The depth of the point
3992ba2698f1SMatthew G. Knepley 
3993ba2698f1SMatthew G. Knepley   Level: intermediate
3994ba2698f1SMatthew G. Knepley 
39953dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
3996ba2698f1SMatthew G. Knepley @*/
3997ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
3998ba2698f1SMatthew G. Knepley {
3999ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4000ba2698f1SMatthew G. Knepley 
4001ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4002ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400340a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
4004ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
4005ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4006ba2698f1SMatthew G. Knepley }
4007ba2698f1SMatthew G. Knepley 
4008ba2698f1SMatthew G. Knepley /*@
40090c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
40100c0a32dcSVaclav Hapla 
40110c0a32dcSVaclav Hapla   Not Collective
40120c0a32dcSVaclav Hapla 
40130c0a32dcSVaclav Hapla   Input Parameter:
40140c0a32dcSVaclav Hapla + dm    - The DMPlex object
40150c0a32dcSVaclav Hapla - point - The point
40160c0a32dcSVaclav Hapla 
40170c0a32dcSVaclav Hapla   Output Parameter:
40180c0a32dcSVaclav Hapla . height - The height of the point
40190c0a32dcSVaclav Hapla 
40200c0a32dcSVaclav Hapla   Level: intermediate
40210c0a32dcSVaclav Hapla 
40223dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
40230c0a32dcSVaclav Hapla @*/
40240c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
40250c0a32dcSVaclav Hapla {
40260c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
40270c0a32dcSVaclav Hapla   PetscErrorCode ierr;
40280c0a32dcSVaclav Hapla 
40290c0a32dcSVaclav Hapla   PetscFunctionBegin;
40300c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40310c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
40320c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
40330c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
40340c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
40350c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
40360c0a32dcSVaclav Hapla }
40370c0a32dcSVaclav Hapla 
40380c0a32dcSVaclav Hapla /*@
4039ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
4040ba2698f1SMatthew G. Knepley 
4041ba2698f1SMatthew G. Knepley   Not Collective
4042ba2698f1SMatthew G. Knepley 
4043ba2698f1SMatthew G. Knepley   Input Parameter:
4044ba2698f1SMatthew G. Knepley . dm - The DMPlex object
4045ba2698f1SMatthew G. Knepley 
4046ba2698f1SMatthew G. Knepley   Output Parameter:
4047ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
4048ba2698f1SMatthew G. Knepley 
4049412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
4050412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
4051412e9a14SMatthew G. Knepley 
4052ba2698f1SMatthew G. Knepley   Level: developer
4053ba2698f1SMatthew G. Knepley 
4054dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
4055ba2698f1SMatthew G. Knepley @*/
4056ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4057ba2698f1SMatthew G. Knepley {
4058ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4059ba2698f1SMatthew G. Knepley 
4060ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4061ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4062ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
4063ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
4064ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
4065ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4066ba2698f1SMatthew G. Knepley }
4067ba2698f1SMatthew G. Knepley 
4068ba2698f1SMatthew G. Knepley /*@
4069ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
4070ba2698f1SMatthew G. Knepley 
4071ba2698f1SMatthew G. Knepley   Not Collective
4072ba2698f1SMatthew G. Knepley 
4073ba2698f1SMatthew G. Knepley   Input Parameter:
4074ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
4075ba2698f1SMatthew G. Knepley - cell - The cell
4076ba2698f1SMatthew G. Knepley 
4077ba2698f1SMatthew G. Knepley   Output Parameter:
4078ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
4079ba2698f1SMatthew G. Knepley 
4080ba2698f1SMatthew G. Knepley   Level: intermediate
4081ba2698f1SMatthew G. Knepley 
4082ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
4083ba2698f1SMatthew G. Knepley @*/
4084ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4085ba2698f1SMatthew G. Knepley {
4086ba2698f1SMatthew G. Knepley   DMLabel        label;
4087ba2698f1SMatthew G. Knepley   PetscInt       ct;
4088ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4089ba2698f1SMatthew G. Knepley 
4090ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4091ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4092ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
4093ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4094ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
4095ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
4096ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4097ba2698f1SMatthew G. Knepley }
4098ba2698f1SMatthew G. Knepley 
4099412e9a14SMatthew G. Knepley /*@
4100412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
4101412e9a14SMatthew G. Knepley 
4102412e9a14SMatthew G. Knepley   Not Collective
4103412e9a14SMatthew G. Knepley 
4104412e9a14SMatthew G. Knepley   Input Parameters:
4105412e9a14SMatthew G. Knepley + dm   - The DMPlex object
4106412e9a14SMatthew G. Knepley . cell - The cell
4107412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
4108412e9a14SMatthew G. Knepley 
4109412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
4110412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
4111412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
4112412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
4113412e9a14SMatthew G. Knepley 
4114412e9a14SMatthew G. Knepley   Level: advanced
4115412e9a14SMatthew G. Knepley 
4116412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
4117412e9a14SMatthew G. Knepley @*/
4118412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4119412e9a14SMatthew G. Knepley {
4120412e9a14SMatthew G. Knepley   DMLabel        label;
4121412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
4122412e9a14SMatthew G. Knepley 
4123412e9a14SMatthew G. Knepley   PetscFunctionBegin;
4124412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4125412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4126412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
4127412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
4128412e9a14SMatthew G. Knepley }
4129412e9a14SMatthew G. Knepley 
41300adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
41310adebc6cSBarry Smith {
4132efe440bfSMatthew G. Knepley   PetscSection   section, s;
4133efe440bfSMatthew G. Knepley   Mat            m;
41343e922f36SToby Isaac   PetscInt       maxHeight;
4135552f7358SJed Brown   PetscErrorCode ierr;
4136552f7358SJed Brown 
4137552f7358SJed Brown   PetscFunctionBegin;
413838221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
41393e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
41403e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
414182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
414292fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
41431d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4144efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
4145efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
4146efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
4147efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
4148efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
41498f4c458bSMatthew G. Knepley 
41508f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
41518f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
4152552f7358SJed Brown   PetscFunctionReturn(0);
4153552f7358SJed Brown }
4154552f7358SJed Brown 
4155f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4156f19dbd58SToby Isaac {
4157f19dbd58SToby Isaac   Vec            coordsLocal;
4158f19dbd58SToby Isaac   DM             coordsDM;
4159f19dbd58SToby Isaac   PetscErrorCode ierr;
4160f19dbd58SToby Isaac 
4161f19dbd58SToby Isaac   PetscFunctionBegin;
4162f19dbd58SToby Isaac   *field = NULL;
4163f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
4164f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
4165f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
4166f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
4167f19dbd58SToby Isaac   }
4168f19dbd58SToby Isaac   PetscFunctionReturn(0);
4169f19dbd58SToby Isaac }
4170f19dbd58SToby Isaac 
41717cd05799SMatthew G. Knepley /*@C
41727cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
41737cd05799SMatthew G. Knepley 
41747cd05799SMatthew G. Knepley   Not Collective
41757cd05799SMatthew G. Knepley 
41767cd05799SMatthew G. Knepley   Input Parameters:
41777cd05799SMatthew G. Knepley . dm        - The DMPlex object
41787cd05799SMatthew G. Knepley 
41797cd05799SMatthew G. Knepley   Output Parameter:
41807cd05799SMatthew G. Knepley . section - The PetscSection object
41817cd05799SMatthew G. Knepley 
41827cd05799SMatthew G. Knepley   Level: developer
41837cd05799SMatthew G. Knepley 
41847cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
41857cd05799SMatthew G. Knepley @*/
41860adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
41870adebc6cSBarry Smith {
4188552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4189552f7358SJed Brown 
4190552f7358SJed Brown   PetscFunctionBegin;
4191552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4192552f7358SJed Brown   if (section) *section = mesh->coneSection;
4193552f7358SJed Brown   PetscFunctionReturn(0);
4194552f7358SJed Brown }
4195552f7358SJed Brown 
41967cd05799SMatthew G. Knepley /*@C
41977cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
41987cd05799SMatthew G. Knepley 
41997cd05799SMatthew G. Knepley   Not Collective
42007cd05799SMatthew G. Knepley 
42017cd05799SMatthew G. Knepley   Input Parameters:
42027cd05799SMatthew G. Knepley . dm        - The DMPlex object
42037cd05799SMatthew G. Knepley 
42047cd05799SMatthew G. Knepley   Output Parameter:
42057cd05799SMatthew G. Knepley . section - The PetscSection object
42067cd05799SMatthew G. Knepley 
42077cd05799SMatthew G. Knepley   Level: developer
42087cd05799SMatthew G. Knepley 
42097cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
42107cd05799SMatthew G. Knepley @*/
42118cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
42128cb4d582SMatthew G. Knepley {
42138cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
42148cb4d582SMatthew G. Knepley 
42158cb4d582SMatthew G. Knepley   PetscFunctionBegin;
42168cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42178cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
42188cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
42198cb4d582SMatthew G. Knepley }
42208cb4d582SMatthew G. Knepley 
42217cd05799SMatthew G. Knepley /*@C
42227cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
42237cd05799SMatthew G. Knepley 
42247cd05799SMatthew G. Knepley   Not Collective
42257cd05799SMatthew G. Knepley 
42267cd05799SMatthew G. Knepley   Input Parameters:
42277cd05799SMatthew G. Knepley . dm        - The DMPlex object
42287cd05799SMatthew G. Knepley 
42297cd05799SMatthew G. Knepley   Output Parameter:
42307cd05799SMatthew G. Knepley . cones - The cone for each point
42317cd05799SMatthew G. Knepley 
42327cd05799SMatthew G. Knepley   Level: developer
42337cd05799SMatthew G. Knepley 
42347cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
42357cd05799SMatthew G. Knepley @*/
4236a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4237a6dfd86eSKarl Rupp {
4238552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4239552f7358SJed Brown 
4240552f7358SJed Brown   PetscFunctionBegin;
4241552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4242552f7358SJed Brown   if (cones) *cones = mesh->cones;
4243552f7358SJed Brown   PetscFunctionReturn(0);
4244552f7358SJed Brown }
4245552f7358SJed Brown 
42467cd05799SMatthew G. Knepley /*@C
42477cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
42487cd05799SMatthew G. Knepley 
42497cd05799SMatthew G. Knepley   Not Collective
42507cd05799SMatthew G. Knepley 
42517cd05799SMatthew G. Knepley   Input Parameters:
42527cd05799SMatthew G. Knepley . dm        - The DMPlex object
42537cd05799SMatthew G. Knepley 
42547cd05799SMatthew G. Knepley   Output Parameter:
42557cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
42567cd05799SMatthew G. Knepley 
42577cd05799SMatthew G. Knepley   Level: developer
42587cd05799SMatthew G. Knepley 
42597cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
42607cd05799SMatthew G. Knepley @*/
4261a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4262a6dfd86eSKarl Rupp {
4263552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4264552f7358SJed Brown 
4265552f7358SJed Brown   PetscFunctionBegin;
4266552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4267552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4268552f7358SJed Brown   PetscFunctionReturn(0);
4269552f7358SJed Brown }
4270552f7358SJed Brown 
4271552f7358SJed Brown /******************************** FEM Support **********************************/
4272552f7358SJed Brown 
42739e8305c2SJed Brown /*
42749e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
42759e8305c2SJed Brown  representing a line in the section.
42769e8305c2SJed Brown */
42779e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
42789e8305c2SJed Brown {
42799e8305c2SJed Brown   PetscErrorCode ierr;
42809e8305c2SJed Brown 
42819e8305c2SJed Brown   PetscFunctionBeginHot;
42829e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4283a433471fSStefano Zampini   if (line < 0) {
4284a433471fSStefano Zampini     *k = 0;
4285a433471fSStefano Zampini     *Nc = 0;
4286a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
42879e8305c2SJed Brown     *k = 1;
42889e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
42899e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
42909e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
42919e8305c2SJed Brown     *k = *k / *Nc + 1;
42929e8305c2SJed Brown   }
42939e8305c2SJed Brown   PetscFunctionReturn(0);
42949e8305c2SJed Brown }
42959e8305c2SJed Brown 
4296a4355906SMatthew Knepley /*@
4297bc1eb3faSJed Brown 
4298bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4299bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
43001bb6d2a8SBarry Smith   section provided (or the section of the DM).
4301a4355906SMatthew Knepley 
4302a4355906SMatthew Knepley   Input Parameters:
4303a4355906SMatthew Knepley + dm      - The DM
4304a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4305a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4306a4355906SMatthew Knepley 
4307a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4308a4355906SMatthew Knepley   degree of the basis.
4309a4355906SMatthew Knepley 
4310bc1eb3faSJed Brown   Example:
4311bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4312bc1eb3faSJed Brown .vb
4313bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4314bc1eb3faSJed Brown 
4315bc1eb3faSJed Brown   v4 -- e6 -- v3
4316bc1eb3faSJed Brown   |           |
4317bc1eb3faSJed Brown   e7    c0    e8
4318bc1eb3faSJed Brown   |           |
4319bc1eb3faSJed Brown   v1 -- e5 -- v2
4320bc1eb3faSJed Brown .ve
4321bc1eb3faSJed Brown 
4322bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4323bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4324bc1eb3faSJed Brown .vb
4325bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4326bc1eb3faSJed Brown     v1 -> [4]
4327bc1eb3faSJed Brown     ...
4328bc1eb3faSJed Brown     e5 -> [8, 9]
4329bc1eb3faSJed Brown .ve
4330bc1eb3faSJed Brown 
4331bc1eb3faSJed Brown   which corresponds to the dofs
4332bc1eb3faSJed Brown .vb
4333bc1eb3faSJed Brown     6   10  11  7
4334bc1eb3faSJed Brown     13  2   3   15
4335bc1eb3faSJed Brown     12  0   1   14
4336bc1eb3faSJed Brown     4   8   9   5
4337bc1eb3faSJed Brown .ve
4338bc1eb3faSJed Brown 
4339bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4340bc1eb3faSJed Brown .vb
4341bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4342bc1eb3faSJed Brown .ve
4343bc1eb3faSJed Brown 
4344bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4345bc1eb3faSJed Brown .vb
4346bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4347bc1eb3faSJed Brown .ve
4348bc1eb3faSJed Brown 
4349a4355906SMatthew Knepley   Level: developer
4350a4355906SMatthew Knepley 
43519df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4352a4355906SMatthew Knepley @*/
4353bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
43543194fc30SMatthew G. Knepley {
43557391a63aSMatthew G. Knepley   DMLabel        label;
4356bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
43579e8305c2SJed Brown   PetscBool      vertexchart;
43583194fc30SMatthew G. Knepley   PetscErrorCode ierr;
43593194fc30SMatthew G. Knepley 
43603194fc30SMatthew G. Knepley   PetscFunctionBegin;
43613194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4362a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4363a433471fSStefano Zampini   if (point < 0) {
4364a433471fSStefano Zampini     PetscInt sStart,sEnd;
4365a433471fSStefano Zampini 
4366a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4367a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4368a433471fSStefano Zampini   }
43697391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4370a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4371a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
43727391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
43737391a63aSMatthew G. Knepley   else if  (depth == dim) {
43747391a63aSMatthew G. Knepley     const PetscInt *cone;
43757391a63aSMatthew G. Knepley 
43767391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4377d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4378d4e6627bSStefano Zampini     else if (dim == 3) {
4379d4e6627bSStefano Zampini       const PetscInt *cone2;
4380d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4381d4e6627bSStefano Zampini       eStart = cone2[0];
4382d4e6627bSStefano 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);
4383a433471fSStefano 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);
43849e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
43859e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
43869e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
43879e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
43889e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
43899e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
43909e8305c2SJed Brown   }
43913194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4392bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
4393bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4394bb197d40SJed Brown     PetscInt *perm;
4395bb197d40SJed Brown 
43963194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
43979e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4398bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
43993194fc30SMatthew G. Knepley     }
44003194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
44013194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4402bb197d40SJed Brown       switch (d) {
4403babf31e0SJed Brown       case 1:
44049e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4405babf31e0SJed Brown         /*
4406babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4407babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
4408babf31e0SJed Brown          */
4409babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4410babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4411babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4412babf31e0SJed Brown         foffset = offset;
4413babf31e0SJed Brown         break;
441489eabcffSMatthew G. Knepley       case 2:
44153194fc30SMatthew 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} */
44169e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
44173194fc30SMatthew G. Knepley         /* The SEM order is
44183194fc30SMatthew G. Knepley 
44193194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
442089eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
44213194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
44223194fc30SMatthew G. Knepley          */
44233194fc30SMatthew G. Knepley         {
44243194fc30SMatthew G. Knepley           const PetscInt of   = 0;
44253194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
44263194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
44273194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
44283194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
44293194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
44303194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
44313194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
44323194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
44333194fc30SMatthew G. Knepley           PetscInt       o;
44343194fc30SMatthew G. Knepley 
44353194fc30SMatthew G. Knepley           /* bottom */
44363194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
44373194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
44383194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
44393194fc30SMatthew G. Knepley           /* middle */
44403194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
44413194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
44423194fc30SMatthew 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;
44433194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
44443194fc30SMatthew G. Knepley           }
44453194fc30SMatthew G. Knepley           /* top */
44463194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
44473194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
44483194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
44493194fc30SMatthew G. Knepley           foffset = offset;
44503194fc30SMatthew G. Knepley         }
445189eabcffSMatthew G. Knepley         break;
445289eabcffSMatthew G. Knepley       case 3:
445389eabcffSMatthew G. Knepley         /* The original hex closure is
445489eabcffSMatthew G. Knepley 
445589eabcffSMatthew G. Knepley          {c,
445689eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
445789eabcffSMatthew 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,
445889eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
445989eabcffSMatthew G. Knepley          */
44609e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
446189eabcffSMatthew G. Knepley         /* The SEM order is
446289eabcffSMatthew G. Knepley          Bottom Slice
446389eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
446489eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
446589eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
446689eabcffSMatthew G. Knepley 
446789eabcffSMatthew G. Knepley          Middle Slice (j)
446889eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
446989eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
447089eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
447189eabcffSMatthew G. Knepley 
447289eabcffSMatthew G. Knepley          Top Slice
447389eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
447489eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
447589eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
447689eabcffSMatthew G. Knepley          */
447789eabcffSMatthew G. Knepley         {
447889eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
447989eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
448089eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
448189eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
448289eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
448389eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
448489eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
448589eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
448689eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
448789eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
448889eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
448989eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
449089eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
449189eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
449289eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
449389eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
449489eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
449589eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
449689eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
449789eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
449889eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
449989eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
450089eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
450189eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
450289eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
450389eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
450489eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
450589eabcffSMatthew G. Knepley           PetscInt       o, n;
450689eabcffSMatthew G. Knepley 
450789eabcffSMatthew G. Knepley           /* Bottom Slice */
450889eabcffSMatthew G. Knepley           /*   bottom */
450989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
451089eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
451189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
451289eabcffSMatthew G. Knepley           /*   middle */
451389eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
451489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4515316b7f87SMax 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;}
451689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
45173194fc30SMatthew G. Knepley           }
451889eabcffSMatthew G. Knepley           /*   top */
451989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
452089eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
452189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
452289eabcffSMatthew G. Knepley 
452389eabcffSMatthew G. Knepley           /* Middle Slice */
452489eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
452589eabcffSMatthew G. Knepley             /*   bottom */
452689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
452789eabcffSMatthew 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;
452889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
452989eabcffSMatthew G. Knepley             /*   middle */
453089eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
453189eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
453289eabcffSMatthew 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;
453389eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
453489eabcffSMatthew G. Knepley             }
453589eabcffSMatthew G. Knepley             /*   top */
453689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
453789eabcffSMatthew 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;
453889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
453989eabcffSMatthew G. Knepley           }
454089eabcffSMatthew G. Knepley 
454189eabcffSMatthew G. Knepley           /* Top Slice */
454289eabcffSMatthew G. Knepley           /*   bottom */
454389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
454489eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
454589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
454689eabcffSMatthew G. Knepley           /*   middle */
454789eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
454889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
454989eabcffSMatthew 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;
455089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
455189eabcffSMatthew G. Knepley           }
455289eabcffSMatthew G. Knepley           /*   top */
455389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
455489eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
455589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
455689eabcffSMatthew G. Knepley 
455789eabcffSMatthew G. Knepley           foffset = offset;
455889eabcffSMatthew G. Knepley         }
455989eabcffSMatthew G. Knepley         break;
4560bb197d40SJed Brown       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
456189eabcffSMatthew G. Knepley       }
456289eabcffSMatthew G. Knepley     }
456389eabcffSMatthew G. Knepley     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
45643194fc30SMatthew G. Knepley     /* Check permutation */
45653194fc30SMatthew G. Knepley     {
45663194fc30SMatthew G. Knepley       PetscInt *check;
45673194fc30SMatthew G. Knepley 
45683194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
45693194fc30SMatthew 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]);}
45703194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
45713194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
45723194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
45733194fc30SMatthew G. Knepley     }
4574bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
4575bb197d40SJed Brown   }
45763194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
45773194fc30SMatthew G. Knepley }
45783194fc30SMatthew G. Knepley 
4579e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4580e071409bSToby Isaac {
4581e071409bSToby Isaac   PetscDS        prob;
4582e071409bSToby Isaac   PetscInt       depth, Nf, h;
4583e071409bSToby Isaac   DMLabel        label;
4584e071409bSToby Isaac   PetscErrorCode ierr;
4585e071409bSToby Isaac 
4586e071409bSToby Isaac   PetscFunctionBeginHot;
4587e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4588e071409bSToby Isaac   Nf      = prob->Nf;
4589e071409bSToby Isaac   label   = dm->depthLabel;
4590e071409bSToby Isaac   *dspace = NULL;
4591e071409bSToby Isaac   if (field < Nf) {
4592e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4593e071409bSToby Isaac 
4594e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4595e071409bSToby Isaac       PetscDualSpace dsp;
4596e071409bSToby Isaac 
4597e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4598e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4599e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4600e071409bSToby Isaac       h    = depth - 1 - h;
4601e071409bSToby Isaac       if (h) {
4602e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4603e071409bSToby Isaac       } else {
4604e071409bSToby Isaac         *dspace = dsp;
4605e071409bSToby Isaac       }
4606e071409bSToby Isaac     }
4607e071409bSToby Isaac   }
4608e071409bSToby Isaac   PetscFunctionReturn(0);
4609e071409bSToby Isaac }
4610e071409bSToby Isaac 
4611e071409bSToby Isaac 
46121a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4613a6dfd86eSKarl Rupp {
4614552f7358SJed Brown   PetscScalar    *array, *vArray;
4615d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
46161a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4617552f7358SJed Brown   PetscErrorCode  ierr;
4618552f7358SJed Brown 
46191b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
46202a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
46215a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
46225a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
46235a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
46243f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
46259df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
46269df71ca4SMatthew G. Knepley       PetscInt dof;
4627d9917b9dSMatthew G. Knepley 
46289df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
46299df71ca4SMatthew G. Knepley       size += dof;
46309df71ca4SMatthew G. Knepley     }
46319df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
46329df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
46332a3aaacfSMatthew G. Knepley       PetscInt       dof;
46345a1bb5cfSMatthew G. Knepley 
46355a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
46362a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
46375a1bb5cfSMatthew G. Knepley       size += dof;
46385a1bb5cfSMatthew G. Knepley     }
46393f7cbbe7SMatthew G. Knepley     if (!values) {
46403f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
46413f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
46423f7cbbe7SMatthew G. Knepley     }
464369291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4644982e9ed1SMatthew G. Knepley   } else {
4645982e9ed1SMatthew G. Knepley     array = *values;
4646982e9ed1SMatthew G. Knepley   }
46479df71ca4SMatthew G. Knepley   size = 0;
46485a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
46499df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
46509df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
46519df71ca4SMatthew G. Knepley     PetscScalar *varr;
4652d9917b9dSMatthew G. Knepley 
46539df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
46549df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
46559df71ca4SMatthew G. Knepley     varr = &vArray[off];
46561a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
46571a271a75SMatthew G. Knepley       array[offset] = varr[d];
46589df71ca4SMatthew G. Knepley     }
46599df71ca4SMatthew G. Knepley     size += dof;
46609df71ca4SMatthew G. Knepley   }
46619df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
46629df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
46639df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
46645a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
46655a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
46665a1bb5cfSMatthew G. Knepley 
466752ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
46685a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
46695a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
46705a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
46715a1bb5cfSMatthew G. Knepley     if (o >= 0) {
46721a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
46731a271a75SMatthew G. Knepley         array[offset] = varr[d];
46745a1bb5cfSMatthew G. Knepley       }
46755a1bb5cfSMatthew G. Knepley     } else {
46761a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
46771a271a75SMatthew G. Knepley         array[offset] = varr[d];
46785a1bb5cfSMatthew G. Knepley       }
46795a1bb5cfSMatthew G. Knepley     }
46809df71ca4SMatthew G. Knepley     size += dof;
46815a1bb5cfSMatthew G. Knepley   }
46825a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
46839df71ca4SMatthew G. Knepley   if (!*values) {
46845a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
46855a1bb5cfSMatthew G. Knepley     *values = array;
46869df71ca4SMatthew G. Knepley   } else {
46878ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
46888c312ff3SMatthew G. Knepley     *csize = size;
46899df71ca4SMatthew G. Knepley   }
46905a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
46915a1bb5cfSMatthew G. Knepley }
4692d9917b9dSMatthew G. Knepley 
469327f02ce8SMatthew G. Knepley /* Compress out points not in the section */
469427f02ce8SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
469527f02ce8SMatthew G. Knepley {
469627f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
469727f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
469827f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
469927f02ce8SMatthew G. Knepley 
470027f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
470127f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
470227f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
470327f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
470427f02ce8SMatthew G. Knepley       points[q*2]   = r;
470527f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
470627f02ce8SMatthew G. Knepley       ++q;
470727f02ce8SMatthew G. Knepley     }
470827f02ce8SMatthew G. Knepley   }
470927f02ce8SMatthew G. Knepley   *numPoints = q;
471027f02ce8SMatthew G. Knepley   return 0;
471127f02ce8SMatthew G. Knepley }
471227f02ce8SMatthew G. Knepley 
471327f02ce8SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
471427f02ce8SMatthew G. Knepley {
471527f02ce8SMatthew G. Knepley   const PetscInt *cone, *ornt;
471627f02ce8SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
471727f02ce8SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
471827f02ce8SMatthew G. Knepley   PetscErrorCode  ierr;
471927f02ce8SMatthew G. Knepley 
472027f02ce8SMatthew G. Knepley   PetscFunctionBeginHot;
472127f02ce8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
472227f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
472327f02ce8SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
472427f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
472527f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
472627f02ce8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, np*2, MPIU_INT, &pts);CHKERRQ(ierr);
472727f02ce8SMatthew G. Knepley   c    = 0;
472827f02ce8SMatthew G. Knepley   pts[c*2+0] = point;
472927f02ce8SMatthew G. Knepley   pts[c*2+1] = 0;
473027f02ce8SMatthew G. Knepley   ++c;
473127f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
473227f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
473327f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
473427f02ce8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
473527f02ce8SMatthew G. Knepley   if (dim >= 2) {
473627f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
473727f02ce8SMatthew G. Knepley   }
473827f02ce8SMatthew G. Knepley   if (dim >= 3) {
473927f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
474027f02ce8SMatthew G. Knepley       const PetscInt  fpoint = cone[d];
474127f02ce8SMatthew G. Knepley       const PetscInt *fcone;
474227f02ce8SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
474327f02ce8SMatthew G. Knepley 
474427f02ce8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
474527f02ce8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
474627f02ce8SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
474727f02ce8SMatthew G. Knepley         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
474827f02ce8SMatthew G. Knepley         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
474927f02ce8SMatthew G. Knepley       }
475027f02ce8SMatthew G. Knepley     }
475127f02ce8SMatthew G. Knepley   }
475227f02ce8SMatthew G. Knepley   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
475327f02ce8SMatthew G. Knepley   *numPoints = np;
475427f02ce8SMatthew G. Knepley   *points    = pts;
475527f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
475627f02ce8SMatthew G. Knepley }
475727f02ce8SMatthew G. Knepley 
475897529cf3SJed Brown /* Compressed closure does not apply closure permutation */
47591dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4760923c78e0SToby Isaac {
476127f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
4762923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4763923c78e0SToby Isaac   PetscErrorCode ierr;
4764923c78e0SToby Isaac 
4765923c78e0SToby Isaac   PetscFunctionBeginHot;
4766923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
476727f02ce8SMatthew G. Knepley   if (*clPoints) {
4768923c78e0SToby Isaac     PetscInt dof, off;
4769923c78e0SToby Isaac 
4770923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4771923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4772923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4773923c78e0SToby Isaac     np   = dof/2;
4774923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
477527f02ce8SMatthew G. Knepley   } else {
4776665f567fSMatthew G. Knepley     DMPolytopeType ct;
4777665f567fSMatthew G. Knepley 
477827f02ce8SMatthew G. Knepley     /* Do not make the label if it does not exist */
477927f02ce8SMatthew G. Knepley     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
478027f02ce8SMatthew G. Knepley     else                    {ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);}
478127f02ce8SMatthew G. Knepley     switch (ct) {
478227f02ce8SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
478327f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);CHKERRQ(ierr);
478427f02ce8SMatthew G. Knepley         break;
478527f02ce8SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
478627f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);CHKERRQ(ierr);
478727f02ce8SMatthew G. Knepley         break;
478827f02ce8SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
478927f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);CHKERRQ(ierr);
479027f02ce8SMatthew G. Knepley         break;
479127f02ce8SMatthew G. Knepley       default:
479227f02ce8SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
479327f02ce8SMatthew G. Knepley     }
479427f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
4795923c78e0SToby Isaac   }
4796923c78e0SToby Isaac   *numPoints = np;
4797923c78e0SToby Isaac   *points    = pts;
4798923c78e0SToby Isaac   *clp       = cla;
4799923c78e0SToby Isaac   PetscFunctionReturn(0);
4800923c78e0SToby Isaac }
4801923c78e0SToby Isaac 
48021dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4803923c78e0SToby Isaac {
4804923c78e0SToby Isaac   PetscErrorCode ierr;
4805923c78e0SToby Isaac 
4806923c78e0SToby Isaac   PetscFunctionBeginHot;
4807923c78e0SToby Isaac   if (!*clPoints) {
4808923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4809923c78e0SToby Isaac   } else {
4810923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4811923c78e0SToby Isaac   }
4812923c78e0SToby Isaac   *numPoints = 0;
4813923c78e0SToby Isaac   *points    = NULL;
4814923c78e0SToby Isaac   *clSec     = NULL;
4815923c78e0SToby Isaac   *clPoints  = NULL;
4816923c78e0SToby Isaac   *clp       = NULL;
4817923c78e0SToby Isaac   PetscFunctionReturn(0);
4818923c78e0SToby Isaac }
4819923c78e0SToby Isaac 
482097e99dd9SToby 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[])
48211a271a75SMatthew G. Knepley {
48221a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
482397e99dd9SToby Isaac   const PetscInt    **perms = NULL;
482497e99dd9SToby Isaac   const PetscScalar **flips = NULL;
48251a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
48261a271a75SMatthew G. Knepley 
48271a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4828fe02ba77SJed Brown   *size = 0;
482997e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
483097e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
483197e99dd9SToby Isaac     const PetscInt    point = points[2*p];
483297e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
483397e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
48341a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
48351a271a75SMatthew G. Knepley     const PetscScalar *varr;
48361a271a75SMatthew G. Knepley 
48371a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
48381a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
48391a271a75SMatthew G. Knepley     varr = &vArray[off];
484097e99dd9SToby Isaac     if (clperm) {
484197e99dd9SToby Isaac       if (perm) {
484297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
48431a271a75SMatthew G. Knepley       } else {
484497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
484597e99dd9SToby Isaac       }
484697e99dd9SToby Isaac       if (flip) {
484797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
484897e99dd9SToby Isaac       }
484997e99dd9SToby Isaac     } else {
485097e99dd9SToby Isaac       if (perm) {
485197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
485297e99dd9SToby Isaac       } else {
485397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
485497e99dd9SToby Isaac       }
485597e99dd9SToby Isaac       if (flip) {
485697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
48571a271a75SMatthew G. Knepley       }
48581a271a75SMatthew G. Knepley     }
485997e99dd9SToby Isaac     offset += dof;
486097e99dd9SToby Isaac   }
486197e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
48621a271a75SMatthew G. Knepley   *size = offset;
48631a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
48641a271a75SMatthew G. Knepley }
48651a271a75SMatthew G. Knepley 
486697e99dd9SToby 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[])
48671a271a75SMatthew G. Knepley {
48681a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
48691a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
48701a271a75SMatthew G. Knepley 
48711a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4872fe02ba77SJed Brown   *size = 0;
48731a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
487497e99dd9SToby Isaac     PetscInt          p;
487597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
487697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
48771a271a75SMatthew G. Knepley 
487897e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
487997e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
488097e99dd9SToby Isaac       const PetscInt    point = points[2*p];
488197e99dd9SToby Isaac       PetscInt          fdof, foff, b;
48821a271a75SMatthew G. Knepley       const PetscScalar *varr;
488397e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
488497e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
48851a271a75SMatthew G. Knepley 
48861a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
48871a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
48881a271a75SMatthew G. Knepley       varr = &vArray[foff];
488997e99dd9SToby Isaac       if (clperm) {
489097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
489197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
489297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
48931a271a75SMatthew G. Knepley       } else {
489497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
489597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
489697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
48971a271a75SMatthew G. Knepley       }
489897e99dd9SToby Isaac       offset += fdof;
48991a271a75SMatthew G. Knepley     }
490097e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
49011a271a75SMatthew G. Knepley   }
49021a271a75SMatthew G. Knepley   *size = offset;
49031a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
49041a271a75SMatthew G. Knepley }
49051a271a75SMatthew G. Knepley 
4906552f7358SJed Brown /*@C
4907552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4908552f7358SJed Brown 
4909552f7358SJed Brown   Not collective
4910552f7358SJed Brown 
4911552f7358SJed Brown   Input Parameters:
4912552f7358SJed Brown + dm - The DM
4913552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4914552f7358SJed Brown . v - The local vector
491522c1ee49SMatthew G. Knepley . point - The point in the DM
491622c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
491722c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4918552f7358SJed Brown 
4919552f7358SJed Brown   Output Parameters:
492022c1ee49SMatthew G. Knepley + csize - The number of values in the closure
492122c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
492222c1ee49SMatthew G. Knepley 
492322c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
492422c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
492522c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
492622c1ee49SMatthew G. Knepley $
492722c1ee49SMatthew G. Knepley $ A typical use could be
492822c1ee49SMatthew G. Knepley $
492922c1ee49SMatthew G. Knepley $  values = NULL;
493022c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
493122c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
493222c1ee49SMatthew G. Knepley $    <Compute on closure>
493322c1ee49SMatthew G. Knepley $  }
493422c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
493522c1ee49SMatthew G. Knepley $
493622c1ee49SMatthew G. Knepley $ or
493722c1ee49SMatthew G. Knepley $
493822c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
493922c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
494022c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
494122c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
494222c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
494322c1ee49SMatthew G. Knepley $      <Compute on closure>
494422c1ee49SMatthew G. Knepley $    }
494522c1ee49SMatthew G. Knepley $  }
494622c1ee49SMatthew G. Knepley $  PetscFree(values);
4947552f7358SJed Brown 
4948552f7358SJed Brown   Fortran Notes:
4949552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4950552f7358SJed Brown   include petsc.h90 in your code.
4951552f7358SJed Brown 
4952552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4953552f7358SJed Brown 
4954552f7358SJed Brown   Level: intermediate
4955552f7358SJed Brown 
4956552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4957552f7358SJed Brown @*/
4958552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4959552f7358SJed Brown {
4960552f7358SJed Brown   PetscSection       clSection;
4961d9917b9dSMatthew G. Knepley   IS                 clPoints;
4962552f7358SJed Brown   PetscInt          *points = NULL;
4963c459fbc1SJed Brown   const PetscInt    *clp, *perm;
4964c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
4965552f7358SJed Brown   PetscErrorCode     ierr;
4966552f7358SJed Brown 
4967d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4968552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
496992fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
49701a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
49711a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4972552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4973552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4974552f7358SJed Brown   if (depth == 1 && numFields < 2) {
49751a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4976552f7358SJed Brown     PetscFunctionReturn(0);
4977552f7358SJed Brown   }
49781a271a75SMatthew G. Knepley   /* Get points */
4979923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4980c459fbc1SJed Brown   /* Get sizes */
4981c459fbc1SJed Brown   asize = 0;
4982c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
4983c459fbc1SJed Brown     PetscInt dof;
4984552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
49851a271a75SMatthew G. Knepley     asize += dof;
4986552f7358SJed Brown   }
4987c459fbc1SJed Brown   if (values) {
4988c459fbc1SJed Brown     const PetscScalar *vArray;
4989c459fbc1SJed Brown     PetscInt          size;
4990c459fbc1SJed Brown 
4991c459fbc1SJed Brown     if (*values) {
4992c459fbc1SJed 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);
4993c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
4994c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
49958a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
49961a271a75SMatthew G. Knepley     /* Get values */
4997c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
4998c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
4999c459fbc1SJed Brown     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
50001a271a75SMatthew G. Knepley     /* Cleanup array */
50018a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5002d0f6b257SMatthew G. Knepley   }
5003c459fbc1SJed Brown   if (csize) *csize = asize;
5004c459fbc1SJed Brown   /* Cleanup points */
5005c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5006552f7358SJed Brown   PetscFunctionReturn(0);
5007552f7358SJed Brown }
5008552f7358SJed Brown 
5009e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5010e5c487bfSMatthew G. Knepley {
5011e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
5012e5c487bfSMatthew G. Knepley   PetscSection       clSection;
5013e5c487bfSMatthew G. Knepley   IS                 clPoints;
5014e5c487bfSMatthew G. Knepley   PetscScalar       *array;
5015e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
5016e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
5017c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
5018c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
5019e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
5020e5c487bfSMatthew G. Knepley 
5021e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
5022e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5023e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5024e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5025e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5026e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
5027e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
5028e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5029e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
5030e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5031e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
5032e5c487bfSMatthew G. Knepley   }
5033e5c487bfSMatthew G. Knepley   /* Get points */
5034e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5035c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
5036c459fbc1SJed Brown     PetscInt dof;
5037c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5038c459fbc1SJed Brown     clsize += dof;
5039c459fbc1SJed Brown   }
5040c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
5041e5c487bfSMatthew G. Knepley   /* Filter points */
5042e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
5043e5c487bfSMatthew G. Knepley     PetscInt dep;
5044e5c487bfSMatthew G. Knepley 
5045e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
5046e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
5047e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
5048e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
5049e5c487bfSMatthew G. Knepley     ++Np;
5050e5c487bfSMatthew G. Knepley   }
5051e5c487bfSMatthew G. Knepley   /* Get array */
5052e5c487bfSMatthew G. Knepley   if (!values || !*values) {
5053e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
5054e5c487bfSMatthew G. Knepley 
5055e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
5056e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5057e5c487bfSMatthew G. Knepley       asize += dof;
5058e5c487bfSMatthew G. Knepley     }
5059e5c487bfSMatthew G. Knepley     if (!values) {
5060e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5061e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
5062e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
5063e5c487bfSMatthew G. Knepley     }
5064e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
5065e5c487bfSMatthew G. Knepley   } else {
5066e5c487bfSMatthew G. Knepley     array = *values;
5067e5c487bfSMatthew G. Knepley   }
5068e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
5069e5c487bfSMatthew G. Knepley   /* Get values */
5070e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
5071e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
5072e5c487bfSMatthew G. Knepley   /* Cleanup points */
5073e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5074e5c487bfSMatthew G. Knepley   /* Cleanup array */
5075e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5076e5c487bfSMatthew G. Knepley   if (!*values) {
5077e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
5078e5c487bfSMatthew G. Knepley     *values = array;
5079e5c487bfSMatthew G. Knepley   } else {
5080e5c487bfSMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
5081e5c487bfSMatthew G. Knepley     *csize = size;
5082e5c487bfSMatthew G. Knepley   }
5083e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
5084e5c487bfSMatthew G. Knepley }
5085e5c487bfSMatthew G. Knepley 
5086552f7358SJed Brown /*@C
5087552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5088552f7358SJed Brown 
5089552f7358SJed Brown   Not collective
5090552f7358SJed Brown 
5091552f7358SJed Brown   Input Parameters:
5092552f7358SJed Brown + dm - The DM
50930298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
5094552f7358SJed Brown . v - The local vector
5095eaf898f9SPatrick Sanan . point - The point in the DM
50960298fd71SBarry Smith . csize - The number of values in the closure, or NULL
5097552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
5098552f7358SJed Brown 
509922c1ee49SMatthew 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()
510022c1ee49SMatthew G. Knepley 
51013813dfbdSMatthew G Knepley   Fortran Notes:
51023813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
51033813dfbdSMatthew G Knepley   include petsc.h90 in your code.
51043813dfbdSMatthew G Knepley 
51053813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
51063813dfbdSMatthew G Knepley 
5107552f7358SJed Brown   Level: intermediate
5108552f7358SJed Brown 
5109552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5110552f7358SJed Brown @*/
51117c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5112a6dfd86eSKarl Rupp {
5113552f7358SJed Brown   PetscInt       size = 0;
5114552f7358SJed Brown   PetscErrorCode ierr;
5115552f7358SJed Brown 
5116552f7358SJed Brown   PetscFunctionBegin;
5117552f7358SJed Brown   /* Should work without recalculating size */
511869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
5119c9fdaa05SMatthew G. Knepley   *values = NULL;
5120552f7358SJed Brown   PetscFunctionReturn(0);
5121552f7358SJed Brown }
5122552f7358SJed Brown 
5123552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
5124552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
5125552f7358SJed Brown 
512697e99dd9SToby 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[])
5127552f7358SJed Brown {
5128552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5129552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5130552f7358SJed Brown   PetscScalar    *a;
5131552f7358SJed Brown   PetscInt        off, cind = 0, k;
5132552f7358SJed Brown   PetscErrorCode  ierr;
5133552f7358SJed Brown 
5134552f7358SJed Brown   PetscFunctionBegin;
5135552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5136552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5137552f7358SJed Brown   a    = &array[off];
5138552f7358SJed Brown   if (!cdof || setBC) {
513997e99dd9SToby Isaac     if (clperm) {
514097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
514197e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5142552f7358SJed Brown     } else {
514397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
514497e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5145552f7358SJed Brown     }
5146552f7358SJed Brown   } else {
5147552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
514897e99dd9SToby Isaac     if (clperm) {
514997e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
5150552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
515197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5152552f7358SJed Brown         }
5153552f7358SJed Brown       } else {
5154552f7358SJed Brown         for (k = 0; k < dof; ++k) {
5155552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
515697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
515797e99dd9SToby Isaac         }
515897e99dd9SToby Isaac       }
515997e99dd9SToby Isaac     } else {
516097e99dd9SToby Isaac       if (perm) {
516197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
516297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
516397e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
516497e99dd9SToby Isaac         }
516597e99dd9SToby Isaac       } else {
516697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
516797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
516897e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
516997e99dd9SToby Isaac         }
5170552f7358SJed Brown       }
5171552f7358SJed Brown     }
5172552f7358SJed Brown   }
5173552f7358SJed Brown   PetscFunctionReturn(0);
5174552f7358SJed Brown }
5175552f7358SJed Brown 
517697e99dd9SToby 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[])
5177a5e93ea8SMatthew G. Knepley {
5178a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5179a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5180a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5181a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5182a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5183a5e93ea8SMatthew G. Knepley 
5184a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5185a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5186a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5187a5e93ea8SMatthew G. Knepley   a    = &array[off];
5188a5e93ea8SMatthew G. Knepley   if (cdof) {
5189a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
519097e99dd9SToby Isaac     if (clperm) {
519197e99dd9SToby Isaac       if (perm) {
5192a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5193a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
519497e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
519597e99dd9SToby Isaac             cind++;
5196a5e93ea8SMatthew G. Knepley           }
5197a5e93ea8SMatthew G. Knepley         }
5198a5e93ea8SMatthew G. Knepley       } else {
5199a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5200a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
520197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
520297e99dd9SToby Isaac             cind++;
520397e99dd9SToby Isaac           }
520497e99dd9SToby Isaac         }
520597e99dd9SToby Isaac       }
520697e99dd9SToby Isaac     } else {
520797e99dd9SToby Isaac       if (perm) {
520897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
520997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
521097e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
521197e99dd9SToby Isaac             cind++;
521297e99dd9SToby Isaac           }
521397e99dd9SToby Isaac         }
521497e99dd9SToby Isaac       } else {
521597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
521697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
521797e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
521897e99dd9SToby Isaac             cind++;
521997e99dd9SToby Isaac           }
5220a5e93ea8SMatthew G. Knepley         }
5221a5e93ea8SMatthew G. Knepley       }
5222a5e93ea8SMatthew G. Knepley     }
5223a5e93ea8SMatthew G. Knepley   }
5224a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5225a5e93ea8SMatthew G. Knepley }
5226a5e93ea8SMatthew G. Knepley 
522797e99dd9SToby 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[])
5228a6dfd86eSKarl Rupp {
5229552f7358SJed Brown   PetscScalar    *a;
52301a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
52311a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
523297e99dd9SToby Isaac   PetscInt        cind = 0, b;
5233552f7358SJed Brown   PetscErrorCode  ierr;
5234552f7358SJed Brown 
5235552f7358SJed Brown   PetscFunctionBegin;
5236552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5237552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
52381a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
52391a271a75SMatthew G. Knepley   a    = &array[foff];
5240552f7358SJed Brown   if (!fcdof || setBC) {
524197e99dd9SToby Isaac     if (clperm) {
524297e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
524397e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5244552f7358SJed Brown     } else {
524597e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
524697e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5247552f7358SJed Brown     }
5248552f7358SJed Brown   } else {
5249552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
525097e99dd9SToby Isaac     if (clperm) {
525197e99dd9SToby Isaac       if (perm) {
525297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
525397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
525497e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5255552f7358SJed Brown         }
5256552f7358SJed Brown       } else {
525797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
525897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
525997e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
526097e99dd9SToby Isaac         }
526197e99dd9SToby Isaac       }
526297e99dd9SToby Isaac     } else {
526397e99dd9SToby Isaac       if (perm) {
526497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
526597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
526697e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
526797e99dd9SToby Isaac         }
526897e99dd9SToby Isaac       } else {
526997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
527097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
527197e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5272552f7358SJed Brown         }
5273552f7358SJed Brown       }
5274552f7358SJed Brown     }
5275552f7358SJed Brown   }
52761a271a75SMatthew G. Knepley   *offset += fdof;
5277552f7358SJed Brown   PetscFunctionReturn(0);
5278552f7358SJed Brown }
5279552f7358SJed Brown 
5280ba322698SMatthew 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[])
5281a5e93ea8SMatthew G. Knepley {
5282a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
52831a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
52841a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
52855da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
5286ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
5287a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5288a5e93ea8SMatthew G. Knepley 
5289a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
52905da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
5291a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5292a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
52931a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
52941a271a75SMatthew G. Knepley   a    = &array[foff];
5295a5e93ea8SMatthew G. Knepley   if (fcdof) {
5296ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
5297a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
529897e99dd9SToby Isaac     if (clperm) {
529997e99dd9SToby Isaac       if (perm) {
5300ba322698SMatthew G. Knepley         if (comps) {
5301ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5302ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
53035da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5304ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5305ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5306ba322698SMatthew G. Knepley           }
5307ba322698SMatthew G. Knepley         } else {
530897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
530997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
531097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5311a5e93ea8SMatthew G. Knepley               ++cind;
5312a5e93ea8SMatthew G. Knepley             }
5313a5e93ea8SMatthew G. Knepley           }
5314ba322698SMatthew G. Knepley         }
5315ba322698SMatthew G. Knepley       } else {
5316ba322698SMatthew G. Knepley         if (comps) {
5317ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5318ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
53195da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5320ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5321ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5322ba322698SMatthew G. Knepley           }
5323a5e93ea8SMatthew G. Knepley         } else {
532497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
532597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
532697e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
532797e99dd9SToby Isaac               ++cind;
532897e99dd9SToby Isaac             }
532997e99dd9SToby Isaac           }
533097e99dd9SToby Isaac         }
5331ba322698SMatthew G. Knepley       }
533297e99dd9SToby Isaac     } else {
533397e99dd9SToby Isaac       if (perm) {
5334ba322698SMatthew G. Knepley         if (comps) {
5335ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5336ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
53375da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5338ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5339ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5340ba322698SMatthew G. Knepley           }
5341ba322698SMatthew G. Knepley         } else {
534297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
534397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
534497e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
534597e99dd9SToby Isaac               ++cind;
534697e99dd9SToby Isaac             }
534797e99dd9SToby Isaac           }
5348ba322698SMatthew G. Knepley         }
5349ba322698SMatthew G. Knepley       } else {
5350ba322698SMatthew G. Knepley         if (comps) {
5351ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5352ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
53535da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5354ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5355ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5356ba322698SMatthew G. Knepley           }
535797e99dd9SToby Isaac         } else {
535897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
535997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
536097e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5361a5e93ea8SMatthew G. Knepley               ++cind;
5362a5e93ea8SMatthew G. Knepley             }
5363a5e93ea8SMatthew G. Knepley           }
5364a5e93ea8SMatthew G. Knepley         }
5365a5e93ea8SMatthew G. Knepley       }
5366a5e93ea8SMatthew G. Knepley     }
5367ba322698SMatthew G. Knepley   }
53681a271a75SMatthew G. Knepley   *offset += fdof;
5369a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5370a5e93ea8SMatthew G. Knepley }
5371a5e93ea8SMatthew G. Knepley 
53728f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5373a6dfd86eSKarl Rupp {
5374552f7358SJed Brown   PetscScalar    *array;
53751b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
53761b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5377552f7358SJed Brown   PetscErrorCode  ierr;
5378552f7358SJed Brown 
53791b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5380b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5381b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5382b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5383b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5384b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5385b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5386b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5387b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5388b6ebb6e6SMatthew G. Knepley 
5389b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5390b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5391b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5392b6ebb6e6SMatthew G. Knepley     {
5393b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5394b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5395b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5396b6ebb6e6SMatthew G. Knepley 
5397b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5398b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5399b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5400b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5401b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5402b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5403b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5404b6ebb6e6SMatthew G. Knepley           }
5405b6ebb6e6SMatthew G. Knepley         } else {
5406b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5407b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5408b6ebb6e6SMatthew G. Knepley           }
5409b6ebb6e6SMatthew G. Knepley         }
5410b6ebb6e6SMatthew G. Knepley       } else {
5411b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5412b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5413b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5414b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5415b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5416b6ebb6e6SMatthew G. Knepley           }
5417b6ebb6e6SMatthew G. Knepley         } else {
5418b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5419b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5420b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5421b6ebb6e6SMatthew G. Knepley           }
5422b6ebb6e6SMatthew G. Knepley         }
5423b6ebb6e6SMatthew G. Knepley       }
5424b6ebb6e6SMatthew G. Knepley     }
5425b6ebb6e6SMatthew G. Knepley   }
5426b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5427b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5428b6ebb6e6SMatthew G. Knepley }
54291b406b76SMatthew G. Knepley 
54301b406b76SMatthew G. Knepley /*@C
54311b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
54321b406b76SMatthew G. Knepley 
54331b406b76SMatthew G. Knepley   Not collective
54341b406b76SMatthew G. Knepley 
54351b406b76SMatthew G. Knepley   Input Parameters:
54361b406b76SMatthew G. Knepley + dm - The DM
54371b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54381b406b76SMatthew G. Knepley . v - The local vector
5439eaf898f9SPatrick Sanan . point - The point in the DM
54401b406b76SMatthew G. Knepley . values - The array of values
544122c1ee49SMatthew 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,
544222c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
54431b406b76SMatthew G. Knepley 
54441b406b76SMatthew G. Knepley   Fortran Notes:
54451b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
54461b406b76SMatthew G. Knepley 
54471b406b76SMatthew G. Knepley   Level: intermediate
54481b406b76SMatthew G. Knepley 
54491b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
54501b406b76SMatthew G. Knepley @*/
54511b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
54521b406b76SMatthew G. Knepley {
54531b406b76SMatthew G. Knepley   PetscSection    clSection;
54541b406b76SMatthew G. Knepley   IS              clPoints;
54551b406b76SMatthew G. Knepley   PetscScalar    *array;
54561b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
545727f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
5458c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
54591b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
54601b406b76SMatthew G. Knepley 
54611a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
54621b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
546392fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
54641a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
54651a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
54661b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
54671b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
54681b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
54698f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
54701b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
54711b406b76SMatthew G. Knepley   }
54721a271a75SMatthew G. Knepley   /* Get points */
5473923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5474c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
5475c459fbc1SJed Brown     PetscInt dof;
5476c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5477c459fbc1SJed Brown     clsize += dof;
5478c459fbc1SJed Brown   }
5479c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
54801a271a75SMatthew G. Knepley   /* Get array */
5481552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
54821a271a75SMatthew G. Knepley   /* Get values */
5483ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
548497e99dd9SToby Isaac     PetscInt offset = 0, f;
5485552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
548697e99dd9SToby Isaac       const PetscInt    **perms = NULL;
548797e99dd9SToby Isaac       const PetscScalar **flips = NULL;
548897e99dd9SToby Isaac 
548997e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5490552f7358SJed Brown       switch (mode) {
5491552f7358SJed Brown       case INSERT_VALUES:
549297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
549397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
549497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
549597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
549697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5497552f7358SJed Brown         } break;
5498552f7358SJed Brown       case INSERT_ALL_VALUES:
549997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
550097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
550197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
550297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
550397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5504552f7358SJed Brown         } break;
5505a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
550697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
550797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
550897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
550997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5510ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5511a5e93ea8SMatthew G. Knepley         } break;
5512552f7358SJed Brown       case ADD_VALUES:
551397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
551497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
551597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
551697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
551797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5518552f7358SJed Brown         } break;
5519552f7358SJed Brown       case ADD_ALL_VALUES:
552097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
552197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
552297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
552397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
552497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5525552f7358SJed Brown         } break;
5526304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
552797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
552897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
552997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
553097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5531ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5532304ab55fSMatthew G. Knepley         } break;
5533552f7358SJed Brown       default:
5534f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5535552f7358SJed Brown       }
553697e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
55371a271a75SMatthew G. Knepley     }
5538552f7358SJed Brown   } else {
55391a271a75SMatthew G. Knepley     PetscInt dof, off;
554097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
554197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
55421a271a75SMatthew G. Knepley 
554397e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5544552f7358SJed Brown     switch (mode) {
5545552f7358SJed Brown     case INSERT_VALUES:
554697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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;
555097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
555197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5552552f7358SJed Brown       } break;
5553552f7358SJed Brown     case INSERT_ALL_VALUES:
555497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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;
555897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
555997e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5560552f7358SJed Brown       } break;
5561a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
556297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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;
556697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
556797e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5568a5e93ea8SMatthew G. Knepley       } break;
5569552f7358SJed Brown     case ADD_VALUES:
557097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
557197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
557297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
557397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
557497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
557597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5576552f7358SJed Brown       } break;
5577552f7358SJed Brown     case ADD_ALL_VALUES:
557897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
557997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
558097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
558197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
558297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
558397e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5584552f7358SJed Brown       } break;
5585304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
558697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
558797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
558897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
558997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
559097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
559197e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5592304ab55fSMatthew G. Knepley       } break;
5593552f7358SJed Brown     default:
5594f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5595552f7358SJed Brown     }
559697e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5597552f7358SJed Brown   }
55981a271a75SMatthew G. Knepley   /* Cleanup points */
5599923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
56001a271a75SMatthew G. Knepley   /* Cleanup array */
5601552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5602552f7358SJed Brown   PetscFunctionReturn(0);
5603552f7358SJed Brown }
5604552f7358SJed Brown 
56055f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
56065f790a90SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
56075f790a90SMatthew G. Knepley {
56085f790a90SMatthew G. Knepley   PetscFunctionBegin;
56095f790a90SMatthew G. Knepley   if (label) {
56105f790a90SMatthew G. Knepley     PetscInt       val, fdof;
56115f790a90SMatthew G. Knepley     PetscErrorCode ierr;
56125f790a90SMatthew G. Knepley 
56135f790a90SMatthew G. Knepley     /* There is a problem with this:
56145f790a90SMatthew 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
56155f790a90SMatthew 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.
56165f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
56175f790a90SMatthew G. Knepley     */
56185f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
56195f790a90SMatthew G. Knepley     if (val < 0) {
56205f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
56215f790a90SMatthew G. Knepley       *offset += fdof;
56225f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
56235f790a90SMatthew G. Knepley     }
56245f790a90SMatthew G. Knepley   }
56255f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
56265f790a90SMatthew G. Knepley }
56275f790a90SMatthew G. Knepley 
562897529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
56295f790a90SMatthew 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)
5630e07394fbSMatthew G. Knepley {
5631e07394fbSMatthew G. Knepley   PetscSection      clSection;
5632e07394fbSMatthew G. Knepley   IS                clPoints;
5633e07394fbSMatthew G. Knepley   PetscScalar       *array;
5634e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
563597529cf3SJed Brown   const PetscInt    *clp;
5636e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
563797e99dd9SToby Isaac   PetscInt          offset = 0, f;
5638e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5639e07394fbSMatthew G. Knepley 
5640e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5641e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
564292fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5643e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5644e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5645e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5646e07394fbSMatthew G. Knepley   /* Get points */
5647923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5648e07394fbSMatthew G. Knepley   /* Get array */
5649e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5650e07394fbSMatthew G. Knepley   /* Get values */
5651e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
565297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
565397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
565497e99dd9SToby Isaac 
5655e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5656e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5657e07394fbSMatthew G. Knepley         PetscInt fdof;
5658e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5659e07394fbSMatthew G. Knepley         offset += fdof;
5660e07394fbSMatthew G. Knepley       }
5661e07394fbSMatthew G. Knepley       continue;
5662e07394fbSMatthew G. Knepley     }
566397e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5664e07394fbSMatthew G. Knepley     switch (mode) {
5665e07394fbSMatthew G. Knepley     case INSERT_VALUES:
566697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
566797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
566897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
566997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
56705f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
567197529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5672e07394fbSMatthew G. Knepley       } break;
5673e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
567497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
567597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
567697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
567797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
56785f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
567997529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5680e07394fbSMatthew G. Knepley       } break;
5681e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
568297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
568397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
568497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
568597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
56865f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
568797529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5688e07394fbSMatthew G. Knepley       } break;
5689e07394fbSMatthew G. Knepley     case ADD_VALUES:
569097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
569197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
569297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
569397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
56945f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
569597529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5696e07394fbSMatthew G. Knepley       } break;
5697e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
569897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
569997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
570097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
570197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
57025f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
570397529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5704e07394fbSMatthew G. Knepley       } break;
5705e07394fbSMatthew G. Knepley     default:
5706f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5707e07394fbSMatthew G. Knepley     }
570897e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5709e07394fbSMatthew G. Knepley   }
5710e07394fbSMatthew G. Knepley   /* Cleanup points */
5711923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5712e07394fbSMatthew G. Knepley   /* Cleanup array */
5713e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5714e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5715e07394fbSMatthew G. Knepley }
5716e07394fbSMatthew G. Knepley 
57177cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5718552f7358SJed Brown {
5719552f7358SJed Brown   PetscMPIInt    rank;
5720552f7358SJed Brown   PetscInt       i, j;
5721552f7358SJed Brown   PetscErrorCode ierr;
5722552f7358SJed Brown 
5723552f7358SJed Brown   PetscFunctionBegin;
5724ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr);
5725eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5726e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5727e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5728b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5729557cf195SMatthew G. Knepley   if (!values) PetscFunctionReturn(0);
5730b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5731e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5732b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5733519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
57347eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5735552f7358SJed Brown #else
5736b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5737552f7358SJed Brown #endif
5738552f7358SJed Brown     }
573977a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5740552f7358SJed Brown   }
5741552f7358SJed Brown   PetscFunctionReturn(0);
5742552f7358SJed Brown }
5743552f7358SJed Brown 
574405586334SMatthew G. Knepley /*
574505586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
574605586334SMatthew G. Knepley 
574705586334SMatthew G. Knepley   Input Parameters:
574805586334SMatthew G. Knepley + section - The section for this data layout
574936fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
575005586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
575105586334SMatthew G. Knepley . off     - The global offset of this point
575205586334SMatthew G. Knepley . loff    - The local offset of each field
575305586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
575405586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
575505586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
575605586334SMatthew G. Knepley 
575705586334SMatthew G. Knepley   Output Parameter:
575805586334SMatthew G. Knepley . indices - Indices for dofs on this point
575905586334SMatthew G. Knepley 
576005586334SMatthew G. Knepley   Level: developer
576105586334SMatthew G. Knepley 
576205586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
576305586334SMatthew G. Knepley */
576436fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5765a6dfd86eSKarl Rupp {
5766e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5767552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5768552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5769552f7358SJed Brown   PetscInt        cind = 0, k;
5770552f7358SJed Brown   PetscErrorCode  ierr;
5771552f7358SJed Brown 
5772552f7358SJed Brown   PetscFunctionBegin;
577336fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5774552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5775552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5776552f7358SJed Brown   if (!cdof || setBC) {
577705586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
577805586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
577905586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
578005586334SMatthew G. Knepley 
578105586334SMatthew G. Knepley       indices[ind] = off + k;
5782552f7358SJed Brown     }
5783552f7358SJed Brown   } else {
5784552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
57854acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
578605586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
578705586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
578805586334SMatthew G. Knepley 
57894acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
57904acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
579105586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
57924acb8e1eSToby Isaac         ++cind;
57934acb8e1eSToby Isaac       } else {
579436fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
5795552f7358SJed Brown       }
5796552f7358SJed Brown     }
5797552f7358SJed Brown   }
5798e6ccafaeSMatthew G Knepley   *loff += dof;
5799552f7358SJed Brown   PetscFunctionReturn(0);
5800552f7358SJed Brown }
5801552f7358SJed Brown 
58027e29afd2SMatthew G. Knepley /*
580336fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
58047e29afd2SMatthew G. Knepley 
580536fa2b79SJed Brown  Input Parameters:
580636fa2b79SJed Brown + section - a section (global or local)
580736fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
580836fa2b79SJed Brown . point - point within section
580936fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
581036fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
581136fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
581236fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
581336fa2b79SJed Brown . permsoff - offset
581436fa2b79SJed Brown - indperm - index permutation
581536fa2b79SJed Brown 
581636fa2b79SJed Brown  Output Parameter:
581736fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
581836fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
581936fa2b79SJed Brown 
582036fa2b79SJed Brown  Notes:
582136fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
582236fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
582336fa2b79SJed Brown  in the local vector.
582436fa2b79SJed Brown 
582536fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
582636fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
582736fa2b79SJed Brown 
582836fa2b79SJed Brown  Developer Note:
582936fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
583036fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
583136fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
583236fa2b79SJed Brown 
583336fa2b79SJed Brown  Example:
583436fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
583536fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
583636fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
583736fa2b79SJed 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.
583836fa2b79SJed Brown 
583936fa2b79SJed Brown  Level: developer
58407e29afd2SMatthew G. Knepley */
584136fa2b79SJed 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[])
5842a6dfd86eSKarl Rupp {
5843552f7358SJed Brown   PetscInt       numFields, foff, f;
5844552f7358SJed Brown   PetscErrorCode ierr;
5845552f7358SJed Brown 
5846552f7358SJed Brown   PetscFunctionBegin;
584736fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5848552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5849552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
58504acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5851552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
58524acb8e1eSToby Isaac     PetscInt        cind = 0, b;
58534acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5854552f7358SJed Brown 
5855552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5856552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5857552f7358SJed Brown     if (!cfdof || setBC) {
585805586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
585905586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
586005586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
586105586334SMatthew G. Knepley 
586205586334SMatthew G. Knepley         indices[ind] = off+foff+b;
586305586334SMatthew G. Knepley       }
5864552f7358SJed Brown     } else {
5865552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
586605586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
586705586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
586805586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
586905586334SMatthew G. Knepley 
58704acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
587105586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5872552f7358SJed Brown           ++cind;
5873552f7358SJed Brown         } else {
587436fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
5875552f7358SJed Brown         }
5876552f7358SJed Brown       }
5877552f7358SJed Brown     }
587836fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
5879552f7358SJed Brown     foffs[f] += fdof;
5880552f7358SJed Brown   }
5881552f7358SJed Brown   PetscFunctionReturn(0);
5882552f7358SJed Brown }
5883552f7358SJed Brown 
58847e29afd2SMatthew G. Knepley /*
58857e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
58867e29afd2SMatthew G. Knepley 
58877e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
5888645102dcSJed Brown 
5889645102dcSJed Brown  Notes:
5890645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
5891645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
58927e29afd2SMatthew G. Knepley */
5893645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
58947e29afd2SMatthew G. Knepley {
58957e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
58967e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
58977e29afd2SMatthew G. Knepley 
58987e29afd2SMatthew G. Knepley   PetscFunctionBegin;
58997e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
59007e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
59017e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
59027e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
59037e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
59047e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
59057e29afd2SMatthew G. Knepley 
59067e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
59077e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
59087e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
5909645102dcSJed Brown     if (!cfdof) {
591005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
591105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
591205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
591305586334SMatthew G. Knepley 
591405586334SMatthew G. Knepley         indices[ind] = foff+b;
591505586334SMatthew G. Knepley       }
59167e29afd2SMatthew G. Knepley     } else {
59177e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
591805586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
591905586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
592005586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
592105586334SMatthew G. Knepley 
59227e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
592305586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
59247e29afd2SMatthew G. Knepley           ++cind;
59257e29afd2SMatthew G. Knepley         } else {
592605586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
59277e29afd2SMatthew G. Knepley         }
59287e29afd2SMatthew G. Knepley       }
59297e29afd2SMatthew G. Knepley     }
59307e29afd2SMatthew G. Knepley     foffs[f] += fdof;
59317e29afd2SMatthew G. Knepley   }
59327e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
59337e29afd2SMatthew G. Knepley }
59347e29afd2SMatthew G. Knepley 
59354acb8e1eSToby 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)
5936d3d1a6afSToby Isaac {
5937d3d1a6afSToby Isaac   Mat             cMat;
5938d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5939d3d1a6afSToby Isaac   IS              aIS;
5940d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5941d3d1a6afSToby Isaac   const PetscInt  *anchors;
5942e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5943d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5944d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5945d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5946d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5947d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5948d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5949d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
59506ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5951d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5952d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5953d3d1a6afSToby Isaac 
5954d3d1a6afSToby Isaac   PetscFunctionBegin;
5955d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5956d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5957d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5958d3d1a6afSToby Isaac 
5959a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5960d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5961d3d1a6afSToby Isaac   if (aSec) {
5962580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5963d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5964d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5965d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5966d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5967d3d1a6afSToby Isaac      * into the global matrix anyway) */
5968d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5969d3d1a6afSToby Isaac       PetscInt b    = points[p];
59704b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5971d3d1a6afSToby Isaac 
59724b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
59734b2f2278SToby Isaac       if (!bSecDof) {
59744b2f2278SToby Isaac         continue;
59754b2f2278SToby Isaac       }
5976d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5977d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5978d3d1a6afSToby Isaac       }
5979d3d1a6afSToby Isaac       if (bDof) {
5980d3d1a6afSToby Isaac         /* this point is constrained */
5981d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5982d3d1a6afSToby Isaac         PetscInt bOff, q;
5983d3d1a6afSToby Isaac 
5984d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5985d3d1a6afSToby Isaac         newNumPoints  += bDof;
5986d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5987d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5988d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5989d3d1a6afSToby Isaac           PetscInt aDof;
5990d3d1a6afSToby Isaac 
5991d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5992d3d1a6afSToby Isaac           newNumIndices += aDof;
5993d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5994d3d1a6afSToby Isaac             PetscInt fDof;
5995d3d1a6afSToby Isaac 
5996d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5997d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5998d3d1a6afSToby Isaac           }
5999d3d1a6afSToby Isaac         }
6000d3d1a6afSToby Isaac       }
6001d3d1a6afSToby Isaac       else {
6002d3d1a6afSToby Isaac         /* this point is not constrained */
6003d3d1a6afSToby Isaac         newNumPoints++;
60044b2f2278SToby Isaac         newNumIndices += bSecDof;
6005d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
6006d3d1a6afSToby Isaac           PetscInt fDof;
6007d3d1a6afSToby Isaac 
6008d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6009d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
6010d3d1a6afSToby Isaac         }
6011d3d1a6afSToby Isaac       }
6012d3d1a6afSToby Isaac     }
6013d3d1a6afSToby Isaac   }
6014d3d1a6afSToby Isaac   if (!anyConstrained) {
601572b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
601672b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
601772b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
601872b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
601972b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
6020d3d1a6afSToby Isaac     PetscFunctionReturn(0);
6021d3d1a6afSToby Isaac   }
6022d3d1a6afSToby Isaac 
60236ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
60246ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
60256ecaa68aSToby Isaac 
6026f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
6027d3d1a6afSToby Isaac 
60286ecaa68aSToby Isaac   if (!outPoints && !outValues) {
60296ecaa68aSToby Isaac     if (offsets) {
60306ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
60316ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
60326ecaa68aSToby Isaac       }
60336ecaa68aSToby Isaac     }
60346ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
60356ecaa68aSToby Isaac     PetscFunctionReturn(0);
60366ecaa68aSToby Isaac   }
60376ecaa68aSToby Isaac 
6038f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
6039d3d1a6afSToby Isaac 
6040f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
6041d3d1a6afSToby Isaac 
6042d3d1a6afSToby Isaac   /* workspaces */
6043d3d1a6afSToby Isaac   if (numFields) {
6044d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
604569291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
604669291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6047d3d1a6afSToby Isaac     }
6048d3d1a6afSToby Isaac   }
6049d3d1a6afSToby Isaac   else {
605069291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
605169291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6052d3d1a6afSToby Isaac   }
6053d3d1a6afSToby Isaac 
6054d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
6055d3d1a6afSToby Isaac   if (numFields) {
60564b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
60574b2f2278SToby Isaac 
6058d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6059d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
60604b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6061d3d1a6afSToby Isaac 
60624b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
60634b2f2278SToby Isaac       if (!bSecDof) {
60644b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
60654b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
60664b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
60674b2f2278SToby Isaac         }
60684b2f2278SToby Isaac         continue;
60694b2f2278SToby Isaac       }
6070d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6071d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6072d3d1a6afSToby Isaac       }
6073d3d1a6afSToby Isaac       if (bDof) {
6074d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6075d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
6076d3d1a6afSToby Isaac 
6077d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6078d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6079d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
6080d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
6081d3d1a6afSToby Isaac             PetscInt aFDof;
6082d3d1a6afSToby Isaac 
6083d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
6084d3d1a6afSToby Isaac             allFDof += aFDof;
6085d3d1a6afSToby Isaac           }
6086d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
6087d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
6088d3d1a6afSToby Isaac         }
6089d3d1a6afSToby Isaac       }
6090d3d1a6afSToby Isaac       else {
6091d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6092d3d1a6afSToby Isaac           PetscInt fDof;
6093d3d1a6afSToby Isaac 
6094d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6095d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
6096d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
6097d3d1a6afSToby Isaac         }
6098d3d1a6afSToby Isaac       }
6099d3d1a6afSToby Isaac     }
61004b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
61014b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
61024b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
6103d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6104d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
6105d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
6106d3d1a6afSToby Isaac       }
610719f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
610819f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
610969291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
6110d3d1a6afSToby Isaac     }
6111d3d1a6afSToby Isaac   }
6112d3d1a6afSToby Isaac   else {
6113d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6114d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
61154b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6116d3d1a6afSToby Isaac 
61174b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
61184b2f2278SToby Isaac       if (!bSecDof) {
61194b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
61204b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
61214b2f2278SToby Isaac         continue;
61224b2f2278SToby Isaac       }
6123d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6124d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6125d3d1a6afSToby Isaac       }
6126d3d1a6afSToby Isaac       if (bDof) {
61274b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
6128d3d1a6afSToby Isaac 
6129d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6130d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6131d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
6132d3d1a6afSToby Isaac 
6133d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
6134d3d1a6afSToby Isaac           allDof += aDof;
6135d3d1a6afSToby Isaac         }
6136d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
61374b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
6138d3d1a6afSToby Isaac       }
6139d3d1a6afSToby Isaac       else {
61404b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
6141d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
6142d3d1a6afSToby Isaac       }
6143d3d1a6afSToby Isaac     }
6144d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
6145d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
6146d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6147d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6148d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6149d3d1a6afSToby Isaac     }
615069291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
6151d3d1a6afSToby Isaac   }
6152d3d1a6afSToby Isaac 
61536ecaa68aSToby Isaac   /* output arrays */
615469291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
61556ecaa68aSToby Isaac 
6156d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
6157d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
6158d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
615969291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
616069291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
6161d3d1a6afSToby Isaac   if (numFields) {
6162d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6163d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6164d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
61654b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6166d3d1a6afSToby Isaac 
61674b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
61684b2f2278SToby Isaac       if (!bSecDof) {
61694b2f2278SToby Isaac         continue;
61704b2f2278SToby Isaac       }
6171d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6172d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6173d3d1a6afSToby Isaac       }
6174d3d1a6afSToby Isaac       if (bDof) {
6175d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6176d3d1a6afSToby Isaac 
6177d3d1a6afSToby Isaac         fStart[0] = 0;
6178d3d1a6afSToby Isaac         fEnd[0]   = 0;
6179d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6180d3d1a6afSToby Isaac           PetscInt fDof;
6181d3d1a6afSToby Isaac 
6182d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6183d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6184d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6185d3d1a6afSToby Isaac         }
6186d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
618736fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6188d3d1a6afSToby Isaac 
6189d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6190d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6191d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6192d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6193d3d1a6afSToby Isaac 
6194d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6195d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6196d3d1a6afSToby Isaac         }
6197d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6198d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6199d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6200d3d1a6afSToby Isaac 
6201d3d1a6afSToby 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 */
6202d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6203d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6204302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
620536fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
6206d3d1a6afSToby Isaac         }
6207d3d1a6afSToby Isaac         newP += bDof;
6208d3d1a6afSToby Isaac 
62096ecaa68aSToby Isaac         if (outValues) {
6210d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
6211d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
6212d3d1a6afSToby 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);
6213d3d1a6afSToby Isaac           }
6214d3d1a6afSToby Isaac         }
62156ecaa68aSToby Isaac       }
6216d3d1a6afSToby Isaac       else {
6217d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6218d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6219d3d1a6afSToby Isaac         newP++;
6220d3d1a6afSToby Isaac       }
6221d3d1a6afSToby Isaac     }
6222d3d1a6afSToby Isaac   } else {
6223d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6224d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6225d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
62264b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6227d3d1a6afSToby Isaac 
62284b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
62294b2f2278SToby Isaac       if (!bSecDof) {
62304b2f2278SToby Isaac         continue;
62314b2f2278SToby Isaac       }
6232d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6233d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6234d3d1a6afSToby Isaac       }
6235d3d1a6afSToby Isaac       if (bDof) {
6236d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
6237d3d1a6afSToby Isaac 
6238d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
623936fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
6240d3d1a6afSToby Isaac 
6241d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
6242d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6243d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6244d3d1a6afSToby Isaac 
6245d3d1a6afSToby 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 */
6246d3d1a6afSToby Isaac 
6247d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6248d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6249302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
625036fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
6251d3d1a6afSToby Isaac         }
6252d3d1a6afSToby Isaac         newP += bDof;
6253d3d1a6afSToby Isaac 
6254d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
62556ecaa68aSToby Isaac         if (outValues) {
6256d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
6257d3d1a6afSToby Isaac         }
62586ecaa68aSToby Isaac       }
6259d3d1a6afSToby Isaac       else {
6260d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6261d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6262d3d1a6afSToby Isaac         newP++;
6263d3d1a6afSToby Isaac       }
6264d3d1a6afSToby Isaac     }
6265d3d1a6afSToby Isaac   }
6266d3d1a6afSToby Isaac 
62676ecaa68aSToby Isaac   if (outValues) {
626869291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
6269580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
6270d3d1a6afSToby Isaac     /* multiply constraints on the right */
6271d3d1a6afSToby Isaac     if (numFields) {
6272d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
6273d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
6274d3d1a6afSToby Isaac 
6275d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6276d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
6277d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6278d3d1a6afSToby Isaac           PetscInt c, r, k;
6279d3d1a6afSToby Isaac           PetscInt dof;
6280d3d1a6afSToby Isaac 
6281d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
62824b2f2278SToby Isaac           if (!dof) {
62834b2f2278SToby Isaac             continue;
62844b2f2278SToby Isaac           }
6285d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6286d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6287d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
6288d3d1a6afSToby Isaac 
6289d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6290d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
6291d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
62924acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6293d3d1a6afSToby Isaac                 }
6294d3d1a6afSToby Isaac               }
6295d3d1a6afSToby Isaac             }
6296d3d1a6afSToby Isaac           }
6297d3d1a6afSToby Isaac           else {
6298d3d1a6afSToby Isaac             /* copy this column as is */
6299d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6300d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
6301d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6302d3d1a6afSToby Isaac               }
6303d3d1a6afSToby Isaac             }
6304d3d1a6afSToby Isaac           }
6305d3d1a6afSToby Isaac           oldOff += dof;
6306d3d1a6afSToby Isaac         }
6307d3d1a6afSToby Isaac       }
6308d3d1a6afSToby Isaac     }
6309d3d1a6afSToby Isaac     else {
6310d3d1a6afSToby Isaac       PetscInt oldOff = 0;
6311d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6312d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
6313d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
6314d3d1a6afSToby Isaac         PetscInt c, r, k;
6315d3d1a6afSToby Isaac         PetscInt dof;
6316d3d1a6afSToby Isaac 
6317d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
63184b2f2278SToby Isaac         if (!dof) {
63194b2f2278SToby Isaac           continue;
63204b2f2278SToby Isaac         }
6321d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6322d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6323d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
6324d3d1a6afSToby Isaac 
6325d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6326d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
6327d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
6328d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6329d3d1a6afSToby Isaac               }
6330d3d1a6afSToby Isaac             }
6331d3d1a6afSToby Isaac           }
6332d3d1a6afSToby Isaac         }
6333d3d1a6afSToby Isaac         else {
6334d3d1a6afSToby Isaac           /* copy this column as is */
6335d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6336d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
6337d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6338d3d1a6afSToby Isaac             }
6339d3d1a6afSToby Isaac           }
6340d3d1a6afSToby Isaac         }
6341d3d1a6afSToby Isaac         oldOff += dof;
6342d3d1a6afSToby Isaac       }
6343d3d1a6afSToby Isaac     }
6344d3d1a6afSToby Isaac 
63456ecaa68aSToby Isaac     if (multiplyLeft) {
634669291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
6347580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
6348d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
6349d3d1a6afSToby Isaac       if (numFields) {
6350d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6351d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
6352d3d1a6afSToby Isaac 
6353d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
6354d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
6355d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
6356d3d1a6afSToby Isaac             PetscInt c, r, k;
6357d3d1a6afSToby Isaac             PetscInt dof;
6358d3d1a6afSToby Isaac 
6359d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
6360d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6361d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
6362d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
6363d3d1a6afSToby Isaac 
6364d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
6365d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6366d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
6367d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6368d3d1a6afSToby Isaac                   }
6369d3d1a6afSToby Isaac                 }
6370d3d1a6afSToby Isaac               }
6371d3d1a6afSToby Isaac             }
6372d3d1a6afSToby Isaac             else {
6373d3d1a6afSToby Isaac               /* copy this row as is */
6374d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6375d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6376d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6377d3d1a6afSToby Isaac                 }
6378d3d1a6afSToby Isaac               }
6379d3d1a6afSToby Isaac             }
6380d3d1a6afSToby Isaac             oldOff += dof;
6381d3d1a6afSToby Isaac           }
6382d3d1a6afSToby Isaac         }
6383d3d1a6afSToby Isaac       }
6384d3d1a6afSToby Isaac       else {
6385d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6386d3d1a6afSToby Isaac 
6387d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6388d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6389d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6390d3d1a6afSToby Isaac           PetscInt c, r, k;
6391d3d1a6afSToby Isaac           PetscInt dof;
6392d3d1a6afSToby Isaac 
6393d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6394d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6395d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6396d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6397d3d1a6afSToby Isaac 
6398d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6399d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6400d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6401d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6402d3d1a6afSToby Isaac                 }
6403d3d1a6afSToby Isaac               }
6404d3d1a6afSToby Isaac             }
6405d3d1a6afSToby Isaac           }
6406d3d1a6afSToby Isaac           else {
6407d3d1a6afSToby Isaac             /* copy this row as is */
64089fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6409d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6410d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6411d3d1a6afSToby Isaac               }
6412d3d1a6afSToby Isaac             }
6413d3d1a6afSToby Isaac           }
6414d3d1a6afSToby Isaac           oldOff += dof;
6415d3d1a6afSToby Isaac         }
6416d3d1a6afSToby Isaac       }
6417d3d1a6afSToby Isaac 
641869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
64196ecaa68aSToby Isaac     }
64206ecaa68aSToby Isaac     else {
64216ecaa68aSToby Isaac       newValues = tmpValues;
64226ecaa68aSToby Isaac     }
64236ecaa68aSToby Isaac   }
64246ecaa68aSToby Isaac 
6425d3d1a6afSToby Isaac   /* clean up */
642669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
642769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
64286ecaa68aSToby Isaac 
6429d3d1a6afSToby Isaac   if (numFields) {
6430d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
643169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
643269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
643369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6434d3d1a6afSToby Isaac     }
6435d3d1a6afSToby Isaac   }
6436d3d1a6afSToby Isaac   else {
643769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
643869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
643969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6440d3d1a6afSToby Isaac   }
6441d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6442d3d1a6afSToby Isaac 
6443d3d1a6afSToby Isaac   /* output */
64446ecaa68aSToby Isaac   if (outPoints) {
6445d3d1a6afSToby Isaac     *outPoints = newPoints;
64466ecaa68aSToby Isaac   }
64476ecaa68aSToby Isaac   else {
644869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
64496ecaa68aSToby Isaac   }
645031620726SToby Isaac   if (outValues) {
6451d3d1a6afSToby Isaac     *outValues = newValues;
64526ecaa68aSToby Isaac   }
64536ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6454d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6455d3d1a6afSToby Isaac   }
6456d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6457d3d1a6afSToby Isaac }
6458d3d1a6afSToby Isaac 
64594a1e0b3eSMatthew G. Knepley /*@C
646071f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
64617cd05799SMatthew G. Knepley 
64627cd05799SMatthew G. Knepley   Not collective
64637cd05799SMatthew G. Knepley 
64647cd05799SMatthew G. Knepley   Input Parameters:
64657cd05799SMatthew G. Knepley + dm         - The DM
646671f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
646771f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
646871f0bbf9SMatthew G. Knepley . point      - The point defining the closure
646971f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
64707cd05799SMatthew G. Knepley 
647171f0bbf9SMatthew G. Knepley   Output Parameters:
647271f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
647371f0bbf9SMatthew G. Knepley . indices    - The dof indices
647471f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
647571f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
64767cd05799SMatthew G. Knepley 
647736fa2b79SJed Brown   Notes:
647836fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
647936fa2b79SJed Brown 
648036fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
648136fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
648236fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
648336fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
648436fa2b79SJed Brown   indices (with the above semantics) are implied.
64857cd05799SMatthew G. Knepley 
64867cd05799SMatthew G. Knepley   Level: advanced
64877cd05799SMatthew G. Knepley 
648836fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
64894a1e0b3eSMatthew G. Knepley @*/
649071f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
649171f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
64927773e69fSMatthew G. Knepley {
649371f0bbf9SMatthew G. Knepley   /* Closure ordering */
64947773e69fSMatthew G. Knepley   PetscSection        clSection;
64957773e69fSMatthew G. Knepley   IS                  clPoints;
649671f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
649771f0bbf9SMatthew G. Knepley   PetscInt           *points;
649871f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
649971f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
65004acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
650171f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
650271f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
650371f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
650471f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
650571f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
650671f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
650771f0bbf9SMatthew G. Knepley 
650871f0bbf9SMatthew G. Knepley   PetscInt           *idx;
650971f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
651071f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
65117773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
65127773e69fSMatthew G. Knepley 
651371f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
65147773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65157773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
651636fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
651771f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
651871f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
651971f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
652071f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
65217773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
65227773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6523580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
652471f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
652571f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
6526c459fbc1SJed Brown   if (useClPerm) {
6527c459fbc1SJed Brown     PetscInt depth, clsize;
6528c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
6529c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
6530c459fbc1SJed Brown       PetscInt dof;
6531c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6532c459fbc1SJed Brown       clsize += dof;
6533c459fbc1SJed Brown     }
6534c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
6535c459fbc1SJed Brown   }
653671f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
653771f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
65387773e69fSMatthew G. Knepley     PetscInt dof, fdof;
65397773e69fSMatthew G. Knepley 
65407773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
65417773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
65427773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
65437773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
65447773e69fSMatthew G. Knepley     }
654571f0bbf9SMatthew G. Knepley     Ni += dof;
65467773e69fSMatthew G. Knepley   }
65477773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
654871f0bbf9SMatthew 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);
654971f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
655071f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
655171f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
655271f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
655371f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
655471f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
655571f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
65566ecaa68aSToby Isaac 
655771f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
655871f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
655971f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
656071f0bbf9SMatthew G. Knepley 
656171f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
656271f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
656371f0bbf9SMatthew G. Knepley         if (flip) {
656471f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
656571f0bbf9SMatthew G. Knepley 
656671f0bbf9SMatthew G. Knepley           if (!valCopy) {
656771f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
656871f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
656971f0bbf9SMatthew G. Knepley             *values = valCopy;
657071f0bbf9SMatthew G. Knepley           }
657171f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
657271f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
657371f0bbf9SMatthew G. Knepley 
657471f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
657571f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
657671f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
65776ecaa68aSToby Isaac             }
65786ecaa68aSToby Isaac           }
657971f0bbf9SMatthew G. Knepley         }
658071f0bbf9SMatthew G. Knepley         foffset += fdof;
658171f0bbf9SMatthew G. Knepley       }
658271f0bbf9SMatthew G. Knepley     }
658371f0bbf9SMatthew G. Knepley   }
658471f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
658571f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
658671f0bbf9SMatthew G. Knepley   if (NclC) {
658771f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
658871f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
658971f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
659071f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
659171f0bbf9SMatthew G. Knepley     }
659271f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
659371f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
659471f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
659571f0bbf9SMatthew G. Knepley     }
659671f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
659771f0bbf9SMatthew G. Knepley     Ncl     = NclC;
659871f0bbf9SMatthew G. Knepley     Ni      = NiC;
659971f0bbf9SMatthew G. Knepley     points  = pointsC;
660071f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
660171f0bbf9SMatthew G. Knepley   }
660271f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
660371f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
660471f0bbf9SMatthew G. Knepley   if (Nf) {
660571f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
660671f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
660771f0bbf9SMatthew G. Knepley 
660871f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
660971f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
661071f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
661171f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
661271f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
661371f0bbf9SMatthew G. Knepley 
661471f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
66157773e69fSMatthew G. Knepley       }
66167773e69fSMatthew G. Knepley     } else {
661771f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
661871f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
661971f0bbf9SMatthew G. Knepley 
662071f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
662171f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
662271f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
662371f0bbf9SMatthew G. Knepley          * global section. */
662471f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
662571f0bbf9SMatthew G. Knepley       }
662671f0bbf9SMatthew G. Knepley     }
662771f0bbf9SMatthew G. Knepley   } else {
662871f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
662971f0bbf9SMatthew G. Knepley 
663071f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
663171f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
66324acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
66334acb8e1eSToby Isaac 
663471f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
663571f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
663671f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
663771f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
66387773e69fSMatthew G. Knepley     }
66397773e69fSMatthew G. Knepley   }
664071f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
664171f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
664271f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
664371f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
66444acb8e1eSToby Isaac   }
664571f0bbf9SMatthew G. Knepley   if (NclC) {
664671f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
66477773e69fSMatthew G. Knepley   } else {
664871f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
66497773e69fSMatthew G. Knepley   }
665071f0bbf9SMatthew G. Knepley 
665171f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
665271f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
66537773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
66547773e69fSMatthew G. Knepley }
66557773e69fSMatthew G. Knepley 
66567cd05799SMatthew G. Knepley /*@C
665771f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
66587cd05799SMatthew G. Knepley 
66597cd05799SMatthew G. Knepley   Not collective
66607cd05799SMatthew G. Knepley 
66617cd05799SMatthew G. Knepley   Input Parameters:
66627cd05799SMatthew G. Knepley + dm         - The DM
666371f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
666471f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
666571f0bbf9SMatthew G. Knepley . point      - The point defining the closure
666671f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
666771f0bbf9SMatthew G. Knepley 
666871f0bbf9SMatthew G. Knepley   Output Parameters:
666971f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
667071f0bbf9SMatthew G. Knepley . indices    - The dof indices
667171f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
667271f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
667371f0bbf9SMatthew G. Knepley 
667471f0bbf9SMatthew G. Knepley   Notes:
667571f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
667671f0bbf9SMatthew G. Knepley 
667771f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
667871f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
667971f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
668071f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
668171f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
66827cd05799SMatthew G. Knepley 
66837cd05799SMatthew G. Knepley   Level: advanced
66847cd05799SMatthew G. Knepley 
668571f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
66867cd05799SMatthew G. Knepley @*/
668771f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
668871f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
66897773e69fSMatthew G. Knepley {
66907773e69fSMatthew G. Knepley   PetscErrorCode ierr;
66917773e69fSMatthew G. Knepley 
66927773e69fSMatthew G. Knepley   PetscFunctionBegin;
66937773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66947773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
669569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
66967773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
66977773e69fSMatthew G. Knepley }
66987773e69fSMatthew G. Knepley 
66997f5d1fdeSMatthew G. Knepley /*@C
67007f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
67017f5d1fdeSMatthew G. Knepley 
67027f5d1fdeSMatthew G. Knepley   Not collective
67037f5d1fdeSMatthew G. Knepley 
67047f5d1fdeSMatthew G. Knepley   Input Parameters:
67057f5d1fdeSMatthew G. Knepley + dm - The DM
6706ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6707ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
67087f5d1fdeSMatthew G. Knepley . A - The matrix
6709eaf898f9SPatrick Sanan . point - The point in the DM
67107f5d1fdeSMatthew G. Knepley . values - The array of values
67117f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
67127f5d1fdeSMatthew G. Knepley 
67137f5d1fdeSMatthew G. Knepley   Fortran Notes:
67147f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
67157f5d1fdeSMatthew G. Knepley 
67167f5d1fdeSMatthew G. Knepley   Level: intermediate
67177f5d1fdeSMatthew G. Knepley 
67184a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
67197f5d1fdeSMatthew G. Knepley @*/
67207c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6721552f7358SJed Brown {
6722552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
6723552f7358SJed Brown   PetscInt          *indices;
672471f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
672571f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
6726552f7358SJed Brown   PetscErrorCode     ierr;
6727552f7358SJed Brown 
6728552f7358SJed Brown   PetscFunctionBegin;
6729552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
673092fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
67313dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6732e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
67333dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
67343dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6735552f7358SJed Brown 
673671f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
67370d644c17SKarl Rupp 
6738b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
67394a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6740552f7358SJed Brown   if (ierr) {
6741552f7358SJed Brown     PetscMPIInt    rank;
6742552f7358SJed Brown     PetscErrorCode ierr2;
6743552f7358SJed Brown 
674455b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6745e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6746b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
674771f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
674871f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6749552f7358SJed Brown     CHKERRQ(ierr);
6750552f7358SJed Brown   }
67514a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
67524a1e0b3eSMatthew G. Knepley     PetscInt i;
67534a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
67544a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
67554a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
67564a1e0b3eSMatthew G. Knepley   }
675771f0bbf9SMatthew G. Knepley 
675871f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
675971f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
676071f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
67614acb8e1eSToby Isaac }
676271f0bbf9SMatthew G. Knepley 
67634a1e0b3eSMatthew G. Knepley /*@C
67644a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
67654a1e0b3eSMatthew G. Knepley 
67664a1e0b3eSMatthew G. Knepley   Not collective
67674a1e0b3eSMatthew G. Knepley 
67684a1e0b3eSMatthew G. Knepley   Input Parameters:
67694a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
67704a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
67714a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
67724a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
67734a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
67744a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
67754a1e0b3eSMatthew G. Knepley . A - The matrix
67764a1e0b3eSMatthew G. Knepley . point - The point in the DMs
67774a1e0b3eSMatthew G. Knepley . values - The array of values
67784a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
67794a1e0b3eSMatthew G. Knepley 
67804a1e0b3eSMatthew G. Knepley   Level: intermediate
67814a1e0b3eSMatthew G. Knepley 
67824a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
67834a1e0b3eSMatthew G. Knepley @*/
678471f0bbf9SMatthew 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)
678571f0bbf9SMatthew G. Knepley {
678671f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
678771f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
678871f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
678971f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
679071f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
679171f0bbf9SMatthew G. Knepley 
679271f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
679371f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
679471f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
679571f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
679671f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
679771f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
679871f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
679971f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
680071f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
680171f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
680271f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
680371f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
680471f0bbf9SMatthew G. Knepley 
680571f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
680671f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
680771f0bbf9SMatthew G. Knepley 
680871f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
68094a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
681071f0bbf9SMatthew G. Knepley   if (ierr) {
681171f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
681271f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
681371f0bbf9SMatthew G. Knepley 
681455b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
681571f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
681671f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
681771f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
681871f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
681971f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
682071f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
6821d3d1a6afSToby Isaac   }
682271f0bbf9SMatthew G. Knepley 
682371f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
682471f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
682571f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
6826552f7358SJed Brown   PetscFunctionReturn(0);
6827552f7358SJed Brown }
6828552f7358SJed Brown 
6829de41b84cSMatthew 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)
6830de41b84cSMatthew G. Knepley {
6831de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6832de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6833de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6834de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
683517c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6836de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6837412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
68384ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6839de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6840de41b84cSMatthew G. Knepley 
6841de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6842de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6843de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
684492fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6845de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
684692fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6847de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6848e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6849de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6850e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6851de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6852de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6853de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6854de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6855580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6856580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
6857de41b84cSMatthew G. Knepley   /* Column indices */
6858de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
68594ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6860de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6861de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6862de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6863de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6864de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6865de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6866de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6867de41b84cSMatthew G. Knepley       ++q;
6868de41b84cSMatthew G. Knepley     }
6869de41b84cSMatthew G. Knepley   }
6870de41b84cSMatthew G. Knepley   numCPoints = q;
6871de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6872de41b84cSMatthew G. Knepley     PetscInt fdof;
6873de41b84cSMatthew G. Knepley 
6874de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
68754ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6876de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6877de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6878de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6879de41b84cSMatthew G. Knepley     }
6880de41b84cSMatthew G. Knepley     numCIndices += dof;
6881de41b84cSMatthew G. Knepley   }
6882de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6883de41b84cSMatthew G. Knepley   /* Row indices */
6884412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6885412e9a14SMatthew G. Knepley   {
6886412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6887412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6888412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6889412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6890412e9a14SMatthew G. Knepley   }
689169291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6892de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6893de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6894de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6895de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6896de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6897de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6898de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
68994ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
69004ca5e9f5SMatthew G. Knepley         if (!dof) continue;
69014ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
69024ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6903de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6904de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6905de41b84cSMatthew G. Knepley         ++q;
6906de41b84cSMatthew G. Knepley       }
6907de41b84cSMatthew G. Knepley     }
6908de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6909de41b84cSMatthew G. Knepley   }
6910de41b84cSMatthew G. Knepley   numFPoints = q;
6911de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6912de41b84cSMatthew G. Knepley     PetscInt fdof;
6913de41b84cSMatthew G. Knepley 
6914de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
69154ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6916de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6917de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6918de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6919de41b84cSMatthew G. Knepley     }
6920de41b84cSMatthew G. Knepley     numFIndices += dof;
6921de41b84cSMatthew G. Knepley   }
6922de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6923de41b84cSMatthew G. Knepley 
69248ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
69258ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
692669291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
692769291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6928de41b84cSMatthew G. Knepley   if (numFields) {
69294acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
69304acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
69314acb8e1eSToby Isaac 
69324acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
69334acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
69344acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6935de41b84cSMatthew G. Knepley     }
69364acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
69374acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
693836fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
69394acb8e1eSToby Isaac     }
69404acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
69414acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
694236fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
69434acb8e1eSToby Isaac     }
69444acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
69454acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
69464acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6947de41b84cSMatthew G. Knepley     }
6948de41b84cSMatthew G. Knepley   } else {
69494acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
69504acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
69514acb8e1eSToby Isaac 
69524acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
69534acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
69544acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
69554acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
69564acb8e1eSToby Isaac 
69574acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
695836fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6959de41b84cSMatthew G. Knepley     }
69604acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
69614acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
69624acb8e1eSToby Isaac 
69634acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
696436fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6965de41b84cSMatthew G. Knepley     }
69664acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
69674acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6968de41b84cSMatthew G. Knepley   }
6969de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
69704acb8e1eSToby Isaac   /* TODO: flips */
6971de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6972de41b84cSMatthew G. Knepley   if (ierr) {
6973de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6974de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6975de41b84cSMatthew G. Knepley 
697655b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6977e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6978de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
697969291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
698069291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6981de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6982de41b84cSMatthew G. Knepley   }
698369291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6984de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
698569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
698669291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6987de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6988de41b84cSMatthew G. Knepley }
6989de41b84cSMatthew G. Knepley 
69907c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
69917c927364SMatthew G. Knepley {
69927c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
69937c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
69947c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
699517c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6996412e9a14SMatthew G. Knepley   DMPolytopeType ct;
69977c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
69987c927364SMatthew G. Knepley   PetscErrorCode ierr;
69997c927364SMatthew G. Knepley 
70007c927364SMatthew G. Knepley   PetscFunctionBegin;
70017c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
70027c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
700392fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
70047c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
700592fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
70067c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7007e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
70087c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7009e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
70107c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
70117c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
70127c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7013580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7014580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
70157c927364SMatthew G. Knepley   /* Column indices */
70167c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
70177c927364SMatthew G. Knepley   maxFPoints = numCPoints;
70187c927364SMatthew G. Knepley   /* Compress out points not in the section */
70197c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
70207c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
70217c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
70227c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
70237c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
70247c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
70257c927364SMatthew G. Knepley       ++q;
70267c927364SMatthew G. Knepley     }
70277c927364SMatthew G. Knepley   }
70287c927364SMatthew G. Knepley   numCPoints = q;
70297c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
70307c927364SMatthew G. Knepley     PetscInt fdof;
70317c927364SMatthew G. Knepley 
70327c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
70337c927364SMatthew G. Knepley     if (!dof) continue;
70347c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
70357c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
70367c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
70377c927364SMatthew G. Knepley     }
70387c927364SMatthew G. Knepley     numCIndices += dof;
70397c927364SMatthew G. Knepley   }
70407c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
70417c927364SMatthew G. Knepley   /* Row indices */
7042412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7043412e9a14SMatthew G. Knepley   {
7044412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7045412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7046412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7047412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7048412e9a14SMatthew G. Knepley   }
704969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
70507c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
70517c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
70527c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
70537c927364SMatthew G. Knepley     /* Compress out points not in the section */
70547c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
70557c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
70567c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
70577c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
70587c927364SMatthew G. Knepley         if (!dof) continue;
70597c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
70607c927364SMatthew G. Knepley         if (s < q) continue;
70617c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
70627c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
70637c927364SMatthew G. Knepley         ++q;
70647c927364SMatthew G. Knepley       }
70657c927364SMatthew G. Knepley     }
70667c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
70677c927364SMatthew G. Knepley   }
70687c927364SMatthew G. Knepley   numFPoints = q;
70697c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
70707c927364SMatthew G. Knepley     PetscInt fdof;
70717c927364SMatthew G. Knepley 
70727c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
70737c927364SMatthew G. Knepley     if (!dof) continue;
70747c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
70757c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
70767c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
70777c927364SMatthew G. Knepley     }
70787c927364SMatthew G. Knepley     numFIndices += dof;
70797c927364SMatthew G. Knepley   }
70807c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
70817c927364SMatthew G. Knepley 
70828ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
70838ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
70847c927364SMatthew G. Knepley   if (numFields) {
70854acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
70864acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
70874acb8e1eSToby Isaac 
70884acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
70894acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
70904acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
70917c927364SMatthew G. Knepley     }
70924acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
70934acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
709436fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
70954acb8e1eSToby Isaac     }
70964acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
70974acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
709836fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
70994acb8e1eSToby Isaac     }
71004acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
71014acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
71024acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
71037c927364SMatthew G. Knepley     }
71047c927364SMatthew G. Knepley   } else {
71054acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
71064acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
71074acb8e1eSToby Isaac 
71084acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71094acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
71104acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
71114acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
71124acb8e1eSToby Isaac 
71134acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
711436fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
71157c927364SMatthew G. Knepley     }
71164acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
71174acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
71184acb8e1eSToby Isaac 
71194acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
712036fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
71217c927364SMatthew G. Knepley     }
71224acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71234acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
71247c927364SMatthew G. Knepley   }
712569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
71267c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
71277c927364SMatthew G. Knepley   PetscFunctionReturn(0);
71287c927364SMatthew G. Knepley }
71297c927364SMatthew G. Knepley 
71307cd05799SMatthew G. Knepley /*@C
71317cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
71327cd05799SMatthew G. Knepley 
71337cd05799SMatthew G. Knepley   Input Parameter:
71347cd05799SMatthew G. Knepley . dm   - The DMPlex object
71357cd05799SMatthew G. Knepley 
71367cd05799SMatthew G. Knepley   Output Parameter:
71377cd05799SMatthew G. Knepley . cellHeight - The height of a cell
71387cd05799SMatthew G. Knepley 
71397cd05799SMatthew G. Knepley   Level: developer
71407cd05799SMatthew G. Knepley 
71417cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
71427cd05799SMatthew G. Knepley @*/
7143552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7144552f7358SJed Brown {
7145552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7146552f7358SJed Brown 
7147552f7358SJed Brown   PetscFunctionBegin;
7148552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7149552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
7150552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
7151552f7358SJed Brown   PetscFunctionReturn(0);
7152552f7358SJed Brown }
7153552f7358SJed Brown 
71547cd05799SMatthew G. Knepley /*@C
71557cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
71567cd05799SMatthew G. Knepley 
71577cd05799SMatthew G. Knepley   Input Parameters:
71587cd05799SMatthew G. Knepley + dm   - The DMPlex object
71597cd05799SMatthew G. Knepley - cellHeight - The height of a cell
71607cd05799SMatthew G. Knepley 
71617cd05799SMatthew G. Knepley   Level: developer
71627cd05799SMatthew G. Knepley 
71637cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
71647cd05799SMatthew G. Knepley @*/
7165552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7166552f7358SJed Brown {
7167552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7168552f7358SJed Brown 
7169552f7358SJed Brown   PetscFunctionBegin;
7170552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7171552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7172552f7358SJed Brown   PetscFunctionReturn(0);
7173552f7358SJed Brown }
7174552f7358SJed Brown 
7175e6139122SMatthew G. Knepley /*@
7176e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7177e6139122SMatthew G. Knepley 
7178e6139122SMatthew G. Knepley   Input Parameter:
7179e6139122SMatthew G. Knepley . dm - The DMPlex object
7180e6139122SMatthew G. Knepley 
7181e6139122SMatthew G. Knepley   Output Parameters:
71822a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
71832a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7184e6139122SMatthew G. Knepley 
71852a9f31c0SMatthew G. Knepley   Level: advanced
7186e6139122SMatthew G. Knepley 
71878065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7188e6139122SMatthew G. Knepley @*/
7189e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7190e6139122SMatthew G. Knepley {
7191412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7192e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7193e6139122SMatthew G. Knepley 
7194e6139122SMatthew G. Knepley   PetscFunctionBegin;
7195e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7196412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
7197412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
7198e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
7199e6139122SMatthew G. Knepley }
7200e6139122SMatthew G. Knepley 
7201552f7358SJed Brown /* We can easily have a form that takes an IS instead */
72029886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7203552f7358SJed Brown {
7204552f7358SJed Brown   PetscSection   section, globalSection;
7205552f7358SJed Brown   PetscInt      *numbers, p;
7206552f7358SJed Brown   PetscErrorCode ierr;
7207552f7358SJed Brown 
7208552f7358SJed Brown   PetscFunctionBegin;
720982f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
7210552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
7211552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7212552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
7213552f7358SJed Brown   }
7214552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
721515b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
7216854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
7217552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7218552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
7219ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7220ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
7221552f7358SJed Brown   }
722282f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
7223ef48cebcSMatthew G. Knepley   if (globalSize) {
7224ef48cebcSMatthew G. Knepley     PetscLayout layout;
7225ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
7226ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
7227ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
7228ef48cebcSMatthew G. Knepley   }
7229552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
7230552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
7231552f7358SJed Brown   PetscFunctionReturn(0);
7232552f7358SJed Brown }
7233552f7358SJed Brown 
723481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7235552f7358SJed Brown {
7236412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
7237552f7358SJed Brown   PetscErrorCode ierr;
7238552f7358SJed Brown 
7239552f7358SJed Brown   PetscFunctionBegin;
7240552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7241412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
7242412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
72439886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
724481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
7245552f7358SJed Brown }
724681ed3555SMatthew G. Knepley 
72478dab3259SMatthew G. Knepley /*@
72487cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
72497cd05799SMatthew G. Knepley 
72507cd05799SMatthew G. Knepley   Input Parameter:
72517cd05799SMatthew G. Knepley . dm   - The DMPlex object
72527cd05799SMatthew G. Knepley 
72537cd05799SMatthew G. Knepley   Output Parameter:
72547cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
72557cd05799SMatthew G. Knepley 
72567cd05799SMatthew G. Knepley   Level: developer
72577cd05799SMatthew G. Knepley 
72587cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
72597cd05799SMatthew G. Knepley @*/
726081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
726181ed3555SMatthew G. Knepley {
726281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
726381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
726481ed3555SMatthew G. Knepley 
726581ed3555SMatthew G. Knepley   PetscFunctionBegin;
726681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
726781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
7268552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
7269552f7358SJed Brown   PetscFunctionReturn(0);
7270552f7358SJed Brown }
7271552f7358SJed Brown 
727281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
727381ed3555SMatthew G. Knepley {
7274412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
727581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
727681ed3555SMatthew G. Knepley 
727781ed3555SMatthew G. Knepley   PetscFunctionBegin;
727881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
727981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
72809886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
728181ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
728281ed3555SMatthew G. Knepley }
728381ed3555SMatthew G. Knepley 
72848dab3259SMatthew G. Knepley /*@
72856aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
72867cd05799SMatthew G. Knepley 
72877cd05799SMatthew G. Knepley   Input Parameter:
72887cd05799SMatthew G. Knepley . dm   - The DMPlex object
72897cd05799SMatthew G. Knepley 
72907cd05799SMatthew G. Knepley   Output Parameter:
72917cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
72927cd05799SMatthew G. Knepley 
72937cd05799SMatthew G. Knepley   Level: developer
72947cd05799SMatthew G. Knepley 
72957cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
72967cd05799SMatthew G. Knepley @*/
7297552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7298552f7358SJed Brown {
7299552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
7300552f7358SJed Brown   PetscErrorCode ierr;
7301552f7358SJed Brown 
7302552f7358SJed Brown   PetscFunctionBegin;
7303552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
730481ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
7305552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
7306552f7358SJed Brown   PetscFunctionReturn(0);
7307552f7358SJed Brown }
7308552f7358SJed Brown 
73098dab3259SMatthew G. Knepley /*@
73107cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
73117cd05799SMatthew G. Knepley 
73127cd05799SMatthew G. Knepley   Input Parameter:
73137cd05799SMatthew G. Knepley . dm   - The DMPlex object
73147cd05799SMatthew G. Knepley 
73157cd05799SMatthew G. Knepley   Output Parameter:
73167cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
73177cd05799SMatthew G. Knepley 
73187cd05799SMatthew G. Knepley   Level: developer
73197cd05799SMatthew G. Knepley 
73207cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
73217cd05799SMatthew G. Knepley @*/
7322ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7323ef48cebcSMatthew G. Knepley {
7324ef48cebcSMatthew G. Knepley   IS             nums[4];
7325862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
7326ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
7327ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
7328ef48cebcSMatthew G. Knepley 
7329ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
7330ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7331ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
73328abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
73338abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
7334862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7335862913ffSStefano Zampini     PetscInt end;
7336862913ffSStefano Zampini 
7337862913ffSStefano Zampini     depths[d] = depth-d;
7338862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
7339862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7340862913ffSStefano Zampini   }
7341862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
7342820f2d46SBarry Smith   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
7343862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7344862913ffSStefano 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]);
7345862913ffSStefano Zampini   }
7346ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
7347ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
7348ef48cebcSMatthew G. Knepley 
7349862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
73509886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
7351ef48cebcSMatthew G. Knepley     shift += gsize;
7352ef48cebcSMatthew G. Knepley   }
7353302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7354ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7355ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7356ef48cebcSMatthew G. Knepley }
7357ef48cebcSMatthew G. Knepley 
735808a22f4bSMatthew G. Knepley 
735908a22f4bSMatthew G. Knepley /*@
736008a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
736108a22f4bSMatthew G. Knepley 
736208a22f4bSMatthew G. Knepley   Input Parameter:
736308a22f4bSMatthew G. Knepley . dm - The DMPlex object
736408a22f4bSMatthew G. Knepley 
736508a22f4bSMatthew G. Knepley   Output Parameter:
736608a22f4bSMatthew G. Knepley . ranks - The rank field
736708a22f4bSMatthew G. Knepley 
736808a22f4bSMatthew G. Knepley   Options Database Keys:
736908a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
737008a22f4bSMatthew G. Knepley 
737108a22f4bSMatthew G. Knepley   Level: intermediate
737208a22f4bSMatthew G. Knepley 
737308a22f4bSMatthew G. Knepley .seealso: DMView()
737408a22f4bSMatthew G. Knepley @*/
737508a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
737608a22f4bSMatthew G. Knepley {
737708a22f4bSMatthew G. Knepley   DM             rdm;
737808a22f4bSMatthew G. Knepley   PetscFE        fe;
737908a22f4bSMatthew G. Knepley   PetscScalar   *r;
738008a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
7381a55f9a55SMatthew G. Knepley   DMPolytopeType ct;
738208a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
7383a55f9a55SMatthew G. Knepley   PetscBool      simplex;
738408a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
738508a22f4bSMatthew G. Knepley 
738608a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7387f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7388f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
7389ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
739008a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
739108a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7392a55f9a55SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7393a55f9a55SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
7394a55f9a55SMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
7395a55f9a55SMatthew G. Knepley   ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
739608a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7397e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
739808a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7399e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
740008a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
740108a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
740208a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
740308a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
740408a22f4bSMatthew G. Knepley     PetscScalar *lr;
740508a22f4bSMatthew G. Knepley 
740608a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
740771f09efeSPierre Jolivet     if (lr) *lr = rank;
740808a22f4bSMatthew G. Knepley   }
740908a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
741008a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
741108a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
741208a22f4bSMatthew G. Knepley }
741308a22f4bSMatthew G. Knepley 
7414ca8062c8SMatthew G. Knepley /*@
741518e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
741618e14f0cSMatthew G. Knepley 
741718e14f0cSMatthew G. Knepley   Input Parameters:
741818e14f0cSMatthew G. Knepley + dm    - The DMPlex
741918e14f0cSMatthew G. Knepley - label - The DMLabel
742018e14f0cSMatthew G. Knepley 
742118e14f0cSMatthew G. Knepley   Output Parameter:
742218e14f0cSMatthew G. Knepley . val - The label value field
742318e14f0cSMatthew G. Knepley 
742418e14f0cSMatthew G. Knepley   Options Database Keys:
742518e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
742618e14f0cSMatthew G. Knepley 
742718e14f0cSMatthew G. Knepley   Level: intermediate
742818e14f0cSMatthew G. Knepley 
742918e14f0cSMatthew G. Knepley .seealso: DMView()
743018e14f0cSMatthew G. Knepley @*/
743118e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
743218e14f0cSMatthew G. Knepley {
743318e14f0cSMatthew G. Knepley   DM             rdm;
743418e14f0cSMatthew G. Knepley   PetscFE        fe;
743518e14f0cSMatthew G. Knepley   PetscScalar   *v;
743618e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
743718e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
743818e14f0cSMatthew G. Knepley 
743918e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
744018e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
744118e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
744218e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
744318e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
744418e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
744518e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
744618e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7447e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
744818e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7449e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
745018e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
745118e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7452effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
745318e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
745418e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
745518e14f0cSMatthew G. Knepley     PetscScalar *lv;
745618e14f0cSMatthew G. Knepley     PetscInt     cval;
745718e14f0cSMatthew G. Knepley 
745818e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
745918e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
746018e14f0cSMatthew G. Knepley     *lv = cval;
746118e14f0cSMatthew G. Knepley   }
746218e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
746318e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
746418e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
746518e14f0cSMatthew G. Knepley }
746618e14f0cSMatthew G. Knepley 
746718e14f0cSMatthew G. Knepley /*@
7468ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7469ca8062c8SMatthew G. Knepley 
747069916449SMatthew G. Knepley   Input Parameter:
747169916449SMatthew G. Knepley . dm - The DMPlex object
7472ca8062c8SMatthew G. Knepley 
747395eb5ee5SVaclav Hapla   Notes:
747495eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
747595eb5ee5SVaclav Hapla 
747695eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7477ca8062c8SMatthew G. Knepley 
7478ca8062c8SMatthew G. Knepley   Level: developer
7479ca8062c8SMatthew G. Knepley 
748095eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7481ca8062c8SMatthew G. Knepley @*/
7482ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7483ca8062c8SMatthew G. Knepley {
7484ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7485ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7486ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
748757beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
748857beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7489ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7490ca8062c8SMatthew G. Knepley 
7491ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7492ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7493412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7494ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7495ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7496ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7497ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7498ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7499ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7500ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7501ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
750242e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
750342e66dfaSMatthew G. Knepley       PetscInt  d;
750442e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
750542e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
750642e66dfaSMatthew G. Knepley       }
7507ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7508ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7509ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7510ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7511ca8062c8SMatthew G. Knepley       }
751242e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
75138ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7514ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
75158ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7516ca8062c8SMatthew G. Knepley         }
7517302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
75188ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7519ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
75208ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7521ca8062c8SMatthew G. Knepley         }
7522302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
75238ccfff9cSToby 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]);
75248ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7525ca8062c8SMatthew G. Knepley       }
752642e66dfaSMatthew G. Knepley     }
752757beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
752857beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7529ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7530ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7531ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7532ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7533ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7534ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
753557beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
753657beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7537ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7538ca8062c8SMatthew G. Knepley       }
7539ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
75408ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7541ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
75428ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7543ca8062c8SMatthew G. Knepley         }
7544302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
75458ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7546ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
75478ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7548ca8062c8SMatthew G. Knepley         }
7549302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
75508ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7551ca8062c8SMatthew G. Knepley       }
7552ca8062c8SMatthew G. Knepley     }
7553ca8062c8SMatthew G. Knepley   }
755457beb4faSStefano Zampini   if (storagecheck) {
7555ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7556ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
75578ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
755857beb4faSStefano Zampini   }
7559ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7560ca8062c8SMatthew G. Knepley }
7561ca8062c8SMatthew G. Knepley 
7562412e9a14SMatthew G. Knepley /*
7563412e9a14SMatthew 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.
7564412e9a14SMatthew G. Knepley */
7565412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7566412e9a14SMatthew G. Knepley {
7567412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7568412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7569412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7570412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7571412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7572412e9a14SMatthew G. Knepley 
7573412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7574412e9a14SMatthew G. Knepley   *unsplit = 0;
7575412e9a14SMatthew G. Knepley   switch (ct) {
7576412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7577412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7578412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7579412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7580412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7581412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7582412e9a14SMatthew G. Knepley       }
7583412e9a14SMatthew G. Knepley       break;
7584412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7585412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7586412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7587412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7588412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7589412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7590412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7591412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7592412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7593412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7594412e9a14SMatthew G. Knepley             PetscInt p;
7595412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7596412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7597412e9a14SMatthew G. Knepley           }
7598412e9a14SMatthew G. Knepley         }
7599412e9a14SMatthew G. Knepley       }
7600412e9a14SMatthew G. Knepley       break;
7601412e9a14SMatthew G. Knepley     default: break;
7602412e9a14SMatthew G. Knepley   }
7603412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7604412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7605412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7606412e9a14SMatthew G. Knepley   }
7607412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7608412e9a14SMatthew G. Knepley }
7609412e9a14SMatthew G. Knepley 
7610ca8062c8SMatthew G. Knepley /*@
7611ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7612ca8062c8SMatthew G. Knepley 
7613ca8062c8SMatthew G. Knepley   Input Parameters:
7614ca8062c8SMatthew G. Knepley + dm - The DMPlex object
761558723a97SMatthew G. Knepley - cellHeight - Normally 0
7616ca8062c8SMatthew G. Knepley 
761795eb5ee5SVaclav Hapla   Notes:
761895eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
761925c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7620ca8062c8SMatthew G. Knepley 
762195eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
762295eb5ee5SVaclav Hapla 
7623ca8062c8SMatthew G. Knepley   Level: developer
7624ca8062c8SMatthew G. Knepley 
762595eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7626ca8062c8SMatthew G. Knepley @*/
762725c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7628ca8062c8SMatthew G. Knepley {
7629412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7630412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7631412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7632ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7633ca8062c8SMatthew G. Knepley 
7634ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7635ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7636412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
763725c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
763858723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7639412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7640412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7641412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
764258723a97SMatthew G. Knepley 
7643412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7644412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7645412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7646412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7647412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7648d4961f80SStefano 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));
7649412e9a14SMatthew G. Knepley     }
765058723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
765158723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
765258723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7653412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
765458723a97SMatthew G. Knepley     }
765558723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7656412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7657412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7658412e9a14SMatthew G. Knepley       PetscInt unsplit;
765942363296SMatthew G. Knepley 
7660412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7661412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
766242363296SMatthew G. Knepley     }
7663d4961f80SStefano 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));
766442363296SMatthew G. Knepley   }
7665ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7666ca8062c8SMatthew G. Knepley }
76679bf0dad6SMatthew G. Knepley 
76689bf0dad6SMatthew G. Knepley /*@
76699bf0dad6SMatthew 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
76709bf0dad6SMatthew G. Knepley 
7671899ea2b8SJacob Faibussowitsch   Not Collective
7672899ea2b8SJacob Faibussowitsch 
76739bf0dad6SMatthew G. Knepley   Input Parameters:
76749bf0dad6SMatthew G. Knepley + dm - The DMPlex object
76759bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
76769bf0dad6SMatthew G. Knepley 
767745da879fSVaclav Hapla   Notes:
767845da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
767945da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
768045da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
768145da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
76829bf0dad6SMatthew G. Knepley 
768395eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
768495eb5ee5SVaclav Hapla 
76859bf0dad6SMatthew G. Knepley   Level: developer
76869bf0dad6SMatthew G. Knepley 
768795eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
76889bf0dad6SMatthew G. Knepley @*/
768925c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
76909bf0dad6SMatthew G. Knepley {
7691ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
76929bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7693899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
76949bf0dad6SMatthew G. Knepley 
76959bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
76969bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7697899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
769845da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
769945da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7700899ea2b8SJacob Faibussowitsch     PetscMPIInt rank;
7701899ea2b8SJacob Faibussowitsch     MPI_Comm    comm;
7702899ea2b8SJacob Faibussowitsch 
7703899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7704ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
770545da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7706899ea2b8SJacob Faibussowitsch   }
7707899ea2b8SJacob Faibussowitsch 
7708c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7709ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
77109bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7711ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
77123554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
77133554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7714412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7715412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7716ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7717412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7718412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
77199bf0dad6SMatthew G. Knepley 
7720ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7721412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7722412e9a14SMatthew G. Knepley       if (unsplit) continue;
77239bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
77249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
77259bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
77269bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
77279bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
77289bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
77299bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
77309bf0dad6SMatthew G. Knepley       }
7731412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
7732d4961f80SStefano 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);
77339bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
7734d4961f80SStefano Zampini         DMPolytopeType fct;
77359bf0dad6SMatthew G. Knepley         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
77369bf0dad6SMatthew G. Knepley 
7737d4961f80SStefano Zampini         ierr = DMPlexGetCellType(dm, cone[f], &fct);CHKERRQ(ierr);
77389bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
77399bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
77409bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
77419bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
77429bf0dad6SMatthew G. Knepley         }
7743d4961f80SStefano 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]);
77449bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7745d4961f80SStefano 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]);
77469bf0dad6SMatthew G. Knepley         }
77479bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7748412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
77499bf0dad6SMatthew G. Knepley       }
7750412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
77519bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
77529bf0dad6SMatthew G. Knepley     }
77533554e41dSMatthew G. Knepley   }
7754552f7358SJed Brown   PetscFunctionReturn(0);
7755552f7358SJed Brown }
77563913d7c8SMatthew G. Knepley 
7757bb6a34a8SMatthew G. Knepley /*@
7758bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7759bb6a34a8SMatthew G. Knepley 
7760bb6a34a8SMatthew G. Knepley   Input Parameter:
7761bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7762bb6a34a8SMatthew G. Knepley 
776395eb5ee5SVaclav Hapla   Notes:
776495eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
776595eb5ee5SVaclav Hapla 
776695eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7767bb6a34a8SMatthew G. Knepley 
7768bb6a34a8SMatthew G. Knepley   Level: developer
7769bb6a34a8SMatthew G. Knepley 
777095eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7771bb6a34a8SMatthew G. Knepley @*/
7772bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7773bb6a34a8SMatthew G. Knepley {
7774bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7775bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7776412e9a14SMatthew G. Knepley   PetscBool      periodic;
777751a74b61SMatthew G. Knepley   PetscInt       dim, depth, dE, d, cStart, cEnd, c;
7778bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7779bb6a34a8SMatthew G. Knepley 
7780bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7781bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
778251a74b61SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
778351a74b61SMatthew G. Knepley   if (dim != dE) PetscFunctionReturn(0);
7784bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7785412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7786bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7787bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7788412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7789412e9a14SMatthew G. Knepley     DMPolytopeType ct;
7790412e9a14SMatthew G. Knepley     PetscInt       unsplit;
7791412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
7792412e9a14SMatthew G. Knepley 
7793412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7794412e9a14SMatthew G. Knepley     switch (ct) {
7795412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7796412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7797412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7798412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
7799412e9a14SMatthew G. Knepley       default: break;
7800412e9a14SMatthew G. Knepley     }
7801412e9a14SMatthew G. Knepley     switch (ct) {
7802412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
7803412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7804412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7805412e9a14SMatthew G. Knepley         continue;
7806412e9a14SMatthew G. Knepley       default: break;
7807412e9a14SMatthew G. Knepley     }
7808412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7809412e9a14SMatthew G. Knepley     if (unsplit) continue;
7810bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7811d4961f80SStefano 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);
7812bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7813412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
7814bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7815d4961f80SStefano 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);
7816bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7817bb6a34a8SMatthew G. Knepley     }
7818bb6a34a8SMatthew G. Knepley   }
7819bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7820bb6a34a8SMatthew G. Knepley }
7821bb6a34a8SMatthew G. Knepley 
782203da9461SVaclav Hapla /*@
7823e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
782403da9461SVaclav Hapla 
782503da9461SVaclav Hapla   Input Parameters:
782603da9461SVaclav Hapla . dm - The DMPlex object
782703da9461SVaclav Hapla 
7828e83a0d2dSVaclav Hapla   Notes:
7829e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
78308918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
783103da9461SVaclav Hapla 
783295eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
783395eb5ee5SVaclav Hapla 
783403da9461SVaclav Hapla   Level: developer
783503da9461SVaclav Hapla 
783695eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
783703da9461SVaclav Hapla @*/
783803da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
783903da9461SVaclav Hapla {
7840f0cfc026SVaclav Hapla   PetscSF         pointSF;
7841f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7842f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
7843f0cfc026SVaclav Hapla   PetscBool       distributed;
784403da9461SVaclav Hapla   PetscErrorCode  ierr;
784503da9461SVaclav Hapla 
784603da9461SVaclav Hapla   PetscFunctionBegin;
784703da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7848f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7849f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7850f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7851f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7852f0cfc026SVaclav Hapla   if (overlap) {
78538918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
78548918e3e2SVaclav Hapla     PetscFunctionReturn(0);
78558918e3e2SVaclav Hapla   }
7856f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7857f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7858f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7859f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
7860f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
786103da9461SVaclav Hapla 
7862ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7863f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7864f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7865f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7866f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
7867f5869d18SMatthew G. Knepley 
7868f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
786903da9461SVaclav Hapla   }
7870ece87651SVaclav Hapla 
7871f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
7872f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7873f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
7874f5869d18SMatthew G. Knepley     const PetscInt *cone;
7875f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
7876f5869d18SMatthew G. Knepley 
7877f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
7878f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
7879f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
7880f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
7881f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
7882f5869d18SMatthew 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]);
7883f5869d18SMatthew G. Knepley       }
7884f5869d18SMatthew G. Knepley     }
7885ece87651SVaclav Hapla   }
788603da9461SVaclav Hapla   PetscFunctionReturn(0);
788703da9461SVaclav Hapla }
788803da9461SVaclav Hapla 
7889068a5610SStefano Zampini typedef struct cell_stats
7890068a5610SStefano Zampini {
7891068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7892068a5610SStefano Zampini   PetscInt  count;
7893068a5610SStefano Zampini } cell_stats_t;
7894068a5610SStefano Zampini 
789525befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7896068a5610SStefano Zampini {
7897068a5610SStefano Zampini   PetscInt i, N = *len;
7898068a5610SStefano Zampini 
7899068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7900068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7901068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7902068a5610SStefano Zampini 
7903068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7904068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7905068a5610SStefano Zampini     B->sum += A->sum;
7906068a5610SStefano Zampini     B->squaresum += A->squaresum;
7907068a5610SStefano Zampini     B->count += A->count;
7908068a5610SStefano Zampini   }
7909068a5610SStefano Zampini }
7910068a5610SStefano Zampini 
7911068a5610SStefano Zampini /*@
791243fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
7913068a5610SStefano Zampini 
79148261a58bSMatthew G. Knepley   Collective on dm
79158261a58bSMatthew G. Knepley 
7916068a5610SStefano Zampini   Input Parameters:
7917068a5610SStefano Zampini + dm        - The DMPlex object
791843fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
791943fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
7920068a5610SStefano Zampini 
792195eb5ee5SVaclav Hapla   Notes:
792295eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
792395eb5ee5SVaclav Hapla 
792495eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7925068a5610SStefano Zampini 
7926068a5610SStefano Zampini   Level: developer
7927068a5610SStefano Zampini 
7928f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
7929068a5610SStefano Zampini @*/
793043fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7931068a5610SStefano Zampini {
7932068a5610SStefano Zampini   DM             dmCoarse;
793343fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
793443fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
793543fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
793643fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
7937412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
793843fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
7939068a5610SStefano Zampini   PetscErrorCode ierr;
7940068a5610SStefano Zampini 
7941068a5610SStefano Zampini   PetscFunctionBegin;
7942068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7943068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7944068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7945068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7946068a5610SStefano Zampini   stats.count = 0;
7947068a5610SStefano Zampini 
7948ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
7949ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
795043fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
795143fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
7952412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
795343fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
7954412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
7955068a5610SStefano Zampini     PetscInt  i;
7956068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7957068a5610SStefano Zampini 
7958068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7959068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
796043fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
7961068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7962068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7963068a5610SStefano Zampini     }
7964068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7965068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7966068a5610SStefano Zampini 
7967068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7968068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7969068a5610SStefano Zampini     stats.sum       += cond;
7970068a5610SStefano Zampini     stats.squaresum += cond2;
7971068a5610SStefano Zampini     stats.count++;
79728261a58bSMatthew G. Knepley     if (output && cond > limit) {
797343fa8764SMatthew G. Knepley       PetscSection coordSection;
797443fa8764SMatthew G. Knepley       Vec          coordsLocal;
797543fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
797643fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
797743fa8764SMatthew G. Knepley 
797843fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
797943fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
798043fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
7981087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
798243fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
798343fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
798443fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
798543fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
798648afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
798743fa8764SMatthew G. Knepley         }
798843fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
798943fa8764SMatthew G. Knepley       }
799043fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
799143fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
799243fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
799343fa8764SMatthew G. Knepley 
799443fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
799543fa8764SMatthew G. Knepley           PetscReal len;
799643fa8764SMatthew G. Knepley 
799743fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
7998087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
799943fa8764SMatthew G. Knepley         }
800043fa8764SMatthew G. Knepley       }
800143fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
800243fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
800343fa8764SMatthew G. Knepley     }
8004068a5610SStefano Zampini   }
80058261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
8006068a5610SStefano Zampini 
8007068a5610SStefano Zampini   if (size > 1) {
8008068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
8009068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
8010068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
8011068a5610SStefano Zampini     MPI_Op        statReduce;
8012068a5610SStefano Zampini 
8013ffc4695bSBarry Smith     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRMPI(ierr);
8014ffc4695bSBarry Smith     ierr = MPI_Type_commit(&statType);CHKERRMPI(ierr);
8015ffc4695bSBarry Smith     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRMPI(ierr);
8016ffc4695bSBarry Smith     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRMPI(ierr);
8017ffc4695bSBarry Smith     ierr = MPI_Op_free(&statReduce);CHKERRMPI(ierr);
8018ffc4695bSBarry Smith     ierr = MPI_Type_free(&statType);CHKERRMPI(ierr);
8019068a5610SStefano Zampini   } else {
8020580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
8021068a5610SStefano Zampini   }
8022068a5610SStefano Zampini   if (!rank) {
8023068a5610SStefano Zampini     count = globalStats.count;
8024068a5610SStefano Zampini     min   = globalStats.min;
8025068a5610SStefano Zampini     max   = globalStats.max;
8026068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
8027068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
8028068a5610SStefano Zampini   }
8029068a5610SStefano Zampini 
8030068a5610SStefano Zampini   if (output) {
8031068a5610SStefano 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);
8032068a5610SStefano Zampini   }
8033068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
8034068a5610SStefano Zampini 
8035068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
8036068a5610SStefano Zampini   if (dmCoarse) {
8037068a5610SStefano Zampini     PetscBool isplex;
8038068a5610SStefano Zampini 
8039068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
8040068a5610SStefano Zampini     if (isplex) {
804143fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
8042068a5610SStefano Zampini     }
8043068a5610SStefano Zampini   }
8044068a5610SStefano Zampini   PetscFunctionReturn(0);
8045068a5610SStefano Zampini }
8046068a5610SStefano Zampini 
8047f108dbd7SJacob Faibussowitsch /*@
8048f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8049f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
8050f108dbd7SJacob Faibussowitsch 
8051f108dbd7SJacob Faibussowitsch   Collective
8052f108dbd7SJacob Faibussowitsch 
8053f108dbd7SJacob Faibussowitsch   Input Parameters:
8054f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
8055f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
8056f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
8057f108dbd7SJacob Faibussowitsch 
8058f108dbd7SJacob Faibussowitsch   Output Parameters:
8059f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
8060f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
8061f108dbd7SJacob Faibussowitsch 
8062f108dbd7SJacob Faibussowitsch   Options Database Keys:
8063f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
8064f108dbd7SJacob Faibussowitsch supported.
8065f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
8066f108dbd7SJacob Faibussowitsch 
8067f108dbd7SJacob Faibussowitsch   Notes:
8068f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
8069f108dbd7SJacob Faibussowitsch 
8070f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
8071f108dbd7SJacob Faibussowitsch 
8072f108dbd7SJacob 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
8073f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8074f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8075f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
8076f108dbd7SJacob Faibussowitsch 
8077f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
8078f108dbd7SJacob Faibussowitsch 
8079f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
8080f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
8081f108dbd7SJacob Faibussowitsch 
8082f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
8083f108dbd7SJacob Faibussowitsch 
8084f108dbd7SJacob Faibussowitsch   Level: intermediate
8085f108dbd7SJacob Faibussowitsch 
8086f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
8087f108dbd7SJacob Faibussowitsch @*/
8088f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
8089f108dbd7SJacob Faibussowitsch {
8090f108dbd7SJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell;
8091f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
8092f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
8093f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
8094f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
8095f108dbd7SJacob Faibussowitsch   IS                      glob;
8096f108dbd7SJacob Faibussowitsch   DMPlexInterpolatedFlag  interpFlag;
8097f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
8098f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
8099f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
8100f108dbd7SJacob Faibussowitsch 
8101f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
8102f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8103f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
8104f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
8105f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
8106f108dbd7SJacob Faibussowitsch   if (nc < 2) {
8107f108dbd7SJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
8108f108dbd7SJacob Faibussowitsch   }
8109f108dbd7SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
8110f108dbd7SJacob Faibussowitsch   if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
8111f108dbd7SJacob Faibussowitsch     PetscMPIInt  rank;
8112f108dbd7SJacob Faibussowitsch 
8113ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
8114f108dbd7SJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
8115f108dbd7SJacob Faibussowitsch   }
8116f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8117f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
8118f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
8119f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
8120f108dbd7SJacob Faibussowitsch   } else {
8121f108dbd7SJacob Faibussowitsch     *OrthQualLabel = NULL;
8122f108dbd7SJacob Faibussowitsch   }
8123f108dbd7SJacob Faibussowitsch 
8124f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8125f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8126f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
8127f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
8128f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
8129f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
8130f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
8131f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
8132f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
8133f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
8134f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
8135f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
8136f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
8137f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8138f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8139f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
8140f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
8141f108dbd7SJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cell++) {
8142f108dbd7SJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, nf, adjSize = PETSC_DETERMINE, ix = cell-cStart;
8143f108dbd7SJacob Faibussowitsch     const PetscInt     *cone;
8144f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
8145f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
8146f108dbd7SJacob Faibussowitsch     PetscReal          minvalc = 1.0, minvalf = 1.0, OQ;
8147f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
8148f108dbd7SJacob Faibussowitsch 
8149f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
8150f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
8151f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
8152f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
8153f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetConeSize(dm, cell, &nf);CHKERRQ(ierr);
8154f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
8155f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8156f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
8157f108dbd7SJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneigh++) {
8158f108dbd7SJacob Faibussowitsch       PetscInt         numcovpts, i, neigh = adj[cellneigh];
8159f108dbd7SJacob Faibussowitsch       const PetscInt   *covpts;
8160f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
8161f108dbd7SJacob Faibussowitsch       PetscReal        *ci, *fi, *Ai;
8162f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
8163f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
8164f108dbd7SJacob Faibussowitsch 
8165f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
8166f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
8167f108dbd7SJacob Faibussowitsch       ierr = PetscMalloc3(nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
8168f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
8169f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
8170f108dbd7SJacob Faibussowitsch       ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8171f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
8172f108dbd7SJacob Faibussowitsch       ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8173f108dbd7SJacob Faibussowitsch 
8174f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
8175f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8176f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8177f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
8178f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
8179f108dbd7SJacob Faibussowitsch         normci += PetscPowScalar(ci[i], 2);
8180f108dbd7SJacob Faibussowitsch         normfi += PetscPowScalar(fi[i], 2);
8181f108dbd7SJacob Faibussowitsch         normai += PetscPowScalar(Ai[i], 2);
8182f108dbd7SJacob Faibussowitsch       }
8183f108dbd7SJacob Faibussowitsch       normci = PetscSqrtScalar(normci);
8184f108dbd7SJacob Faibussowitsch       normfi = PetscSqrtScalar(normfi);
8185f108dbd7SJacob Faibussowitsch       normai = PetscSqrtScalar(normai);
8186f108dbd7SJacob Faibussowitsch 
8187f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
8188f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8189f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
8190f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
8191f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
8192f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
8193f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8194f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8195f108dbd7SJacob Faibussowitsch       }
8196f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8197f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
8198f108dbd7SJacob Faibussowitsch       }
8199f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8200f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
8201f108dbd7SJacob Faibussowitsch       }
8202f108dbd7SJacob Faibussowitsch       cellneighiter++;
8203f108dbd7SJacob Faibussowitsch       ierr = PetscFree3(ci, fi, Ai);CHKERRQ(ierr);
8204f108dbd7SJacob Faibussowitsch     }
8205f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
8206f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
8207f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
8208f108dbd7SJacob Faibussowitsch     OQ = PetscMin(minvalf, minvalc);
8209f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
8210f108dbd7SJacob Faibussowitsch       if (OQ <= atol) {
8211f108dbd7SJacob Faibussowitsch         ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);
8212f108dbd7SJacob Faibussowitsch       }
8213f108dbd7SJacob Faibussowitsch     }
8214f108dbd7SJacob Faibussowitsch     ierr = VecSetValuesLocal(*OrthQual, 1, (const PetscInt *) &ix, (const PetscScalar *) &OQ, INSERT_VALUES);CHKERRQ(ierr);
8215f108dbd7SJacob Faibussowitsch   }
8216f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
8217f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
8218f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8219f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8220f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
8221f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8222f108dbd7SJacob Faibussowitsch     if (vwr) {
8223f108dbd7SJacob Faibussowitsch       ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);
8224f108dbd7SJacob Faibussowitsch     }
8225f108dbd7SJacob Faibussowitsch   }
8226f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
8227f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
8228f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
8229f108dbd7SJacob Faibussowitsch }
8230f108dbd7SJacob Faibussowitsch 
82311eb70e55SToby Isaac /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
82321eb70e55SToby Isaac  * interpolator construction */
82331eb70e55SToby Isaac static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
82341eb70e55SToby Isaac {
82351eb70e55SToby Isaac   PetscSection   section, newSection, gsection;
82361eb70e55SToby Isaac   PetscSF        sf;
82371eb70e55SToby Isaac   PetscBool      hasConstraints, ghasConstraints;
82381eb70e55SToby Isaac   PetscErrorCode ierr;
82391eb70e55SToby Isaac 
82401eb70e55SToby Isaac   PetscFunctionBegin;
82411eb70e55SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82421eb70e55SToby Isaac   PetscValidPointer(odm,2);
82431eb70e55SToby Isaac   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
82441eb70e55SToby Isaac   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
82451eb70e55SToby Isaac   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
82461eb70e55SToby Isaac   if (!ghasConstraints) {
82471eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
82481eb70e55SToby Isaac     *odm = dm;
82491eb70e55SToby Isaac     PetscFunctionReturn(0);
82501eb70e55SToby Isaac   }
82511eb70e55SToby Isaac   ierr = DMClone(dm, odm);CHKERRQ(ierr);
82521eb70e55SToby Isaac   ierr = DMCopyFields(dm, *odm);CHKERRQ(ierr);
82531eb70e55SToby Isaac   ierr = DMGetLocalSection(*odm, &newSection);CHKERRQ(ierr);
82541eb70e55SToby Isaac   ierr = DMGetPointSF(*odm, &sf);CHKERRQ(ierr);
82551eb70e55SToby Isaac   ierr = PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
82561eb70e55SToby Isaac   ierr = DMSetGlobalSection(*odm, gsection);CHKERRQ(ierr);
82571eb70e55SToby Isaac   ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
82581eb70e55SToby Isaac   PetscFunctionReturn(0);
82591eb70e55SToby Isaac }
82601eb70e55SToby Isaac 
82611eb70e55SToby Isaac static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
82621eb70e55SToby Isaac {
82631eb70e55SToby Isaac   DM             dmco, dmfo;
82641eb70e55SToby Isaac   Mat            interpo;
82651eb70e55SToby Isaac   Vec            rscale;
82661eb70e55SToby Isaac   Vec            cglobalo, clocal;
82671eb70e55SToby Isaac   Vec            fglobal, fglobalo, flocal;
82681eb70e55SToby Isaac   PetscBool      regular;
82691eb70e55SToby Isaac   PetscErrorCode ierr;
82701eb70e55SToby Isaac 
82711eb70e55SToby Isaac   PetscFunctionBegin;
82721eb70e55SToby Isaac   ierr = DMGetFullDM(dmc, &dmco);CHKERRQ(ierr);
82731eb70e55SToby Isaac   ierr = DMGetFullDM(dmf, &dmfo);CHKERRQ(ierr);
82741eb70e55SToby Isaac   ierr = DMSetCoarseDM(dmfo, dmco);CHKERRQ(ierr);
82751eb70e55SToby Isaac   ierr = DMPlexGetRegularRefinement(dmf, &regular);CHKERRQ(ierr);
82761eb70e55SToby Isaac   ierr = DMPlexSetRegularRefinement(dmfo, regular);CHKERRQ(ierr);
82771eb70e55SToby Isaac   ierr = DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);CHKERRQ(ierr);
82781eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmco, &cglobalo);CHKERRQ(ierr);
82791eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmc, &clocal);CHKERRQ(ierr);
82801eb70e55SToby Isaac   ierr = VecSet(cglobalo, 0.);CHKERRQ(ierr);
82811eb70e55SToby Isaac   ierr = VecSet(clocal, 0.);CHKERRQ(ierr);
82821eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmf, &fglobal);CHKERRQ(ierr);
82831eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmfo, &fglobalo);CHKERRQ(ierr);
82841eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmf, &flocal);CHKERRQ(ierr);
82851eb70e55SToby Isaac   ierr = VecSet(fglobal, 0.);CHKERRQ(ierr);
82861eb70e55SToby Isaac   ierr = VecSet(fglobalo, 0.);CHKERRQ(ierr);
82871eb70e55SToby Isaac   ierr = VecSet(flocal, 0.);CHKERRQ(ierr);
82881eb70e55SToby Isaac   ierr = DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);CHKERRQ(ierr);
82891eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
82901eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
82911eb70e55SToby Isaac   ierr = MatMult(interpo, cglobalo, fglobalo);CHKERRQ(ierr);
82921eb70e55SToby Isaac   ierr = DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
82931eb70e55SToby Isaac   ierr = DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
82941eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
82951eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
82961eb70e55SToby Isaac   *shift = fglobal;
82971eb70e55SToby Isaac   ierr = VecDestroy(&flocal);CHKERRQ(ierr);
82981eb70e55SToby Isaac   ierr = VecDestroy(&fglobalo);CHKERRQ(ierr);
82991eb70e55SToby Isaac   ierr = VecDestroy(&clocal);CHKERRQ(ierr);
83001eb70e55SToby Isaac   ierr = VecDestroy(&cglobalo);CHKERRQ(ierr);
83011eb70e55SToby Isaac   ierr = VecDestroy(&rscale);CHKERRQ(ierr);
83021eb70e55SToby Isaac   ierr = MatDestroy(&interpo);CHKERRQ(ierr);
83031eb70e55SToby Isaac   ierr = DMDestroy(&dmfo);CHKERRQ(ierr);
83041eb70e55SToby Isaac   ierr = DMDestroy(&dmco);CHKERRQ(ierr);
83051eb70e55SToby Isaac   PetscFunctionReturn(0);
83061eb70e55SToby Isaac }
83071eb70e55SToby Isaac 
83081eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
83091eb70e55SToby Isaac {
83101eb70e55SToby Isaac   PetscObject    shifto;
83111eb70e55SToby Isaac   Vec            shift;
83121eb70e55SToby Isaac 
83131eb70e55SToby Isaac   PetscErrorCode ierr;
83141eb70e55SToby Isaac 
83151eb70e55SToby Isaac   PetscFunctionBegin;
83161eb70e55SToby Isaac   if (!interp) {
83171eb70e55SToby Isaac     Vec rscale;
83181eb70e55SToby Isaac 
83191eb70e55SToby Isaac     ierr = DMCreateInterpolation(coarse, fine, &interp, &rscale);CHKERRQ(ierr);
83201eb70e55SToby Isaac     ierr = VecDestroy(&rscale);CHKERRQ(ierr);
83211eb70e55SToby Isaac   } else {
83221eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)interp);CHKERRQ(ierr);
83231eb70e55SToby Isaac   }
83241eb70e55SToby Isaac   ierr = PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);CHKERRQ(ierr);
83251eb70e55SToby Isaac   if (!shifto) {
83261eb70e55SToby Isaac     ierr = DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);CHKERRQ(ierr);
83271eb70e55SToby Isaac     ierr = PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);CHKERRQ(ierr);
83281eb70e55SToby Isaac     shifto = (PetscObject) shift;
83291eb70e55SToby Isaac     ierr = VecDestroy(&shift);CHKERRQ(ierr);
83301eb70e55SToby Isaac   }
83311eb70e55SToby Isaac   shift = (Vec) shifto;
83321eb70e55SToby Isaac   ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
83331eb70e55SToby Isaac   ierr = VecAXPY(fineSol, 1.0, shift);CHKERRQ(ierr);
83341eb70e55SToby Isaac   ierr = MatDestroy(&interp);CHKERRQ(ierr);
83351eb70e55SToby Isaac   PetscFunctionReturn(0);
83361eb70e55SToby Isaac }
83371eb70e55SToby Isaac 
8338bceba477SMatthew G. Knepley /* Pointwise interpolation
8339bceba477SMatthew G. Knepley      Just code FEM for now
8340bceba477SMatthew G. Knepley      u^f = I u^c
83414ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
83424ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
83434ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
8344bceba477SMatthew G. Knepley */
8345bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8346bceba477SMatthew G. Knepley {
8347bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
8348bceba477SMatthew G. Knepley   PetscInt       m, n;
8349a063dac3SMatthew G. Knepley   void          *ctx;
835068132eb9SMatthew G. Knepley   DM             cdm;
8351cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
8352bceba477SMatthew G. Knepley   PetscErrorCode ierr;
8353bceba477SMatthew G. Knepley 
8354bceba477SMatthew G. Knepley   PetscFunctionBegin;
8355e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8356bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8357e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8358bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
835968132eb9SMatthew G. Knepley 
8360fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
8361bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
8362bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8363fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
8364a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
836568132eb9SMatthew G. Knepley 
8366a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
836768132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8368cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
836968132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
837068132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
83714db47ee9SStefano Zampini   if (scaling) {
83725d1c2e58SMatthew G. Knepley     /* Use naive scaling */
83735d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
83744db47ee9SStefano Zampini   }
8375a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
8376a063dac3SMatthew G. Knepley }
8377bceba477SMatthew G. Knepley 
83786dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8379a063dac3SMatthew G. Knepley {
838090748bafSMatthew G. Knepley   PetscErrorCode ierr;
83816dbf9973SLawrence Mitchell   VecScatter     ctx;
838290748bafSMatthew G. Knepley 
8383a063dac3SMatthew G. Knepley   PetscFunctionBegin;
83846dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
83856dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
83866dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
8387bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
8388bceba477SMatthew G. Knepley }
8389bceba477SMatthew G. Knepley 
83903e9753d6SMatthew G. Knepley static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
83913e9753d6SMatthew G. Knepley                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
83923e9753d6SMatthew G. Knepley                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
83933e9753d6SMatthew G. Knepley                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
83943e9753d6SMatthew G. Knepley {
83953e9753d6SMatthew G. Knepley   g0[0] = 1.0;
83963e9753d6SMatthew G. Knepley }
83973e9753d6SMatthew G. Knepley 
8398bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8399bd041c0cSMatthew G. Knepley {
8400bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
8401bd041c0cSMatthew G. Knepley   PetscInt       m, n;
8402bd041c0cSMatthew G. Knepley   void          *ctx;
8403bd041c0cSMatthew G. Knepley   DM             cdm;
8404bd041c0cSMatthew G. Knepley   PetscBool      regular;
8405bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
8406bd041c0cSMatthew G. Knepley 
8407bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
84083e9753d6SMatthew G. Knepley   if (dmFine == dmCoarse) {
84093e9753d6SMatthew G. Knepley     DM       dmc;
84103e9753d6SMatthew G. Knepley     PetscDS  ds;
84113e9753d6SMatthew G. Knepley     Vec      u;
84123e9753d6SMatthew G. Knepley     IS       cellIS;
84136528b96dSMatthew G. Knepley     PetscHashFormKey key;
84143e9753d6SMatthew G. Knepley     PetscInt depth;
84153e9753d6SMatthew G. Knepley 
84163e9753d6SMatthew G. Knepley     ierr = DMClone(dmFine, &dmc);CHKERRQ(ierr);
84173e9753d6SMatthew G. Knepley     ierr = DMCopyDisc(dmFine, dmc);CHKERRQ(ierr);
84183e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
84193e9753d6SMatthew G. Knepley     ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
84203e9753d6SMatthew G. Knepley     ierr = DMCreateMatrix(dmc, mass);CHKERRQ(ierr);
84213e9753d6SMatthew G. Knepley     ierr = DMGetGlobalVector(dmc, &u);CHKERRQ(ierr);
84223e9753d6SMatthew G. Knepley     ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
84233e9753d6SMatthew G. Knepley     ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
84243e9753d6SMatthew G. Knepley     ierr = MatZeroEntries(*mass);CHKERRQ(ierr);
84256528b96dSMatthew G. Knepley     key.label = NULL;
84266528b96dSMatthew G. Knepley     key.value = 0;
84276528b96dSMatthew G. Knepley     key.field = 0;
84286528b96dSMatthew G. Knepley     ierr = DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);CHKERRQ(ierr);
84293e9753d6SMatthew G. Knepley     ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
84303e9753d6SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmc, &u);CHKERRQ(ierr);
84313e9753d6SMatthew G. Knepley     ierr = DMDestroy(&dmc);CHKERRQ(ierr);
84323e9753d6SMatthew G. Knepley   } else {
8433e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8434bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8435e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8436bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
8437bd041c0cSMatthew G. Knepley 
8438bd041c0cSMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
8439bd041c0cSMatthew G. Knepley     ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8440bd041c0cSMatthew G. Knepley     ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
8441bd041c0cSMatthew G. Knepley     ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
8442bd041c0cSMatthew G. Knepley 
8443bd041c0cSMatthew G. Knepley     ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
8444bd041c0cSMatthew G. Knepley     ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8445bd041c0cSMatthew G. Knepley     if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8446bd041c0cSMatthew G. Knepley     else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
84473e9753d6SMatthew G. Knepley   }
8448bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
8449bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
8450bd041c0cSMatthew G. Knepley }
8451bd041c0cSMatthew G. Knepley 
84520aef6b92SMatthew G. Knepley /*@
84530aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
84540aef6b92SMatthew G. Knepley 
84550aef6b92SMatthew G. Knepley   Input Parameter:
84560aef6b92SMatthew G. Knepley . dm - The DMPlex object
84570aef6b92SMatthew G. Knepley 
84580aef6b92SMatthew G. Knepley   Output Parameter:
84590aef6b92SMatthew G. Knepley . regular - The flag
84600aef6b92SMatthew G. Knepley 
84610aef6b92SMatthew G. Knepley   Level: intermediate
84620aef6b92SMatthew G. Knepley 
84630aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
84640aef6b92SMatthew G. Knepley @*/
84650aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
84660aef6b92SMatthew G. Knepley {
84670aef6b92SMatthew G. Knepley   PetscFunctionBegin;
84680aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84690aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
84700aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
84710aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
84720aef6b92SMatthew G. Knepley }
84730aef6b92SMatthew G. Knepley 
84740aef6b92SMatthew G. Knepley /*@
84750aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
84760aef6b92SMatthew G. Knepley 
84770aef6b92SMatthew G. Knepley   Input Parameters:
84780aef6b92SMatthew G. Knepley + dm - The DMPlex object
84790aef6b92SMatthew G. Knepley - regular - The flag
84800aef6b92SMatthew G. Knepley 
84810aef6b92SMatthew G. Knepley   Level: intermediate
84820aef6b92SMatthew G. Knepley 
84830aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
84840aef6b92SMatthew G. Knepley @*/
84850aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
84860aef6b92SMatthew G. Knepley {
84870aef6b92SMatthew G. Knepley   PetscFunctionBegin;
84880aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84890aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
84900aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
84910aef6b92SMatthew G. Knepley }
84920aef6b92SMatthew G. Knepley 
8493412e9a14SMatthew G. Knepley /*@
8494412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
8495412e9a14SMatthew G. Knepley 
8496412e9a14SMatthew G. Knepley   Input Parameter:
8497412e9a14SMatthew G. Knepley . dm - The DMPlex object
8498412e9a14SMatthew G. Knepley 
8499412e9a14SMatthew G. Knepley   Output Parameter:
8500412e9a14SMatthew G. Knepley . cr - The strategy number
8501412e9a14SMatthew G. Knepley 
8502412e9a14SMatthew G. Knepley   Level: intermediate
8503412e9a14SMatthew G. Knepley 
8504412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8505412e9a14SMatthew G. Knepley @*/
8506412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8507412e9a14SMatthew G. Knepley {
8508412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8509412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8510412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
8511412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8512412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8513412e9a14SMatthew G. Knepley }
8514412e9a14SMatthew G. Knepley 
8515412e9a14SMatthew G. Knepley /*@
8516412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
8517412e9a14SMatthew G. Knepley 
8518412e9a14SMatthew G. Knepley   Input Parameters:
8519412e9a14SMatthew G. Knepley + dm - The DMPlex object
8520412e9a14SMatthew G. Knepley - cr - The strategy number
8521412e9a14SMatthew G. Knepley 
8522412e9a14SMatthew G. Knepley   Level: intermediate
8523412e9a14SMatthew G. Knepley 
8524412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8525412e9a14SMatthew G. Knepley @*/
8526412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8527412e9a14SMatthew G. Knepley {
8528412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8529412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8530412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
8531412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8532412e9a14SMatthew G. Knepley }
8533412e9a14SMatthew G. Knepley 
8534f7c74593SToby Isaac /* anchors */
8535a68b90caSToby Isaac /*@
8536f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
8537f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
8538a68b90caSToby Isaac 
8539e228b242SToby Isaac   not collective
8540a68b90caSToby Isaac 
8541a68b90caSToby Isaac   Input Parameters:
8542a68b90caSToby Isaac . dm - The DMPlex object
8543a68b90caSToby Isaac 
8544a68b90caSToby Isaac   Output Parameters:
8545a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
8546a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
8547a68b90caSToby Isaac 
8548a68b90caSToby Isaac 
8549a68b90caSToby Isaac   Level: intermediate
8550a68b90caSToby Isaac 
8551f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8552a68b90caSToby Isaac @*/
8553a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8554a68b90caSToby Isaac {
8555a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
855641e6d900SToby Isaac   PetscErrorCode ierr;
8557a68b90caSToby Isaac 
8558a68b90caSToby Isaac   PetscFunctionBegin;
8559a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
856041e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
8561a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
8562a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
8563a68b90caSToby Isaac   PetscFunctionReturn(0);
8564a68b90caSToby Isaac }
8565a68b90caSToby Isaac 
8566a68b90caSToby Isaac /*@
8567f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
8568f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
8569a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
8570a68b90caSToby Isaac 
8571a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
8572f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
8573a68b90caSToby Isaac 
8574e228b242SToby Isaac   collective on dm
8575a68b90caSToby Isaac 
8576a68b90caSToby Isaac   Input Parameters:
8577a68b90caSToby Isaac + dm - The DMPlex object
8578e228b242SToby 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).
8579e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
8580a68b90caSToby Isaac 
8581a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
8582a68b90caSToby Isaac 
8583a68b90caSToby Isaac   Level: intermediate
8584a68b90caSToby Isaac 
8585f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8586a68b90caSToby Isaac @*/
8587a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8588a68b90caSToby Isaac {
8589a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8590e228b242SToby Isaac   PetscMPIInt    result;
8591a68b90caSToby Isaac   PetscErrorCode ierr;
8592a68b90caSToby Isaac 
8593a68b90caSToby Isaac   PetscFunctionBegin;
8594a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8595e228b242SToby Isaac   if (anchorSection) {
8596e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
8597ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRMPI(ierr);
8598f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8599e228b242SToby Isaac   }
8600e228b242SToby Isaac   if (anchorIS) {
8601e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
8602ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRMPI(ierr);
8603f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8604e228b242SToby Isaac   }
8605a68b90caSToby Isaac 
8606a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
8607a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
8608a68b90caSToby Isaac   plex->anchorSection = anchorSection;
8609a68b90caSToby Isaac 
8610a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
8611a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
8612a68b90caSToby Isaac   plex->anchorIS = anchorIS;
8613a68b90caSToby Isaac 
8614cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8615a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
8616a68b90caSToby Isaac     const PetscInt *anchors;
8617a68b90caSToby Isaac 
8618a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
8619a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
8620a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
8621a68b90caSToby Isaac     for (a = 0; a < size; a++) {
8622a68b90caSToby Isaac       PetscInt p;
8623a68b90caSToby Isaac 
8624a68b90caSToby Isaac       p = anchors[a];
8625a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
8626a68b90caSToby Isaac         PetscInt dof;
8627a68b90caSToby Isaac 
8628a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8629a68b90caSToby Isaac         if (dof) {
8630a68b90caSToby Isaac           PetscErrorCode ierr2;
8631a68b90caSToby Isaac 
8632a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
86338ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8634a68b90caSToby Isaac         }
8635a68b90caSToby Isaac       }
8636a68b90caSToby Isaac     }
8637a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
8638a68b90caSToby Isaac   }
8639f7c74593SToby Isaac   /* reset the generic constraints */
8640f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
8641a68b90caSToby Isaac   PetscFunctionReturn(0);
8642a68b90caSToby Isaac }
8643a68b90caSToby Isaac 
8644f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8645a68b90caSToby Isaac {
8646f7c74593SToby Isaac   PetscSection anchorSection;
86476995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
8648a68b90caSToby Isaac   PetscErrorCode ierr;
8649a68b90caSToby Isaac 
8650a68b90caSToby Isaac   PetscFunctionBegin;
8651a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8652a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
8653e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
8654a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
86556995de1eSToby Isaac   if (numFields) {
8656719ab38cSToby Isaac     PetscInt f;
8657a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
8658719ab38cSToby Isaac 
8659719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
8660719ab38cSToby Isaac       PetscInt numComp;
8661719ab38cSToby Isaac 
8662719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
8663719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
8664719ab38cSToby Isaac     }
86656995de1eSToby Isaac   }
8666a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
86676995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
86686995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
86696995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
86706995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
8671a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
8672a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
8673a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8674a68b90caSToby Isaac     if (dof) {
8675a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
8676a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
8677a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
8678a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
8679a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
8680a68b90caSToby Isaac       }
8681a68b90caSToby Isaac     }
8682a68b90caSToby Isaac   }
8683a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
8684a68b90caSToby Isaac   PetscFunctionReturn(0);
8685a68b90caSToby Isaac }
8686a68b90caSToby Isaac 
8687f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8688a68b90caSToby Isaac {
8689f7c74593SToby Isaac   PetscSection   aSec;
86900ac89760SToby Isaac   PetscInt       pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
86910ac89760SToby Isaac   const PetscInt *anchors;
86920ac89760SToby Isaac   PetscInt       numFields, f;
869366ad2231SToby Isaac   IS             aIS;
86940ac89760SToby Isaac   PetscErrorCode ierr;
8695e19f7ee6SMark Adams   MatType        mtype;
8696e19f7ee6SMark Adams   PetscBool      iscuda,iskokkos;
86970ac89760SToby Isaac 
86980ac89760SToby Isaac   PetscFunctionBegin;
86990ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87000ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
87010ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
87020ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
87030ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
8704e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);CHKERRQ(ierr);
8705e19f7ee6SMark Adams   if (!iscuda) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);CHKERRQ(ierr); }
8706e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);CHKERRQ(ierr);
8707e19f7ee6SMark Adams   if (!iskokkos) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);CHKERRQ(ierr); }
8708e19f7ee6SMark Adams   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8709e19f7ee6SMark Adams   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8710e19f7ee6SMark Adams   else mtype = MATSEQAIJ;
8711e19f7ee6SMark Adams   ierr = MatSetType(*cMat,mtype);CHKERRQ(ierr);
8712a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
871366ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
87146995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
87156995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
87160ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
87170ac89760SToby Isaac   i[0] = 0;
87180ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
87190ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
8720f19733c5SToby Isaac     PetscInt rDof, rOff, r;
8721f19733c5SToby Isaac 
8722f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
8723f19733c5SToby Isaac     if (!rDof) continue;
8724f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
87250ac89760SToby Isaac     if (numFields) {
87260ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
87270ac89760SToby Isaac         annz = 0;
8728f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
8729f19733c5SToby Isaac           a = anchors[rOff + r];
87300ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
87310ac89760SToby Isaac           annz += aDof;
87320ac89760SToby Isaac         }
87330ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
87340ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
87350ac89760SToby Isaac         for (q = 0; q < dof; q++) {
87360ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
87370ac89760SToby Isaac         }
87380ac89760SToby Isaac       }
87390ac89760SToby Isaac     }
87400ac89760SToby Isaac     else {
87410ac89760SToby Isaac       annz = 0;
8742326b8f31SMatthew G. Knepley       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
87430ac89760SToby Isaac       for (q = 0; q < dof; q++) {
87440ac89760SToby Isaac         a = anchors[off + q];
87450ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
87460ac89760SToby Isaac         annz += aDof;
87470ac89760SToby Isaac       }
87480ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
87490ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
87500ac89760SToby Isaac       for (q = 0; q < dof; q++) {
87510ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
87520ac89760SToby Isaac       }
87530ac89760SToby Isaac     }
87540ac89760SToby Isaac   }
87550ac89760SToby Isaac   nnz = i[m];
87560ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
87570ac89760SToby Isaac   offset = 0;
87580ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
87590ac89760SToby Isaac     if (numFields) {
87600ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
87610ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
87620ac89760SToby Isaac         for (q = 0; q < dof; q++) {
87630ac89760SToby Isaac           PetscInt rDof, rOff, r;
87640ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
87650ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
87660ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
87670ac89760SToby Isaac             PetscInt s;
87680ac89760SToby Isaac 
87690ac89760SToby Isaac             a = anchors[rOff + r];
87700ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
87710ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
87720ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
87730ac89760SToby Isaac               j[offset++] = aOff + s;
87740ac89760SToby Isaac             }
87750ac89760SToby Isaac           }
87760ac89760SToby Isaac         }
87770ac89760SToby Isaac       }
87780ac89760SToby Isaac     }
87790ac89760SToby Isaac     else {
87800ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
87810ac89760SToby Isaac       for (q = 0; q < dof; q++) {
87820ac89760SToby Isaac         PetscInt rDof, rOff, r;
87830ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
87840ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
87850ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
87860ac89760SToby Isaac           PetscInt s;
87870ac89760SToby Isaac 
87880ac89760SToby Isaac           a = anchors[rOff + r];
87890ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
87900ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
87910ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
87920ac89760SToby Isaac             j[offset++] = aOff + s;
87930ac89760SToby Isaac           }
87940ac89760SToby Isaac         }
87950ac89760SToby Isaac       }
87960ac89760SToby Isaac     }
87970ac89760SToby Isaac   }
87980ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
879925570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
880025570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
880166ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
88020ac89760SToby Isaac   PetscFunctionReturn(0);
88030ac89760SToby Isaac }
88040ac89760SToby Isaac 
880566ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
880666ad2231SToby Isaac {
8807f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8808f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
880966ad2231SToby Isaac   Mat            cMat;
881066ad2231SToby Isaac   PetscErrorCode ierr;
881166ad2231SToby Isaac 
881266ad2231SToby Isaac   PetscFunctionBegin;
881366ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8814a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
881566ad2231SToby Isaac   if (anchorSection) {
881644a7f3ddSMatthew G. Knepley     PetscInt Nf;
8817e228b242SToby Isaac 
881892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
8819f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
8820f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
882144a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
882244a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
882366ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
882466ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
882566ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
882666ad2231SToby Isaac   }
882766ad2231SToby Isaac   PetscFunctionReturn(0);
882866ad2231SToby Isaac }
8829a93c429eSMatthew G. Knepley 
8830a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8831a93c429eSMatthew G. Knepley {
8832a93c429eSMatthew G. Knepley   IS             subis;
8833a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
8834a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
8835a93c429eSMatthew G. Knepley 
8836a93c429eSMatthew G. Knepley   PetscFunctionBegin;
883792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
8838a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8839a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8840a93c429eSMatthew G. Knepley   /* Create subdomain */
8841a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
8842a93c429eSMatthew G. Knepley   /* Create submodel */
884397d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
8844a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
884592fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
8846a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
8847e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
8848a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
8849a93c429eSMatthew G. Knepley   if (is) {
8850a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
8851a93c429eSMatthew G. Knepley     IS              spIS;
8852a93c429eSMatthew G. Knepley     const PetscInt *spmap;
8853a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
8854a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8855a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
8856a93c429eSMatthew G. Knepley 
885797d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
8858a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
8859a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
88606f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
88616f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
8862a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
8863a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8864a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
8865a93c429eSMatthew G. Knepley 
8866a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
8867a93c429eSMatthew G. Knepley       if (gdof > 0) {
8868a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8869a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
8870a93c429eSMatthew G. Knepley 
8871a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
8872a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
8873a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
8874a93c429eSMatthew G. Knepley         }
8875a93c429eSMatthew G. Knepley         subSize += pSubSize;
8876a93c429eSMatthew G. Knepley         if (pSubSize) {
8877a93c429eSMatthew G. Knepley           if (bs < 0) {
8878a93c429eSMatthew G. Knepley             bs = pSubSize;
8879a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
8880a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
8881a93c429eSMatthew G. Knepley             bs = 1;
8882a93c429eSMatthew G. Knepley           }
8883a93c429eSMatthew G. Knepley         }
8884a93c429eSMatthew G. Knepley       }
8885a93c429eSMatthew G. Knepley     }
8886a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
8887a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8888a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
8889a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8890a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
8891a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
8892a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8893a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
8894a93c429eSMatthew G. Knepley 
8895a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
8896a93c429eSMatthew G. Knepley       if (gdof > 0) {
8897a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
8898a93c429eSMatthew G. Knepley 
8899a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
8900a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8901a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
8902a93c429eSMatthew G. Knepley 
8903a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
8904a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
8905a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
8906a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
8907a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
8908a93c429eSMatthew G. Knepley           }
8909a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
8910a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
8911a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8912a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
8913a93c429eSMatthew G. Knepley           }
8914a93c429eSMatthew G. Knepley         }
8915a93c429eSMatthew G. Knepley       }
8916a93c429eSMatthew G. Knepley     }
8917a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
8918a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
8919a93c429eSMatthew G. Knepley     if (bs > 1) {
8920a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
8921a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
8922a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
8923a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
8924a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8925a93c429eSMatthew G. Knepley         }
8926a93c429eSMatthew G. Knepley       }
8927a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
8928a93c429eSMatthew G. Knepley     }
8929a93c429eSMatthew G. Knepley     /* Attach nullspace */
8930a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
8931a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8932a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
8933a93c429eSMatthew G. Knepley     }
8934a93c429eSMatthew G. Knepley     if (f < Nf) {
8935a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
89368cda7954SMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);CHKERRQ(ierr);
89376823f3c5SBlaise Bourdin 
8938a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
8939a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
8940a93c429eSMatthew G. Knepley     }
8941a93c429eSMatthew G. Knepley   }
8942a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
8943a93c429eSMatthew G. Knepley }
8944c0f0dcc3SMatthew G. Knepley 
8945c0f0dcc3SMatthew G. Knepley /*@
8946c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
8947c0f0dcc3SMatthew G. Knepley 
8948c0f0dcc3SMatthew G. Knepley   Input Parameter:
8949c0f0dcc3SMatthew G. Knepley - dm - The DM
8950c0f0dcc3SMatthew G. Knepley 
8951c0f0dcc3SMatthew G. Knepley   Level: developer
8952c0f0dcc3SMatthew G. Knepley 
8953c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8954c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
8955c0f0dcc3SMatthew G. Knepley 
8956c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
8957c0f0dcc3SMatthew G. Knepley @*/
8958c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8959c0f0dcc3SMatthew G. Knepley {
8960e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
8961c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
8962c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
8963c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
8964c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
8965c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
8966c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
8967c0f0dcc3SMatthew G. Knepley   const char        *name;
8968c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
8969e5ed2c37SJose E. Roman #endif
8970c0f0dcc3SMatthew G. Knepley 
8971c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8972c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8973c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
8974c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
8975c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8976c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
8977c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
8978c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
8979c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
8980c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
8981c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
8982c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
8983c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
8984fae64647SBarry 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);
8985c0f0dcc3SMatthew G. Knepley #else
8986c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8987c0f0dcc3SMatthew G. Knepley #endif
8988c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8989c0f0dcc3SMatthew G. Knepley }
8990