xref: /petsc/src/dm/impls/plex/plex.c (revision 806c51de1705b43762b5c7e08cbee43a2ba7fba4)
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 /*@
169318fe57SMatthew G. Knepley   DMPlexIsSimplex - Is the first cell in this mesh a simplex?
179318fe57SMatthew G. Knepley 
189318fe57SMatthew G. Knepley   Input Parameter:
199318fe57SMatthew G. Knepley . dm      - The DMPlex object
209318fe57SMatthew G. Knepley 
219318fe57SMatthew G. Knepley   Output Parameter:
229318fe57SMatthew G. Knepley . simplex - Flag checking for a simplex
239318fe57SMatthew G. Knepley 
249318fe57SMatthew 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.
259318fe57SMatthew G. Knepley   If the mesh has no cells, this returns PETSC_FALSE.
269318fe57SMatthew G. Knepley 
279318fe57SMatthew G. Knepley   Level: intermediate
289318fe57SMatthew G. Knepley 
299318fe57SMatthew G. Knepley .seealso DMPlexGetSimplexOrBoxCells(), DMPlexGetCellType(), DMPlexGetHeightStratum(), DMPolytopeTypeGetNumVertices()
309318fe57SMatthew G. Knepley @*/
319318fe57SMatthew G. Knepley PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
329318fe57SMatthew G. Knepley {
339318fe57SMatthew G. Knepley   DMPolytopeType ct;
349318fe57SMatthew G. Knepley   PetscInt       cStart, cEnd;
359318fe57SMatthew G. Knepley   PetscErrorCode ierr;
369318fe57SMatthew G. Knepley 
379318fe57SMatthew G. Knepley   PetscFunctionBegin;
389318fe57SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
399318fe57SMatthew G. Knepley   if (cEnd <= cStart) {*simplex = PETSC_FALSE; PetscFunctionReturn(0);}
409318fe57SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
419318fe57SMatthew G. Knepley   *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
429318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
439318fe57SMatthew G. Knepley }
449318fe57SMatthew G. Knepley 
459318fe57SMatthew G. Knepley /*@
46412e9a14SMatthew G. Knepley   DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells
47e5337592SStefano Zampini 
48412e9a14SMatthew G. Knepley   Input Parameter:
49412e9a14SMatthew G. Knepley + dm     - The DMPlex object
50412e9a14SMatthew G. Knepley - height - The cell height in the Plex, 0 is the default
51e5337592SStefano Zampini 
52e5337592SStefano Zampini   Output Parameters:
53412e9a14SMatthew G. Knepley + cStart - The first "normal" cell
54412e9a14SMatthew G. Knepley - cEnd   - The upper bound on "normal"" cells
55e5337592SStefano Zampini 
56412e9a14SMatthew 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.
57e5337592SStefano Zampini 
58412e9a14SMatthew G. Knepley   Level: developer
59e5337592SStefano Zampini 
608065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
61e5337592SStefano Zampini @*/
62412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
63e5337592SStefano Zampini {
64412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
65412e9a14SMatthew G. Knepley   PetscInt       cS, cE, c;
66e5337592SStefano Zampini   PetscErrorCode ierr;
67e5337592SStefano Zampini 
68e5337592SStefano Zampini   PetscFunctionBegin;
69412e9a14SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);CHKERRQ(ierr);
70412e9a14SMatthew G. Knepley   for (c = cS; c < cE; ++c) {
71412e9a14SMatthew G. Knepley     DMPolytopeType cct;
72412e9a14SMatthew G. Knepley 
73412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &cct);CHKERRQ(ierr);
74412e9a14SMatthew G. Knepley     if ((PetscInt) cct < 0) break;
75412e9a14SMatthew G. Knepley     switch (cct) {
76ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_POINT:
77ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_SEGMENT:
78ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TRIANGLE:
79ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_QUADRILATERAL:
80ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TETRAHEDRON:
81ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_HEXAHEDRON:
82412e9a14SMatthew G. Knepley         ct = cct;
83e5337592SStefano Zampini         break;
84412e9a14SMatthew G. Knepley       default: break;
85e5337592SStefano Zampini     }
86412e9a14SMatthew G. Knepley     if (ct != DM_POLYTOPE_UNKNOWN) break;
87e5337592SStefano Zampini   }
88412e9a14SMatthew G. Knepley   if (ct != DM_POLYTOPE_UNKNOWN) {
89412e9a14SMatthew G. Knepley     DMLabel ctLabel;
90412e9a14SMatthew G. Knepley 
91412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
92412e9a14SMatthew G. Knepley     ierr = DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);CHKERRQ(ierr);
93e5337592SStefano Zampini   }
94412e9a14SMatthew G. Knepley   if (cStart) *cStart = cS;
95412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = cE;
96e5337592SStefano Zampini   PetscFunctionReturn(0);
97e5337592SStefano Zampini }
98e5337592SStefano Zampini 
997afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
1007e42fee7SMatthew G. Knepley {
101412e9a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
102a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
1037e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
1047e42fee7SMatthew G. Knepley 
1057e42fee7SMatthew G. Knepley   PetscFunctionBegin;
106e630c359SToby Isaac   *ft  = PETSC_VTK_INVALID;
107f094498dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1087e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
109412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1107e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1117e42fee7SMatthew G. Knepley   if (field >= 0) {
112a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
113a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1147e42fee7SMatthew G. Knepley   } else {
115a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
116a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1177e42fee7SMatthew G. Knepley   }
118ffc4695bSBarry Smith   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
119a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1207e42fee7SMatthew G. Knepley     *sStart = vStart;
1217e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
122f094498dSMatthew G. Knepley     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1237e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_POINT_FIELD;
124a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1257e42fee7SMatthew G. Knepley     *sStart = cStart;
1267e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
127f094498dSMatthew G. Knepley     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1287e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_CELL_FIELD;
129e630c359SToby Isaac   } else {
130e630c359SToby Isaac     if (field >= 0) {
131e630c359SToby Isaac       const char *fieldname;
132e630c359SToby Isaac 
133e630c359SToby Isaac       ierr = PetscSectionGetFieldName(section, field, &fieldname);CHKERRQ(ierr);
134e630c359SToby Isaac       ierr = PetscInfo2((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);CHKERRQ(ierr);
135e630c359SToby Isaac     } else {
1366823f3c5SBlaise Bourdin       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output type of section\"%s\"\n");CHKERRQ(ierr);
137e630c359SToby Isaac     }
138e630c359SToby Isaac   }
1397e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1407e42fee7SMatthew G. Knepley }
1417e42fee7SMatthew G. Knepley 
1427cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
143e412dcbdSMatthew G. Knepley {
144e412dcbdSMatthew G. Knepley   DM                 dm;
145d1df6f1dSMatthew G. Knepley   PetscSection       s;
146e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
147e412dcbdSMatthew G. Knepley   DM                 cdm;
148e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
149e412dcbdSMatthew G. Knepley   Vec                coordinates;
150e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
151e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
152339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
153339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
154d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
155e412dcbdSMatthew G. Knepley   const char        *name;
156339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
157e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
158e412dcbdSMatthew G. Knepley 
159e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
160d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
161d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
162d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
163d1df6f1dSMatthew G. Knepley 
164e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1668135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
16792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
168d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
17192fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
172e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
173e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
175e412dcbdSMatthew G. Knepley 
176e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
177339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
178e412dcbdSMatthew G. Knepley 
179e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
180e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
181e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1820c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1830c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
184e412dcbdSMatthew G. Knepley   }
185e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
186e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
187e412dcbdSMatthew G. Knepley 
188d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
189d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
190d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
191d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
192d1df6f1dSMatthew G. Knepley     IS   fis;
193d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
194d1df6f1dSMatthew G. Knepley     const char *fname;
195d1df6f1dSMatthew G. Knepley 
196d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
197d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
198d1df6f1dSMatthew G. Knepley 
199a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
200d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
201d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
202d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
203d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
204a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
205a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
206d1df6f1dSMatthew G. Knepley     }
207d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
208d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
209d1df6f1dSMatthew G. Knepley 
210d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
211d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
212d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
213d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
214d1df6f1dSMatthew G. Knepley 
215d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
216d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
217339e3443SMatthew G. Knepley       if (!flg) {
218d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
219d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
220d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
221339e3443SMatthew G. Knepley       }
222e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
223339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
224d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
225e412dcbdSMatthew G. Knepley 
226d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
227e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22899a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
229e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
230e412dcbdSMatthew G. Knepley 
231d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
232339e3443SMatthew G. Knepley         if (a) {
233d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
234339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
235339e3443SMatthew G. Knepley         } else {
236339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
237339e3443SMatthew G. Knepley           PetscInt     numVals, va;
238339e3443SMatthew G. Knepley 
239d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
240d1df6f1dSMatthew 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);
241d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
242d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
243d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
244d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
245339e3443SMatthew G. Knepley             break;
246d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
247d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
248d1df6f1dSMatthew 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]);
249d1df6f1dSMatthew G. Knepley             break;
250d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
251339e3443SMatthew G. Knepley           }
252d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
253339e3443SMatthew G. Knepley         }
254e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
255e412dcbdSMatthew G. Knepley         switch (numCoords) {
256e412dcbdSMatthew G. Knepley         case 6:
257339e3443SMatthew 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);
258e412dcbdSMatthew G. Knepley           break;
259e412dcbdSMatthew G. Knepley         case 8:
260339e3443SMatthew 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);
261339e3443SMatthew 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);
262e412dcbdSMatthew G. Knepley           break;
263e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
264e412dcbdSMatthew G. Knepley         }
265e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
266e412dcbdSMatthew G. Knepley       }
267d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
268e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
269e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
270e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
271d1df6f1dSMatthew G. Knepley     }
272d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
273d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
274d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
275d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
276d1df6f1dSMatthew G. Knepley     }
277d1df6f1dSMatthew G. Knepley   }
278e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
279e412dcbdSMatthew G. Knepley }
280e412dcbdSMatthew G. Knepley 
281684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
282684b87d9SLisandro Dalcin {
283684b87d9SLisandro Dalcin   DM                      dm;
284684b87d9SLisandro Dalcin   Vec                     locv;
285684b87d9SLisandro Dalcin   const char              *name;
286684b87d9SLisandro Dalcin   PetscSection            section;
287684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
288e630c359SToby Isaac   PetscInt                numFields;
289684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
290684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
291684b87d9SLisandro Dalcin 
292684b87d9SLisandro Dalcin   PetscFunctionBegin;
293684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
294684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
295684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
296684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
297684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
29892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
299e630c359SToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
300e630c359SToby Isaac   if (!numFields) {
301684b87d9SLisandro Dalcin     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
302e630c359SToby Isaac     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
303e630c359SToby Isaac   } else {
304e630c359SToby Isaac     PetscInt f;
305e630c359SToby Isaac 
306e630c359SToby Isaac     for (f = 0; f < numFields; f++) {
307e630c359SToby Isaac       ierr = DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);CHKERRQ(ierr);
308e630c359SToby Isaac       if (ft == PETSC_VTK_INVALID) continue;
309e630c359SToby Isaac       ierr = PetscObjectReference((PetscObject)locv);CHKERRQ(ierr);
310e630c359SToby Isaac       ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
311e630c359SToby Isaac     }
312e630c359SToby Isaac     ierr = VecDestroy(&locv);CHKERRQ(ierr);
313e630c359SToby Isaac   }
314684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
315684b87d9SLisandro Dalcin }
316684b87d9SLisandro Dalcin 
317552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
318552f7358SJed Brown {
319552f7358SJed Brown   DM             dm;
320684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
321552f7358SJed Brown   PetscErrorCode ierr;
322552f7358SJed Brown 
323552f7358SJed Brown   PetscFunctionBegin;
324552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
32582f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
326552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
327b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
328f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3298135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
330684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
331684b87d9SLisandro Dalcin     PetscInt    i,numFields;
332684b87d9SLisandro Dalcin     PetscObject fe;
333ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
334684b87d9SLisandro Dalcin     Vec         locv = v;
335684b87d9SLisandro Dalcin     const char  *name;
336684b87d9SLisandro Dalcin     PetscInt    step;
337684b87d9SLisandro Dalcin     PetscReal   time;
338ef31f671SMatthew G. Knepley 
339ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
340684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
34144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &fe);CHKERRQ(ierr);
342684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
343ef31f671SMatthew G. Knepley     }
344684b87d9SLisandro Dalcin     if (fem) {
345798534f6SMatthew G. Knepley       PetscObject isZero;
346798534f6SMatthew G. Knepley 
347684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
348684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
349684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
350798534f6SMatthew G. Knepley       ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
351798534f6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
352684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
353684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
354684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
355ef31f671SMatthew G. Knepley     }
356552f7358SJed Brown     if (isvtk) {
357684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
358b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
359b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
360684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
361b136c2c9SMatthew G. Knepley #else
362b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
363b136c2c9SMatthew G. Knepley #endif
364f13a32a3SMatthew G. Knepley     } else if (isdraw) {
365684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
366684b87d9SLisandro Dalcin     } else if (isglvis) {
367684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
368684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
369684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
370684b87d9SLisandro Dalcin     }
371798534f6SMatthew G. Knepley     if (fem) {
372798534f6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
373798534f6SMatthew G. Knepley       ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
374798534f6SMatthew G. Knepley     }
375552f7358SJed Brown   } else {
376684b87d9SLisandro Dalcin     PetscBool isseq;
377684b87d9SLisandro Dalcin 
378684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
37955f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
38055f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
381552f7358SJed Brown   }
382552f7358SJed Brown   PetscFunctionReturn(0);
383552f7358SJed Brown }
384552f7358SJed Brown 
385552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
386552f7358SJed Brown {
387552f7358SJed Brown   DM             dm;
3886823f3c5SBlaise Bourdin   PetscBool      isvtk, ishdf5, isdraw, isglvis, isexodusii;
389552f7358SJed Brown   PetscErrorCode ierr;
390552f7358SJed Brown 
391552f7358SJed Brown   PetscFunctionBegin;
392552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
39382f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
394552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
39533c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
396e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
3978135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
3986823f3c5SBlaise Bourdin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);CHKERRQ(ierr);
399684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
400552f7358SJed Brown     Vec         locv;
401798534f6SMatthew G. Knepley     PetscObject isZero;
402552f7358SJed Brown     const char *name;
403552f7358SJed Brown 
404c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
405552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
406552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
407552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
408552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
409798534f6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
410798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
411552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
412798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
413c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
414b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
415b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
41639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
417b136c2c9SMatthew G. Knepley #else
418b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
419b136c2c9SMatthew G. Knepley #endif
4206823f3c5SBlaise Bourdin   } else if (isexodusii) {
4216823f3c5SBlaise Bourdin #if defined(PETSC_HAVE_EXODUSII)
4226823f3c5SBlaise Bourdin     ierr = VecView_PlexExodusII_Internal(v, viewer);CHKERRQ(ierr);
4236823f3c5SBlaise Bourdin #else
4246823f3c5SBlaise Bourdin     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
4256823f3c5SBlaise Bourdin #endif
426552f7358SJed Brown   } else {
427684b87d9SLisandro Dalcin     PetscBool isseq;
428684b87d9SLisandro Dalcin 
429684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
43055f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
43155f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
432552f7358SJed Brown   }
433552f7358SJed Brown   PetscFunctionReturn(0);
434552f7358SJed Brown }
435552f7358SJed Brown 
436d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
437d930f514SMatthew G. Knepley {
438d930f514SMatthew G. Knepley   DM                dm;
439d930f514SMatthew G. Knepley   MPI_Comm          comm;
440d930f514SMatthew G. Knepley   PetscViewerFormat format;
441d930f514SMatthew G. Knepley   Vec               v;
442d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
443d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
444d930f514SMatthew G. Knepley 
445d930f514SMatthew G. Knepley   PetscFunctionBegin;
446d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
447d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4482c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
449d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
450d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
451d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
452d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
453a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
454a8ad634aSStefano Zampini     /* this need a better fix */
455a8ad634aSStefano Zampini     if (dm->useNatural) {
456a8ad634aSStefano Zampini       if (dm->sfNatural) {
457d930f514SMatthew G. Knepley         const char *vecname;
458d930f514SMatthew G. Knepley         PetscInt    n, nroots;
459d930f514SMatthew G. Knepley 
460ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
461d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
462d930f514SMatthew G. Knepley         if (n == nroots) {
463d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
464d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
465d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
466d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
467d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
468d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
469d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
470a8ad634aSStefano Zampini     } else v = originalv;
471a8ad634aSStefano Zampini   } else v = originalv;
472a8ad634aSStefano Zampini 
473d930f514SMatthew G. Knepley   if (ishdf5) {
474d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
47539d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
476d930f514SMatthew G. Knepley #else
477d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
478d930f514SMatthew G. Knepley #endif
479d930f514SMatthew G. Knepley   } else if (isvtk) {
480d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
481d930f514SMatthew G. Knepley   } else {
482d930f514SMatthew G. Knepley     PetscBool isseq;
483d930f514SMatthew G. Knepley 
484d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
485d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
486d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
487d930f514SMatthew G. Knepley   }
488a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
489d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
490d930f514SMatthew G. Knepley }
491d930f514SMatthew G. Knepley 
4922c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4932c40f234SMatthew G. Knepley {
4942c40f234SMatthew G. Knepley   DM             dm;
4952c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4962c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4972c40f234SMatthew G. Knepley 
4982c40f234SMatthew G. Knepley   PetscFunctionBegin;
4992c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
5002c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
5012c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5022c40f234SMatthew G. Knepley   if (ishdf5) {
5032c40f234SMatthew G. Knepley     DM          dmBC;
5042c40f234SMatthew G. Knepley     Vec         gv;
5052c40f234SMatthew G. Knepley     const char *name;
5062c40f234SMatthew G. Knepley 
5072c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
5082c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
5092c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
5102c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
5112c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
5122c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
5132c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
5142c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
5152c40f234SMatthew G. Knepley   } else {
5162c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
5172c40f234SMatthew G. Knepley   }
5182c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
5192c40f234SMatthew G. Knepley }
5202c40f234SMatthew G. Knepley 
5212c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
5222c40f234SMatthew G. Knepley {
5232c40f234SMatthew G. Knepley   DM             dm;
5246823f3c5SBlaise Bourdin   PetscBool      ishdf5,isexodusii;
5252c40f234SMatthew G. Knepley   PetscErrorCode ierr;
5262c40f234SMatthew G. Knepley 
5272c40f234SMatthew G. Knepley   PetscFunctionBegin;
5282c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
5292c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
5302c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
5316823f3c5SBlaise Bourdin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);CHKERRQ(ierr);
5322c40f234SMatthew G. Knepley   if (ishdf5) {
533878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
53439d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
535b136c2c9SMatthew G. Knepley #else
536b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
537878b459fSMatthew G. Knepley #endif
5386823f3c5SBlaise Bourdin   } else if (isexodusii) {
5396823f3c5SBlaise Bourdin #if defined(PETSC_HAVE_EXODUSII)
5406823f3c5SBlaise Bourdin     ierr = VecLoad_PlexExodusII_Internal(v, viewer);CHKERRQ(ierr);
5416823f3c5SBlaise Bourdin #else
5426823f3c5SBlaise Bourdin     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
5436823f3c5SBlaise Bourdin #endif
5442c40f234SMatthew G. Knepley   } else {
5452c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
546552f7358SJed Brown   }
547552f7358SJed Brown   PetscFunctionReturn(0);
548552f7358SJed Brown }
549552f7358SJed Brown 
550d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
551d930f514SMatthew G. Knepley {
552d930f514SMatthew G. Knepley   DM                dm;
553d930f514SMatthew G. Knepley   PetscViewerFormat format;
554d930f514SMatthew G. Knepley   PetscBool         ishdf5;
555d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
556d930f514SMatthew G. Knepley 
557d930f514SMatthew G. Knepley   PetscFunctionBegin;
558d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
559d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
560d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
561d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
562d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
563a8ad634aSStefano Zampini     if (dm->useNatural) {
564d930f514SMatthew G. Knepley       if (dm->sfNatural) {
565d930f514SMatthew G. Knepley         if (ishdf5) {
566d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
567d930f514SMatthew G. Knepley           Vec         v;
568d930f514SMatthew G. Knepley           const char *vecname;
569d930f514SMatthew G. Knepley 
570d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
571d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
572d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
57339d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
574d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
575d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
576d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
577d930f514SMatthew G. Knepley #else
578d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
579d930f514SMatthew G. Knepley #endif
580d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
581d930f514SMatthew G. Knepley       }
582a8ad634aSStefano Zampini     } else {
583a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
584a8ad634aSStefano Zampini     }
585d930f514SMatthew G. Knepley   }
586d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
587d930f514SMatthew G. Knepley }
588d930f514SMatthew G. Knepley 
5897cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
590731e8ddeSMatthew G. Knepley {
591731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
592731e8ddeSMatthew G. Knepley   Vec                coordinates;
593ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
594731e8ddeSMatthew G. Knepley   const char        *name[4];
595731e8ddeSMatthew G. Knepley   const PetscScalar *a;
596731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
597731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
598731e8ddeSMatthew G. Knepley 
599731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
600731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
601731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
602731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
603731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
604ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
605731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
606731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
607731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
608731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
609731e8ddeSMatthew G. Knepley   name[1]     = "edge";
610731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
611731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
612731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
613731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
614ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
615731e8ddeSMatthew G. Knepley 
616ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
617ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
618731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
619731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
620731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
621731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
622731e8ddeSMatthew G. Knepley 
623731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
624731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
625731e8ddeSMatthew G. Knepley       if (!dof) continue;
626731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
627731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
628f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
629731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
630731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
631731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
632731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
633087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
634731e8ddeSMatthew G. Knepley         }
635731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
636731e8ddeSMatthew G. Knepley       }
637731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
638731e8ddeSMatthew G. Knepley     }
639731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
640731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
641731e8ddeSMatthew G. Knepley   }
642731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
643731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
644731e8ddeSMatthew G. Knepley }
645731e8ddeSMatthew G. Knepley 
6467cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
647552f7358SJed Brown {
648552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
649552f7358SJed Brown   DM                cdm;
650552f7358SJed Brown   PetscSection      coordSection;
651552f7358SJed Brown   Vec               coordinates;
652552f7358SJed Brown   PetscViewerFormat format;
653552f7358SJed Brown   PetscErrorCode    ierr;
654552f7358SJed Brown 
655552f7358SJed Brown   PetscFunctionBegin;
656552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
65792fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
658552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
659552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
660552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
661552f7358SJed Brown     const char *name;
662f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
6639318fe57SMatthew G. Knepley     PetscInt    pStart, pEnd, p, numLabels, l;
664552f7358SJed Brown     PetscMPIInt rank, size;
665552f7358SJed Brown 
666ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
667ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
668552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
669552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
670552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
671f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
672f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
673f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
674f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
675f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
676e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6774d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
678e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
679552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
680552f7358SJed Brown       PetscInt dof, off, s;
681552f7358SJed Brown 
682552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
683552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
684552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
685e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
686552f7358SJed Brown       }
687552f7358SJed Brown     }
688552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
689e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
690e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
691552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
692552f7358SJed Brown       PetscInt dof, off, c;
693552f7358SJed Brown 
694552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
695552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
696552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
697e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
698552f7358SJed Brown       }
699552f7358SJed Brown     }
700552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7014d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
7023d2e540fSStefano Zampini     if (coordSection && coordinates) {
70380180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
7043d2e540fSStefano Zampini     }
7059318fe57SMatthew G. Knepley     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
7069318fe57SMatthew G. Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
7079318fe57SMatthew G. Knepley     for (l = 0; l < numLabels; ++l) {
7089318fe57SMatthew G. Knepley       DMLabel     label;
7099318fe57SMatthew G. Knepley       PetscBool   isdepth;
7109318fe57SMatthew G. Knepley       const char *name;
7119318fe57SMatthew G. Knepley 
7129318fe57SMatthew G. Knepley       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
7139318fe57SMatthew G. Knepley       ierr = PetscStrcmp(name, "depth", &isdepth);CHKERRQ(ierr);
7149318fe57SMatthew G. Knepley       if (isdepth) continue;
7159318fe57SMatthew G. Knepley       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7169318fe57SMatthew G. Knepley       ierr = DMLabelView(label, viewer);CHKERRQ(ierr);
7179318fe57SMatthew G. Knepley     }
718552f7358SJed Brown     if (size > 1) {
719552f7358SJed Brown       PetscSF sf;
720552f7358SJed Brown 
721552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
722552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
723552f7358SJed Brown     }
724552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
725552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
7260588280cSMatthew G. Knepley     const char  *name, *color;
7270588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
7280588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
729fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
730552f7358SJed Brown     PetscReal    scale         = 2.0;
73178081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
7320588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
7330588280cSMatthew G. Knepley     double       tcoords[3];
734552f7358SJed Brown     PetscScalar *coords;
7350588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
736552f7358SJed Brown     PetscMPIInt  rank, size;
7370588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
738fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
739fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
740fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
741552f7358SJed Brown 
7420588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
743552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
744c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
7450588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
7460588280cSMatthew G. Knepley     numColors  = 10;
7470588280cSMatthew G. Knepley     numLColors = 10;
7480588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
749c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
75078081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
751c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
752c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
7530588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
754c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
7550588280cSMatthew G. Knepley     if (!useColors) {
7560588280cSMatthew G. Knepley       numColors = 3;
7570588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
7580588280cSMatthew G. Knepley     }
759c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
7600588280cSMatthew G. Knepley     if (!useColors) {
7610588280cSMatthew G. Knepley       numLColors = 4;
7620588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7630588280cSMatthew G. Knepley     }
764589a23caSBarry Smith     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);CHKERRQ(ierr);
765202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
766202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
767202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
768202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
769fe1cc32dSStefano Zampini 
770fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
771fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
772fe1cc32dSStefano Zampini     if (lflg) {
773fe1cc32dSStefano Zampini       DMLabel lbl;
774fe1cc32dSStefano Zampini 
775fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
776fe1cc32dSStefano Zampini       if (lbl) {
777fe1cc32dSStefano Zampini         PetscInt val, defval;
778fe1cc32dSStefano Zampini 
779fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
780fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
781fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
782fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
783fe1cc32dSStefano Zampini           PetscInt  closureSize;
784fe1cc32dSStefano Zampini 
785fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
786fe1cc32dSStefano Zampini           if (val == defval) continue;
787fe1cc32dSStefano Zampini 
788fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
789fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
790fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
791fe1cc32dSStefano Zampini           }
792fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
793fe1cc32dSStefano Zampini         }
794fe1cc32dSStefano Zampini       }
795fe1cc32dSStefano Zampini     }
796fe1cc32dSStefano Zampini 
797ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
798ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
799552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
800770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
8010588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
802552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
803552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
804552f7358SJed Brown \\usetikzlibrary{arrows}\n\
8050588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
8060588280cSMatthew G. Knepley     if (size > 1) {
807f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
808770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
809770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
810770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
811770b213bSMatthew G Knepley         } else if (p > 0) {
812770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
813770b213bSMatthew G Knepley         }
814770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
815770b213bSMatthew G Knepley       }
8160588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
8170588280cSMatthew G. Knepley     }
818087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
819fe1cc32dSStefano Zampini 
820552f7358SJed Brown     /* Plot vertices */
821552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
822552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
8234d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
824552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
825552f7358SJed Brown       PetscInt  off, dof, d;
8260588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
827552f7358SJed Brown 
828fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
829552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
830552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
8310588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
832f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
8330588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
8340588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
835c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
8360588280cSMatthew G. Knepley       }
8370588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
8380588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
839552f7358SJed Brown       for (d = 0; d < dof; ++d) {
840552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
841087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
842552f7358SJed Brown       }
8430588280cSMatthew G. Knepley       color = colors[rank%numColors];
8440588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
8450588280cSMatthew G. Knepley         PetscInt val;
846c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8470588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
8480588280cSMatthew G. Knepley       }
8490588280cSMatthew G. Knepley       if (useNumbers) {
850e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
8510588280cSMatthew G. Knepley       } else {
852e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
8530588280cSMatthew G. Knepley       }
854552f7358SJed Brown     }
855552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
856552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
857846a3e8bSMatthew G. Knepley     /* Plot cells */
858846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
85978081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
860846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
861846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
862846a3e8bSMatthew G. Knepley         const PetscInt *cone;
863846a3e8bSMatthew G. Knepley 
864fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
865846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
866846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
867846a3e8bSMatthew G. Knepley           PetscInt val;
868846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
869846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
870846a3e8bSMatthew G. Knepley         }
871846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
872846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
873846a3e8bSMatthew G. Knepley       }
874846a3e8bSMatthew G. Knepley     } else {
875846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
876846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
877846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
878846a3e8bSMatthew G. Knepley 
879fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
880846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
881846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
882846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
883846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
884846a3e8bSMatthew G. Knepley 
885846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
886846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
887846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
888846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
889846a3e8bSMatthew G. Knepley         }
890846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
891846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
892846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
893846a3e8bSMatthew G. Knepley       }
894846a3e8bSMatthew G. Knepley     }
895846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
896846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
897846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
898846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
899846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
900846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
901846a3e8bSMatthew G. Knepley 
902fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
903846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
904846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
905846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
906846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
907846a3e8bSMatthew G. Knepley         PetscInt       off;
908846a3e8bSMatthew G. Knepley 
909846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
910846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
911846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
912846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
913846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
914846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
915846a3e8bSMatthew G. Knepley         }
916846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
917846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
918846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
919846a3e8bSMatthew G. Knepley         ++n;
920846a3e8bSMatthew G. Knepley       }
921846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
922846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
923846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
924846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
925087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
926846a3e8bSMatthew G. Knepley       }
927846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
928846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
929846a3e8bSMatthew G. Knepley         PetscInt val;
930846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
931846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
932846a3e8bSMatthew G. Knepley       }
933846a3e8bSMatthew G. Knepley       if (useNumbers) {
934846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
935846a3e8bSMatthew G. Knepley       } else {
936846a3e8bSMatthew 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);
937846a3e8bSMatthew G. Knepley       }
938846a3e8bSMatthew G. Knepley     }
939846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
940552f7358SJed Brown     /* Plot edges */
941202fd40aSStefano Zampini     if (plotEdges) {
942552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
943552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
944552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
945552f7358SJed Brown         const PetscInt *cone;
946552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
947552f7358SJed Brown 
948fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
949552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
950f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
951552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
952552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
953552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
954552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
955552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
956552f7358SJed Brown         for (d = 0; d < dof; ++d) {
957202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
958c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
959552f7358SJed Brown         }
9600588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
9610588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
9620588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9630588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
964f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9650588280cSMatthew G. Knepley         }
9660588280cSMatthew G. Knepley         color = colors[rank%numColors];
9670588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9680588280cSMatthew G. Knepley           PetscInt val;
969c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9700750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9710588280cSMatthew G. Knepley         }
972e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
973552f7358SJed Brown       }
974552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
975552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
976552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9770588280cSMatthew G. Knepley     }
978552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9794d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9800588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
981770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9820588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9830588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9840588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9850588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
986fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9870f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9880f7d6e4aSStefano Zampini     Vec                    cown,acown;
9890f7d6e4aSStefano Zampini     VecScatter             sct;
9900f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9910f7d6e4aSStefano Zampini     IS                     gid,acis;
9920f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9930f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9940f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9950f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9960f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9970f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9980f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9990f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
10000f7d6e4aSStefano Zampini 
10010f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1002ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
1003b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1004ffc4695bSBarry Smith     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRMPI(ierr);
10050f7d6e4aSStefano Zampini #endif
10060f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
1007ffc4695bSBarry Smith       ierr = MPI_Comm_group(comm,&ggroup);CHKERRMPI(ierr);
1008ffc4695bSBarry Smith       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRMPI(ierr);
10090f7d6e4aSStefano Zampini       d1   = 0;
1010ffc4695bSBarry Smith       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRMPI(ierr);
10110f7d6e4aSStefano Zampini       nid  = d2;
1012ffc4695bSBarry Smith       ierr = MPI_Group_free(&ggroup);CHKERRMPI(ierr);
1013ffc4695bSBarry Smith       ierr = MPI_Group_free(&ngroup);CHKERRMPI(ierr);
1014ffc4695bSBarry Smith       ierr = MPI_Comm_free(&ncomm);CHKERRMPI(ierr);
10150f7d6e4aSStefano Zampini     } else nid = 0.0;
10160f7d6e4aSStefano Zampini 
10170f7d6e4aSStefano Zampini     /* Get connectivity */
10180f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
10190f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
10200f7d6e4aSStefano Zampini 
10210f7d6e4aSStefano Zampini     /* filter overlapped local cells */
10220f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
10230f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
10240f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
10250f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
10260f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
10270f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
10280f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
10290f7d6e4aSStefano Zampini     }
10300f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
10310f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
10320f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
10330f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
10340f7d6e4aSStefano Zampini 
10350f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
10360f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
10370f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
10380f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
10390f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
10400f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
10410f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
10420f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
10430f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10440f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10450f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
10460f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
10470f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
10480f7d6e4aSStefano Zampini 
10490f7d6e4aSStefano Zampini     /* compute edgeCut */
10500f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
10510f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
10520f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
10530f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
10540f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
10550f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
10560f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
10570f7d6e4aSStefano Zampini       PetscInt totl;
10580f7d6e4aSStefano Zampini 
10590f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
10600f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
10610f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
10620f7d6e4aSStefano Zampini         if (work[i] < 0) {
10630f7d6e4aSStefano Zampini           ect  += 1;
10640f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10650f7d6e4aSStefano Zampini         }
10660f7d6e4aSStefano Zampini       }
10670f7d6e4aSStefano Zampini     }
10680f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10690f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10700f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10710f7d6e4aSStefano Zampini     lm[1] = -numVertices;
1072820f2d46SBarry Smith     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRMPI(ierr);
10730f7d6e4aSStefano 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);
10740f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10750f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10760f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1077820f2d46SBarry Smith     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRMPI(ierr);
10780f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1079b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
10800f7d6e4aSStefano 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);
10810f7d6e4aSStefano Zampini #else
10820f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10830f7d6e4aSStefano Zampini #endif
10840f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10850f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10860f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10870f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1088552f7358SJed Brown   } else {
1089412e9a14SMatthew G. Knepley     const char    *name;
1090d80ece95SMatthew G. Knepley     PetscInt      *sizes, *hybsizes, *ghostsizes;
1091412e9a14SMatthew G. Knepley     PetscInt       locDepth, depth, cellHeight, dim, d;
1092d80ece95SMatthew G. Knepley     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1093a57dd577SMatthew G Knepley     PetscInt       numLabels, l;
10949318fe57SMatthew G. Knepley     DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1095412e9a14SMatthew G. Knepley     MPI_Comm       comm;
1096412e9a14SMatthew G. Knepley     PetscMPIInt    size, rank;
1097552f7358SJed Brown 
109882f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
1099ffc4695bSBarry Smith     ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
1100ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
1101c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1102f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
11035f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1104f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1105f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1106f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1107552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1108820f2d46SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRMPI(ierr);
1109d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1110d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1111d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1112412e9a14SMatthew G. Knepley     for (d = 0; d <= depth; d++) {
1113412e9a14SMatthew G. Knepley       PetscInt Nc[2] = {0, 0}, ict;
1114412e9a14SMatthew G. Knepley 
1115552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
11169318fe57SMatthew G. Knepley       if (pStart < pEnd) {ierr = DMPlexGetCellType(dm, pStart, &ct0);CHKERRQ(ierr);}
1117412e9a14SMatthew G. Knepley       ict  = ct0;
1118ffc4695bSBarry Smith       ierr = MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1119412e9a14SMatthew G. Knepley       ct0  = (DMPolytopeType) ict;
1120412e9a14SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1121412e9a14SMatthew G. Knepley         DMPolytopeType ct;
1122412e9a14SMatthew G. Knepley 
1123412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
1124412e9a14SMatthew G. Knepley         if (ct == ct0) ++Nc[0];
1125412e9a14SMatthew G. Knepley         else           ++Nc[1];
1126412e9a14SMatthew G. Knepley       }
1127ffc4695bSBarry Smith       ierr = MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1128ffc4695bSBarry Smith       ierr = MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1129ffc4695bSBarry Smith       if (d == depth) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);}
1130412e9a14SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1131834065abSMatthew G. Knepley       for (p = 0; p < size; ++p) {
1132cbb7f117SMark Adams         if (!rank) {
1133412e9a14SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);CHKERRQ(ierr);
1134412e9a14SMatthew G. Knepley           if (hybsizes[p]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);CHKERRQ(ierr);}
1135412e9a14SMatthew G. Knepley           if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1136834065abSMatthew G. Knepley         }
1137cbb7f117SMark Adams       }
1138552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1139552f7358SJed Brown     }
1140d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
11419318fe57SMatthew G. Knepley     {
11429318fe57SMatthew G. Knepley       const PetscReal      *maxCell;
11439318fe57SMatthew G. Knepley       const PetscReal      *L;
11449318fe57SMatthew G. Knepley       const DMBoundaryType *bd;
11459318fe57SMatthew G. Knepley       PetscBool             per, localized;
11469318fe57SMatthew G. Knepley 
11479318fe57SMatthew G. Knepley       ierr = DMGetPeriodicity(dm, &per, &maxCell, &L, &bd);CHKERRQ(ierr);
11489318fe57SMatthew G. Knepley       ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
11499318fe57SMatthew G. Knepley       if (per) {
11509318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "Periodic mesh (");CHKERRQ(ierr);
11519318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
11529318fe57SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
11539318fe57SMatthew G. Knepley           if (bd && d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
11549318fe57SMatthew G. Knepley           if (bd)    {ierr = PetscViewerASCIIPrintf(viewer, "%s", DMBoundaryTypes[bd[d]]);CHKERRQ(ierr);}
11559318fe57SMatthew G. Knepley         }
11569318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ") coordinates %s\n", localized ? "localized" : "not localized");CHKERRQ(ierr);
11579318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
11589318fe57SMatthew G. Knepley       }
11599318fe57SMatthew G. Knepley     }
1160c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1161a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1162a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1163a57dd577SMatthew G Knepley       DMLabel         label;
1164a57dd577SMatthew G Knepley       const char     *name;
1165a57dd577SMatthew G Knepley       IS              valueIS;
1166a57dd577SMatthew G Knepley       const PetscInt *values;
1167a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1168a57dd577SMatthew G Knepley 
1169c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1170c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1171a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1172d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1173a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1174a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1175120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1176a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1177a57dd577SMatthew G Knepley         PetscInt size;
1178a57dd577SMatthew G Knepley 
1179a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1180a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1181d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1182a57dd577SMatthew G Knepley       }
1183a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1184120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1185a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11864d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1187a57dd577SMatthew G Knepley     }
118834aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
118934aa8a36SMatthew G. Knepley     if (dm->Nf) {
119034aa8a36SMatthew G. Knepley       PetscInt f;
119134aa8a36SMatthew G. Knepley 
119234aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
119334aa8a36SMatthew G. Knepley         const char *name;
119434aa8a36SMatthew G. Knepley 
119534aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
119634aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
119734aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
119834aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
119934aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
120034aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1201ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
120234aa8a36SMatthew G. Knepley         } else {
120334aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
120434aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
120534aa8a36SMatthew G. Knepley         }
120634aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
120734aa8a36SMatthew G. Knepley       }
120834aa8a36SMatthew G. Knepley     }
1209a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
12108e7ff633SMatthew G. Knepley     if (cdm) {
12118e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
12128e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
12138e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
12148e7ff633SMatthew G. Knepley     }
1215552f7358SJed Brown   }
1216552f7358SJed Brown   PetscFunctionReturn(0);
1217552f7358SJed Brown }
1218552f7358SJed Brown 
1219e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1220e5c487bfSMatthew G. Knepley {
1221e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1222e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1223e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1224e5c487bfSMatthew G. Knepley 
1225e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1226ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1227e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1228e5c487bfSMatthew G. Knepley   switch (ct) {
1229e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1230e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1231e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1232e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1233e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1234e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1235e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1236e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1237e5c487bfSMatthew G. Knepley     break;
1238e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
1239e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1240e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1241e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1242e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1243e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1244e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1245e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1246e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1247e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1248e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1249e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1250e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1251e5c487bfSMatthew G. Knepley     break;
1252e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1253e5c487bfSMatthew G. Knepley   }
1254e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1255e5c487bfSMatthew G. Knepley }
1256e5c487bfSMatthew G. Knepley 
1257e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1258e5c487bfSMatthew G. Knepley {
1259e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1260e5c487bfSMatthew G. Knepley   PetscReal      centroid[2] = {0., 0.};
1261e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1262e5c487bfSMatthew G. Knepley   PetscInt       fillColor, v, e, d;
1263e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1264e5c487bfSMatthew G. Knepley 
1265e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1266ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1267e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1268e5c487bfSMatthew G. Knepley   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1269e5c487bfSMatthew G. Knepley   switch (ct) {
1270e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1271e5c487bfSMatthew G. Knepley     {
1272e5c487bfSMatthew G. Knepley       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
1273e5c487bfSMatthew G. Knepley 
1274e5c487bfSMatthew G. Knepley       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1275e5c487bfSMatthew G. Knepley       for (e = 0; e < 3; ++e) {
1276e5c487bfSMatthew G. Knepley         refCoords[0] = refVertices[e*2+0];
1277e5c487bfSMatthew G. Knepley         refCoords[1] = refVertices[e*2+1];
1278e5c487bfSMatthew G. Knepley         for (d = 1; d <= edgeDiv; ++d) {
1279e5c487bfSMatthew G. Knepley           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1280e5c487bfSMatthew G. Knepley           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1281e5c487bfSMatthew G. Knepley         }
1282e5c487bfSMatthew G. Knepley         ierr = DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);CHKERRQ(ierr);
1283e5c487bfSMatthew G. Knepley         for (d = 0; d < edgeDiv; ++d) {
1284e5c487bfSMatthew 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);
1285e5c487bfSMatthew 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);
1286e5c487bfSMatthew G. Knepley         }
1287e5c487bfSMatthew G. Knepley       }
1288e5c487bfSMatthew G. Knepley     }
1289e5c487bfSMatthew G. Knepley     break;
1290e5c487bfSMatthew G. Knepley   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1291e5c487bfSMatthew G. Knepley   }
1292e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1293e5c487bfSMatthew G. Knepley }
1294e5c487bfSMatthew G. Knepley 
12957cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1296e412dcbdSMatthew G. Knepley {
1297e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1298e412dcbdSMatthew G. Knepley   DM                 cdm;
1299e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1300e412dcbdSMatthew G. Knepley   Vec                coordinates;
1301e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
130229494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1303e5c487bfSMatthew G. Knepley   PetscReal         *refCoords, *edgeCoords;
1304e5c487bfSMatthew G. Knepley   PetscBool          isnull, drawAffine = PETSC_TRUE;
1305e5c487bfSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1306e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1307e412dcbdSMatthew G. Knepley 
1308e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1309e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1310e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1311e5c487bfSMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);CHKERRQ(ierr);
1312e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);CHKERRQ(ierr);}
1313e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
131492fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1315e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1316e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1317e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1318e412dcbdSMatthew G. Knepley 
1319e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1320e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1321e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1322e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1323e412dcbdSMatthew G. Knepley 
1324e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1325e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1326e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
13270c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
13280c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1329e412dcbdSMatthew G. Knepley   }
1330e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
1331820f2d46SBarry Smith   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1332820f2d46SBarry Smith   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
133329494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1334e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1335e412dcbdSMatthew G. Knepley 
1336cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1337cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1338ba2698f1SMatthew G. Knepley     PetscInt     numCoords;
1339cf3064d3SMatthew G. Knepley 
1340e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);CHKERRQ(ierr);
1341e5c487bfSMatthew G. Knepley     if (drawAffine) {
1342e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCell(dm, draw, c, coords);CHKERRQ(ierr);
1343e5c487bfSMatthew G. Knepley     } else {
1344e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);CHKERRQ(ierr);
1345cf3064d3SMatthew G. Knepley     }
1346cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1347cf3064d3SMatthew G. Knepley   }
1348e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscFree2(refCoords, edgeCoords);CHKERRQ(ierr);}
1349e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1350e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1351e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1352e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1353e412dcbdSMatthew G. Knepley }
1354e412dcbdSMatthew G. Knepley 
13551e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
13561e50132fSMatthew G. Knepley #include <exodusII.h>
13576823f3c5SBlaise Bourdin #include <petscviewerexodusii.h>
13581e50132fSMatthew G. Knepley #endif
13591e50132fSMatthew G. Knepley 
1360552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1361552f7358SJed Brown {
13621e50132fSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1363002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1364552f7358SJed Brown   PetscErrorCode ierr;
1365552f7358SJed Brown 
1366552f7358SJed Brown   PetscFunctionBegin;
1367552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1368552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1369552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);CHKERRQ(ierr);
1370fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
1371c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
1372e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
13738135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
13741e50132fSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);CHKERRQ(ierr);
1375552f7358SJed Brown   if (iascii) {
13768135c375SStefano Zampini     PetscViewerFormat format;
13778135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13788135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
13798135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13808135c375SStefano Zampini     } else {
1381552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
13828135c375SStefano Zampini     }
1383c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1384c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
138539d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1386c6ccd67eSMatthew G. Knepley #else
1387c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1388552f7358SJed Brown #endif
1389e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1390fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1391e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1392e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
13938135c375SStefano Zampini   } else if (isglvis) {
13948135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
13951e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
13961e50132fSMatthew G. Knepley   } else if (isexodus) {
13976823f3c5SBlaise Bourdin /*
13986823f3c5SBlaise Bourdin       exodusII requires that all sets be part of exactly one cell set.
13996823f3c5SBlaise Bourdin       If the dm does not have a "Cell Sets" label defined, we create one
14006823f3c5SBlaise Bourdin       with ID 1, containig all cells.
14016823f3c5SBlaise Bourdin       Note that if the Cell Sets label is defined but does not cover all cells,
14026823f3c5SBlaise Bourdin       we may still have a problem. This should probably be checked here or in the viewer;
14036823f3c5SBlaise Bourdin     */
14046823f3c5SBlaise Bourdin     PetscInt numCS;
14056823f3c5SBlaise Bourdin     ierr = DMGetLabelSize(dm,"Cell Sets",&numCS);CHKERRQ(ierr);
14066823f3c5SBlaise Bourdin     if (!numCS) {
14071e50132fSMatthew G. Knepley       PetscInt cStart, cEnd, c;
14081e50132fSMatthew G. Knepley       ierr = DMCreateLabel(dm, "Cell Sets");CHKERRQ(ierr);
14091e50132fSMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
14101e50132fSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {ierr = DMSetLabelValue(dm, "Cell Sets", c, 1);CHKERRQ(ierr);}
14116823f3c5SBlaise Bourdin     }
14126823f3c5SBlaise Bourdin     ierr = DMView_PlexExodusII(dm, viewer);CHKERRQ(ierr);
14131e50132fSMatthew G. Knepley #endif
141462201deeSVaclav Hapla   } else {
1415a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1416fcf6c8fdSToby Isaac   }
1417cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1418cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1419cb3ba0daSMatthew G. Knepley   if (flg) {
1420cb3ba0daSMatthew G. Knepley     Vec ranks;
1421cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1422cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1423cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1424cb3ba0daSMatthew G. Knepley   }
1425002a2709SMatthew G. Knepley   /* Optionally view a label */
1426589a23caSBarry Smith   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);CHKERRQ(ierr);
1427002a2709SMatthew G. Knepley   if (flg) {
1428002a2709SMatthew G. Knepley     DMLabel label;
1429002a2709SMatthew G. Knepley     Vec     val;
1430002a2709SMatthew G. Knepley 
1431002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1432002a2709SMatthew 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);
1433002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1434002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1435002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1436002a2709SMatthew G. Knepley   }
1437552f7358SJed Brown   PetscFunctionReturn(0);
1438552f7358SJed Brown }
1439552f7358SJed Brown 
14407f96f51bSksagiyam /*@
14417f96f51bSksagiyam   DMPlexTopologyView - Saves a DMPlex topology into a file
14427f96f51bSksagiyam 
14437f96f51bSksagiyam   Collective on DM
14447f96f51bSksagiyam 
14457f96f51bSksagiyam   Input Parameters:
14467f96f51bSksagiyam + dm     - The DM whose topology is to be saved
14477f96f51bSksagiyam - viewer - The PetscViewer for saving
14487f96f51bSksagiyam 
14497f96f51bSksagiyam   Level: advanced
14507f96f51bSksagiyam 
14517f96f51bSksagiyam .seealso: DMView(), DMPlexCoordinatesView(), DMPlexLabelsView(), DMPlexTopologyLoad()
14527f96f51bSksagiyam @*/
14537f96f51bSksagiyam PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
14547f96f51bSksagiyam {
14557f96f51bSksagiyam   PetscBool      ishdf5;
14567f96f51bSksagiyam   PetscErrorCode ierr;
14577f96f51bSksagiyam 
14587f96f51bSksagiyam   PetscFunctionBegin;
14597f96f51bSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14607f96f51bSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
14617f96f51bSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
14627f96f51bSksagiyam   if (ishdf5) {
14637f96f51bSksagiyam #if defined(PETSC_HAVE_HDF5)
14647f96f51bSksagiyam     PetscViewerFormat format;
14657f96f51bSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
14667f96f51bSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
14677f96f51bSksagiyam       IS globalPointNumbering;
14687f96f51bSksagiyam 
14697f96f51bSksagiyam       ierr = DMPlexCreatePointNumbering(dm, &globalPointNumbering);CHKERRQ(ierr);
14707f96f51bSksagiyam       ierr = DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer);CHKERRQ(ierr);
14717f96f51bSksagiyam       ierr = ISDestroy(&globalPointNumbering);CHKERRQ(ierr);
14727f96f51bSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
14737f96f51bSksagiyam #else
14747f96f51bSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
14757f96f51bSksagiyam #endif
14767f96f51bSksagiyam   }
14777f96f51bSksagiyam   PetscFunctionReturn(0);
14787f96f51bSksagiyam }
14797f96f51bSksagiyam 
148077b8e257Sksagiyam /*@
148177b8e257Sksagiyam   DMPlexCoordinatesView - Saves DMPlex coordinates into a file
148277b8e257Sksagiyam 
148377b8e257Sksagiyam   Collective on DM
148477b8e257Sksagiyam 
148577b8e257Sksagiyam   Input Parameters:
148677b8e257Sksagiyam + dm     - The DM whose coordinates are to be saved
148777b8e257Sksagiyam - viewer - The PetscViewer for saving
148877b8e257Sksagiyam 
148977b8e257Sksagiyam   Level: advanced
149077b8e257Sksagiyam 
149177b8e257Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexLabelsView(), DMPlexCoordinatesLoad()
149277b8e257Sksagiyam @*/
149377b8e257Sksagiyam PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
149477b8e257Sksagiyam {
149577b8e257Sksagiyam   PetscBool      ishdf5;
149677b8e257Sksagiyam   PetscErrorCode ierr;
149777b8e257Sksagiyam 
149877b8e257Sksagiyam   PetscFunctionBegin;
149977b8e257Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
150077b8e257Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
150177b8e257Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
150277b8e257Sksagiyam   if (ishdf5) {
150377b8e257Sksagiyam #if defined(PETSC_HAVE_HDF5)
150477b8e257Sksagiyam     PetscViewerFormat format;
150577b8e257Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
150677b8e257Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
150777b8e257Sksagiyam       ierr = DMPlexCoordinatesView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
150877b8e257Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
150977b8e257Sksagiyam #else
151077b8e257Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
151177b8e257Sksagiyam #endif
151277b8e257Sksagiyam   }
151377b8e257Sksagiyam   PetscFunctionReturn(0);
151477b8e257Sksagiyam }
151577b8e257Sksagiyam 
1516bd6565f1Sksagiyam /*@
1517bd6565f1Sksagiyam   DMPlexLabelsView - Saves DMPlex labels into a file
1518bd6565f1Sksagiyam 
1519bd6565f1Sksagiyam   Collective on DM
1520bd6565f1Sksagiyam 
1521bd6565f1Sksagiyam   Input Parameters:
1522bd6565f1Sksagiyam + dm     - The DM whose labels are to be saved
1523bd6565f1Sksagiyam - viewer - The PetscViewer for saving
1524bd6565f1Sksagiyam 
1525bd6565f1Sksagiyam   Level: advanced
1526bd6565f1Sksagiyam 
1527bd6565f1Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexCoordinatesView(), DMPlexLabelsLoad()
1528bd6565f1Sksagiyam @*/
1529bd6565f1Sksagiyam PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
1530bd6565f1Sksagiyam {
1531bd6565f1Sksagiyam   PetscBool      ishdf5;
1532bd6565f1Sksagiyam   PetscErrorCode ierr;
1533bd6565f1Sksagiyam 
1534bd6565f1Sksagiyam   PetscFunctionBegin;
1535bd6565f1Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1536bd6565f1Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1537bd6565f1Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1538bd6565f1Sksagiyam   if (ishdf5) {
1539bd6565f1Sksagiyam #if defined(PETSC_HAVE_HDF5)
1540bd6565f1Sksagiyam     IS                globalPointNumbering;
1541bd6565f1Sksagiyam     PetscViewerFormat format;
1542bd6565f1Sksagiyam 
1543bd6565f1Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1544bd6565f1Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1545bd6565f1Sksagiyam       ierr = DMPlexCreatePointNumbering(dm, &globalPointNumbering);CHKERRQ(ierr);
1546bd6565f1Sksagiyam       ierr = DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer);CHKERRQ(ierr);
1547bd6565f1Sksagiyam       ierr = ISDestroy(&globalPointNumbering);CHKERRQ(ierr);
1548bd6565f1Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1549bd6565f1Sksagiyam #else
1550bd6565f1Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1551bd6565f1Sksagiyam #endif
1552bd6565f1Sksagiyam   }
1553bd6565f1Sksagiyam   PetscFunctionReturn(0);
1554bd6565f1Sksagiyam }
1555bd6565f1Sksagiyam 
15562c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
15572c40f234SMatthew G. Knepley {
1558d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
15592c40f234SMatthew G. Knepley   PetscErrorCode ierr;
15602c40f234SMatthew G. Knepley 
15612c40f234SMatthew G. Knepley   PetscFunctionBegin;
15622c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15632c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
15642c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1565d4f5a9a0SVaclav Hapla   if (ishdf5) {
15662c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
15679c48423bSVaclav Hapla     PetscViewerFormat format;
15689c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
15699c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
15709c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1571509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
157239d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1573509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1574b458e8f1SJose E. Roman     PetscFunctionReturn(0);
15752c40f234SMatthew G. Knepley #else
15762c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1577552f7358SJed Brown #endif
1578b458e8f1SJose E. Roman   } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1579552f7358SJed Brown }
1580552f7358SJed Brown 
1581ea8e1828Sksagiyam /*@
1582ea8e1828Sksagiyam   DMPlexTopologyLoad - Loads a topology into a DMPlex
1583ea8e1828Sksagiyam 
1584ea8e1828Sksagiyam   Collective on DM
1585ea8e1828Sksagiyam 
1586ea8e1828Sksagiyam   Input Parameters:
1587ea8e1828Sksagiyam + dm     - The DM into which the topology is loaded
1588ea8e1828Sksagiyam - viewer - The PetscViewer for the saved topology
1589ea8e1828Sksagiyam 
1590dec9e869Sksagiyam   Output Parameters:
1591dec9e869Sksagiyam . 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
1592dec9e869Sksagiyam 
1593ea8e1828Sksagiyam   Level: advanced
1594ea8e1828Sksagiyam 
1595b08ad5deSksagiyam .seealso: DMLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1596ea8e1828Sksagiyam @*/
1597dec9e869Sksagiyam PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *sf)
1598ea8e1828Sksagiyam {
1599ea8e1828Sksagiyam   PetscBool      ishdf5;
1600ea8e1828Sksagiyam   PetscErrorCode ierr;
1601ea8e1828Sksagiyam 
1602ea8e1828Sksagiyam   PetscFunctionBegin;
1603ea8e1828Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604ea8e1828Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1605*806c51deSksagiyam   if (sf) PetscValidPointer(sf, 3);
1606ea8e1828Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1607ea8e1828Sksagiyam   if (ishdf5) {
1608ea8e1828Sksagiyam #if defined(PETSC_HAVE_HDF5)
1609ea8e1828Sksagiyam     PetscViewerFormat format;
1610ea8e1828Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1611ea8e1828Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1612dec9e869Sksagiyam       ierr = DMPlexTopologyLoad_HDF5_Internal(dm, viewer, sf);CHKERRQ(ierr);
1613ea8e1828Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1614ea8e1828Sksagiyam #else
1615ea8e1828Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1616ea8e1828Sksagiyam #endif
1617ea8e1828Sksagiyam   }
1618ea8e1828Sksagiyam   PetscFunctionReturn(0);
1619ea8e1828Sksagiyam }
1620ea8e1828Sksagiyam 
16213e701f1cSksagiyam /*@
16223e701f1cSksagiyam   DMPlexCoordinatesLoad - Loads coordinates into a DMPlex
16233e701f1cSksagiyam 
16243e701f1cSksagiyam   Collective on DM
16253e701f1cSksagiyam 
16263e701f1cSksagiyam   Input Parameters:
16273e701f1cSksagiyam + dm     - The DM into which the coordinates are loaded
16283e701f1cSksagiyam - viewer - The PetscViewer for the saved coordinates
16293e701f1cSksagiyam 
16303e701f1cSksagiyam   Level: advanced
16313e701f1cSksagiyam 
1632b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
16333e701f1cSksagiyam @*/
16343e701f1cSksagiyam PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer)
16353e701f1cSksagiyam {
16363e701f1cSksagiyam   PetscBool      ishdf5;
16373e701f1cSksagiyam   PetscErrorCode ierr;
16383e701f1cSksagiyam 
16393e701f1cSksagiyam   PetscFunctionBegin;
16403e701f1cSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16413e701f1cSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
16423e701f1cSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
16433e701f1cSksagiyam   if (ishdf5) {
16443e701f1cSksagiyam #if defined(PETSC_HAVE_HDF5)
16453e701f1cSksagiyam     PetscViewerFormat format;
16463e701f1cSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
16473e701f1cSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
16483e701f1cSksagiyam       ierr = DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
16493e701f1cSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
16503e701f1cSksagiyam #else
16513e701f1cSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
16523e701f1cSksagiyam #endif
16533e701f1cSksagiyam   }
16543e701f1cSksagiyam   PetscFunctionReturn(0);
16553e701f1cSksagiyam }
16563e701f1cSksagiyam 
1657b08ad5deSksagiyam /*@
1658b08ad5deSksagiyam   DMPlexLabelsLoad - Loads labels into a DMPlex
1659b08ad5deSksagiyam 
1660b08ad5deSksagiyam   Collective on DM
1661b08ad5deSksagiyam 
1662b08ad5deSksagiyam   Input Parameters:
1663b08ad5deSksagiyam + dm     - The DM into which the labels are loaded
1664b08ad5deSksagiyam - viewer - The PetscViewer for the saved labels
1665b08ad5deSksagiyam 
1666b08ad5deSksagiyam   Level: advanced
1667b08ad5deSksagiyam 
1668b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1669b08ad5deSksagiyam @*/
1670b08ad5deSksagiyam PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer)
1671b08ad5deSksagiyam {
1672b08ad5deSksagiyam   PetscBool      ishdf5;
1673b08ad5deSksagiyam   PetscErrorCode ierr;
1674b08ad5deSksagiyam 
1675b08ad5deSksagiyam   PetscFunctionBegin;
1676b08ad5deSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1677b08ad5deSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1678b08ad5deSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1679b08ad5deSksagiyam   if (ishdf5) {
1680b08ad5deSksagiyam #if defined(PETSC_HAVE_HDF5)
1681b08ad5deSksagiyam     PetscViewerFormat format;
1682b08ad5deSksagiyam 
1683b08ad5deSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1684b08ad5deSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1685b08ad5deSksagiyam       ierr = DMPlexLabelsLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1686b08ad5deSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1687b08ad5deSksagiyam #else
1688b08ad5deSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1689b08ad5deSksagiyam #endif
1690b08ad5deSksagiyam   }
1691b08ad5deSksagiyam   PetscFunctionReturn(0);
1692b08ad5deSksagiyam }
1693b08ad5deSksagiyam 
1694552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1695552f7358SJed Brown {
1696552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1697552f7358SJed Brown   PetscErrorCode ierr;
1698552f7358SJed Brown 
1699552f7358SJed Brown   PetscFunctionBegin;
17008135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
17018135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
170228d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
17031eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);CHKERRQ(ierr);
17040d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1705552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1706552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1707552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1708552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1709be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1710552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1711552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1712d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1713d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
171477623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1715a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
171697d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
1717552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1718552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1719a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1720a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1721d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1722d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1723d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1724d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1725d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1726d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1727fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
17280a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
1729552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1730552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1731552f7358SJed Brown   PetscFunctionReturn(0);
1732552f7358SJed Brown }
1733552f7358SJed Brown 
1734b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1735552f7358SJed Brown {
17368d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1737acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1738552f7358SJed Brown   PetscInt               localSize;
1739837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1740552f7358SJed Brown   PetscErrorCode         ierr;
1741b412c318SBarry Smith   MatType                mtype;
17421428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1743552f7358SJed Brown 
1744552f7358SJed Brown   PetscFunctionBegin;
1745607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1746b412c318SBarry Smith   mtype = dm->mattype;
1747e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1748552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1749552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
175082f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1751552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1752552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1753552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1754acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1755acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1756552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1757552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1758552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1759552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1760552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1761552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1762552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1763837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1764552f7358SJed Brown   if (!isShell) {
1765be36d101SStefano Zampini     PetscSection subSection;
1766837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
17670be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1768fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1769552f7358SJed Brown 
177000140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1771be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1772be36d101SStefano Zampini       PetscSection section;
1773be36d101SStefano Zampini       PetscInt     size;
1774be36d101SStefano Zampini 
177592fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1776be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1777be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1778be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1779be36d101SStefano Zampini     } else {
178000140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1781be36d101SStefano Zampini     }
1782552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1783be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1784a9d99c84SMatthew G. Knepley       PetscInt bdof;
1785a9d99c84SMatthew G. Knepley 
1786552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1787fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
17881d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
17891d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
17901d17a0a3SMatthew G. Knepley       if (dof) {
17911d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1792be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1793be36d101SStefano Zampini       }
1794be36d101SStefano Zampini       if (isMatIS) {
1795be36d101SStefano Zampini         PetscInt loff,c,off;
1796be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1797be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1798be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1799552f7358SJed Brown       }
18002a28c762SMatthew G Knepley     }
18012a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
18020be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
18030be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
18040be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
18050be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
18066fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
18076fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
18084fa26246SMatthew G. Knepley       PetscInt l;
18096fd5c86aSStefano Zampini 
18106fd5c86aSStefano Zampini       lsize = lsize/bs;
18116fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
18124fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1813be36d101SStefano Zampini     }
1814be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1815be36d101SStefano Zampini     if (isMatIS) {
1816be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1817be36d101SStefano Zampini     }
18181795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
18198d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1820552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1821552f7358SJed Brown   }
1822b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1823552f7358SJed Brown   PetscFunctionReturn(0);
1824552f7358SJed Brown }
1825552f7358SJed Brown 
18267cd05799SMatthew G. Knepley /*@
1827a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1828be36d101SStefano Zampini 
1829be36d101SStefano Zampini   Not collective
1830be36d101SStefano Zampini 
1831be36d101SStefano Zampini   Input Parameter:
1832be36d101SStefano Zampini . mesh - The DMPlex
1833be36d101SStefano Zampini 
1834be36d101SStefano Zampini   Output Parameters:
1835be36d101SStefano Zampini . subsection - The subdomain section
1836be36d101SStefano Zampini 
1837be36d101SStefano Zampini   Level: developer
1838be36d101SStefano Zampini 
1839be36d101SStefano Zampini .seealso:
18407cd05799SMatthew G. Knepley @*/
1841be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1842be36d101SStefano Zampini {
1843be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1844be36d101SStefano Zampini   PetscErrorCode ierr;
1845be36d101SStefano Zampini 
1846be36d101SStefano Zampini   PetscFunctionBegin;
1847be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1848be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1849be36d101SStefano Zampini     PetscSection section;
1850be36d101SStefano Zampini     PetscSF      sf;
1851be36d101SStefano Zampini 
1852be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
185392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1854be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1855be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1856be36d101SStefano Zampini   }
1857be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1858be36d101SStefano Zampini   PetscFunctionReturn(0);
1859be36d101SStefano Zampini }
1860be36d101SStefano Zampini 
1861552f7358SJed Brown /*@
1862552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1863552f7358SJed Brown 
1864552f7358SJed Brown   Not collective
1865552f7358SJed Brown 
1866552f7358SJed Brown   Input Parameter:
1867552f7358SJed Brown . mesh - The DMPlex
1868552f7358SJed Brown 
1869552f7358SJed Brown   Output Parameters:
1870552f7358SJed Brown + pStart - The first mesh point
1871552f7358SJed Brown - pEnd   - The upper bound for mesh points
1872552f7358SJed Brown 
1873552f7358SJed Brown   Level: beginner
1874552f7358SJed Brown 
1875552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1876552f7358SJed Brown @*/
1877552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1878552f7358SJed Brown {
1879552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1880552f7358SJed Brown   PetscErrorCode ierr;
1881552f7358SJed Brown 
1882552f7358SJed Brown   PetscFunctionBegin;
1883552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1884552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1885552f7358SJed Brown   PetscFunctionReturn(0);
1886552f7358SJed Brown }
1887552f7358SJed Brown 
1888552f7358SJed Brown /*@
1889552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1890552f7358SJed Brown 
1891552f7358SJed Brown   Not collective
1892552f7358SJed Brown 
1893552f7358SJed Brown   Input Parameters:
1894552f7358SJed Brown + mesh - The DMPlex
1895552f7358SJed Brown . pStart - The first mesh point
1896552f7358SJed Brown - pEnd   - The upper bound for mesh points
1897552f7358SJed Brown 
1898552f7358SJed Brown   Output Parameters:
1899552f7358SJed Brown 
1900552f7358SJed Brown   Level: beginner
1901552f7358SJed Brown 
1902552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1903552f7358SJed Brown @*/
1904552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1905552f7358SJed Brown {
1906552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1907552f7358SJed Brown   PetscErrorCode ierr;
1908552f7358SJed Brown 
1909552f7358SJed Brown   PetscFunctionBegin;
1910552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1911552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1912552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1913552f7358SJed Brown   PetscFunctionReturn(0);
1914552f7358SJed Brown }
1915552f7358SJed Brown 
1916552f7358SJed Brown /*@
1917eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1918552f7358SJed Brown 
1919552f7358SJed Brown   Not collective
1920552f7358SJed Brown 
1921552f7358SJed Brown   Input Parameters:
1922552f7358SJed Brown + mesh - The DMPlex
1923eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1924552f7358SJed Brown 
1925552f7358SJed Brown   Output Parameter:
1926552f7358SJed Brown . size - The cone size for point p
1927552f7358SJed Brown 
1928552f7358SJed Brown   Level: beginner
1929552f7358SJed Brown 
1930552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1931552f7358SJed Brown @*/
1932552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1933552f7358SJed Brown {
1934552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1935552f7358SJed Brown   PetscErrorCode ierr;
1936552f7358SJed Brown 
1937552f7358SJed Brown   PetscFunctionBegin;
1938552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1939552f7358SJed Brown   PetscValidPointer(size, 3);
1940552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1941552f7358SJed Brown   PetscFunctionReturn(0);
1942552f7358SJed Brown }
1943552f7358SJed Brown 
1944552f7358SJed Brown /*@
1945eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1946552f7358SJed Brown 
1947552f7358SJed Brown   Not collective
1948552f7358SJed Brown 
1949552f7358SJed Brown   Input Parameters:
1950552f7358SJed Brown + mesh - The DMPlex
1951eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1952552f7358SJed Brown - size - The cone size for point p
1953552f7358SJed Brown 
1954552f7358SJed Brown   Output Parameter:
1955552f7358SJed Brown 
1956552f7358SJed Brown   Note:
1957552f7358SJed Brown   This should be called after DMPlexSetChart().
1958552f7358SJed Brown 
1959552f7358SJed Brown   Level: beginner
1960552f7358SJed Brown 
1961552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1962552f7358SJed Brown @*/
1963552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1964552f7358SJed Brown {
1965552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1966552f7358SJed Brown   PetscErrorCode ierr;
1967552f7358SJed Brown 
1968552f7358SJed Brown   PetscFunctionBegin;
1969552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1970552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
19710d644c17SKarl Rupp 
1972552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1973552f7358SJed Brown   PetscFunctionReturn(0);
1974552f7358SJed Brown }
1975552f7358SJed Brown 
1976f5a469b9SMatthew G. Knepley /*@
1977eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1978f5a469b9SMatthew G. Knepley 
1979f5a469b9SMatthew G. Knepley   Not collective
1980f5a469b9SMatthew G. Knepley 
1981f5a469b9SMatthew G. Knepley   Input Parameters:
1982f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1983eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1984f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1985f5a469b9SMatthew G. Knepley 
1986f5a469b9SMatthew G. Knepley   Output Parameter:
1987f5a469b9SMatthew G. Knepley 
1988f5a469b9SMatthew G. Knepley   Note:
1989f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1990f5a469b9SMatthew G. Knepley 
1991f5a469b9SMatthew G. Knepley   Level: beginner
1992f5a469b9SMatthew G. Knepley 
1993f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1994f5a469b9SMatthew G. Knepley @*/
1995f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1996f5a469b9SMatthew G. Knepley {
1997f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1998f5a469b9SMatthew G. Knepley   PetscInt       csize;
1999f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
2000f5a469b9SMatthew G. Knepley 
2001f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
2002f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2003f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
2004f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
2005f5a469b9SMatthew G. Knepley 
2006f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
2007f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
2008f5a469b9SMatthew G. Knepley }
2009f5a469b9SMatthew G. Knepley 
2010552f7358SJed Brown /*@C
2011eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
2012552f7358SJed Brown 
2013552f7358SJed Brown   Not collective
2014552f7358SJed Brown 
2015552f7358SJed Brown   Input Parameters:
2016833c876bSVaclav Hapla + dm - The DMPlex
2017eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2018552f7358SJed Brown 
2019552f7358SJed Brown   Output Parameter:
2020552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
2021552f7358SJed Brown 
2022552f7358SJed Brown   Level: beginner
2023552f7358SJed Brown 
20243813dfbdSMatthew G Knepley   Fortran Notes:
20253813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20263813dfbdSMatthew G Knepley   include petsc.h90 in your code.
2027922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
2028922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
20293813dfbdSMatthew G Knepley 
2030e45f02b0SMatthew G. Knepley .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
2031552f7358SJed Brown @*/
2032552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
2033552f7358SJed Brown {
2034552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2035552f7358SJed Brown   PetscInt       off;
2036552f7358SJed Brown   PetscErrorCode ierr;
2037552f7358SJed Brown 
2038552f7358SJed Brown   PetscFunctionBegin;
2039552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2040552f7358SJed Brown   PetscValidPointer(cone, 3);
2041552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2042552f7358SJed Brown   *cone = &mesh->cones[off];
2043552f7358SJed Brown   PetscFunctionReturn(0);
2044552f7358SJed Brown }
2045552f7358SJed Brown 
20460ce7577fSVaclav Hapla /*@C
20470ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
20480ce7577fSVaclav Hapla 
20490ce7577fSVaclav Hapla   Not collective
20500ce7577fSVaclav Hapla 
20510ce7577fSVaclav Hapla   Input Parameters:
20520ce7577fSVaclav Hapla + dm - The DMPlex
20530ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
20540ce7577fSVaclav Hapla 
20550ce7577fSVaclav Hapla   Output Parameter:
20560ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
20570ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
20580ce7577fSVaclav Hapla 
20590ce7577fSVaclav Hapla   Level: intermediate
20600ce7577fSVaclav Hapla 
2061d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
20620ce7577fSVaclav Hapla @*/
20630ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
20640ce7577fSVaclav Hapla {
20650ce7577fSVaclav Hapla   PetscSection        cs, newcs;
20660ce7577fSVaclav Hapla   PetscInt            *cones;
20670ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
20680ce7577fSVaclav Hapla   PetscInt            n;
20690ce7577fSVaclav Hapla   PetscErrorCode      ierr;
20700ce7577fSVaclav Hapla 
20710ce7577fSVaclav Hapla   PetscFunctionBegin;
20720ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
20730ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
20740ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
20750ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
20760ce7577fSVaclav Hapla   if (pCones) {
20770ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
20780ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
20790ce7577fSVaclav Hapla   }
20800ce7577fSVaclav Hapla   PetscFunctionReturn(0);
20810ce7577fSVaclav Hapla }
20820ce7577fSVaclav Hapla 
2083af9eab45SVaclav Hapla /*@
2084af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
2085d4636a37SVaclav Hapla 
2086d4636a37SVaclav Hapla   Not collective
2087d4636a37SVaclav Hapla 
2088d4636a37SVaclav Hapla   Input Parameters:
2089d4636a37SVaclav Hapla + dm - The DMPlex
2090af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2091d4636a37SVaclav Hapla 
2092d4636a37SVaclav Hapla   Output Parameter:
2093af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
2094d4636a37SVaclav Hapla 
2095d4636a37SVaclav Hapla   Level: advanced
2096d4636a37SVaclav Hapla 
2097af9eab45SVaclav Hapla   Notes:
2098af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
2099af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
2100af9eab45SVaclav Hapla 
2101af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
2102d4636a37SVaclav Hapla @*/
2103af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2104d4636a37SVaclav Hapla {
2105af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
2106af9eab45SVaclav Hapla   PetscInt            depth;
2107d4636a37SVaclav Hapla   PetscErrorCode      ierr;
2108d4636a37SVaclav Hapla 
2109d4636a37SVaclav Hapla   PetscFunctionBegin;
2110af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2111af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2112af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
2113af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2114af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
2115af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
2116af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2117af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2118af9eab45SVaclav Hapla }
2119af9eab45SVaclav Hapla 
2120af9eab45SVaclav Hapla /*@
2121af9eab45SVaclav 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).
2122af9eab45SVaclav Hapla 
2123af9eab45SVaclav Hapla   Not collective
2124af9eab45SVaclav Hapla 
2125af9eab45SVaclav Hapla   Input Parameters:
2126af9eab45SVaclav Hapla + dm - The DMPlex
2127af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2128af9eab45SVaclav Hapla 
2129af9eab45SVaclav Hapla   Output Parameter:
2130af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2131af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
2132af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2133af9eab45SVaclav Hapla 
2134af9eab45SVaclav Hapla   Level: advanced
2135af9eab45SVaclav Hapla 
2136af9eab45SVaclav Hapla   Notes:
2137af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
2138af9eab45SVaclav Hapla 
2139af9eab45SVaclav 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.
2140af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
2141af9eab45SVaclav Hapla 
2142af9eab45SVaclav 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:
2143af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
2144af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
2145af9eab45SVaclav Hapla 
2146af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2147af9eab45SVaclav Hapla @*/
2148af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2149af9eab45SVaclav Hapla {
2150af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
2151af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
2152af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
2153af9eab45SVaclav Hapla   IS                  *expandedPoints_;
2154af9eab45SVaclav Hapla   PetscSection        *sections_;
2155af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2156af9eab45SVaclav Hapla 
2157af9eab45SVaclav Hapla   PetscFunctionBegin;
2158af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2159af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2160af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
2161af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
2162af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
2163af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
2164af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
2165af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2166af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
2167af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
2168af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
2169af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
2170af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
2171af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
2172af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2173af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
2174af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
2175af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
2176af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
2177af9eab45SVaclav Hapla       } else {
2178af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
2179af9eab45SVaclav Hapla       }
2180af9eab45SVaclav Hapla     }
2181af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
2182af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
2183af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
2184af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2185af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
2186af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
2187af9eab45SVaclav Hapla       if (cn > 1) {
2188af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
2189af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
2190af9eab45SVaclav Hapla       } else {
2191af9eab45SVaclav Hapla         newarr[co] = arr[i];
2192af9eab45SVaclav Hapla       }
2193af9eab45SVaclav Hapla     }
2194af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
2195af9eab45SVaclav Hapla     arr = newarr;
2196af9eab45SVaclav Hapla     n = newn;
2197af9eab45SVaclav Hapla   }
2198ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
2199af9eab45SVaclav Hapla   *depth = depth_;
2200af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
2201af9eab45SVaclav Hapla   else {
2202af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
2203af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
2204af9eab45SVaclav Hapla   }
2205af9eab45SVaclav Hapla   if (sections) *sections = sections_;
2206af9eab45SVaclav Hapla   else {
2207af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
2208af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
2209af9eab45SVaclav Hapla   }
2210af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2211af9eab45SVaclav Hapla }
2212af9eab45SVaclav Hapla 
2213af9eab45SVaclav Hapla /*@
2214af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
2215af9eab45SVaclav Hapla 
2216af9eab45SVaclav Hapla   Not collective
2217af9eab45SVaclav Hapla 
2218af9eab45SVaclav Hapla   Input Parameters:
2219af9eab45SVaclav Hapla + dm - The DMPlex
2220af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2221af9eab45SVaclav Hapla 
2222af9eab45SVaclav Hapla   Output Parameter:
2223af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2224af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
2225af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2226af9eab45SVaclav Hapla 
2227af9eab45SVaclav Hapla   Level: advanced
2228af9eab45SVaclav Hapla 
2229af9eab45SVaclav Hapla   Notes:
2230af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
2231af9eab45SVaclav Hapla 
2232af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2233af9eab45SVaclav Hapla @*/
2234af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2235af9eab45SVaclav Hapla {
2236af9eab45SVaclav Hapla   PetscInt            d, depth_;
2237af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2238af9eab45SVaclav Hapla 
2239af9eab45SVaclav Hapla   PetscFunctionBegin;
2240af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2241af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
2242af9eab45SVaclav Hapla   if (depth) *depth = 0;
2243af9eab45SVaclav Hapla   if (expandedPoints) {
2244af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
2245af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
2246af9eab45SVaclav Hapla   }
2247af9eab45SVaclav Hapla   if (sections)  {
2248af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
2249af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
2250af9eab45SVaclav Hapla   }
2251d4636a37SVaclav Hapla   PetscFunctionReturn(0);
2252d4636a37SVaclav Hapla }
2253d4636a37SVaclav Hapla 
2254552f7358SJed Brown /*@
225592371b87SBarry 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
2256552f7358SJed Brown 
2257552f7358SJed Brown   Not collective
2258552f7358SJed Brown 
2259552f7358SJed Brown   Input Parameters:
2260552f7358SJed Brown + mesh - The DMPlex
2261eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2262552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
2263552f7358SJed Brown 
2264552f7358SJed Brown   Output Parameter:
2265552f7358SJed Brown 
2266552f7358SJed Brown   Note:
2267552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2268552f7358SJed Brown 
226992371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
227092371b87SBarry Smith 
2271552f7358SJed Brown   Level: beginner
2272552f7358SJed Brown 
227392371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
2274552f7358SJed Brown @*/
2275552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
2276552f7358SJed Brown {
2277552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2278552f7358SJed Brown   PetscInt       pStart, pEnd;
2279552f7358SJed Brown   PetscInt       dof, off, c;
2280552f7358SJed Brown   PetscErrorCode ierr;
2281552f7358SJed Brown 
2282552f7358SJed Brown   PetscFunctionBegin;
2283552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2284552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2285552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2286552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
2287552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
228882f516ccSBarry 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);
2289552f7358SJed Brown   for (c = 0; c < dof; ++c) {
229082f516ccSBarry 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);
2291552f7358SJed Brown     mesh->cones[off+c] = cone[c];
2292552f7358SJed Brown   }
2293552f7358SJed Brown   PetscFunctionReturn(0);
2294552f7358SJed Brown }
2295552f7358SJed Brown 
2296552f7358SJed Brown /*@C
2297eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
2298552f7358SJed Brown 
2299552f7358SJed Brown   Not collective
2300552f7358SJed Brown 
2301552f7358SJed Brown   Input Parameters:
2302552f7358SJed Brown + mesh - The DMPlex
2303eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2304552f7358SJed Brown 
2305552f7358SJed Brown   Output Parameter:
2306552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2307552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2308552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2309552f7358SJed Brown                     the index of the cone point on which to start.
2310552f7358SJed Brown 
2311552f7358SJed Brown   Level: beginner
2312552f7358SJed Brown 
23133813dfbdSMatthew G Knepley   Fortran Notes:
23143813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23153813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23163b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2317922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
23183813dfbdSMatthew G Knepley 
2319552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2320552f7358SJed Brown @*/
2321552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2322552f7358SJed Brown {
2323552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2324552f7358SJed Brown   PetscInt       off;
2325552f7358SJed Brown   PetscErrorCode ierr;
2326552f7358SJed Brown 
2327552f7358SJed Brown   PetscFunctionBegin;
2328552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
232976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2330552f7358SJed Brown     PetscInt dof;
2331552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2332552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2333552f7358SJed Brown   }
2334552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
23350d644c17SKarl Rupp 
2336552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2337552f7358SJed Brown   PetscFunctionReturn(0);
2338552f7358SJed Brown }
2339552f7358SJed Brown 
2340552f7358SJed Brown /*@
2341eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-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 - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2349552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2350552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2351552f7358SJed Brown                     the index of the cone point on which to start.
2352552f7358SJed Brown 
2353552f7358SJed Brown   Output Parameter:
2354552f7358SJed Brown 
2355552f7358SJed Brown   Note:
2356552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2357552f7358SJed Brown 
2358552f7358SJed Brown   Level: beginner
2359552f7358SJed Brown 
2360552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2361552f7358SJed Brown @*/
2362552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2363552f7358SJed Brown {
2364552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2365552f7358SJed Brown   PetscInt       pStart, pEnd;
2366552f7358SJed Brown   PetscInt       dof, off, c;
2367552f7358SJed Brown   PetscErrorCode ierr;
2368552f7358SJed Brown 
2369552f7358SJed Brown   PetscFunctionBegin;
2370552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2371552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2372552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2373552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2374552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
237582f516ccSBarry 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);
2376552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2377552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2378552f7358SJed Brown 
2379552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
238082f516ccSBarry 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);
2381552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2382552f7358SJed Brown   }
2383552f7358SJed Brown   PetscFunctionReturn(0);
2384552f7358SJed Brown }
2385552f7358SJed Brown 
23867cd05799SMatthew G. Knepley /*@
2387eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
23887cd05799SMatthew G. Knepley 
23897cd05799SMatthew G. Knepley   Not collective
23907cd05799SMatthew G. Knepley 
23917cd05799SMatthew G. Knepley   Input Parameters:
23927cd05799SMatthew G. Knepley + mesh - The DMPlex
2393eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
23947cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
23957cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
23967cd05799SMatthew G. Knepley 
23977cd05799SMatthew G. Knepley   Level: beginner
23987cd05799SMatthew G. Knepley 
23997cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
24007cd05799SMatthew G. Knepley @*/
2401552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2402552f7358SJed Brown {
2403552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2404552f7358SJed Brown   PetscInt       pStart, pEnd;
2405552f7358SJed Brown   PetscInt       dof, off;
2406552f7358SJed Brown   PetscErrorCode ierr;
2407552f7358SJed Brown 
2408552f7358SJed Brown   PetscFunctionBegin;
2409552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2410552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
241182f516ccSBarry 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);
241282f516ccSBarry 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);
241377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
241477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
241577c88f5bSMatthew 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);
2416552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2417552f7358SJed Brown   PetscFunctionReturn(0);
2418552f7358SJed Brown }
2419552f7358SJed Brown 
24207cd05799SMatthew G. Knepley /*@
2421eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
24227cd05799SMatthew G. Knepley 
24237cd05799SMatthew G. Knepley   Not collective
24247cd05799SMatthew G. Knepley 
24257cd05799SMatthew G. Knepley   Input Parameters:
24267cd05799SMatthew G. Knepley + mesh - The DMPlex
2427eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24287cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
24297cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
24307cd05799SMatthew G. Knepley 
24317cd05799SMatthew G. Knepley   Level: beginner
24327cd05799SMatthew G. Knepley 
24337cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
24347cd05799SMatthew G. Knepley @*/
243577c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
243677c88f5bSMatthew G Knepley {
243777c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
243877c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
243977c88f5bSMatthew G Knepley   PetscInt       dof, off;
244077c88f5bSMatthew G Knepley   PetscErrorCode ierr;
244177c88f5bSMatthew G Knepley 
244277c88f5bSMatthew G Knepley   PetscFunctionBegin;
244377c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
244477c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
244577c88f5bSMatthew 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);
244677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
244777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
244877c88f5bSMatthew 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);
244977c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
245077c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
245177c88f5bSMatthew G Knepley }
245277c88f5bSMatthew G Knepley 
2453552f7358SJed Brown /*@
2454eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2455552f7358SJed Brown 
2456552f7358SJed Brown   Not collective
2457552f7358SJed Brown 
2458552f7358SJed Brown   Input Parameters:
2459552f7358SJed Brown + mesh - The DMPlex
2460eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2461552f7358SJed Brown 
2462552f7358SJed Brown   Output Parameter:
2463552f7358SJed Brown . size - The support size for point p
2464552f7358SJed Brown 
2465552f7358SJed Brown   Level: beginner
2466552f7358SJed Brown 
2467552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2468552f7358SJed Brown @*/
2469552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2470552f7358SJed Brown {
2471552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2472552f7358SJed Brown   PetscErrorCode ierr;
2473552f7358SJed Brown 
2474552f7358SJed Brown   PetscFunctionBegin;
2475552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2476552f7358SJed Brown   PetscValidPointer(size, 3);
2477552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2478552f7358SJed Brown   PetscFunctionReturn(0);
2479552f7358SJed Brown }
2480552f7358SJed Brown 
2481552f7358SJed Brown /*@
2482eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2483552f7358SJed Brown 
2484552f7358SJed Brown   Not collective
2485552f7358SJed Brown 
2486552f7358SJed Brown   Input Parameters:
2487552f7358SJed Brown + mesh - The DMPlex
2488eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2489552f7358SJed Brown - size - The support size for point p
2490552f7358SJed Brown 
2491552f7358SJed Brown   Output Parameter:
2492552f7358SJed Brown 
2493552f7358SJed Brown   Note:
2494552f7358SJed Brown   This should be called after DMPlexSetChart().
2495552f7358SJed Brown 
2496552f7358SJed Brown   Level: beginner
2497552f7358SJed Brown 
2498552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2499552f7358SJed Brown @*/
2500552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2501552f7358SJed Brown {
2502552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2503552f7358SJed Brown   PetscErrorCode ierr;
2504552f7358SJed Brown 
2505552f7358SJed Brown   PetscFunctionBegin;
2506552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2507552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
25080d644c17SKarl Rupp 
2509552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2510552f7358SJed Brown   PetscFunctionReturn(0);
2511552f7358SJed Brown }
2512552f7358SJed Brown 
2513552f7358SJed Brown /*@C
2514eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2515552f7358SJed Brown 
2516552f7358SJed Brown   Not collective
2517552f7358SJed Brown 
2518552f7358SJed Brown   Input Parameters:
2519552f7358SJed Brown + mesh - The DMPlex
2520eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2521552f7358SJed Brown 
2522552f7358SJed Brown   Output Parameter:
2523552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2524552f7358SJed Brown 
2525552f7358SJed Brown   Level: beginner
2526552f7358SJed Brown 
25273813dfbdSMatthew G Knepley   Fortran Notes:
25283813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25293813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25303b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2531922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
25323813dfbdSMatthew G Knepley 
2533e45f02b0SMatthew G. Knepley .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
2534552f7358SJed Brown @*/
2535552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2536552f7358SJed Brown {
2537552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2538552f7358SJed Brown   PetscInt       off;
2539552f7358SJed Brown   PetscErrorCode ierr;
2540552f7358SJed Brown 
2541552f7358SJed Brown   PetscFunctionBegin;
2542552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2543552f7358SJed Brown   PetscValidPointer(support, 3);
2544552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2545552f7358SJed Brown   *support = &mesh->supports[off];
2546552f7358SJed Brown   PetscFunctionReturn(0);
2547552f7358SJed Brown }
2548552f7358SJed Brown 
2549552f7358SJed Brown /*@
255092371b87SBarry 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
2551552f7358SJed Brown 
2552552f7358SJed Brown   Not collective
2553552f7358SJed Brown 
2554552f7358SJed Brown   Input Parameters:
2555552f7358SJed Brown + mesh - The DMPlex
2556eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
255792371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2558552f7358SJed Brown 
2559552f7358SJed Brown   Output Parameter:
2560552f7358SJed Brown 
2561552f7358SJed Brown   Note:
2562552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2563552f7358SJed Brown 
2564552f7358SJed Brown   Level: beginner
2565552f7358SJed Brown 
256692371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2567552f7358SJed Brown @*/
2568552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2569552f7358SJed Brown {
2570552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2571552f7358SJed Brown   PetscInt       pStart, pEnd;
2572552f7358SJed Brown   PetscInt       dof, off, c;
2573552f7358SJed Brown   PetscErrorCode ierr;
2574552f7358SJed Brown 
2575552f7358SJed Brown   PetscFunctionBegin;
2576552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2577552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2578552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2579552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2580552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
258182f516ccSBarry 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);
2582552f7358SJed Brown   for (c = 0; c < dof; ++c) {
258382f516ccSBarry 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);
2584552f7358SJed Brown     mesh->supports[off+c] = support[c];
2585552f7358SJed Brown   }
2586552f7358SJed Brown   PetscFunctionReturn(0);
2587552f7358SJed Brown }
2588552f7358SJed Brown 
25897cd05799SMatthew G. Knepley /*@
2590eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
25917cd05799SMatthew G. Knepley 
25927cd05799SMatthew G. Knepley   Not collective
25937cd05799SMatthew G. Knepley 
25947cd05799SMatthew G. Knepley   Input Parameters:
25957cd05799SMatthew G. Knepley + mesh - The DMPlex
2596eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
25977cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
25987cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
25997cd05799SMatthew G. Knepley 
26007cd05799SMatthew G. Knepley   Level: beginner
26017cd05799SMatthew G. Knepley 
26027cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
26037cd05799SMatthew G. Knepley @*/
2604552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2605552f7358SJed Brown {
2606552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2607552f7358SJed Brown   PetscInt       pStart, pEnd;
2608552f7358SJed Brown   PetscInt       dof, off;
2609552f7358SJed Brown   PetscErrorCode ierr;
2610552f7358SJed Brown 
2611552f7358SJed Brown   PetscFunctionBegin;
2612552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2613552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2614552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2615552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
261682f516ccSBarry 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);
261782f516ccSBarry 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);
261882f516ccSBarry 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);
2619552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2620552f7358SJed Brown   PetscFunctionReturn(0);
2621552f7358SJed Brown }
2622552f7358SJed Brown 
2623552f7358SJed Brown /*@C
2624eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2625552f7358SJed Brown 
2626552f7358SJed Brown   Not collective
2627552f7358SJed Brown 
2628552f7358SJed Brown   Input Parameters:
2629552f7358SJed Brown + mesh - The DMPlex
2630eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2631552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
26320298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2633552f7358SJed Brown 
2634552f7358SJed Brown   Output Parameters:
2635552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2636552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2637552f7358SJed Brown 
2638552f7358SJed Brown   Note:
26390298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2640552f7358SJed Brown 
26413813dfbdSMatthew G Knepley   Fortran Notes:
26423813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26433813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26443813dfbdSMatthew G Knepley 
26453813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26463813dfbdSMatthew G Knepley 
2647552f7358SJed Brown   Level: beginner
2648552f7358SJed Brown 
2649552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2650552f7358SJed Brown @*/
2651552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2652552f7358SJed Brown {
2653552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2654552f7358SJed Brown   PetscInt       *closure, *fifo;
26550298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2656552f7358SJed Brown   PetscInt        tmpSize, t;
2657552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2658552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2659552f7358SJed Brown   PetscErrorCode  ierr;
2660552f7358SJed Brown 
2661552f7358SJed Brown   PetscFunctionBegin;
2662552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2663552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2664552f7358SJed Brown   /* This is only 1-level */
2665552f7358SJed Brown   if (useCone) {
2666552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2667552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2668552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2669552f7358SJed Brown   } else {
2670552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2671552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2672552f7358SJed Brown   }
2673bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2674bfbcdd7aSMatthew G. Knepley     if (*points) {
2675bfbcdd7aSMatthew G. Knepley       closure = *points;
2676bfbcdd7aSMatthew G. Knepley     } else {
2677bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
267869291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2679bfbcdd7aSMatthew G. Knepley     }
2680bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2681bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2682bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2683bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2684bfbcdd7aSMatthew G. Knepley     }
2685bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2686bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2687bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2688bfbcdd7aSMatthew G. Knepley   }
268924c766afSToby Isaac   {
269024c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
269124c766afSToby Isaac 
269224c766afSToby Isaac     c = mesh->maxConeSize;
269324c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
269424c766afSToby Isaac     s = mesh->maxSupportSize;
269524c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
269624c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
269724c766afSToby Isaac   }
269869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2699bfbcdd7aSMatthew G. Knepley   if (*points) {
2700bfbcdd7aSMatthew G. Knepley     closure = *points;
2701bfbcdd7aSMatthew G. Knepley   } else {
270269291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2703bfbcdd7aSMatthew G. Knepley   }
2704bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2705552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2706552f7358SJed Brown     const PetscInt cp = tmp[t];
2707552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2708552f7358SJed Brown 
2709552f7358SJed Brown     closure[closureSize]   = cp;
2710552f7358SJed Brown     closure[closureSize+1] = co;
2711552f7358SJed Brown     fifo[fifoSize]         = cp;
2712552f7358SJed Brown     fifo[fifoSize+1]       = co;
2713552f7358SJed Brown   }
2714bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2715552f7358SJed Brown   while (fifoSize - fifoStart) {
2716552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2717552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2718552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2719552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2720552f7358SJed Brown 
2721552f7358SJed Brown     if (useCone) {
2722552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2723552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2724552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2725552f7358SJed Brown     } else {
2726552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2727552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
27280298fd71SBarry Smith       tmpO = NULL;
2729552f7358SJed Brown     }
2730552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2731552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2732552f7358SJed Brown       const PetscInt cp = tmp[i];
27332e1b13c2SMatthew 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. */
27342e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
27352e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
27362e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2737552f7358SJed Brown       PetscInt       c;
2738552f7358SJed Brown 
27392e1b13c2SMatthew G. Knepley       if (rev) {
27402e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
27412e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
27422e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
27432e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
27442e1b13c2SMatthew G. Knepley       }
2745552f7358SJed Brown       /* Check for duplicate */
2746552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2747552f7358SJed Brown         if (closure[c] == cp) break;
2748552f7358SJed Brown       }
2749552f7358SJed Brown       if (c == closureSize) {
2750552f7358SJed Brown         closure[closureSize]   = cp;
2751552f7358SJed Brown         closure[closureSize+1] = co;
2752552f7358SJed Brown         fifo[fifoSize]         = cp;
2753552f7358SJed Brown         fifo[fifoSize+1]       = co;
2754552f7358SJed Brown         closureSize           += 2;
2755552f7358SJed Brown         fifoSize              += 2;
2756552f7358SJed Brown       }
2757552f7358SJed Brown     }
2758552f7358SJed Brown     fifoStart += 2;
2759552f7358SJed Brown   }
2760552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2761552f7358SJed Brown   if (points)    *points    = closure;
276269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2763552f7358SJed Brown   PetscFunctionReturn(0);
2764552f7358SJed Brown }
2765552f7358SJed Brown 
27669bf0dad6SMatthew G. Knepley /*@C
2767eaf898f9SPatrick 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
27689bf0dad6SMatthew G. Knepley 
27699bf0dad6SMatthew G. Knepley   Not collective
27709bf0dad6SMatthew G. Knepley 
27719bf0dad6SMatthew G. Knepley   Input Parameters:
27729bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2773eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
27749bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
27759bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
27769bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
27779bf0dad6SMatthew G. Knepley 
27789bf0dad6SMatthew G. Knepley   Output Parameters:
27799bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
27809bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
27819bf0dad6SMatthew G. Knepley 
27829bf0dad6SMatthew G. Knepley   Note:
27839bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
27849bf0dad6SMatthew G. Knepley 
27859bf0dad6SMatthew G. Knepley   Fortran Notes:
27869bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27879bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
27889bf0dad6SMatthew G. Knepley 
27899bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27909bf0dad6SMatthew G. Knepley 
27919bf0dad6SMatthew G. Knepley   Level: beginner
27929bf0dad6SMatthew G. Knepley 
27939bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
27949bf0dad6SMatthew G. Knepley @*/
27959bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
27969bf0dad6SMatthew G. Knepley {
27979bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
27989bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
27999bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
28009bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
28019bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
28029bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
28039bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
28049bf0dad6SMatthew G. Knepley 
28059bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
28069bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28079bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
28089bf0dad6SMatthew G. Knepley   /* This is only 1-level */
28099bf0dad6SMatthew G. Knepley   if (useCone) {
28109bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
28119bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
28129bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
28139bf0dad6SMatthew G. Knepley   } else {
28149bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
28159bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
28169bf0dad6SMatthew G. Knepley   }
28179bf0dad6SMatthew G. Knepley   if (depth == 1) {
28189bf0dad6SMatthew G. Knepley     if (*points) {
28199bf0dad6SMatthew G. Knepley       closure = *points;
28209bf0dad6SMatthew G. Knepley     } else {
28219bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
282269291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
28239bf0dad6SMatthew G. Knepley     }
28249bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
28259bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
28269bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
28279bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
28289bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
28299bf0dad6SMatthew G. Knepley     }
28309bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
28319bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
28329bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
28339bf0dad6SMatthew G. Knepley   }
283424c766afSToby Isaac   {
283524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
283624c766afSToby Isaac 
283724c766afSToby Isaac     c = mesh->maxConeSize;
283824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
283924c766afSToby Isaac     s = mesh->maxSupportSize;
284024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
284124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
284224c766afSToby Isaac   }
284369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
28449bf0dad6SMatthew G. Knepley   if (*points) {
28459bf0dad6SMatthew G. Knepley     closure = *points;
28469bf0dad6SMatthew G. Knepley   } else {
284769291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
28489bf0dad6SMatthew G. Knepley   }
28499bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
28509bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
28519bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
28529bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
28539bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
28549bf0dad6SMatthew G. Knepley 
28559bf0dad6SMatthew G. Knepley     if (ornt < 0) {
28569bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
28579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
285886b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
28599bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
28609bf0dad6SMatthew G. Knepley     }
28619bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
28629bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
28639bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
28649bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
28659bf0dad6SMatthew G. Knepley   }
28669bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
28679bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
28689bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
28699bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
28709bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
28719bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
28729bf0dad6SMatthew G. Knepley 
28739bf0dad6SMatthew G. Knepley     if (useCone) {
28749bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
28759bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
28769bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
28779bf0dad6SMatthew G. Knepley     } else {
28789bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
28799bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
28809bf0dad6SMatthew G. Knepley       tmpO = NULL;
28819bf0dad6SMatthew G. Knepley     }
28829bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
28839bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
28849bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
28859bf0dad6SMatthew 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. */
28869bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
28879bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
28889bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
28899bf0dad6SMatthew G. Knepley       PetscInt       c;
28909bf0dad6SMatthew G. Knepley 
28919bf0dad6SMatthew G. Knepley       if (rev) {
28929bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
28939bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
28949bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
28959bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
28969bf0dad6SMatthew G. Knepley       }
28979bf0dad6SMatthew G. Knepley       /* Check for duplicate */
28989bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
28999bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
29009bf0dad6SMatthew G. Knepley       }
29019bf0dad6SMatthew G. Knepley       if (c == closureSize) {
29029bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
29039bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
29049bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
29059bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
29069bf0dad6SMatthew G. Knepley         closureSize           += 2;
29079bf0dad6SMatthew G. Knepley         fifoSize              += 2;
29089bf0dad6SMatthew G. Knepley       }
29099bf0dad6SMatthew G. Knepley     }
29109bf0dad6SMatthew G. Knepley     fifoStart += 2;
29119bf0dad6SMatthew G. Knepley   }
29129bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
29139bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
291469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
29159bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
29169bf0dad6SMatthew G. Knepley }
29179bf0dad6SMatthew G. Knepley 
2918552f7358SJed Brown /*@C
2919eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2920552f7358SJed Brown 
2921552f7358SJed Brown   Not collective
2922552f7358SJed Brown 
2923552f7358SJed Brown   Input Parameters:
2924552f7358SJed Brown + mesh - The DMPlex
2925eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2926552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2927e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2928e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2929552f7358SJed Brown 
2930552f7358SJed Brown   Note:
29310298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2932552f7358SJed Brown 
29333813dfbdSMatthew G Knepley   Fortran Notes:
29343813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29353813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29363813dfbdSMatthew G Knepley 
29373813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29383813dfbdSMatthew G Knepley 
2939552f7358SJed Brown   Level: beginner
2940552f7358SJed Brown 
2941552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2942552f7358SJed Brown @*/
2943552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2944552f7358SJed Brown {
2945552f7358SJed Brown   PetscErrorCode ierr;
2946552f7358SJed Brown 
2947552f7358SJed Brown   PetscFunctionBegin;
2948552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2949e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2950e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
295169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
29524ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2953552f7358SJed Brown   PetscFunctionReturn(0);
2954552f7358SJed Brown }
2955552f7358SJed Brown 
2956552f7358SJed Brown /*@
2957eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2958552f7358SJed Brown 
2959552f7358SJed Brown   Not collective
2960552f7358SJed Brown 
2961552f7358SJed Brown   Input Parameter:
2962552f7358SJed Brown . mesh - The DMPlex
2963552f7358SJed Brown 
2964552f7358SJed Brown   Output Parameters:
2965552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2966552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2967552f7358SJed Brown 
2968552f7358SJed Brown   Level: beginner
2969552f7358SJed Brown 
2970552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2971552f7358SJed Brown @*/
2972552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2973552f7358SJed Brown {
2974552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2975552f7358SJed Brown 
2976552f7358SJed Brown   PetscFunctionBegin;
2977552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2978552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2979552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2980552f7358SJed Brown   PetscFunctionReturn(0);
2981552f7358SJed Brown }
2982552f7358SJed Brown 
2983552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2984552f7358SJed Brown {
2985552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2986552f7358SJed Brown   PetscInt       size;
2987552f7358SJed Brown   PetscErrorCode ierr;
2988552f7358SJed Brown 
2989552f7358SJed Brown   PetscFunctionBegin;
2990552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2991552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2992552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
29931795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
29941795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2995ef1259faSMatthew G. Knepley   ierr = PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));CHKERRQ(ierr);
2996552f7358SJed Brown   if (mesh->maxSupportSize) {
2997552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2998552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
29991795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
3000ef1259faSMatthew G. Knepley     ierr = PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));CHKERRQ(ierr);
3001552f7358SJed Brown   }
3002552f7358SJed Brown   PetscFunctionReturn(0);
3003552f7358SJed Brown }
3004552f7358SJed Brown 
3005276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3006552f7358SJed Brown {
3007552f7358SJed Brown   PetscErrorCode ierr;
3008552f7358SJed Brown 
3009552f7358SJed Brown   PetscFunctionBegin;
30104d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
3011792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
3012c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
3013736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
3014f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
3015f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
3016f94b4a02SBlaise Bourdin 
30173dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
30183dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
3019c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
3020f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
3021f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
3022f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
3023f94b4a02SBlaise Bourdin 
3024f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3025c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
3026f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
3027f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
3028f94b4a02SBlaise Bourdin   }
3029552f7358SJed Brown   PetscFunctionReturn(0);
3030552f7358SJed Brown }
3031552f7358SJed Brown 
30322adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
30332adcc780SMatthew G. Knepley {
30342adcc780SMatthew G. Knepley   PetscErrorCode ierr;
30353dcd263cSBlaise Bourdin   PetscInt       i = 0;
30362adcc780SMatthew G. Knepley 
30372adcc780SMatthew G. Knepley   PetscFunctionBegin;
3038435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
3039792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
3040c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
30413dcd263cSBlaise Bourdin   for (i = 0; i < len; i++) {
30423dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
30433dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
30443dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
30453dcd263cSBlaise Bourdin 
30463dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
30473dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
3048c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
304992fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
30503dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
30513dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
30523dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
30533dcd263cSBlaise Bourdin 
30543dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3055c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
30563dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
30573dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
30583dcd263cSBlaise Bourdin       break;
30593dcd263cSBlaise Bourdin     }
30603dcd263cSBlaise Bourdin   }
30612adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
30622adcc780SMatthew G. Knepley }
30632adcc780SMatthew G. Knepley 
3064552f7358SJed Brown /*@
3065eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
3066552f7358SJed Brown 
3067552f7358SJed Brown   Not collective
3068552f7358SJed Brown 
3069552f7358SJed Brown   Input Parameter:
3070552f7358SJed Brown . mesh - The DMPlex
3071552f7358SJed Brown 
3072552f7358SJed Brown   Output Parameter:
3073552f7358SJed Brown 
3074552f7358SJed Brown   Note:
3075552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
3076552f7358SJed Brown 
3077552f7358SJed Brown   Level: beginner
3078552f7358SJed Brown 
3079552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
3080552f7358SJed Brown @*/
3081552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
3082552f7358SJed Brown {
3083552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3084552f7358SJed Brown   PetscInt      *offsets;
3085552f7358SJed Brown   PetscInt       supportSize;
3086552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3087552f7358SJed Brown   PetscErrorCode ierr;
3088552f7358SJed Brown 
3089552f7358SJed Brown   PetscFunctionBegin;
3090552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
309182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
309230b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3093552f7358SJed Brown   /* Calculate support sizes */
3094552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3095552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3096552f7358SJed Brown     PetscInt dof, off, c;
3097552f7358SJed Brown 
3098552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3099552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3100552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3101552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
3102552f7358SJed Brown     }
3103552f7358SJed Brown   }
3104552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3105552f7358SJed Brown     PetscInt dof;
3106552f7358SJed Brown 
3107552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
31080d644c17SKarl Rupp 
3109552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
3110552f7358SJed Brown   }
3111552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
3112552f7358SJed Brown   /* Calculate supports */
3113552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
31141795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
31151795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
3116552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3117552f7358SJed Brown     PetscInt dof, off, c;
3118552f7358SJed Brown 
3119552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3120552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3121552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3122552f7358SJed Brown       const PetscInt q = mesh->cones[c];
3123552f7358SJed Brown       PetscInt       offS;
3124552f7358SJed Brown 
3125552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
31260d644c17SKarl Rupp 
3127552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
3128552f7358SJed Brown       ++offsets[q];
3129552f7358SJed Brown     }
3130552f7358SJed Brown   }
3131552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
313230b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3133552f7358SJed Brown   PetscFunctionReturn(0);
3134552f7358SJed Brown }
3135552f7358SJed Brown 
3136277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3137277ea44aSLisandro Dalcin {
3138277ea44aSLisandro Dalcin   IS             stratumIS;
3139277ea44aSLisandro Dalcin   PetscErrorCode ierr;
3140277ea44aSLisandro Dalcin 
3141277ea44aSLisandro Dalcin   PetscFunctionBegin;
3142277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
314376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
3144277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
3145277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
3146277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
3147277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
3148277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3149277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
3150277ea44aSLisandro Dalcin     }
3151277ea44aSLisandro 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);
3152277ea44aSLisandro Dalcin   }
3153277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
3154277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
3155277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
3156277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
3157277ea44aSLisandro Dalcin }
3158277ea44aSLisandro Dalcin 
3159552f7358SJed Brown /*@
3160a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
31616dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
3162552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
3163552f7358SJed Brown   the DAG.
3164552f7358SJed Brown 
3165bf4602e4SToby Isaac   Collective on dm
3166552f7358SJed Brown 
3167552f7358SJed Brown   Input Parameter:
3168552f7358SJed Brown . mesh - The DMPlex
3169552f7358SJed Brown 
3170552f7358SJed Brown   Output Parameter:
3171552f7358SJed Brown 
3172552f7358SJed Brown   Notes:
3173b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3174b1bb481bSMatthew 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
3175b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
3176c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
3177150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
3178552f7358SJed Brown 
3179b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3180b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3181b1bb481bSMatthew 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
3182b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
3183b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
3184b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
3185b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
3186b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
3187b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
3188b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
3189b1bb481bSMatthew Knepley 
3190150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
3191552f7358SJed Brown 
3192552f7358SJed Brown   Level: beginner
3193552f7358SJed Brown 
3194ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
3195552f7358SJed Brown @*/
3196552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
3197552f7358SJed Brown {
3198df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3199aa50250dSMatthew G. Knepley   DMLabel        label;
3200552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3201552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
3202552f7358SJed Brown   PetscErrorCode ierr;
3203552f7358SJed Brown 
3204552f7358SJed Brown   PetscFunctionBegin;
3205552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3206552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3207277ea44aSLisandro Dalcin 
3208277ea44aSLisandro Dalcin   /* Create depth label */
3209aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3210c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
3211aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3212277ea44aSLisandro Dalcin 
3213277ea44aSLisandro Dalcin   {
3214552f7358SJed Brown     /* Initialize roots and count leaves */
3215277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3216277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3217552f7358SJed Brown     PetscInt coneSize, supportSize;
3218552f7358SJed Brown 
3219277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3220552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3221552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3222552f7358SJed Brown       if (!coneSize && supportSize) {
3223277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3224277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3225552f7358SJed Brown         ++numRoots;
3226552f7358SJed Brown       } else if (!supportSize && coneSize) {
3227552f7358SJed Brown         ++numLeaves;
3228552f7358SJed Brown       } else if (!supportSize && !coneSize) {
3229552f7358SJed Brown         /* Isolated points */
3230277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3231277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3232552f7358SJed Brown       }
3233552f7358SJed Brown     }
3234277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
3235277ea44aSLisandro Dalcin   }
3236277ea44aSLisandro Dalcin 
3237552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
3238277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3239277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3240552f7358SJed Brown     PetscInt coneSize, supportSize;
3241552f7358SJed Brown 
3242277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3243552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3244552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3245552f7358SJed Brown       if (!supportSize && coneSize) {
3246277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3247277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3248552f7358SJed Brown       }
3249552f7358SJed Brown     }
3250277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
3251552f7358SJed Brown   } else {
3252277ea44aSLisandro Dalcin     PetscInt level = 0;
3253277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
3254552f7358SJed Brown 
3255277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3256277ea44aSLisandro Dalcin     while (qEnd > qStart) {
3257277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
3258277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
325974ef644bSMatthew G. Knepley 
3260277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
326174ef644bSMatthew G. Knepley         const PetscInt *support;
326274ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
326374ef644bSMatthew G. Knepley 
3264277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
3265277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
326674ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3267277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
3268277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
3269552f7358SJed Brown         }
3270552f7358SJed Brown       }
3271277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
3272277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
3273277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
327474ef644bSMatthew G. Knepley     }
327574ef644bSMatthew G. Knepley   }
3276bf4602e4SToby Isaac   { /* just in case there is an empty process */
3277bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
3278bf4602e4SToby Isaac 
3279bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
3280ffc4695bSBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
3281bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
3282367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
3283bf4602e4SToby Isaac     }
3284bf4602e4SToby Isaac   }
3285d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
3286552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3287552f7358SJed Brown   PetscFunctionReturn(0);
3288552f7358SJed Brown }
3289552f7358SJed Brown 
3290412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
3291ba2698f1SMatthew G. Knepley {
3292412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3293412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
3294ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3295ba2698f1SMatthew G. Knepley 
3296412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
3297ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3298ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3299ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3300ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
3301ba2698f1SMatthew G. Knepley   if (depth <= 1) {
3302ba2698f1SMatthew G. Knepley     switch (pdepth) {
3303ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
3304ba2698f1SMatthew G. Knepley       case 1:
3305ba2698f1SMatthew G. Knepley         switch (coneSize) {
3306ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3307ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3308ba2698f1SMatthew G. Knepley           case 4:
3309ba2698f1SMatthew G. Knepley           switch (dim) {
3310ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3311ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3312ba2698f1SMatthew G. Knepley             default: break;
3313ba2698f1SMatthew G. Knepley           }
3314ba2698f1SMatthew G. Knepley           break;
3315da9060c4SMatthew G. Knepley         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3316ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3317ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3318ba2698f1SMatthew G. Knepley         default: break;
3319ba2698f1SMatthew G. Knepley       }
3320ba2698f1SMatthew G. Knepley     }
3321ba2698f1SMatthew G. Knepley   } else {
3322ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
3323ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
3324ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
3325ba2698f1SMatthew G. Knepley       switch (dim) {
3326ba2698f1SMatthew G. Knepley         case 1:
3327ba2698f1SMatthew G. Knepley           switch (coneSize) {
3328ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3329ba2698f1SMatthew G. Knepley             default: break;
3330ba2698f1SMatthew G. Knepley           }
3331ba2698f1SMatthew G. Knepley           break;
3332ba2698f1SMatthew G. Knepley         case 2:
3333ba2698f1SMatthew G. Knepley           switch (coneSize) {
3334ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3335ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3336ba2698f1SMatthew G. Knepley             default: break;
3337ba2698f1SMatthew G. Knepley           }
3338ba2698f1SMatthew G. Knepley           break;
3339ba2698f1SMatthew G. Knepley         case 3:
3340ba2698f1SMatthew G. Knepley           switch (coneSize) {
3341ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3342da9060c4SMatthew G. Knepley             case 5:
3343da9060c4SMatthew G. Knepley             {
3344da9060c4SMatthew G. Knepley               const PetscInt *cone;
3345da9060c4SMatthew G. Knepley               PetscInt        faceConeSize;
3346da9060c4SMatthew G. Knepley 
3347da9060c4SMatthew G. Knepley               ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
3348da9060c4SMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[0], &faceConeSize);CHKERRQ(ierr);
3349da9060c4SMatthew G. Knepley               switch (faceConeSize) {
3350da9060c4SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3351da9060c4SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
3352da9060c4SMatthew G. Knepley               }
3353da9060c4SMatthew G. Knepley             }
3354da9060c4SMatthew G. Knepley             break;
3355ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3356ba2698f1SMatthew G. Knepley             default: break;
3357ba2698f1SMatthew G. Knepley           }
3358ba2698f1SMatthew G. Knepley           break;
3359ba2698f1SMatthew G. Knepley         default: break;
3360ba2698f1SMatthew G. Knepley       }
3361ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
3362ba2698f1SMatthew G. Knepley       switch (coneSize) {
3363ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3364ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3365ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3366ba2698f1SMatthew G. Knepley         default: break;
3367ba2698f1SMatthew G. Knepley       }
3368ba2698f1SMatthew G. Knepley     }
3369ba2698f1SMatthew G. Knepley   }
3370412e9a14SMatthew G. Knepley   *pt = ct;
3371412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3372ba2698f1SMatthew G. Knepley }
3373412e9a14SMatthew G. Knepley 
3374412e9a14SMatthew G. Knepley /*@
3375412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3376412e9a14SMatthew G. Knepley 
3377412e9a14SMatthew G. Knepley   Collective on dm
3378412e9a14SMatthew G. Knepley 
3379412e9a14SMatthew G. Knepley   Input Parameter:
3380412e9a14SMatthew G. Knepley . mesh - The DMPlex
3381412e9a14SMatthew G. Knepley 
3382412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3383412e9a14SMatthew G. Knepley 
3384412e9a14SMatthew G. Knepley   Level: developer
3385412e9a14SMatthew G. Knepley 
3386412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3387412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3388412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3389412e9a14SMatthew G. Knepley 
3390412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3391412e9a14SMatthew G. Knepley @*/
3392412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3393412e9a14SMatthew G. Knepley {
3394412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3395412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3396412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3397412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3398412e9a14SMatthew G. Knepley 
3399412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3400412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3401412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3402412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3403412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3404412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3405412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3406327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3407412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3408412e9a14SMatthew G. Knepley 
3409412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3410412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3411412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3412412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3413412e9a14SMatthew G. Knepley   }
3414412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3415412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3416ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3417ba2698f1SMatthew G. Knepley }
3418ba2698f1SMatthew G. Knepley 
3419552f7358SJed Brown /*@C
3420552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3421552f7358SJed Brown 
3422552f7358SJed Brown   Not Collective
3423552f7358SJed Brown 
3424552f7358SJed Brown   Input Parameters:
3425552f7358SJed Brown + dm - The DMPlex object
3426552f7358SJed Brown . numPoints - The number of input points for the join
3427552f7358SJed Brown - points - The input points
3428552f7358SJed Brown 
3429552f7358SJed Brown   Output Parameters:
3430552f7358SJed Brown + numCoveredPoints - The number of points in the join
3431552f7358SJed Brown - coveredPoints - The points in the join
3432552f7358SJed Brown 
3433552f7358SJed Brown   Level: intermediate
3434552f7358SJed Brown 
3435552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3436552f7358SJed Brown 
34373813dfbdSMatthew G Knepley   Fortran Notes:
34383813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34393813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34403813dfbdSMatthew G Knepley 
34413813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34423813dfbdSMatthew G Knepley 
3443552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3444552f7358SJed Brown @*/
3445552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3446552f7358SJed Brown {
3447552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3448552f7358SJed Brown   PetscInt      *join[2];
3449552f7358SJed Brown   PetscInt       joinSize, i = 0;
3450552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3451552f7358SJed Brown   PetscErrorCode ierr;
3452552f7358SJed Brown 
3453552f7358SJed Brown   PetscFunctionBegin;
3454552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
345548bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
345648bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
345748bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
345869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
345969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3460552f7358SJed Brown   /* Copy in support of first point */
3461552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3462552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3463552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3464552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3465552f7358SJed Brown   }
3466552f7358SJed Brown   /* Check each successive support */
3467552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3468552f7358SJed Brown     PetscInt newJoinSize = 0;
3469552f7358SJed Brown 
3470552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3471552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3472552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3473552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3474552f7358SJed Brown 
3475552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3476552f7358SJed Brown         if (point == join[i][m]) {
3477552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3478552f7358SJed Brown           break;
3479552f7358SJed Brown         }
3480552f7358SJed Brown       }
3481552f7358SJed Brown     }
3482552f7358SJed Brown     joinSize = newJoinSize;
3483552f7358SJed Brown     i        = 1-i;
3484552f7358SJed Brown   }
3485552f7358SJed Brown   *numCoveredPoints = joinSize;
3486552f7358SJed Brown   *coveredPoints    = join[i];
348769291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3488552f7358SJed Brown   PetscFunctionReturn(0);
3489552f7358SJed Brown }
3490552f7358SJed Brown 
3491552f7358SJed Brown /*@C
3492552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3493552f7358SJed Brown 
3494552f7358SJed Brown   Not Collective
3495552f7358SJed Brown 
3496552f7358SJed Brown   Input Parameters:
3497552f7358SJed Brown + dm - The DMPlex object
3498552f7358SJed Brown . numPoints - The number of input points for the join
3499552f7358SJed Brown - points - The input points
3500552f7358SJed Brown 
3501552f7358SJed Brown   Output Parameters:
3502552f7358SJed Brown + numCoveredPoints - The number of points in the join
3503552f7358SJed Brown - coveredPoints - The points in the join
3504552f7358SJed Brown 
35053813dfbdSMatthew G Knepley   Fortran Notes:
35063813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35073813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35083813dfbdSMatthew G Knepley 
35093813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35103813dfbdSMatthew G Knepley 
3511552f7358SJed Brown   Level: intermediate
3512552f7358SJed Brown 
3513552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3514552f7358SJed Brown @*/
3515552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3516552f7358SJed Brown {
3517552f7358SJed Brown   PetscErrorCode ierr;
3518552f7358SJed Brown 
3519552f7358SJed Brown   PetscFunctionBegin;
3520552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3521d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3522d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3523d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
352469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3525d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3526552f7358SJed Brown   PetscFunctionReturn(0);
3527552f7358SJed Brown }
3528552f7358SJed Brown 
3529552f7358SJed Brown /*@C
3530552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3531552f7358SJed Brown 
3532552f7358SJed Brown   Not Collective
3533552f7358SJed Brown 
3534552f7358SJed Brown   Input Parameters:
3535552f7358SJed Brown + dm - The DMPlex object
3536552f7358SJed Brown . numPoints - The number of input points for the join
3537552f7358SJed Brown - points - The input points
3538552f7358SJed Brown 
3539552f7358SJed Brown   Output Parameters:
3540552f7358SJed Brown + numCoveredPoints - The number of points in the join
3541552f7358SJed Brown - coveredPoints - The points in the join
3542552f7358SJed Brown 
35433813dfbdSMatthew G Knepley   Fortran Notes:
35443813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35453813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35463813dfbdSMatthew G Knepley 
35473813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35483813dfbdSMatthew G Knepley 
3549552f7358SJed Brown   Level: intermediate
3550552f7358SJed Brown 
3551552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3552552f7358SJed Brown @*/
3553552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3554552f7358SJed Brown {
3555552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3556552f7358SJed Brown   PetscInt      *offsets, **closures;
3557552f7358SJed Brown   PetscInt      *join[2];
3558552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
355924c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3560552f7358SJed Brown   PetscErrorCode ierr;
3561552f7358SJed Brown 
3562552f7358SJed Brown   PetscFunctionBegin;
3563552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
356448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
356548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
356648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3567552f7358SJed Brown 
3568552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
35691795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
357069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
357124c766afSToby Isaac   ms      = mesh->maxSupportSize;
357224c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
357369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
357469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3575552f7358SJed Brown 
3576552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3577552f7358SJed Brown     PetscInt closureSize;
3578552f7358SJed Brown 
3579552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
35800d644c17SKarl Rupp 
3581552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3582552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3583552f7358SJed Brown       PetscInt pStart, pEnd, i;
3584552f7358SJed Brown 
3585552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3586552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3587552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3588552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3589552f7358SJed Brown           break;
3590552f7358SJed Brown         }
3591552f7358SJed Brown       }
3592552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3593552f7358SJed Brown     }
359482f516ccSBarry 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);
3595552f7358SJed Brown   }
3596552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3597552f7358SJed Brown     PetscInt dof;
3598552f7358SJed Brown 
3599552f7358SJed Brown     /* Copy in support of first point */
3600552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3601552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3602552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3603552f7358SJed Brown     }
3604552f7358SJed Brown     /* Check each successive cone */
3605552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3606552f7358SJed Brown       PetscInt newJoinSize = 0;
3607552f7358SJed Brown 
3608552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3609552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3610552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3611552f7358SJed Brown 
3612552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3613552f7358SJed Brown           if (point == join[i][m]) {
3614552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3615552f7358SJed Brown             break;
3616552f7358SJed Brown           }
3617552f7358SJed Brown         }
3618552f7358SJed Brown       }
3619552f7358SJed Brown       joinSize = newJoinSize;
3620552f7358SJed Brown       i        = 1-i;
3621552f7358SJed Brown     }
3622552f7358SJed Brown     if (joinSize) break;
3623552f7358SJed Brown   }
3624552f7358SJed Brown   *numCoveredPoints = joinSize;
3625552f7358SJed Brown   *coveredPoints    = join[i];
3626552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
36270298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3628552f7358SJed Brown   }
3629552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
363069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
363169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3632552f7358SJed Brown   PetscFunctionReturn(0);
3633552f7358SJed Brown }
3634552f7358SJed Brown 
3635552f7358SJed Brown /*@C
3636552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3637552f7358SJed Brown 
3638552f7358SJed Brown   Not Collective
3639552f7358SJed Brown 
3640552f7358SJed Brown   Input Parameters:
3641552f7358SJed Brown + dm - The DMPlex object
3642552f7358SJed Brown . numPoints - The number of input points for the meet
3643552f7358SJed Brown - points - The input points
3644552f7358SJed Brown 
3645552f7358SJed Brown   Output Parameters:
3646552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3647552f7358SJed Brown - coveredPoints - The points in the meet
3648552f7358SJed Brown 
3649552f7358SJed Brown   Level: intermediate
3650552f7358SJed Brown 
3651552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3652552f7358SJed Brown 
36533813dfbdSMatthew G Knepley   Fortran Notes:
36543813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
36553813dfbdSMatthew G Knepley   include petsc.h90 in your code.
36563813dfbdSMatthew G Knepley 
36573813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
36583813dfbdSMatthew G Knepley 
3659552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3660552f7358SJed Brown @*/
3661552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3662552f7358SJed Brown {
3663552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3664552f7358SJed Brown   PetscInt      *meet[2];
3665552f7358SJed Brown   PetscInt       meetSize, i = 0;
3666552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3667552f7358SJed Brown   PetscErrorCode ierr;
3668552f7358SJed Brown 
3669552f7358SJed Brown   PetscFunctionBegin;
3670552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3671064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
3672064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveringPoints, 4);
3673064a246eSJacob Faibussowitsch   PetscValidPointer(coveringPoints, 5);
367469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
367569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3676552f7358SJed Brown   /* Copy in cone of first point */
3677552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3678552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3679552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3680552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3681552f7358SJed Brown   }
3682552f7358SJed Brown   /* Check each successive cone */
3683552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3684552f7358SJed Brown     PetscInt newMeetSize = 0;
3685552f7358SJed Brown 
3686552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3687552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3688552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3689552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3690552f7358SJed Brown 
3691552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3692552f7358SJed Brown         if (point == meet[i][m]) {
3693552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3694552f7358SJed Brown           break;
3695552f7358SJed Brown         }
3696552f7358SJed Brown       }
3697552f7358SJed Brown     }
3698552f7358SJed Brown     meetSize = newMeetSize;
3699552f7358SJed Brown     i        = 1-i;
3700552f7358SJed Brown   }
3701552f7358SJed Brown   *numCoveringPoints = meetSize;
3702552f7358SJed Brown   *coveringPoints    = meet[i];
370369291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3704552f7358SJed Brown   PetscFunctionReturn(0);
3705552f7358SJed Brown }
3706552f7358SJed Brown 
3707552f7358SJed Brown /*@C
3708552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3709552f7358SJed Brown 
3710552f7358SJed Brown   Not Collective
3711552f7358SJed Brown 
3712552f7358SJed Brown   Input Parameters:
3713552f7358SJed Brown + dm - The DMPlex object
3714552f7358SJed Brown . numPoints - The number of input points for the meet
3715552f7358SJed Brown - points - The input points
3716552f7358SJed Brown 
3717552f7358SJed Brown   Output Parameters:
3718552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3719552f7358SJed Brown - coveredPoints - The points in the meet
3720552f7358SJed Brown 
3721552f7358SJed Brown   Level: intermediate
3722552f7358SJed Brown 
37233813dfbdSMatthew G Knepley   Fortran Notes:
37243813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
37253813dfbdSMatthew G Knepley   include petsc.h90 in your code.
37263813dfbdSMatthew G Knepley 
37273813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
37283813dfbdSMatthew G Knepley 
3729552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3730552f7358SJed Brown @*/
3731552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3732552f7358SJed Brown {
3733552f7358SJed Brown   PetscErrorCode ierr;
3734552f7358SJed Brown 
3735552f7358SJed Brown   PetscFunctionBegin;
3736552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3737d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3738d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3739d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
374069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3741d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3742552f7358SJed Brown   PetscFunctionReturn(0);
3743552f7358SJed Brown }
3744552f7358SJed Brown 
3745552f7358SJed Brown /*@C
3746552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3747552f7358SJed Brown 
3748552f7358SJed Brown   Not Collective
3749552f7358SJed Brown 
3750552f7358SJed Brown   Input Parameters:
3751552f7358SJed Brown + dm - The DMPlex object
3752552f7358SJed Brown . numPoints - The number of input points for the meet
3753552f7358SJed Brown - points - The input points
3754552f7358SJed Brown 
3755552f7358SJed Brown   Output Parameters:
3756552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3757552f7358SJed Brown - coveredPoints - The points in the meet
3758552f7358SJed Brown 
3759552f7358SJed Brown   Level: intermediate
3760552f7358SJed Brown 
37613813dfbdSMatthew G Knepley   Fortran Notes:
37623813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
37633813dfbdSMatthew G Knepley   include petsc.h90 in your code.
37643813dfbdSMatthew G Knepley 
37653813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
37663813dfbdSMatthew G Knepley 
3767552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3768552f7358SJed Brown @*/
3769552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3770552f7358SJed Brown {
3771552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3772552f7358SJed Brown   PetscInt      *offsets, **closures;
3773552f7358SJed Brown   PetscInt      *meet[2];
3774552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
377524c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3776552f7358SJed Brown   PetscErrorCode ierr;
3777552f7358SJed Brown 
3778552f7358SJed Brown   PetscFunctionBegin;
3779552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3780064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
3781064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveredPoints, 4);
3782064a246eSJacob Faibussowitsch   PetscValidPointer(coveredPoints, 5);
3783552f7358SJed Brown 
3784552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3785785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
378669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
378724c766afSToby Isaac   mc      = mesh->maxConeSize;
378824c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
378969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
379069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3791552f7358SJed Brown 
3792552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3793552f7358SJed Brown     PetscInt closureSize;
3794552f7358SJed Brown 
3795552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
37960d644c17SKarl Rupp 
3797552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3798552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3799552f7358SJed Brown       PetscInt pStart, pEnd, i;
3800552f7358SJed Brown 
3801552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3802552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3803552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3804552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3805552f7358SJed Brown           break;
3806552f7358SJed Brown         }
3807552f7358SJed Brown       }
3808552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3809552f7358SJed Brown     }
381082f516ccSBarry 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);
3811552f7358SJed Brown   }
3812552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3813552f7358SJed Brown     PetscInt dof;
3814552f7358SJed Brown 
3815552f7358SJed Brown     /* Copy in cone of first point */
3816552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3817552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3818552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3819552f7358SJed Brown     }
3820552f7358SJed Brown     /* Check each successive cone */
3821552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3822552f7358SJed Brown       PetscInt newMeetSize = 0;
3823552f7358SJed Brown 
3824552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3825552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3826552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3827552f7358SJed Brown 
3828552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3829552f7358SJed Brown           if (point == meet[i][m]) {
3830552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3831552f7358SJed Brown             break;
3832552f7358SJed Brown           }
3833552f7358SJed Brown         }
3834552f7358SJed Brown       }
3835552f7358SJed Brown       meetSize = newMeetSize;
3836552f7358SJed Brown       i        = 1-i;
3837552f7358SJed Brown     }
3838552f7358SJed Brown     if (meetSize) break;
3839552f7358SJed Brown   }
3840552f7358SJed Brown   *numCoveredPoints = meetSize;
3841552f7358SJed Brown   *coveredPoints    = meet[i];
3842552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
38430298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3844552f7358SJed Brown   }
3845552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
384669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
384769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3848552f7358SJed Brown   PetscFunctionReturn(0);
3849552f7358SJed Brown }
3850552f7358SJed Brown 
38514e3744c5SMatthew G. Knepley /*@C
38524e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
38534e3744c5SMatthew G. Knepley 
38544e3744c5SMatthew G. Knepley   Not Collective
38554e3744c5SMatthew G. Knepley 
38564e3744c5SMatthew G. Knepley   Input Parameters:
38574e3744c5SMatthew G. Knepley + dmA - A DMPlex object
38584e3744c5SMatthew G. Knepley - dmB - A DMPlex object
38594e3744c5SMatthew G. Knepley 
38604e3744c5SMatthew G. Knepley   Output Parameters:
38614e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
38624e3744c5SMatthew G. Knepley 
38634e3744c5SMatthew G. Knepley   Level: intermediate
38644e3744c5SMatthew G. Knepley 
38654e3744c5SMatthew G. Knepley   Notes:
38664e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
38674e3744c5SMatthew G. Knepley 
38684e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
38694e3744c5SMatthew G. Knepley @*/
38704e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
38714e3744c5SMatthew G. Knepley {
38724e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
38734e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
38744e3744c5SMatthew G. Knepley 
38754e3744c5SMatthew G. Knepley   PetscFunctionBegin;
38764e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
38774e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
38784e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
38794e3744c5SMatthew G. Knepley 
38804e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
38814e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
38824e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
38834e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
38844e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
38854e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
38864e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
38874e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
38884e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
38894e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
38904e3744c5SMatthew G. Knepley 
38914e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
38924e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
38934e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
38944e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
38954e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
38964e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
38974e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
38984e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
38994e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
39004e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
39014e3744c5SMatthew G. Knepley     }
39024e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
39034e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
39044e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
39054e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
39064e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
39074e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
39084e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
39094e3744c5SMatthew G. Knepley     }
39104e3744c5SMatthew G. Knepley   }
39114e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
39124e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
39134e3744c5SMatthew G. Knepley }
39144e3744c5SMatthew G. Knepley 
39157cd05799SMatthew G. Knepley /*@C
39167cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
39177cd05799SMatthew G. Knepley 
39187cd05799SMatthew G. Knepley   Not Collective
39197cd05799SMatthew G. Knepley 
39207cd05799SMatthew G. Knepley   Input Parameters:
39217cd05799SMatthew G. Knepley + dm         - The DMPlex
39227cd05799SMatthew G. Knepley . cellDim    - The cell dimension
39237cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
39247cd05799SMatthew G. Knepley 
39257cd05799SMatthew G. Knepley   Output Parameters:
39267cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
39277cd05799SMatthew G. Knepley 
39287cd05799SMatthew G. Knepley   Level: developer
39297cd05799SMatthew G. Knepley 
39307cd05799SMatthew G. Knepley   Notes:
39317cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
39327cd05799SMatthew G. Knepley 
39337cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
39347cd05799SMatthew G. Knepley @*/
393518ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3936a6dfd86eSKarl Rupp {
393782f516ccSBarry Smith   MPI_Comm       comm;
3938552f7358SJed Brown   PetscErrorCode ierr;
3939552f7358SJed Brown 
3940552f7358SJed Brown   PetscFunctionBegin;
394182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3942064a246eSJacob Faibussowitsch   PetscValidPointer(numFaceVertices,4);
3943552f7358SJed Brown   switch (cellDim) {
3944552f7358SJed Brown   case 0:
3945552f7358SJed Brown     *numFaceVertices = 0;
3946552f7358SJed Brown     break;
3947552f7358SJed Brown   case 1:
3948552f7358SJed Brown     *numFaceVertices = 1;
3949552f7358SJed Brown     break;
3950552f7358SJed Brown   case 2:
3951552f7358SJed Brown     switch (numCorners) {
395219436ca2SJed Brown     case 3: /* triangle */
395319436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3954552f7358SJed Brown       break;
395519436ca2SJed Brown     case 4: /* quadrilateral */
395619436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3957552f7358SJed Brown       break;
395819436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
395919436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3960552f7358SJed Brown       break;
396119436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
396219436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3963552f7358SJed Brown       break;
3964552f7358SJed Brown     default:
3965f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3966552f7358SJed Brown     }
3967552f7358SJed Brown     break;
3968552f7358SJed Brown   case 3:
3969552f7358SJed Brown     switch (numCorners) {
397019436ca2SJed Brown     case 4: /* tetradehdron */
397119436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3972552f7358SJed Brown       break;
397319436ca2SJed Brown     case 6: /* tet cohesive cells */
397419436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3975552f7358SJed Brown       break;
397619436ca2SJed Brown     case 8: /* hexahedron */
397719436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3978552f7358SJed Brown       break;
397919436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
398019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3981552f7358SJed Brown       break;
398219436ca2SJed Brown     case 10: /* quadratic tetrahedron */
398319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3984552f7358SJed Brown       break;
398519436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
398619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3987552f7358SJed Brown       break;
398819436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
398919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3990552f7358SJed Brown       break;
399119436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
399219436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3993552f7358SJed Brown       break;
3994552f7358SJed Brown     default:
3995f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3996552f7358SJed Brown     }
3997552f7358SJed Brown     break;
3998552f7358SJed Brown   default:
3999f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
4000552f7358SJed Brown   }
4001552f7358SJed Brown   PetscFunctionReturn(0);
4002552f7358SJed Brown }
4003552f7358SJed Brown 
4004552f7358SJed Brown /*@
4005aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
4006552f7358SJed Brown 
4007552f7358SJed Brown   Not Collective
4008552f7358SJed Brown 
4009aa50250dSMatthew G. Knepley   Input Parameter:
4010552f7358SJed Brown . dm    - The DMPlex object
4011552f7358SJed Brown 
4012aa50250dSMatthew G. Knepley   Output Parameter:
4013aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
4014552f7358SJed Brown 
4015552f7358SJed Brown   Level: developer
4016552f7358SJed Brown 
4017dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
4018aa50250dSMatthew G. Knepley @*/
4019aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4020aa50250dSMatthew G. Knepley {
4021aa50250dSMatthew G. Knepley   PetscFunctionBegin;
4022aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4023aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
4024c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
4025aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
4026aa50250dSMatthew G. Knepley }
4027aa50250dSMatthew G. Knepley 
4028aa50250dSMatthew G. Knepley /*@
4029aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
4030aa50250dSMatthew G. Knepley 
4031aa50250dSMatthew G. Knepley   Not Collective
4032aa50250dSMatthew G. Knepley 
4033aa50250dSMatthew G. Knepley   Input Parameter:
4034aa50250dSMatthew G. Knepley . dm    - The DMPlex object
4035aa50250dSMatthew G. Knepley 
4036aa50250dSMatthew G. Knepley   Output Parameter:
4037aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
4038aa50250dSMatthew G. Knepley 
4039aa50250dSMatthew G. Knepley   Level: developer
4040552f7358SJed Brown 
4041b1bb481bSMatthew Knepley   Notes:
4042b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
4043dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
4044dc287ab2SVaclav Hapla   An empty mesh gives -1.
4045b1bb481bSMatthew Knepley 
4046dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
4047552f7358SJed Brown @*/
4048552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4049552f7358SJed Brown {
4050aa50250dSMatthew G. Knepley   DMLabel        label;
4051aa50250dSMatthew G. Knepley   PetscInt       d = 0;
4052552f7358SJed Brown   PetscErrorCode ierr;
4053552f7358SJed Brown 
4054552f7358SJed Brown   PetscFunctionBegin;
4055552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4056552f7358SJed Brown   PetscValidPointer(depth, 2);
4057aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4058aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
4059552f7358SJed Brown   *depth = d-1;
4060552f7358SJed Brown   PetscFunctionReturn(0);
4061552f7358SJed Brown }
4062552f7358SJed Brown 
4063552f7358SJed Brown /*@
4064552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
4065552f7358SJed Brown 
4066552f7358SJed Brown   Not Collective
4067552f7358SJed Brown 
4068552f7358SJed Brown   Input Parameters:
4069552f7358SJed Brown + dm           - The DMPlex object
4070552f7358SJed Brown - stratumValue - The requested depth
4071552f7358SJed Brown 
4072552f7358SJed Brown   Output Parameters:
4073552f7358SJed Brown + start - The first point at this depth
4074552f7358SJed Brown - end   - One beyond the last point at this depth
4075552f7358SJed Brown 
4076647867b2SJed Brown   Notes:
4077647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
4078647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
4079647867b2SJed Brown   higher dimension, e.g., "edges".
4080647867b2SJed Brown 
4081552f7358SJed Brown   Level: developer
4082552f7358SJed Brown 
4083dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
4084552f7358SJed Brown @*/
40850adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
40860adebc6cSBarry Smith {
4087aa50250dSMatthew G. Knepley   DMLabel        label;
408863d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
4089552f7358SJed Brown   PetscErrorCode ierr;
4090552f7358SJed Brown 
4091552f7358SJed Brown   PetscFunctionBegin;
4092552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
409463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4095552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
40960d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
409763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
409863d1a920SMatthew G. Knepley     if (start) *start = pStart;
409963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
410063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4101552f7358SJed Brown   }
4102aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
410363d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
410463d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
4105552f7358SJed Brown   PetscFunctionReturn(0);
4106552f7358SJed Brown }
4107552f7358SJed Brown 
4108552f7358SJed Brown /*@
4109552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
4110552f7358SJed Brown 
4111552f7358SJed Brown   Not Collective
4112552f7358SJed Brown 
4113552f7358SJed Brown   Input Parameters:
4114552f7358SJed Brown + dm           - The DMPlex object
4115552f7358SJed Brown - stratumValue - The requested height
4116552f7358SJed Brown 
4117552f7358SJed Brown   Output Parameters:
4118552f7358SJed Brown + start - The first point at this height
4119552f7358SJed Brown - end   - One beyond the last point at this height
4120552f7358SJed Brown 
4121647867b2SJed Brown   Notes:
4122647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
4123647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
4124647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
4125647867b2SJed Brown 
4126552f7358SJed Brown   Level: developer
4127552f7358SJed Brown 
41283dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
4129552f7358SJed Brown @*/
41300adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
41310adebc6cSBarry Smith {
4132aa50250dSMatthew G. Knepley   DMLabel        label;
413363d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
4134552f7358SJed Brown   PetscErrorCode ierr;
4135552f7358SJed Brown 
4136552f7358SJed Brown   PetscFunctionBegin;
4137552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413863d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
413963d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4140552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
41410d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
414263d1a920SMatthew G. Knepley   if (stratumValue < 0) {
414363d1a920SMatthew G. Knepley     if (start) *start = pStart;
414463d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
414563d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4146552f7358SJed Brown   }
4147aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
414813903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
414963d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
415063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
4151552f7358SJed Brown   PetscFunctionReturn(0);
4152552f7358SJed Brown }
4153552f7358SJed Brown 
4154ba2698f1SMatthew G. Knepley /*@
4155ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
4156ba2698f1SMatthew G. Knepley 
4157ba2698f1SMatthew G. Knepley   Not Collective
4158ba2698f1SMatthew G. Knepley 
4159ba2698f1SMatthew G. Knepley   Input Parameter:
4160ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
4161ba2698f1SMatthew G. Knepley - point - The point
4162ba2698f1SMatthew G. Knepley 
4163ba2698f1SMatthew G. Knepley   Output Parameter:
4164ba2698f1SMatthew G. Knepley . depth - The depth of the point
4165ba2698f1SMatthew G. Knepley 
4166ba2698f1SMatthew G. Knepley   Level: intermediate
4167ba2698f1SMatthew G. Knepley 
41683dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
4169ba2698f1SMatthew G. Knepley @*/
4170ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4171ba2698f1SMatthew G. Knepley {
4172ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4173ba2698f1SMatthew G. Knepley 
4174ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4175ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
417640a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
4177ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
4178ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4179ba2698f1SMatthew G. Knepley }
4180ba2698f1SMatthew G. Knepley 
4181ba2698f1SMatthew G. Knepley /*@
41820c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
41830c0a32dcSVaclav Hapla 
41840c0a32dcSVaclav Hapla   Not Collective
41850c0a32dcSVaclav Hapla 
41860c0a32dcSVaclav Hapla   Input Parameter:
41870c0a32dcSVaclav Hapla + dm    - The DMPlex object
41880c0a32dcSVaclav Hapla - point - The point
41890c0a32dcSVaclav Hapla 
41900c0a32dcSVaclav Hapla   Output Parameter:
41910c0a32dcSVaclav Hapla . height - The height of the point
41920c0a32dcSVaclav Hapla 
41930c0a32dcSVaclav Hapla   Level: intermediate
41940c0a32dcSVaclav Hapla 
41953dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
41960c0a32dcSVaclav Hapla @*/
41970c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
41980c0a32dcSVaclav Hapla {
41990c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
42000c0a32dcSVaclav Hapla   PetscErrorCode ierr;
42010c0a32dcSVaclav Hapla 
42020c0a32dcSVaclav Hapla   PetscFunctionBegin;
42030c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42040c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
42050c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
42060c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
42070c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
42080c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
42090c0a32dcSVaclav Hapla }
42100c0a32dcSVaclav Hapla 
42110c0a32dcSVaclav Hapla /*@
4212ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
4213ba2698f1SMatthew G. Knepley 
4214ba2698f1SMatthew G. Knepley   Not Collective
4215ba2698f1SMatthew G. Knepley 
4216ba2698f1SMatthew G. Knepley   Input Parameter:
4217ba2698f1SMatthew G. Knepley . dm - The DMPlex object
4218ba2698f1SMatthew G. Knepley 
4219ba2698f1SMatthew G. Knepley   Output Parameter:
4220ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
4221ba2698f1SMatthew G. Knepley 
4222412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
4223412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
4224412e9a14SMatthew G. Knepley 
4225ba2698f1SMatthew G. Knepley   Level: developer
4226ba2698f1SMatthew G. Knepley 
4227dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
4228ba2698f1SMatthew G. Knepley @*/
4229ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4230ba2698f1SMatthew G. Knepley {
4231ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4232ba2698f1SMatthew G. Knepley 
4233ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4234ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4235ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
4236ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
4237ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
4238ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4239ba2698f1SMatthew G. Knepley }
4240ba2698f1SMatthew G. Knepley 
4241ba2698f1SMatthew G. Knepley /*@
4242ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
4243ba2698f1SMatthew G. Knepley 
4244ba2698f1SMatthew G. Knepley   Not Collective
4245ba2698f1SMatthew G. Knepley 
4246ba2698f1SMatthew G. Knepley   Input Parameter:
4247ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
4248ba2698f1SMatthew G. Knepley - cell - The cell
4249ba2698f1SMatthew G. Knepley 
4250ba2698f1SMatthew G. Knepley   Output Parameter:
4251ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
4252ba2698f1SMatthew G. Knepley 
4253ba2698f1SMatthew G. Knepley   Level: intermediate
4254ba2698f1SMatthew G. Knepley 
4255ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
4256ba2698f1SMatthew G. Knepley @*/
4257ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4258ba2698f1SMatthew G. Knepley {
4259ba2698f1SMatthew G. Knepley   DMLabel        label;
4260ba2698f1SMatthew G. Knepley   PetscInt       ct;
4261ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4262ba2698f1SMatthew G. Knepley 
4263ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4264ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4265ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
4266ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4267ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
42684a7ee7d0SMatthew G. Knepley   if (ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %D has not been assigned a cell type", cell);
4269ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
4270ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4271ba2698f1SMatthew G. Knepley }
4272ba2698f1SMatthew G. Knepley 
4273412e9a14SMatthew G. Knepley /*@
4274412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
4275412e9a14SMatthew G. Knepley 
4276412e9a14SMatthew G. Knepley   Not Collective
4277412e9a14SMatthew G. Knepley 
4278412e9a14SMatthew G. Knepley   Input Parameters:
4279412e9a14SMatthew G. Knepley + dm   - The DMPlex object
4280412e9a14SMatthew G. Knepley . cell - The cell
4281412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
4282412e9a14SMatthew G. Knepley 
4283412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
4284412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
4285412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
4286412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
4287412e9a14SMatthew G. Knepley 
4288412e9a14SMatthew G. Knepley   Level: advanced
4289412e9a14SMatthew G. Knepley 
4290412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
4291412e9a14SMatthew G. Knepley @*/
4292412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4293412e9a14SMatthew G. Knepley {
4294412e9a14SMatthew G. Knepley   DMLabel        label;
4295412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
4296412e9a14SMatthew G. Knepley 
4297412e9a14SMatthew G. Knepley   PetscFunctionBegin;
4298412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4299412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4300412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
4301412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
4302412e9a14SMatthew G. Knepley }
4303412e9a14SMatthew G. Knepley 
43040adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
43050adebc6cSBarry Smith {
4306efe440bfSMatthew G. Knepley   PetscSection   section, s;
4307efe440bfSMatthew G. Knepley   Mat            m;
43083e922f36SToby Isaac   PetscInt       maxHeight;
4309552f7358SJed Brown   PetscErrorCode ierr;
4310552f7358SJed Brown 
4311552f7358SJed Brown   PetscFunctionBegin;
431238221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
43133e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
43143e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
431582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
431692fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
43171d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4318efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
4319efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
4320efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
4321efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
4322efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
43238f4c458bSMatthew G. Knepley 
43248f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
43258f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
4326552f7358SJed Brown   PetscFunctionReturn(0);
4327552f7358SJed Brown }
4328552f7358SJed Brown 
4329f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4330f19dbd58SToby Isaac {
4331f19dbd58SToby Isaac   Vec            coordsLocal;
4332f19dbd58SToby Isaac   DM             coordsDM;
4333f19dbd58SToby Isaac   PetscErrorCode ierr;
4334f19dbd58SToby Isaac 
4335f19dbd58SToby Isaac   PetscFunctionBegin;
4336f19dbd58SToby Isaac   *field = NULL;
4337f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
4338f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
4339f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
4340f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
4341f19dbd58SToby Isaac   }
4342f19dbd58SToby Isaac   PetscFunctionReturn(0);
4343f19dbd58SToby Isaac }
4344f19dbd58SToby Isaac 
43457cd05799SMatthew G. Knepley /*@C
43467cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
43477cd05799SMatthew G. Knepley 
43487cd05799SMatthew G. Knepley   Not Collective
43497cd05799SMatthew G. Knepley 
43507cd05799SMatthew G. Knepley   Input Parameters:
43517cd05799SMatthew G. Knepley . dm        - The DMPlex object
43527cd05799SMatthew G. Knepley 
43537cd05799SMatthew G. Knepley   Output Parameter:
43547cd05799SMatthew G. Knepley . section - The PetscSection object
43557cd05799SMatthew G. Knepley 
43567cd05799SMatthew G. Knepley   Level: developer
43577cd05799SMatthew G. Knepley 
43587cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
43597cd05799SMatthew G. Knepley @*/
43600adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
43610adebc6cSBarry Smith {
4362552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4363552f7358SJed Brown 
4364552f7358SJed Brown   PetscFunctionBegin;
4365552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4366552f7358SJed Brown   if (section) *section = mesh->coneSection;
4367552f7358SJed Brown   PetscFunctionReturn(0);
4368552f7358SJed Brown }
4369552f7358SJed Brown 
43707cd05799SMatthew G. Knepley /*@C
43717cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
43727cd05799SMatthew G. Knepley 
43737cd05799SMatthew G. Knepley   Not Collective
43747cd05799SMatthew G. Knepley 
43757cd05799SMatthew G. Knepley   Input Parameters:
43767cd05799SMatthew G. Knepley . dm        - The DMPlex object
43777cd05799SMatthew G. Knepley 
43787cd05799SMatthew G. Knepley   Output Parameter:
43797cd05799SMatthew G. Knepley . section - The PetscSection object
43807cd05799SMatthew G. Knepley 
43817cd05799SMatthew G. Knepley   Level: developer
43827cd05799SMatthew G. Knepley 
43837cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
43847cd05799SMatthew G. Knepley @*/
43858cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
43868cb4d582SMatthew G. Knepley {
43878cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
43888cb4d582SMatthew G. Knepley 
43898cb4d582SMatthew G. Knepley   PetscFunctionBegin;
43908cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43918cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
43928cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
43938cb4d582SMatthew G. Knepley }
43948cb4d582SMatthew G. Knepley 
43957cd05799SMatthew G. Knepley /*@C
43967cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
43977cd05799SMatthew G. Knepley 
43987cd05799SMatthew G. Knepley   Not Collective
43997cd05799SMatthew G. Knepley 
44007cd05799SMatthew G. Knepley   Input Parameters:
44017cd05799SMatthew G. Knepley . dm        - The DMPlex object
44027cd05799SMatthew G. Knepley 
44037cd05799SMatthew G. Knepley   Output Parameter:
44047cd05799SMatthew G. Knepley . cones - The cone for each point
44057cd05799SMatthew G. Knepley 
44067cd05799SMatthew G. Knepley   Level: developer
44077cd05799SMatthew G. Knepley 
44087cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
44097cd05799SMatthew G. Knepley @*/
4410a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4411a6dfd86eSKarl Rupp {
4412552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4413552f7358SJed Brown 
4414552f7358SJed Brown   PetscFunctionBegin;
4415552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4416552f7358SJed Brown   if (cones) *cones = mesh->cones;
4417552f7358SJed Brown   PetscFunctionReturn(0);
4418552f7358SJed Brown }
4419552f7358SJed Brown 
44207cd05799SMatthew G. Knepley /*@C
44217cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
44227cd05799SMatthew G. Knepley 
44237cd05799SMatthew G. Knepley   Not Collective
44247cd05799SMatthew G. Knepley 
44257cd05799SMatthew G. Knepley   Input Parameters:
44267cd05799SMatthew G. Knepley . dm        - The DMPlex object
44277cd05799SMatthew G. Knepley 
44287cd05799SMatthew G. Knepley   Output Parameter:
44297cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
44307cd05799SMatthew G. Knepley 
44317cd05799SMatthew G. Knepley   Level: developer
44327cd05799SMatthew G. Knepley 
44337cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
44347cd05799SMatthew G. Knepley @*/
4435a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4436a6dfd86eSKarl Rupp {
4437552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4438552f7358SJed Brown 
4439552f7358SJed Brown   PetscFunctionBegin;
4440552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4441552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4442552f7358SJed Brown   PetscFunctionReturn(0);
4443552f7358SJed Brown }
4444552f7358SJed Brown 
4445552f7358SJed Brown /******************************** FEM Support **********************************/
4446552f7358SJed Brown 
44479e8305c2SJed Brown /*
44489e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
44499e8305c2SJed Brown  representing a line in the section.
44509e8305c2SJed Brown */
44519e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
44529e8305c2SJed Brown {
44539e8305c2SJed Brown   PetscErrorCode ierr;
44549e8305c2SJed Brown 
44559e8305c2SJed Brown   PetscFunctionBeginHot;
44569e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4457a433471fSStefano Zampini   if (line < 0) {
4458a433471fSStefano Zampini     *k = 0;
4459a433471fSStefano Zampini     *Nc = 0;
4460a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
44619e8305c2SJed Brown     *k = 1;
44629e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
44639e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
44649e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
44659e8305c2SJed Brown     *k = *k / *Nc + 1;
44669e8305c2SJed Brown   }
44679e8305c2SJed Brown   PetscFunctionReturn(0);
44689e8305c2SJed Brown }
44699e8305c2SJed Brown 
4470a4355906SMatthew Knepley /*@
4471bc1eb3faSJed Brown 
4472bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4473bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
44741bb6d2a8SBarry Smith   section provided (or the section of the DM).
4475a4355906SMatthew Knepley 
4476a4355906SMatthew Knepley   Input Parameters:
4477a4355906SMatthew Knepley + dm      - The DM
4478a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4479a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4480a4355906SMatthew Knepley 
4481a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4482a4355906SMatthew Knepley   degree of the basis.
4483a4355906SMatthew Knepley 
4484bc1eb3faSJed Brown   Example:
4485bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4486bc1eb3faSJed Brown .vb
4487bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4488bc1eb3faSJed Brown 
4489bc1eb3faSJed Brown   v4 -- e6 -- v3
4490bc1eb3faSJed Brown   |           |
4491bc1eb3faSJed Brown   e7    c0    e8
4492bc1eb3faSJed Brown   |           |
4493bc1eb3faSJed Brown   v1 -- e5 -- v2
4494bc1eb3faSJed Brown .ve
4495bc1eb3faSJed Brown 
4496bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4497bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4498bc1eb3faSJed Brown .vb
4499bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4500bc1eb3faSJed Brown     v1 -> [4]
4501bc1eb3faSJed Brown     ...
4502bc1eb3faSJed Brown     e5 -> [8, 9]
4503bc1eb3faSJed Brown .ve
4504bc1eb3faSJed Brown 
4505bc1eb3faSJed Brown   which corresponds to the dofs
4506bc1eb3faSJed Brown .vb
4507bc1eb3faSJed Brown     6   10  11  7
4508bc1eb3faSJed Brown     13  2   3   15
4509bc1eb3faSJed Brown     12  0   1   14
4510bc1eb3faSJed Brown     4   8   9   5
4511bc1eb3faSJed Brown .ve
4512bc1eb3faSJed Brown 
4513bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4514bc1eb3faSJed Brown .vb
4515bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4516bc1eb3faSJed Brown .ve
4517bc1eb3faSJed Brown 
4518bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4519bc1eb3faSJed Brown .vb
4520bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4521bc1eb3faSJed Brown .ve
4522bc1eb3faSJed Brown 
4523a4355906SMatthew Knepley   Level: developer
4524a4355906SMatthew Knepley 
45259df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4526a4355906SMatthew Knepley @*/
4527bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
45283194fc30SMatthew G. Knepley {
45297391a63aSMatthew G. Knepley   DMLabel        label;
4530bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
45319e8305c2SJed Brown   PetscBool      vertexchart;
45323194fc30SMatthew G. Knepley   PetscErrorCode ierr;
45333194fc30SMatthew G. Knepley 
45343194fc30SMatthew G. Knepley   PetscFunctionBegin;
45353194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4536a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4537a433471fSStefano Zampini   if (point < 0) {
4538a433471fSStefano Zampini     PetscInt sStart,sEnd;
4539a433471fSStefano Zampini 
4540a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4541a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4542a433471fSStefano Zampini   }
45437391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4544a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4545a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
45467391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
45477391a63aSMatthew G. Knepley   else if  (depth == dim) {
45487391a63aSMatthew G. Knepley     const PetscInt *cone;
45497391a63aSMatthew G. Knepley 
45507391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4551d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4552d4e6627bSStefano Zampini     else if (dim == 3) {
4553d4e6627bSStefano Zampini       const PetscInt *cone2;
4554d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4555d4e6627bSStefano Zampini       eStart = cone2[0];
4556d4e6627bSStefano 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);
4557a433471fSStefano 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);
45589e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
45599e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
45609e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
45619e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
45629e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
45639e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
45649e8305c2SJed Brown   }
45653194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4566bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
4567bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4568bb197d40SJed Brown     PetscInt *perm;
4569bb197d40SJed Brown 
45703194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
45719e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4572bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
45733194fc30SMatthew G. Knepley     }
45743194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
45753194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4576bb197d40SJed Brown       switch (d) {
4577babf31e0SJed Brown       case 1:
45789e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4579babf31e0SJed Brown         /*
4580babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4581babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
4582babf31e0SJed Brown          */
4583babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4584babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4585babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4586babf31e0SJed Brown         foffset = offset;
4587babf31e0SJed Brown         break;
458889eabcffSMatthew G. Knepley       case 2:
45893194fc30SMatthew 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} */
45909e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
45913194fc30SMatthew G. Knepley         /* The SEM order is
45923194fc30SMatthew G. Knepley 
45933194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
459489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
45953194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
45963194fc30SMatthew G. Knepley          */
45973194fc30SMatthew G. Knepley         {
45983194fc30SMatthew G. Knepley           const PetscInt of   = 0;
45993194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
46003194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
46013194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
46023194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
46033194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
46043194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
46053194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
46063194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
46073194fc30SMatthew G. Knepley           PetscInt       o;
46083194fc30SMatthew G. Knepley 
46093194fc30SMatthew G. Knepley           /* bottom */
46103194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
46113194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
46123194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
46133194fc30SMatthew G. Knepley           /* middle */
46143194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
46153194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
46163194fc30SMatthew 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;
46173194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
46183194fc30SMatthew G. Knepley           }
46193194fc30SMatthew G. Knepley           /* top */
46203194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
46213194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
46223194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
46233194fc30SMatthew G. Knepley           foffset = offset;
46243194fc30SMatthew G. Knepley         }
462589eabcffSMatthew G. Knepley         break;
462689eabcffSMatthew G. Knepley       case 3:
462789eabcffSMatthew G. Knepley         /* The original hex closure is
462889eabcffSMatthew G. Knepley 
462989eabcffSMatthew G. Knepley          {c,
463089eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
463189eabcffSMatthew 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,
463289eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
463389eabcffSMatthew G. Knepley          */
46349e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
463589eabcffSMatthew G. Knepley         /* The SEM order is
463689eabcffSMatthew G. Knepley          Bottom Slice
463789eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
463889eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
463989eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
464089eabcffSMatthew G. Knepley 
464189eabcffSMatthew G. Knepley          Middle Slice (j)
464289eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
464389eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
464489eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
464589eabcffSMatthew G. Knepley 
464689eabcffSMatthew G. Knepley          Top Slice
464789eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
464889eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
464989eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
465089eabcffSMatthew G. Knepley          */
465189eabcffSMatthew G. Knepley         {
465289eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
465389eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
465489eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
465589eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
465689eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
465789eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
465889eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
465989eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
466089eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
466189eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
466289eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
466389eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
466489eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
466589eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
466689eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
466789eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
466889eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
466989eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
467089eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
467189eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
467289eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
467389eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
467489eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
467589eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
467689eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
467789eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
467889eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
467989eabcffSMatthew G. Knepley           PetscInt       o, n;
468089eabcffSMatthew G. Knepley 
468189eabcffSMatthew G. Knepley           /* Bottom Slice */
468289eabcffSMatthew G. Knepley           /*   bottom */
468389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
468489eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
468589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
468689eabcffSMatthew G. Knepley           /*   middle */
468789eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
468889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4689316b7f87SMax 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;}
469089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
46913194fc30SMatthew G. Knepley           }
469289eabcffSMatthew G. Knepley           /*   top */
469389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
469489eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
469589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
469689eabcffSMatthew G. Knepley 
469789eabcffSMatthew G. Knepley           /* Middle Slice */
469889eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
469989eabcffSMatthew G. Knepley             /*   bottom */
470089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
470189eabcffSMatthew 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;
470289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
470389eabcffSMatthew G. Knepley             /*   middle */
470489eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
470589eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
470689eabcffSMatthew 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;
470789eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
470889eabcffSMatthew G. Knepley             }
470989eabcffSMatthew G. Knepley             /*   top */
471089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
471189eabcffSMatthew 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;
471289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
471389eabcffSMatthew G. Knepley           }
471489eabcffSMatthew G. Knepley 
471589eabcffSMatthew G. Knepley           /* Top Slice */
471689eabcffSMatthew G. Knepley           /*   bottom */
471789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
471889eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
471989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
472089eabcffSMatthew G. Knepley           /*   middle */
472189eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
472289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
472389eabcffSMatthew 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;
472489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
472589eabcffSMatthew G. Knepley           }
472689eabcffSMatthew G. Knepley           /*   top */
472789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
472889eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
472989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
473089eabcffSMatthew G. Knepley 
473189eabcffSMatthew G. Knepley           foffset = offset;
473289eabcffSMatthew G. Knepley         }
473389eabcffSMatthew G. Knepley         break;
4734bb197d40SJed Brown       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
473589eabcffSMatthew G. Knepley       }
473689eabcffSMatthew G. Knepley     }
473789eabcffSMatthew G. Knepley     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
47383194fc30SMatthew G. Knepley     /* Check permutation */
47393194fc30SMatthew G. Knepley     {
47403194fc30SMatthew G. Knepley       PetscInt *check;
47413194fc30SMatthew G. Knepley 
47423194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
47433194fc30SMatthew 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]);}
47443194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
47453194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
47463194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
47473194fc30SMatthew G. Knepley     }
4748bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
4749bb197d40SJed Brown   }
47503194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
47513194fc30SMatthew G. Knepley }
47523194fc30SMatthew G. Knepley 
4753e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4754e071409bSToby Isaac {
4755e071409bSToby Isaac   PetscDS        prob;
4756e071409bSToby Isaac   PetscInt       depth, Nf, h;
4757e071409bSToby Isaac   DMLabel        label;
4758e071409bSToby Isaac   PetscErrorCode ierr;
4759e071409bSToby Isaac 
4760e071409bSToby Isaac   PetscFunctionBeginHot;
4761e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4762e071409bSToby Isaac   Nf      = prob->Nf;
4763e071409bSToby Isaac   label   = dm->depthLabel;
4764e071409bSToby Isaac   *dspace = NULL;
4765e071409bSToby Isaac   if (field < Nf) {
4766e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4767e071409bSToby Isaac 
4768e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4769e071409bSToby Isaac       PetscDualSpace dsp;
4770e071409bSToby Isaac 
4771e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4772e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4773e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4774e071409bSToby Isaac       h    = depth - 1 - h;
4775e071409bSToby Isaac       if (h) {
4776e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4777e071409bSToby Isaac       } else {
4778e071409bSToby Isaac         *dspace = dsp;
4779e071409bSToby Isaac       }
4780e071409bSToby Isaac     }
4781e071409bSToby Isaac   }
4782e071409bSToby Isaac   PetscFunctionReturn(0);
4783e071409bSToby Isaac }
4784e071409bSToby Isaac 
47851a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4786a6dfd86eSKarl Rupp {
4787552f7358SJed Brown   PetscScalar    *array, *vArray;
4788d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
47891a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4790552f7358SJed Brown   PetscErrorCode  ierr;
4791552f7358SJed Brown 
47921b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
47932a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
47945a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
47955a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
47965a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
47973f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
47989df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
47999df71ca4SMatthew G. Knepley       PetscInt dof;
4800d9917b9dSMatthew G. Knepley 
48019df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
48029df71ca4SMatthew G. Knepley       size += dof;
48039df71ca4SMatthew G. Knepley     }
48049df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
48059df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
48062a3aaacfSMatthew G. Knepley       PetscInt       dof;
48075a1bb5cfSMatthew G. Knepley 
48085a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
48092a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
48105a1bb5cfSMatthew G. Knepley       size += dof;
48115a1bb5cfSMatthew G. Knepley     }
48123f7cbbe7SMatthew G. Knepley     if (!values) {
48133f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
48143f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
48153f7cbbe7SMatthew G. Knepley     }
481669291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4817982e9ed1SMatthew G. Knepley   } else {
4818982e9ed1SMatthew G. Knepley     array = *values;
4819982e9ed1SMatthew G. Knepley   }
48209df71ca4SMatthew G. Knepley   size = 0;
48215a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
48229df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
48239df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
48249df71ca4SMatthew G. Knepley     PetscScalar *varr;
4825d9917b9dSMatthew G. Knepley 
48269df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
48279df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
48289df71ca4SMatthew G. Knepley     varr = &vArray[off];
48291a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
48301a271a75SMatthew G. Knepley       array[offset] = varr[d];
48319df71ca4SMatthew G. Knepley     }
48329df71ca4SMatthew G. Knepley     size += dof;
48339df71ca4SMatthew G. Knepley   }
48349df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
48359df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
48369df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
48375a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
48385a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
48395a1bb5cfSMatthew G. Knepley 
484052ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
48415a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
48425a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
48435a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
48445a1bb5cfSMatthew G. Knepley     if (o >= 0) {
48451a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
48461a271a75SMatthew G. Knepley         array[offset] = varr[d];
48475a1bb5cfSMatthew G. Knepley       }
48485a1bb5cfSMatthew G. Knepley     } else {
48491a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
48501a271a75SMatthew G. Knepley         array[offset] = varr[d];
48515a1bb5cfSMatthew G. Knepley       }
48525a1bb5cfSMatthew G. Knepley     }
48539df71ca4SMatthew G. Knepley     size += dof;
48545a1bb5cfSMatthew G. Knepley   }
48555a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
48569df71ca4SMatthew G. Knepley   if (!*values) {
48575a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
48585a1bb5cfSMatthew G. Knepley     *values = array;
48599df71ca4SMatthew G. Knepley   } else {
48608ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
48618c312ff3SMatthew G. Knepley     *csize = size;
48629df71ca4SMatthew G. Knepley   }
48635a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
48645a1bb5cfSMatthew G. Knepley }
4865d9917b9dSMatthew G. Knepley 
486627f02ce8SMatthew G. Knepley /* Compress out points not in the section */
486727f02ce8SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
486827f02ce8SMatthew G. Knepley {
486927f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
487027f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
487127f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
487227f02ce8SMatthew G. Knepley 
487327f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
487427f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
487527f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
487627f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
487727f02ce8SMatthew G. Knepley       points[q*2]   = r;
487827f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
487927f02ce8SMatthew G. Knepley       ++q;
488027f02ce8SMatthew G. Knepley     }
488127f02ce8SMatthew G. Knepley   }
488227f02ce8SMatthew G. Knepley   *numPoints = q;
488327f02ce8SMatthew G. Knepley   return 0;
488427f02ce8SMatthew G. Knepley }
488527f02ce8SMatthew G. Knepley 
488627f02ce8SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
488727f02ce8SMatthew G. Knepley {
488827f02ce8SMatthew G. Knepley   const PetscInt *cone, *ornt;
488927f02ce8SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
489027f02ce8SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
489127f02ce8SMatthew G. Knepley   PetscErrorCode  ierr;
489227f02ce8SMatthew G. Knepley 
489327f02ce8SMatthew G. Knepley   PetscFunctionBeginHot;
489427f02ce8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
489527f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
489627f02ce8SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
489727f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
489827f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
489927f02ce8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, np*2, MPIU_INT, &pts);CHKERRQ(ierr);
490027f02ce8SMatthew G. Knepley   c    = 0;
490127f02ce8SMatthew G. Knepley   pts[c*2+0] = point;
490227f02ce8SMatthew G. Knepley   pts[c*2+1] = 0;
490327f02ce8SMatthew G. Knepley   ++c;
490427f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
490527f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
490627f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
490727f02ce8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
490827f02ce8SMatthew G. Knepley   if (dim >= 2) {
490927f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
491027f02ce8SMatthew G. Knepley   }
491127f02ce8SMatthew G. Knepley   if (dim >= 3) {
491227f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
491327f02ce8SMatthew G. Knepley       const PetscInt  fpoint = cone[d];
491427f02ce8SMatthew G. Knepley       const PetscInt *fcone;
491527f02ce8SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
491627f02ce8SMatthew G. Knepley 
491727f02ce8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
491827f02ce8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
491927f02ce8SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
492027f02ce8SMatthew G. Knepley         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
492127f02ce8SMatthew G. Knepley         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
492227f02ce8SMatthew G. Knepley       }
492327f02ce8SMatthew G. Knepley     }
492427f02ce8SMatthew G. Knepley   }
492527f02ce8SMatthew G. Knepley   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
492627f02ce8SMatthew G. Knepley   *numPoints = np;
492727f02ce8SMatthew G. Knepley   *points    = pts;
492827f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
492927f02ce8SMatthew G. Knepley }
493027f02ce8SMatthew G. Knepley 
493197529cf3SJed Brown /* Compressed closure does not apply closure permutation */
49321dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4933923c78e0SToby Isaac {
493427f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
4935923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4936923c78e0SToby Isaac   PetscErrorCode ierr;
4937923c78e0SToby Isaac 
4938923c78e0SToby Isaac   PetscFunctionBeginHot;
4939923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
494027f02ce8SMatthew G. Knepley   if (*clPoints) {
4941923c78e0SToby Isaac     PetscInt dof, off;
4942923c78e0SToby Isaac 
4943923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4944923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4945923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4946923c78e0SToby Isaac     np   = dof/2;
4947923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
494827f02ce8SMatthew G. Knepley   } else {
4949665f567fSMatthew G. Knepley     DMPolytopeType ct;
4950665f567fSMatthew G. Knepley 
495127f02ce8SMatthew G. Knepley     /* Do not make the label if it does not exist */
495227f02ce8SMatthew G. Knepley     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
495327f02ce8SMatthew G. Knepley     else                    {ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);}
495427f02ce8SMatthew G. Knepley     switch (ct) {
495527f02ce8SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
495627f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);CHKERRQ(ierr);
495727f02ce8SMatthew G. Knepley         break;
495827f02ce8SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
495927f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);CHKERRQ(ierr);
496027f02ce8SMatthew G. Knepley         break;
496127f02ce8SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
496227f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);CHKERRQ(ierr);
496327f02ce8SMatthew G. Knepley         break;
496427f02ce8SMatthew G. Knepley       default:
496527f02ce8SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
496627f02ce8SMatthew G. Knepley     }
496727f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
4968923c78e0SToby Isaac   }
4969923c78e0SToby Isaac   *numPoints = np;
4970923c78e0SToby Isaac   *points    = pts;
4971923c78e0SToby Isaac   *clp       = cla;
4972923c78e0SToby Isaac   PetscFunctionReturn(0);
4973923c78e0SToby Isaac }
4974923c78e0SToby Isaac 
49751dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4976923c78e0SToby Isaac {
4977923c78e0SToby Isaac   PetscErrorCode ierr;
4978923c78e0SToby Isaac 
4979923c78e0SToby Isaac   PetscFunctionBeginHot;
4980923c78e0SToby Isaac   if (!*clPoints) {
4981923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4982923c78e0SToby Isaac   } else {
4983923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4984923c78e0SToby Isaac   }
4985923c78e0SToby Isaac   *numPoints = 0;
4986923c78e0SToby Isaac   *points    = NULL;
4987923c78e0SToby Isaac   *clSec     = NULL;
4988923c78e0SToby Isaac   *clPoints  = NULL;
4989923c78e0SToby Isaac   *clp       = NULL;
4990923c78e0SToby Isaac   PetscFunctionReturn(0);
4991923c78e0SToby Isaac }
4992923c78e0SToby Isaac 
499397e99dd9SToby 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[])
49941a271a75SMatthew G. Knepley {
49951a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
499697e99dd9SToby Isaac   const PetscInt    **perms = NULL;
499797e99dd9SToby Isaac   const PetscScalar **flips = NULL;
49981a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
49991a271a75SMatthew G. Knepley 
50001a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5001fe02ba77SJed Brown   *size = 0;
500297e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
500397e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
500497e99dd9SToby Isaac     const PetscInt    point = points[2*p];
500597e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
500697e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
50071a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
50081a271a75SMatthew G. Knepley     const PetscScalar *varr;
50091a271a75SMatthew G. Knepley 
50101a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
50111a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
50121a271a75SMatthew G. Knepley     varr = &vArray[off];
501397e99dd9SToby Isaac     if (clperm) {
501497e99dd9SToby Isaac       if (perm) {
501597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
50161a271a75SMatthew G. Knepley       } else {
501797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
501897e99dd9SToby Isaac       }
501997e99dd9SToby Isaac       if (flip) {
502097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
502197e99dd9SToby Isaac       }
502297e99dd9SToby Isaac     } else {
502397e99dd9SToby Isaac       if (perm) {
502497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
502597e99dd9SToby Isaac       } else {
502697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
502797e99dd9SToby Isaac       }
502897e99dd9SToby Isaac       if (flip) {
502997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
50301a271a75SMatthew G. Knepley       }
50311a271a75SMatthew G. Knepley     }
503297e99dd9SToby Isaac     offset += dof;
503397e99dd9SToby Isaac   }
503497e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
50351a271a75SMatthew G. Knepley   *size = offset;
50361a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
50371a271a75SMatthew G. Knepley }
50381a271a75SMatthew G. Knepley 
503997e99dd9SToby 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[])
50401a271a75SMatthew G. Knepley {
50411a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
50421a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
50431a271a75SMatthew G. Knepley 
50441a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5045fe02ba77SJed Brown   *size = 0;
50461a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
504797e99dd9SToby Isaac     PetscInt          p;
504897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
504997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
50501a271a75SMatthew G. Knepley 
505197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
505297e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
505397e99dd9SToby Isaac       const PetscInt    point = points[2*p];
505497e99dd9SToby Isaac       PetscInt          fdof, foff, b;
50551a271a75SMatthew G. Knepley       const PetscScalar *varr;
505697e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
505797e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
50581a271a75SMatthew G. Knepley 
50591a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
50601a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
50611a271a75SMatthew G. Knepley       varr = &vArray[foff];
506297e99dd9SToby Isaac       if (clperm) {
506397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
506497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
506597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
50661a271a75SMatthew G. Knepley       } else {
506797e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
506897e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
506997e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
50701a271a75SMatthew G. Knepley       }
507197e99dd9SToby Isaac       offset += fdof;
50721a271a75SMatthew G. Knepley     }
507397e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
50741a271a75SMatthew G. Knepley   }
50751a271a75SMatthew G. Knepley   *size = offset;
50761a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
50771a271a75SMatthew G. Knepley }
50781a271a75SMatthew G. Knepley 
5079552f7358SJed Brown /*@C
5080552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
5081552f7358SJed Brown 
5082552f7358SJed Brown   Not collective
5083552f7358SJed Brown 
5084552f7358SJed Brown   Input Parameters:
5085552f7358SJed Brown + dm - The DM
5086552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5087552f7358SJed Brown . v - The local vector
508822c1ee49SMatthew G. Knepley . point - The point in the DM
508922c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
509022c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
5091552f7358SJed Brown 
5092552f7358SJed Brown   Output Parameters:
509322c1ee49SMatthew G. Knepley + csize - The number of values in the closure
509422c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
509522c1ee49SMatthew G. Knepley 
509622c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
509722c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
509822c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
509922c1ee49SMatthew G. Knepley $
510022c1ee49SMatthew G. Knepley $ A typical use could be
510122c1ee49SMatthew G. Knepley $
510222c1ee49SMatthew G. Knepley $  values = NULL;
510322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
510422c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
510522c1ee49SMatthew G. Knepley $    <Compute on closure>
510622c1ee49SMatthew G. Knepley $  }
510722c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
510822c1ee49SMatthew G. Knepley $
510922c1ee49SMatthew G. Knepley $ or
511022c1ee49SMatthew G. Knepley $
511122c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
511222c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
511322c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
511422c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
511522c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
511622c1ee49SMatthew G. Knepley $      <Compute on closure>
511722c1ee49SMatthew G. Knepley $    }
511822c1ee49SMatthew G. Knepley $  }
511922c1ee49SMatthew G. Knepley $  PetscFree(values);
5120552f7358SJed Brown 
5121552f7358SJed Brown   Fortran Notes:
5122552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
5123552f7358SJed Brown   include petsc.h90 in your code.
5124552f7358SJed Brown 
5125552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
5126552f7358SJed Brown 
5127552f7358SJed Brown   Level: intermediate
5128552f7358SJed Brown 
5129552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5130552f7358SJed Brown @*/
5131552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5132552f7358SJed Brown {
5133552f7358SJed Brown   PetscSection       clSection;
5134d9917b9dSMatthew G. Knepley   IS                 clPoints;
5135552f7358SJed Brown   PetscInt          *points = NULL;
5136c459fbc1SJed Brown   const PetscInt    *clp, *perm;
5137c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
5138552f7358SJed Brown   PetscErrorCode     ierr;
5139552f7358SJed Brown 
5140d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
5141552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
514292fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
51431a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
51441a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5145552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5146552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5147552f7358SJed Brown   if (depth == 1 && numFields < 2) {
51481a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5149552f7358SJed Brown     PetscFunctionReturn(0);
5150552f7358SJed Brown   }
51511a271a75SMatthew G. Knepley   /* Get points */
5152923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5153c459fbc1SJed Brown   /* Get sizes */
5154c459fbc1SJed Brown   asize = 0;
5155c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
5156c459fbc1SJed Brown     PetscInt dof;
5157552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
51581a271a75SMatthew G. Knepley     asize += dof;
5159552f7358SJed Brown   }
5160c459fbc1SJed Brown   if (values) {
5161c459fbc1SJed Brown     const PetscScalar *vArray;
5162c459fbc1SJed Brown     PetscInt          size;
5163c459fbc1SJed Brown 
5164c459fbc1SJed Brown     if (*values) {
5165c459fbc1SJed 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);
5166c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
5167c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
51688a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
51691a271a75SMatthew G. Knepley     /* Get values */
5170c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
5171c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
5172c459fbc1SJed Brown     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
51731a271a75SMatthew G. Knepley     /* Cleanup array */
51748a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5175d0f6b257SMatthew G. Knepley   }
5176c459fbc1SJed Brown   if (csize) *csize = asize;
5177c459fbc1SJed Brown   /* Cleanup points */
5178c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5179552f7358SJed Brown   PetscFunctionReturn(0);
5180552f7358SJed Brown }
5181552f7358SJed Brown 
5182e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5183e5c487bfSMatthew G. Knepley {
5184e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
5185e5c487bfSMatthew G. Knepley   PetscSection       clSection;
5186e5c487bfSMatthew G. Knepley   IS                 clPoints;
5187e5c487bfSMatthew G. Knepley   PetscScalar       *array;
5188e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
5189e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
5190c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
5191c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
5192e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
5193e5c487bfSMatthew G. Knepley 
5194e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
5195e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5196e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5197e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5198e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5199e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
5200e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
5201e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5202e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
5203e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5204e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
5205e5c487bfSMatthew G. Knepley   }
5206e5c487bfSMatthew G. Knepley   /* Get points */
5207e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5208c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
5209c459fbc1SJed Brown     PetscInt dof;
5210c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5211c459fbc1SJed Brown     clsize += dof;
5212c459fbc1SJed Brown   }
5213c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
5214e5c487bfSMatthew G. Knepley   /* Filter points */
5215e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
5216e5c487bfSMatthew G. Knepley     PetscInt dep;
5217e5c487bfSMatthew G. Knepley 
5218e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
5219e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
5220e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
5221e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
5222e5c487bfSMatthew G. Knepley     ++Np;
5223e5c487bfSMatthew G. Knepley   }
5224e5c487bfSMatthew G. Knepley   /* Get array */
5225e5c487bfSMatthew G. Knepley   if (!values || !*values) {
5226e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
5227e5c487bfSMatthew G. Knepley 
5228e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
5229e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5230e5c487bfSMatthew G. Knepley       asize += dof;
5231e5c487bfSMatthew G. Knepley     }
5232e5c487bfSMatthew G. Knepley     if (!values) {
5233e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5234e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
5235e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
5236e5c487bfSMatthew G. Knepley     }
5237e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
5238e5c487bfSMatthew G. Knepley   } else {
5239e5c487bfSMatthew G. Knepley     array = *values;
5240e5c487bfSMatthew G. Knepley   }
5241e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
5242e5c487bfSMatthew G. Knepley   /* Get values */
5243e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
5244e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
5245e5c487bfSMatthew G. Knepley   /* Cleanup points */
5246e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5247e5c487bfSMatthew G. Knepley   /* Cleanup array */
5248e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5249e5c487bfSMatthew G. Knepley   if (!*values) {
5250e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
5251e5c487bfSMatthew G. Knepley     *values = array;
5252e5c487bfSMatthew G. Knepley   } else {
5253e5c487bfSMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
5254e5c487bfSMatthew G. Knepley     *csize = size;
5255e5c487bfSMatthew G. Knepley   }
5256e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
5257e5c487bfSMatthew G. Knepley }
5258e5c487bfSMatthew G. Knepley 
5259552f7358SJed Brown /*@C
5260552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5261552f7358SJed Brown 
5262552f7358SJed Brown   Not collective
5263552f7358SJed Brown 
5264552f7358SJed Brown   Input Parameters:
5265552f7358SJed Brown + dm - The DM
52660298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
5267552f7358SJed Brown . v - The local vector
5268eaf898f9SPatrick Sanan . point - The point in the DM
52690298fd71SBarry Smith . csize - The number of values in the closure, or NULL
5270552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
5271552f7358SJed Brown 
527222c1ee49SMatthew 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()
527322c1ee49SMatthew G. Knepley 
52743813dfbdSMatthew G Knepley   Fortran Notes:
52753813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
52763813dfbdSMatthew G Knepley   include petsc.h90 in your code.
52773813dfbdSMatthew G Knepley 
52783813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
52793813dfbdSMatthew G Knepley 
5280552f7358SJed Brown   Level: intermediate
5281552f7358SJed Brown 
5282552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5283552f7358SJed Brown @*/
52847c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5285a6dfd86eSKarl Rupp {
5286552f7358SJed Brown   PetscInt       size = 0;
5287552f7358SJed Brown   PetscErrorCode ierr;
5288552f7358SJed Brown 
5289552f7358SJed Brown   PetscFunctionBegin;
5290552f7358SJed Brown   /* Should work without recalculating size */
529169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
5292c9fdaa05SMatthew G. Knepley   *values = NULL;
5293552f7358SJed Brown   PetscFunctionReturn(0);
5294552f7358SJed Brown }
5295552f7358SJed Brown 
5296552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
5297552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
5298552f7358SJed Brown 
529997e99dd9SToby 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[])
5300552f7358SJed Brown {
5301552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5302552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5303552f7358SJed Brown   PetscScalar    *a;
5304552f7358SJed Brown   PetscInt        off, cind = 0, k;
5305552f7358SJed Brown   PetscErrorCode  ierr;
5306552f7358SJed Brown 
5307552f7358SJed Brown   PetscFunctionBegin;
5308552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5309552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5310552f7358SJed Brown   a    = &array[off];
5311552f7358SJed Brown   if (!cdof || setBC) {
531297e99dd9SToby Isaac     if (clperm) {
531397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
531497e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5315552f7358SJed Brown     } else {
531697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
531797e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5318552f7358SJed Brown     }
5319552f7358SJed Brown   } else {
5320552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
532197e99dd9SToby Isaac     if (clperm) {
532297e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
5323552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
532497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5325552f7358SJed Brown         }
5326552f7358SJed Brown       } else {
5327552f7358SJed Brown         for (k = 0; k < dof; ++k) {
5328552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
532997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
533097e99dd9SToby Isaac         }
533197e99dd9SToby Isaac       }
533297e99dd9SToby Isaac     } else {
533397e99dd9SToby Isaac       if (perm) {
533497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
533597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
533697e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
533797e99dd9SToby Isaac         }
533897e99dd9SToby Isaac       } else {
533997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
534097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
534197e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
534297e99dd9SToby Isaac         }
5343552f7358SJed Brown       }
5344552f7358SJed Brown     }
5345552f7358SJed Brown   }
5346552f7358SJed Brown   PetscFunctionReturn(0);
5347552f7358SJed Brown }
5348552f7358SJed Brown 
534997e99dd9SToby 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[])
5350a5e93ea8SMatthew G. Knepley {
5351a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5352a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5353a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5354a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5355a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5356a5e93ea8SMatthew G. Knepley 
5357a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5358a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5359a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5360a5e93ea8SMatthew G. Knepley   a    = &array[off];
5361a5e93ea8SMatthew G. Knepley   if (cdof) {
5362a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
536397e99dd9SToby Isaac     if (clperm) {
536497e99dd9SToby Isaac       if (perm) {
5365a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5366a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
536797e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
536897e99dd9SToby Isaac             cind++;
5369a5e93ea8SMatthew G. Knepley           }
5370a5e93ea8SMatthew G. Knepley         }
5371a5e93ea8SMatthew G. Knepley       } else {
5372a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5373a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
537497e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
537597e99dd9SToby Isaac             cind++;
537697e99dd9SToby Isaac           }
537797e99dd9SToby Isaac         }
537897e99dd9SToby Isaac       }
537997e99dd9SToby Isaac     } else {
538097e99dd9SToby Isaac       if (perm) {
538197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
538297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
538397e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
538497e99dd9SToby Isaac             cind++;
538597e99dd9SToby Isaac           }
538697e99dd9SToby Isaac         }
538797e99dd9SToby Isaac       } else {
538897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
538997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
539097e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
539197e99dd9SToby Isaac             cind++;
539297e99dd9SToby Isaac           }
5393a5e93ea8SMatthew G. Knepley         }
5394a5e93ea8SMatthew G. Knepley       }
5395a5e93ea8SMatthew G. Knepley     }
5396a5e93ea8SMatthew G. Knepley   }
5397a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5398a5e93ea8SMatthew G. Knepley }
5399a5e93ea8SMatthew G. Knepley 
540097e99dd9SToby 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[])
5401a6dfd86eSKarl Rupp {
5402552f7358SJed Brown   PetscScalar    *a;
54031a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
54041a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
540597e99dd9SToby Isaac   PetscInt        cind = 0, b;
5406552f7358SJed Brown   PetscErrorCode  ierr;
5407552f7358SJed Brown 
5408552f7358SJed Brown   PetscFunctionBegin;
5409552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5410552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
54111a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
54121a271a75SMatthew G. Knepley   a    = &array[foff];
5413552f7358SJed Brown   if (!fcdof || setBC) {
541497e99dd9SToby Isaac     if (clperm) {
541597e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
541697e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5417552f7358SJed Brown     } else {
541897e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
541997e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5420552f7358SJed Brown     }
5421552f7358SJed Brown   } else {
5422552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
542397e99dd9SToby Isaac     if (clperm) {
542497e99dd9SToby Isaac       if (perm) {
542597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
542697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
542797e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5428552f7358SJed Brown         }
5429552f7358SJed Brown       } else {
543097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
543197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
543297e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
543397e99dd9SToby Isaac         }
543497e99dd9SToby Isaac       }
543597e99dd9SToby Isaac     } else {
543697e99dd9SToby Isaac       if (perm) {
543797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
543897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
543997e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
544097e99dd9SToby Isaac         }
544197e99dd9SToby Isaac       } else {
544297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
544397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
544497e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5445552f7358SJed Brown         }
5446552f7358SJed Brown       }
5447552f7358SJed Brown     }
5448552f7358SJed Brown   }
54491a271a75SMatthew G. Knepley   *offset += fdof;
5450552f7358SJed Brown   PetscFunctionReturn(0);
5451552f7358SJed Brown }
5452552f7358SJed Brown 
5453ba322698SMatthew 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[])
5454a5e93ea8SMatthew G. Knepley {
5455a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
54561a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
54571a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
54585da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
5459ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
5460a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5461a5e93ea8SMatthew G. Knepley 
5462a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
54635da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
5464a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5465a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
54661a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
54671a271a75SMatthew G. Knepley   a    = &array[foff];
5468a5e93ea8SMatthew G. Knepley   if (fcdof) {
5469ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
5470a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
547197e99dd9SToby Isaac     if (clperm) {
547297e99dd9SToby Isaac       if (perm) {
5473ba322698SMatthew G. Knepley         if (comps) {
5474ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5475ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
54765da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5477ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5478ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5479ba322698SMatthew G. Knepley           }
5480ba322698SMatthew G. Knepley         } else {
548197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
548297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
548397e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5484a5e93ea8SMatthew G. Knepley               ++cind;
5485a5e93ea8SMatthew G. Knepley             }
5486a5e93ea8SMatthew G. Knepley           }
5487ba322698SMatthew G. Knepley         }
5488ba322698SMatthew G. Knepley       } else {
5489ba322698SMatthew G. Knepley         if (comps) {
5490ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5491ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
54925da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5493ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5494ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5495ba322698SMatthew G. Knepley           }
5496a5e93ea8SMatthew G. Knepley         } else {
549797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
549897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
549997e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
550097e99dd9SToby Isaac               ++cind;
550197e99dd9SToby Isaac             }
550297e99dd9SToby Isaac           }
550397e99dd9SToby Isaac         }
5504ba322698SMatthew G. Knepley       }
550597e99dd9SToby Isaac     } else {
550697e99dd9SToby Isaac       if (perm) {
5507ba322698SMatthew G. Knepley         if (comps) {
5508ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5509ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
55105da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5511ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5512ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5513ba322698SMatthew G. Knepley           }
5514ba322698SMatthew G. Knepley         } else {
551597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
551697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
551797e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
551897e99dd9SToby Isaac               ++cind;
551997e99dd9SToby Isaac             }
552097e99dd9SToby Isaac           }
5521ba322698SMatthew G. Knepley         }
5522ba322698SMatthew G. Knepley       } else {
5523ba322698SMatthew G. Knepley         if (comps) {
5524ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5525ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
55265da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5527ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5528ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5529ba322698SMatthew G. Knepley           }
553097e99dd9SToby Isaac         } else {
553197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
553297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
553397e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5534a5e93ea8SMatthew G. Knepley               ++cind;
5535a5e93ea8SMatthew G. Knepley             }
5536a5e93ea8SMatthew G. Knepley           }
5537a5e93ea8SMatthew G. Knepley         }
5538a5e93ea8SMatthew G. Knepley       }
5539a5e93ea8SMatthew G. Knepley     }
5540ba322698SMatthew G. Knepley   }
55411a271a75SMatthew G. Knepley   *offset += fdof;
5542a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5543a5e93ea8SMatthew G. Knepley }
5544a5e93ea8SMatthew G. Knepley 
55458f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5546a6dfd86eSKarl Rupp {
5547552f7358SJed Brown   PetscScalar    *array;
55481b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
55491b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5550552f7358SJed Brown   PetscErrorCode  ierr;
5551552f7358SJed Brown 
55521b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5553b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5554b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5555b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5556b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5557b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5558b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5559b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5560b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5561b6ebb6e6SMatthew G. Knepley 
5562b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5563b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5564b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5565b6ebb6e6SMatthew G. Knepley     {
5566b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5567b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5568b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5569b6ebb6e6SMatthew G. Knepley 
5570b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5571b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5572b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5573b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5574b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5575b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5576b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5577b6ebb6e6SMatthew G. Knepley           }
5578b6ebb6e6SMatthew G. Knepley         } else {
5579b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5580b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5581b6ebb6e6SMatthew G. Knepley           }
5582b6ebb6e6SMatthew G. Knepley         }
5583b6ebb6e6SMatthew G. Knepley       } else {
5584b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5585b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5586b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5587b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5588b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5589b6ebb6e6SMatthew G. Knepley           }
5590b6ebb6e6SMatthew G. Knepley         } else {
5591b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5592b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5593b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5594b6ebb6e6SMatthew G. Knepley           }
5595b6ebb6e6SMatthew G. Knepley         }
5596b6ebb6e6SMatthew G. Knepley       }
5597b6ebb6e6SMatthew G. Knepley     }
5598b6ebb6e6SMatthew G. Knepley   }
5599b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5600b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5601b6ebb6e6SMatthew G. Knepley }
56021b406b76SMatthew G. Knepley 
56031b406b76SMatthew G. Knepley /*@C
56041b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
56051b406b76SMatthew G. Knepley 
56061b406b76SMatthew G. Knepley   Not collective
56071b406b76SMatthew G. Knepley 
56081b406b76SMatthew G. Knepley   Input Parameters:
56091b406b76SMatthew G. Knepley + dm - The DM
56101b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
56111b406b76SMatthew G. Knepley . v - The local vector
5612eaf898f9SPatrick Sanan . point - The point in the DM
56131b406b76SMatthew G. Knepley . values - The array of values
561422c1ee49SMatthew 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,
561522c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
56161b406b76SMatthew G. Knepley 
56171b406b76SMatthew G. Knepley   Fortran Notes:
56181b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
56191b406b76SMatthew G. Knepley 
56201b406b76SMatthew G. Knepley   Level: intermediate
56211b406b76SMatthew G. Knepley 
56221b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
56231b406b76SMatthew G. Knepley @*/
56241b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
56251b406b76SMatthew G. Knepley {
56261b406b76SMatthew G. Knepley   PetscSection    clSection;
56271b406b76SMatthew G. Knepley   IS              clPoints;
56281b406b76SMatthew G. Knepley   PetscScalar    *array;
56291b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
563027f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
5631c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
56321b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
56331b406b76SMatthew G. Knepley 
56341a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
56351b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
563692fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
56371a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
56381a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
56391b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
56401b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
56411b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
56428f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
56431b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
56441b406b76SMatthew G. Knepley   }
56451a271a75SMatthew G. Knepley   /* Get points */
5646923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5647c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
5648c459fbc1SJed Brown     PetscInt dof;
5649c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5650c459fbc1SJed Brown     clsize += dof;
5651c459fbc1SJed Brown   }
5652c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
56531a271a75SMatthew G. Knepley   /* Get array */
5654552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
56551a271a75SMatthew G. Knepley   /* Get values */
5656ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
565797e99dd9SToby Isaac     PetscInt offset = 0, f;
5658552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
565997e99dd9SToby Isaac       const PetscInt    **perms = NULL;
566097e99dd9SToby Isaac       const PetscScalar **flips = NULL;
566197e99dd9SToby Isaac 
566297e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5663552f7358SJed Brown       switch (mode) {
5664552f7358SJed Brown       case INSERT_VALUES:
566597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
566697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
566797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
566897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
566997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5670552f7358SJed Brown         } break;
5671552f7358SJed Brown       case INSERT_ALL_VALUES:
567297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
567397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
567497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
567597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
567697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5677552f7358SJed Brown         } break;
5678a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
567997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
568097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
568197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
568297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5683ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5684a5e93ea8SMatthew G. Knepley         } break;
5685552f7358SJed Brown       case ADD_VALUES:
568697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
568797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
568897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
568997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
569097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5691552f7358SJed Brown         } break;
5692552f7358SJed Brown       case ADD_ALL_VALUES:
569397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
569497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
569597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
569697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
569797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5698552f7358SJed Brown         } break;
5699304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
570097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
570197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
570297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
570397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5704ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5705304ab55fSMatthew G. Knepley         } break;
5706552f7358SJed Brown       default:
5707f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5708552f7358SJed Brown       }
570997e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
57101a271a75SMatthew G. Knepley     }
5711552f7358SJed Brown   } else {
57121a271a75SMatthew G. Knepley     PetscInt dof, off;
571397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
571497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
57151a271a75SMatthew G. Knepley 
571697e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5717552f7358SJed Brown     switch (mode) {
5718552f7358SJed Brown     case INSERT_VALUES:
571997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
572097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
572197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
572297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
572397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
572497e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5725552f7358SJed Brown       } break;
5726552f7358SJed Brown     case INSERT_ALL_VALUES:
572797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
572897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
572997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
573097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
573197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
573297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5733552f7358SJed Brown       } break;
5734a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
573597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
573697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
573797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
573897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
573997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
574097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5741a5e93ea8SMatthew G. Knepley       } break;
5742552f7358SJed Brown     case ADD_VALUES:
574397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
574497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
574597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
574697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
574797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
574897e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5749552f7358SJed Brown       } break;
5750552f7358SJed Brown     case ADD_ALL_VALUES:
575197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
575297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
575397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
575497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
575597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
575697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5757552f7358SJed Brown       } break;
5758304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
575997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
576097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
576197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
576297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
576397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
576497e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5765304ab55fSMatthew G. Knepley       } break;
5766552f7358SJed Brown     default:
5767f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5768552f7358SJed Brown     }
576997e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5770552f7358SJed Brown   }
57711a271a75SMatthew G. Knepley   /* Cleanup points */
5772923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
57731a271a75SMatthew G. Knepley   /* Cleanup array */
5774552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5775552f7358SJed Brown   PetscFunctionReturn(0);
5776552f7358SJed Brown }
5777552f7358SJed Brown 
57785f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
57795f790a90SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
57805f790a90SMatthew G. Knepley {
57815f790a90SMatthew G. Knepley   PetscFunctionBegin;
57825f790a90SMatthew G. Knepley   if (label) {
57835f790a90SMatthew G. Knepley     PetscInt       val, fdof;
57845f790a90SMatthew G. Knepley     PetscErrorCode ierr;
57855f790a90SMatthew G. Knepley 
57865f790a90SMatthew G. Knepley     /* There is a problem with this:
57875f790a90SMatthew 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
57885f790a90SMatthew 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.
57895f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
57905f790a90SMatthew G. Knepley     */
57915f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
57925f790a90SMatthew G. Knepley     if (val < 0) {
57935f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
57945f790a90SMatthew G. Knepley       *offset += fdof;
57955f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
57965f790a90SMatthew G. Knepley     }
57975f790a90SMatthew G. Knepley   }
57985f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
57995f790a90SMatthew G. Knepley }
58005f790a90SMatthew G. Knepley 
580197529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
58025f790a90SMatthew 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)
5803e07394fbSMatthew G. Knepley {
5804e07394fbSMatthew G. Knepley   PetscSection      clSection;
5805e07394fbSMatthew G. Knepley   IS                clPoints;
5806e07394fbSMatthew G. Knepley   PetscScalar       *array;
5807e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
580897529cf3SJed Brown   const PetscInt    *clp;
5809e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
581097e99dd9SToby Isaac   PetscInt          offset = 0, f;
5811e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5812e07394fbSMatthew G. Knepley 
5813e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5814e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
581592fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5816e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5817e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5818e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5819e07394fbSMatthew G. Knepley   /* Get points */
5820923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5821e07394fbSMatthew G. Knepley   /* Get array */
5822e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5823e07394fbSMatthew G. Knepley   /* Get values */
5824e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
582597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
582697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
582797e99dd9SToby Isaac 
5828e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5829e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5830e07394fbSMatthew G. Knepley         PetscInt fdof;
5831e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5832e07394fbSMatthew G. Knepley         offset += fdof;
5833e07394fbSMatthew G. Knepley       }
5834e07394fbSMatthew G. Knepley       continue;
5835e07394fbSMatthew G. Knepley     }
583697e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5837e07394fbSMatthew G. Knepley     switch (mode) {
5838e07394fbSMatthew G. Knepley     case INSERT_VALUES:
583997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
584097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
584197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
584297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58435f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
584497529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5845e07394fbSMatthew G. Knepley       } break;
5846e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
584797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
584897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
584997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
585097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58515f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
585297529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5853e07394fbSMatthew G. Knepley       } break;
5854e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
585597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
585697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
585797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
585897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58595f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
586097529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5861e07394fbSMatthew G. Knepley       } break;
5862e07394fbSMatthew G. Knepley     case ADD_VALUES:
586397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
586497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
586597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
586697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58675f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
586897529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5869e07394fbSMatthew G. Knepley       } break;
5870e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
587197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
587297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
587397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
587497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58755f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
587697529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5877e07394fbSMatthew G. Knepley       } break;
5878e07394fbSMatthew G. Knepley     default:
5879f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5880e07394fbSMatthew G. Knepley     }
588197e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5882e07394fbSMatthew G. Knepley   }
5883e07394fbSMatthew G. Knepley   /* Cleanup points */
5884923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5885e07394fbSMatthew G. Knepley   /* Cleanup array */
5886e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5887e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5888e07394fbSMatthew G. Knepley }
5889e07394fbSMatthew G. Knepley 
58907cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5891552f7358SJed Brown {
5892552f7358SJed Brown   PetscMPIInt    rank;
5893552f7358SJed Brown   PetscInt       i, j;
5894552f7358SJed Brown   PetscErrorCode ierr;
5895552f7358SJed Brown 
5896552f7358SJed Brown   PetscFunctionBegin;
5897ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr);
5898eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5899e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5900e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5901b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5902557cf195SMatthew G. Knepley   if (!values) PetscFunctionReturn(0);
5903b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5904e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5905b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5906519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
59077eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5908552f7358SJed Brown #else
5909b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5910552f7358SJed Brown #endif
5911552f7358SJed Brown     }
591277a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5913552f7358SJed Brown   }
5914552f7358SJed Brown   PetscFunctionReturn(0);
5915552f7358SJed Brown }
5916552f7358SJed Brown 
591705586334SMatthew G. Knepley /*
591805586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
591905586334SMatthew G. Knepley 
592005586334SMatthew G. Knepley   Input Parameters:
592105586334SMatthew G. Knepley + section - The section for this data layout
592236fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
592305586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
592405586334SMatthew G. Knepley . off     - The global offset of this point
592505586334SMatthew G. Knepley . loff    - The local offset of each field
592605586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
592705586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
592805586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
592905586334SMatthew G. Knepley 
593005586334SMatthew G. Knepley   Output Parameter:
593105586334SMatthew G. Knepley . indices - Indices for dofs on this point
593205586334SMatthew G. Knepley 
593305586334SMatthew G. Knepley   Level: developer
593405586334SMatthew G. Knepley 
593505586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
593605586334SMatthew G. Knepley */
593736fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5938a6dfd86eSKarl Rupp {
5939e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5940552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5941552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5942552f7358SJed Brown   PetscInt        cind = 0, k;
5943552f7358SJed Brown   PetscErrorCode  ierr;
5944552f7358SJed Brown 
5945552f7358SJed Brown   PetscFunctionBegin;
594636fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5947552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5948552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5949552f7358SJed Brown   if (!cdof || setBC) {
595005586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
595105586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
595205586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
595305586334SMatthew G. Knepley 
595405586334SMatthew G. Knepley       indices[ind] = off + k;
5955552f7358SJed Brown     }
5956552f7358SJed Brown   } else {
5957552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
59584acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
595905586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
596005586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
596105586334SMatthew G. Knepley 
59624acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
59634acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
596405586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
59654acb8e1eSToby Isaac         ++cind;
59664acb8e1eSToby Isaac       } else {
596736fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
5968552f7358SJed Brown       }
5969552f7358SJed Brown     }
5970552f7358SJed Brown   }
5971e6ccafaeSMatthew G Knepley   *loff += dof;
5972552f7358SJed Brown   PetscFunctionReturn(0);
5973552f7358SJed Brown }
5974552f7358SJed Brown 
59757e29afd2SMatthew G. Knepley /*
597636fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
59777e29afd2SMatthew G. Knepley 
597836fa2b79SJed Brown  Input Parameters:
597936fa2b79SJed Brown + section - a section (global or local)
598036fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
598136fa2b79SJed Brown . point - point within section
598236fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
598336fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
598436fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
598536fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
598636fa2b79SJed Brown . permsoff - offset
598736fa2b79SJed Brown - indperm - index permutation
598836fa2b79SJed Brown 
598936fa2b79SJed Brown  Output Parameter:
599036fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
599136fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
599236fa2b79SJed Brown 
599336fa2b79SJed Brown  Notes:
599436fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
599536fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
599636fa2b79SJed Brown  in the local vector.
599736fa2b79SJed Brown 
599836fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
599936fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
600036fa2b79SJed Brown 
600136fa2b79SJed Brown  Developer Note:
600236fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
600336fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
600436fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
600536fa2b79SJed Brown 
600636fa2b79SJed Brown  Example:
600736fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
600836fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
600936fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
601036fa2b79SJed 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.
601136fa2b79SJed Brown 
601236fa2b79SJed Brown  Level: developer
60137e29afd2SMatthew G. Knepley */
601436fa2b79SJed 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[])
6015a6dfd86eSKarl Rupp {
6016552f7358SJed Brown   PetscInt       numFields, foff, f;
6017552f7358SJed Brown   PetscErrorCode ierr;
6018552f7358SJed Brown 
6019552f7358SJed Brown   PetscFunctionBegin;
602036fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
6021552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6022552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
60234acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
6024552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
60254acb8e1eSToby Isaac     PetscInt        cind = 0, b;
60264acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
6027552f7358SJed Brown 
6028552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
6029552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
6030552f7358SJed Brown     if (!cfdof || setBC) {
603105586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
603205586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
603305586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
603405586334SMatthew G. Knepley 
603505586334SMatthew G. Knepley         indices[ind] = off+foff+b;
603605586334SMatthew G. Knepley       }
6037552f7358SJed Brown     } else {
6038552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
603905586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
604005586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
604105586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
604205586334SMatthew G. Knepley 
60434acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
604405586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
6045552f7358SJed Brown           ++cind;
6046552f7358SJed Brown         } else {
604736fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
6048552f7358SJed Brown         }
6049552f7358SJed Brown       }
6050552f7358SJed Brown     }
605136fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
6052552f7358SJed Brown     foffs[f] += fdof;
6053552f7358SJed Brown   }
6054552f7358SJed Brown   PetscFunctionReturn(0);
6055552f7358SJed Brown }
6056552f7358SJed Brown 
60577e29afd2SMatthew G. Knepley /*
60587e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
60597e29afd2SMatthew G. Knepley 
60607e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
6061645102dcSJed Brown 
6062645102dcSJed Brown  Notes:
6063645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6064645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
60657e29afd2SMatthew G. Knepley */
6066645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
60677e29afd2SMatthew G. Knepley {
60687e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
60697e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
60707e29afd2SMatthew G. Knepley 
60717e29afd2SMatthew G. Knepley   PetscFunctionBegin;
60727e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
60737e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
60747e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
60757e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
60767e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
60777e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
60787e29afd2SMatthew G. Knepley 
60797e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
60807e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
60817e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
6082645102dcSJed Brown     if (!cfdof) {
608305586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
608405586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
608505586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
608605586334SMatthew G. Knepley 
608705586334SMatthew G. Knepley         indices[ind] = foff+b;
608805586334SMatthew G. Knepley       }
60897e29afd2SMatthew G. Knepley     } else {
60907e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
609105586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
609205586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
609305586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
609405586334SMatthew G. Knepley 
60957e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
609605586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
60977e29afd2SMatthew G. Knepley           ++cind;
60987e29afd2SMatthew G. Knepley         } else {
609905586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
61007e29afd2SMatthew G. Knepley         }
61017e29afd2SMatthew G. Knepley       }
61027e29afd2SMatthew G. Knepley     }
61037e29afd2SMatthew G. Knepley     foffs[f] += fdof;
61047e29afd2SMatthew G. Knepley   }
61057e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
61067e29afd2SMatthew G. Knepley }
61077e29afd2SMatthew G. Knepley 
61084acb8e1eSToby 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)
6109d3d1a6afSToby Isaac {
6110d3d1a6afSToby Isaac   Mat             cMat;
6111d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
6112d3d1a6afSToby Isaac   IS              aIS;
6113d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
6114d3d1a6afSToby Isaac   const PetscInt  *anchors;
6115e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
6116d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
6117d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
6118d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
6119d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
6120d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
6121d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
6122d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
61236ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
6124d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
6125d3d1a6afSToby Isaac   PetscErrorCode  ierr;
6126d3d1a6afSToby Isaac 
6127d3d1a6afSToby Isaac   PetscFunctionBegin;
6128d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6129d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6130d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6131d3d1a6afSToby Isaac 
6132a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
6133d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
6134d3d1a6afSToby Isaac   if (aSec) {
6135580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
6136d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
6137d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
6138d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
6139d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
6140d3d1a6afSToby Isaac      * into the global matrix anyway) */
6141d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
6142d3d1a6afSToby Isaac       PetscInt b    = points[p];
61434b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6144d3d1a6afSToby Isaac 
61454b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
61464b2f2278SToby Isaac       if (!bSecDof) {
61474b2f2278SToby Isaac         continue;
61484b2f2278SToby Isaac       }
6149d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6150d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
6151d3d1a6afSToby Isaac       }
6152d3d1a6afSToby Isaac       if (bDof) {
6153d3d1a6afSToby Isaac         /* this point is constrained */
6154d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
6155d3d1a6afSToby Isaac         PetscInt bOff, q;
6156d3d1a6afSToby Isaac 
6157d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
6158d3d1a6afSToby Isaac         newNumPoints  += bDof;
6159d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
6160d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6161d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
6162d3d1a6afSToby Isaac           PetscInt aDof;
6163d3d1a6afSToby Isaac 
6164d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
6165d3d1a6afSToby Isaac           newNumIndices += aDof;
6166d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
6167d3d1a6afSToby Isaac             PetscInt fDof;
6168d3d1a6afSToby Isaac 
6169d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
6170d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
6171d3d1a6afSToby Isaac           }
6172d3d1a6afSToby Isaac         }
6173d3d1a6afSToby Isaac       }
6174d3d1a6afSToby Isaac       else {
6175d3d1a6afSToby Isaac         /* this point is not constrained */
6176d3d1a6afSToby Isaac         newNumPoints++;
61774b2f2278SToby Isaac         newNumIndices += bSecDof;
6178d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
6179d3d1a6afSToby Isaac           PetscInt fDof;
6180d3d1a6afSToby Isaac 
6181d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6182d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
6183d3d1a6afSToby Isaac         }
6184d3d1a6afSToby Isaac       }
6185d3d1a6afSToby Isaac     }
6186d3d1a6afSToby Isaac   }
6187d3d1a6afSToby Isaac   if (!anyConstrained) {
618872b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
618972b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
619072b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
619172b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
619272b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
6193d3d1a6afSToby Isaac     PetscFunctionReturn(0);
6194d3d1a6afSToby Isaac   }
6195d3d1a6afSToby Isaac 
61966ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
61976ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
61986ecaa68aSToby Isaac 
6199f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
6200d3d1a6afSToby Isaac 
62016ecaa68aSToby Isaac   if (!outPoints && !outValues) {
62026ecaa68aSToby Isaac     if (offsets) {
62036ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
62046ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
62056ecaa68aSToby Isaac       }
62066ecaa68aSToby Isaac     }
62076ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
62086ecaa68aSToby Isaac     PetscFunctionReturn(0);
62096ecaa68aSToby Isaac   }
62106ecaa68aSToby Isaac 
6211f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
6212d3d1a6afSToby Isaac 
6213f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
6214d3d1a6afSToby Isaac 
6215d3d1a6afSToby Isaac   /* workspaces */
6216d3d1a6afSToby Isaac   if (numFields) {
6217d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
621869291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
621969291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6220d3d1a6afSToby Isaac     }
6221d3d1a6afSToby Isaac   }
6222d3d1a6afSToby Isaac   else {
622369291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
622469291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6225d3d1a6afSToby Isaac   }
6226d3d1a6afSToby Isaac 
6227d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
6228d3d1a6afSToby Isaac   if (numFields) {
62294b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
62304b2f2278SToby Isaac 
6231d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6232d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
62334b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6234d3d1a6afSToby Isaac 
62354b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
62364b2f2278SToby Isaac       if (!bSecDof) {
62374b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
62384b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
62394b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
62404b2f2278SToby Isaac         }
62414b2f2278SToby Isaac         continue;
62424b2f2278SToby Isaac       }
6243d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6244d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6245d3d1a6afSToby Isaac       }
6246d3d1a6afSToby Isaac       if (bDof) {
6247d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6248d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
6249d3d1a6afSToby Isaac 
6250d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6251d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6252d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
6253d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
6254d3d1a6afSToby Isaac             PetscInt aFDof;
6255d3d1a6afSToby Isaac 
6256d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
6257d3d1a6afSToby Isaac             allFDof += aFDof;
6258d3d1a6afSToby Isaac           }
6259d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
6260d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
6261d3d1a6afSToby Isaac         }
6262d3d1a6afSToby Isaac       }
6263d3d1a6afSToby Isaac       else {
6264d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6265d3d1a6afSToby Isaac           PetscInt fDof;
6266d3d1a6afSToby Isaac 
6267d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6268d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
6269d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
6270d3d1a6afSToby Isaac         }
6271d3d1a6afSToby Isaac       }
6272d3d1a6afSToby Isaac     }
62734b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
62744b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
62754b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
6276d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6277d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
6278d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
6279d3d1a6afSToby Isaac       }
628019f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
628119f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
628269291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
6283d3d1a6afSToby Isaac     }
6284d3d1a6afSToby Isaac   }
6285d3d1a6afSToby Isaac   else {
6286d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6287d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
62884b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6289d3d1a6afSToby Isaac 
62904b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
62914b2f2278SToby Isaac       if (!bSecDof) {
62924b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
62934b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
62944b2f2278SToby Isaac         continue;
62954b2f2278SToby Isaac       }
6296d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6297d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6298d3d1a6afSToby Isaac       }
6299d3d1a6afSToby Isaac       if (bDof) {
63004b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
6301d3d1a6afSToby Isaac 
6302d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6303d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6304d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
6305d3d1a6afSToby Isaac 
6306d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
6307d3d1a6afSToby Isaac           allDof += aDof;
6308d3d1a6afSToby Isaac         }
6309d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
63104b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
6311d3d1a6afSToby Isaac       }
6312d3d1a6afSToby Isaac       else {
63134b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
6314d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
6315d3d1a6afSToby Isaac       }
6316d3d1a6afSToby Isaac     }
6317d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
6318d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
6319d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6320d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6321d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6322d3d1a6afSToby Isaac     }
632369291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
6324d3d1a6afSToby Isaac   }
6325d3d1a6afSToby Isaac 
63266ecaa68aSToby Isaac   /* output arrays */
632769291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
63286ecaa68aSToby Isaac 
6329d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
6330d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
6331d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
633269291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
633369291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
6334d3d1a6afSToby Isaac   if (numFields) {
6335d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6336d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6337d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
63384b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6339d3d1a6afSToby Isaac 
63404b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
63414b2f2278SToby Isaac       if (!bSecDof) {
63424b2f2278SToby Isaac         continue;
63434b2f2278SToby Isaac       }
6344d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6345d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6346d3d1a6afSToby Isaac       }
6347d3d1a6afSToby Isaac       if (bDof) {
6348d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6349d3d1a6afSToby Isaac 
6350d3d1a6afSToby Isaac         fStart[0] = 0;
6351d3d1a6afSToby Isaac         fEnd[0]   = 0;
6352d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6353d3d1a6afSToby Isaac           PetscInt fDof;
6354d3d1a6afSToby Isaac 
6355d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6356d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6357d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6358d3d1a6afSToby Isaac         }
6359d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
636036fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6361d3d1a6afSToby Isaac 
6362d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6363d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6364d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6365d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6366d3d1a6afSToby Isaac 
6367d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6368d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6369d3d1a6afSToby Isaac         }
6370d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6371d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6372d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6373d3d1a6afSToby Isaac 
6374d3d1a6afSToby 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 */
6375d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6376d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6377302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
637836fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
6379d3d1a6afSToby Isaac         }
6380d3d1a6afSToby Isaac         newP += bDof;
6381d3d1a6afSToby Isaac 
63826ecaa68aSToby Isaac         if (outValues) {
6383d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
6384d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
6385d3d1a6afSToby 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);
6386d3d1a6afSToby Isaac           }
6387d3d1a6afSToby Isaac         }
63886ecaa68aSToby Isaac       }
6389d3d1a6afSToby Isaac       else {
6390d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6391d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6392d3d1a6afSToby Isaac         newP++;
6393d3d1a6afSToby Isaac       }
6394d3d1a6afSToby Isaac     }
6395d3d1a6afSToby Isaac   } else {
6396d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6397d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6398d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
63994b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6400d3d1a6afSToby Isaac 
64014b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
64024b2f2278SToby Isaac       if (!bSecDof) {
64034b2f2278SToby Isaac         continue;
64044b2f2278SToby Isaac       }
6405d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6406d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6407d3d1a6afSToby Isaac       }
6408d3d1a6afSToby Isaac       if (bDof) {
6409d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
6410d3d1a6afSToby Isaac 
6411d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
641236fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
6413d3d1a6afSToby Isaac 
6414d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
6415d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6416d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6417d3d1a6afSToby Isaac 
6418d3d1a6afSToby 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 */
6419d3d1a6afSToby Isaac 
6420d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6421d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6422302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
642336fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
6424d3d1a6afSToby Isaac         }
6425d3d1a6afSToby Isaac         newP += bDof;
6426d3d1a6afSToby Isaac 
6427d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
64286ecaa68aSToby Isaac         if (outValues) {
6429d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
6430d3d1a6afSToby Isaac         }
64316ecaa68aSToby Isaac       }
6432d3d1a6afSToby Isaac       else {
6433d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6434d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6435d3d1a6afSToby Isaac         newP++;
6436d3d1a6afSToby Isaac       }
6437d3d1a6afSToby Isaac     }
6438d3d1a6afSToby Isaac   }
6439d3d1a6afSToby Isaac 
64406ecaa68aSToby Isaac   if (outValues) {
644169291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
6442580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
6443d3d1a6afSToby Isaac     /* multiply constraints on the right */
6444d3d1a6afSToby Isaac     if (numFields) {
6445d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
6446d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
6447d3d1a6afSToby Isaac 
6448d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6449d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
6450d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6451d3d1a6afSToby Isaac           PetscInt c, r, k;
6452d3d1a6afSToby Isaac           PetscInt dof;
6453d3d1a6afSToby Isaac 
6454d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
64554b2f2278SToby Isaac           if (!dof) {
64564b2f2278SToby Isaac             continue;
64574b2f2278SToby Isaac           }
6458d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6459d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6460d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
6461d3d1a6afSToby Isaac 
6462d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6463d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
6464d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
64654acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6466d3d1a6afSToby Isaac                 }
6467d3d1a6afSToby Isaac               }
6468d3d1a6afSToby Isaac             }
6469d3d1a6afSToby Isaac           }
6470d3d1a6afSToby Isaac           else {
6471d3d1a6afSToby Isaac             /* copy this column as is */
6472d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6473d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
6474d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6475d3d1a6afSToby Isaac               }
6476d3d1a6afSToby Isaac             }
6477d3d1a6afSToby Isaac           }
6478d3d1a6afSToby Isaac           oldOff += dof;
6479d3d1a6afSToby Isaac         }
6480d3d1a6afSToby Isaac       }
6481d3d1a6afSToby Isaac     }
6482d3d1a6afSToby Isaac     else {
6483d3d1a6afSToby Isaac       PetscInt oldOff = 0;
6484d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6485d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
6486d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
6487d3d1a6afSToby Isaac         PetscInt c, r, k;
6488d3d1a6afSToby Isaac         PetscInt dof;
6489d3d1a6afSToby Isaac 
6490d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
64914b2f2278SToby Isaac         if (!dof) {
64924b2f2278SToby Isaac           continue;
64934b2f2278SToby Isaac         }
6494d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6495d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6496d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
6497d3d1a6afSToby Isaac 
6498d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6499d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
6500d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
6501d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6502d3d1a6afSToby Isaac               }
6503d3d1a6afSToby Isaac             }
6504d3d1a6afSToby Isaac           }
6505d3d1a6afSToby Isaac         }
6506d3d1a6afSToby Isaac         else {
6507d3d1a6afSToby Isaac           /* copy this column as is */
6508d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6509d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
6510d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6511d3d1a6afSToby Isaac             }
6512d3d1a6afSToby Isaac           }
6513d3d1a6afSToby Isaac         }
6514d3d1a6afSToby Isaac         oldOff += dof;
6515d3d1a6afSToby Isaac       }
6516d3d1a6afSToby Isaac     }
6517d3d1a6afSToby Isaac 
65186ecaa68aSToby Isaac     if (multiplyLeft) {
651969291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
6520580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
6521d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
6522d3d1a6afSToby Isaac       if (numFields) {
6523d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6524d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
6525d3d1a6afSToby Isaac 
6526d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
6527d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
6528d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
6529d3d1a6afSToby Isaac             PetscInt c, r, k;
6530d3d1a6afSToby Isaac             PetscInt dof;
6531d3d1a6afSToby Isaac 
6532d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
6533d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6534d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
6535d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
6536d3d1a6afSToby Isaac 
6537d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
6538d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6539d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
6540d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6541d3d1a6afSToby Isaac                   }
6542d3d1a6afSToby Isaac                 }
6543d3d1a6afSToby Isaac               }
6544d3d1a6afSToby Isaac             }
6545d3d1a6afSToby Isaac             else {
6546d3d1a6afSToby Isaac               /* copy this row as is */
6547d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6548d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6549d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6550d3d1a6afSToby Isaac                 }
6551d3d1a6afSToby Isaac               }
6552d3d1a6afSToby Isaac             }
6553d3d1a6afSToby Isaac             oldOff += dof;
6554d3d1a6afSToby Isaac           }
6555d3d1a6afSToby Isaac         }
6556d3d1a6afSToby Isaac       }
6557d3d1a6afSToby Isaac       else {
6558d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6559d3d1a6afSToby Isaac 
6560d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6561d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6562d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6563d3d1a6afSToby Isaac           PetscInt c, r, k;
6564d3d1a6afSToby Isaac           PetscInt dof;
6565d3d1a6afSToby Isaac 
6566d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6567d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6568d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6569d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6570d3d1a6afSToby Isaac 
6571d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6572d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6573d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6574d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6575d3d1a6afSToby Isaac                 }
6576d3d1a6afSToby Isaac               }
6577d3d1a6afSToby Isaac             }
6578d3d1a6afSToby Isaac           }
6579d3d1a6afSToby Isaac           else {
6580d3d1a6afSToby Isaac             /* copy this row as is */
65819fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6582d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6583d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6584d3d1a6afSToby Isaac               }
6585d3d1a6afSToby Isaac             }
6586d3d1a6afSToby Isaac           }
6587d3d1a6afSToby Isaac           oldOff += dof;
6588d3d1a6afSToby Isaac         }
6589d3d1a6afSToby Isaac       }
6590d3d1a6afSToby Isaac 
659169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
65926ecaa68aSToby Isaac     }
65936ecaa68aSToby Isaac     else {
65946ecaa68aSToby Isaac       newValues = tmpValues;
65956ecaa68aSToby Isaac     }
65966ecaa68aSToby Isaac   }
65976ecaa68aSToby Isaac 
6598d3d1a6afSToby Isaac   /* clean up */
659969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
660069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
66016ecaa68aSToby Isaac 
6602d3d1a6afSToby Isaac   if (numFields) {
6603d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
660469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
660569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
660669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6607d3d1a6afSToby Isaac     }
6608d3d1a6afSToby Isaac   }
6609d3d1a6afSToby Isaac   else {
661069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
661169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
661269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6613d3d1a6afSToby Isaac   }
6614d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6615d3d1a6afSToby Isaac 
6616d3d1a6afSToby Isaac   /* output */
66176ecaa68aSToby Isaac   if (outPoints) {
6618d3d1a6afSToby Isaac     *outPoints = newPoints;
66196ecaa68aSToby Isaac   }
66206ecaa68aSToby Isaac   else {
662169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
66226ecaa68aSToby Isaac   }
662331620726SToby Isaac   if (outValues) {
6624d3d1a6afSToby Isaac     *outValues = newValues;
66256ecaa68aSToby Isaac   }
66266ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6627d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6628d3d1a6afSToby Isaac   }
6629d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6630d3d1a6afSToby Isaac }
6631d3d1a6afSToby Isaac 
66324a1e0b3eSMatthew G. Knepley /*@C
663371f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
66347cd05799SMatthew G. Knepley 
66357cd05799SMatthew G. Knepley   Not collective
66367cd05799SMatthew G. Knepley 
66377cd05799SMatthew G. Knepley   Input Parameters:
66387cd05799SMatthew G. Knepley + dm         - The DM
663971f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
664071f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
664171f0bbf9SMatthew G. Knepley . point      - The point defining the closure
664271f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
66437cd05799SMatthew G. Knepley 
664471f0bbf9SMatthew G. Knepley   Output Parameters:
664571f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
664671f0bbf9SMatthew G. Knepley . indices    - The dof indices
664771f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
664871f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
66497cd05799SMatthew G. Knepley 
665036fa2b79SJed Brown   Notes:
665136fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
665236fa2b79SJed Brown 
665336fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
665436fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
665536fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
665636fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
665736fa2b79SJed Brown   indices (with the above semantics) are implied.
66587cd05799SMatthew G. Knepley 
66597cd05799SMatthew G. Knepley   Level: advanced
66607cd05799SMatthew G. Knepley 
666136fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
66624a1e0b3eSMatthew G. Knepley @*/
666371f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
666471f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
66657773e69fSMatthew G. Knepley {
666671f0bbf9SMatthew G. Knepley   /* Closure ordering */
66677773e69fSMatthew G. Knepley   PetscSection        clSection;
66687773e69fSMatthew G. Knepley   IS                  clPoints;
666971f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
667071f0bbf9SMatthew G. Knepley   PetscInt           *points;
667171f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
667271f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
66734acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
667471f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
667571f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
667671f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
667771f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
667871f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
667971f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
668071f0bbf9SMatthew G. Knepley 
668171f0bbf9SMatthew G. Knepley   PetscInt           *idx;
668271f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
668371f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
66847773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
66857773e69fSMatthew G. Knepley 
668671f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
66877773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66887773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
668936fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
669071f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
669171f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
669271f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
669371f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
66947773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
66957773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6696580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
669771f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
669871f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
6699c459fbc1SJed Brown   if (useClPerm) {
6700c459fbc1SJed Brown     PetscInt depth, clsize;
6701c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
6702c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
6703c459fbc1SJed Brown       PetscInt dof;
6704c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6705c459fbc1SJed Brown       clsize += dof;
6706c459fbc1SJed Brown     }
6707c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
6708c459fbc1SJed Brown   }
670971f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
671071f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
67117773e69fSMatthew G. Knepley     PetscInt dof, fdof;
67127773e69fSMatthew G. Knepley 
67137773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
67147773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
67157773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
67167773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
67177773e69fSMatthew G. Knepley     }
671871f0bbf9SMatthew G. Knepley     Ni += dof;
67197773e69fSMatthew G. Knepley   }
67207773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
672171f0bbf9SMatthew 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);
672271f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
672371f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
672471f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
672571f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
672671f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
672771f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
672871f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
67296ecaa68aSToby Isaac 
673071f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
673171f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
673271f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
673371f0bbf9SMatthew G. Knepley 
673471f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
673571f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
673671f0bbf9SMatthew G. Knepley         if (flip) {
673771f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
673871f0bbf9SMatthew G. Knepley 
673971f0bbf9SMatthew G. Knepley           if (!valCopy) {
674071f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
674171f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
674271f0bbf9SMatthew G. Knepley             *values = valCopy;
674371f0bbf9SMatthew G. Knepley           }
674471f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
674571f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
674671f0bbf9SMatthew G. Knepley 
674771f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
674871f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
674971f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
67506ecaa68aSToby Isaac             }
67516ecaa68aSToby Isaac           }
675271f0bbf9SMatthew G. Knepley         }
675371f0bbf9SMatthew G. Knepley         foffset += fdof;
675471f0bbf9SMatthew G. Knepley       }
675571f0bbf9SMatthew G. Knepley     }
675671f0bbf9SMatthew G. Knepley   }
675771f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
675871f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
675971f0bbf9SMatthew G. Knepley   if (NclC) {
676071f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
676171f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
676271f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
676371f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
676471f0bbf9SMatthew G. Knepley     }
676571f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
676671f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
676771f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
676871f0bbf9SMatthew G. Knepley     }
676971f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
677071f0bbf9SMatthew G. Knepley     Ncl     = NclC;
677171f0bbf9SMatthew G. Knepley     Ni      = NiC;
677271f0bbf9SMatthew G. Knepley     points  = pointsC;
677371f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
677471f0bbf9SMatthew G. Knepley   }
677571f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
677671f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
677771f0bbf9SMatthew G. Knepley   if (Nf) {
677871f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
677971f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
678071f0bbf9SMatthew G. Knepley 
678171f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
678271f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
678371f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
678471f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
678571f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
678671f0bbf9SMatthew G. Knepley 
678771f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
67887773e69fSMatthew G. Knepley       }
67897773e69fSMatthew G. Knepley     } else {
679071f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
679171f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
679271f0bbf9SMatthew G. Knepley 
679371f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
679471f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
679571f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
679671f0bbf9SMatthew G. Knepley          * global section. */
679771f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
679871f0bbf9SMatthew G. Knepley       }
679971f0bbf9SMatthew G. Knepley     }
680071f0bbf9SMatthew G. Knepley   } else {
680171f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
680271f0bbf9SMatthew G. Knepley 
680371f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
680471f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
68054acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
68064acb8e1eSToby Isaac 
680771f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
680871f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
680971f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
681071f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
68117773e69fSMatthew G. Knepley     }
68127773e69fSMatthew G. Knepley   }
681371f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
681471f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
681571f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
681671f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
68174acb8e1eSToby Isaac   }
681871f0bbf9SMatthew G. Knepley   if (NclC) {
681971f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
68207773e69fSMatthew G. Knepley   } else {
682171f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
68227773e69fSMatthew G. Knepley   }
682371f0bbf9SMatthew G. Knepley 
682471f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
682571f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
68267773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
68277773e69fSMatthew G. Knepley }
68287773e69fSMatthew G. Knepley 
68297cd05799SMatthew G. Knepley /*@C
683071f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
68317cd05799SMatthew G. Knepley 
68327cd05799SMatthew G. Knepley   Not collective
68337cd05799SMatthew G. Knepley 
68347cd05799SMatthew G. Knepley   Input Parameters:
68357cd05799SMatthew G. Knepley + dm         - The DM
683671f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
683771f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
683871f0bbf9SMatthew G. Knepley . point      - The point defining the closure
683971f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
684071f0bbf9SMatthew G. Knepley 
684171f0bbf9SMatthew G. Knepley   Output Parameters:
684271f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
684371f0bbf9SMatthew G. Knepley . indices    - The dof indices
684471f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
684571f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
684671f0bbf9SMatthew G. Knepley 
684771f0bbf9SMatthew G. Knepley   Notes:
684871f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
684971f0bbf9SMatthew G. Knepley 
685071f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
685171f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
685271f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
685371f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
685471f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
68557cd05799SMatthew G. Knepley 
68567cd05799SMatthew G. Knepley   Level: advanced
68577cd05799SMatthew G. Knepley 
685871f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
68597cd05799SMatthew G. Knepley @*/
686071f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
686171f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
68627773e69fSMatthew G. Knepley {
68637773e69fSMatthew G. Knepley   PetscErrorCode ierr;
68647773e69fSMatthew G. Knepley 
68657773e69fSMatthew G. Knepley   PetscFunctionBegin;
68667773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6867064a246eSJacob Faibussowitsch   PetscValidPointer(indices, 7);
686869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
68697773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
68707773e69fSMatthew G. Knepley }
68717773e69fSMatthew G. Knepley 
68727f5d1fdeSMatthew G. Knepley /*@C
68737f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
68747f5d1fdeSMatthew G. Knepley 
68757f5d1fdeSMatthew G. Knepley   Not collective
68767f5d1fdeSMatthew G. Knepley 
68777f5d1fdeSMatthew G. Knepley   Input Parameters:
68787f5d1fdeSMatthew G. Knepley + dm - The DM
6879ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6880ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
68817f5d1fdeSMatthew G. Knepley . A - The matrix
6882eaf898f9SPatrick Sanan . point - The point in the DM
68837f5d1fdeSMatthew G. Knepley . values - The array of values
68847f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
68857f5d1fdeSMatthew G. Knepley 
68867f5d1fdeSMatthew G. Knepley   Fortran Notes:
68877f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
68887f5d1fdeSMatthew G. Knepley 
68897f5d1fdeSMatthew G. Knepley   Level: intermediate
68907f5d1fdeSMatthew G. Knepley 
68914a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
68927f5d1fdeSMatthew G. Knepley @*/
68937c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6894552f7358SJed Brown {
6895552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
6896552f7358SJed Brown   PetscInt          *indices;
689771f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
689871f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
6899552f7358SJed Brown   PetscErrorCode     ierr;
6900552f7358SJed Brown 
6901552f7358SJed Brown   PetscFunctionBegin;
6902552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
690392fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
69043dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6905e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
69063dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
69073dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6908552f7358SJed Brown 
690971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
69100d644c17SKarl Rupp 
6911b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
69124a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6913552f7358SJed Brown   if (ierr) {
6914552f7358SJed Brown     PetscMPIInt    rank;
6915552f7358SJed Brown     PetscErrorCode ierr2;
6916552f7358SJed Brown 
691755b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6918e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6919b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
692071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
692171f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6922552f7358SJed Brown     CHKERRQ(ierr);
6923552f7358SJed Brown   }
69244a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
69254a1e0b3eSMatthew G. Knepley     PetscInt i;
69264a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
69274a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
69284a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
69294a1e0b3eSMatthew G. Knepley   }
693071f0bbf9SMatthew G. Knepley 
693171f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
693271f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
693371f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
69344acb8e1eSToby Isaac }
693571f0bbf9SMatthew G. Knepley 
69364a1e0b3eSMatthew G. Knepley /*@C
69374a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
69384a1e0b3eSMatthew G. Knepley 
69394a1e0b3eSMatthew G. Knepley   Not collective
69404a1e0b3eSMatthew G. Knepley 
69414a1e0b3eSMatthew G. Knepley   Input Parameters:
69424a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
69434a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
69444a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
69454a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
69464a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
69474a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
69484a1e0b3eSMatthew G. Knepley . A - The matrix
69494a1e0b3eSMatthew G. Knepley . point - The point in the DMs
69504a1e0b3eSMatthew G. Knepley . values - The array of values
69514a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
69524a1e0b3eSMatthew G. Knepley 
69534a1e0b3eSMatthew G. Knepley   Level: intermediate
69544a1e0b3eSMatthew G. Knepley 
69554a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
69564a1e0b3eSMatthew G. Knepley @*/
695771f0bbf9SMatthew 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)
695871f0bbf9SMatthew G. Knepley {
695971f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
696071f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
696171f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
696271f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
696371f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
696471f0bbf9SMatthew G. Knepley 
696571f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
696671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
696771f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
696871f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
696971f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
697071f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
697171f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
697271f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
697371f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
697471f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
697571f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
697671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
697771f0bbf9SMatthew G. Knepley 
697871f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
697971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
698071f0bbf9SMatthew G. Knepley 
698171f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
69824a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
698371f0bbf9SMatthew G. Knepley   if (ierr) {
698471f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
698571f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
698671f0bbf9SMatthew G. Knepley 
698755b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
698871f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
698971f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
699071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
699171f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
699271f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
699371f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
6994d3d1a6afSToby Isaac   }
699571f0bbf9SMatthew G. Knepley 
699671f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
699771f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
699871f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
6999552f7358SJed Brown   PetscFunctionReturn(0);
7000552f7358SJed Brown }
7001552f7358SJed Brown 
7002de41b84cSMatthew 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)
7003de41b84cSMatthew G. Knepley {
7004de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
7005de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
7006de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
7007de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
700817c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7009de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
7010412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
70114ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7012de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
7013de41b84cSMatthew G. Knepley 
7014de41b84cSMatthew G. Knepley   PetscFunctionBegin;
7015de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
7016de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
701792fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
7018de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
701992fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
7020de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7021e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
7022de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7023e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
7024de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
7025de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
7026de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
7027de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7028580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7029580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
7030de41b84cSMatthew G. Knepley   /* Column indices */
7031de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
70324ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
7033de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
7034de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
7035de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
7036de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
7037de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7038de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
7039de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
7040de41b84cSMatthew G. Knepley       ++q;
7041de41b84cSMatthew G. Knepley     }
7042de41b84cSMatthew G. Knepley   }
7043de41b84cSMatthew G. Knepley   numCPoints = q;
7044de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
7045de41b84cSMatthew G. Knepley     PetscInt fdof;
7046de41b84cSMatthew G. Knepley 
7047de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
70484ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7049de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7050de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
7051de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
7052de41b84cSMatthew G. Knepley     }
7053de41b84cSMatthew G. Knepley     numCIndices += dof;
7054de41b84cSMatthew G. Knepley   }
7055de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
7056de41b84cSMatthew G. Knepley   /* Row indices */
7057412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7058412e9a14SMatthew G. Knepley   {
7059412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7060412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7061412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7062412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7063412e9a14SMatthew G. Knepley   }
706469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7065de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
7066de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
7067de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7068de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
7069de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
7070de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
7071de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
70724ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
70734ca5e9f5SMatthew G. Knepley         if (!dof) continue;
70744ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
70754ca5e9f5SMatthew G. Knepley         if (s < q) continue;
7076de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
7077de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
7078de41b84cSMatthew G. Knepley         ++q;
7079de41b84cSMatthew G. Knepley       }
7080de41b84cSMatthew G. Knepley     }
7081de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7082de41b84cSMatthew G. Knepley   }
7083de41b84cSMatthew G. Knepley   numFPoints = q;
7084de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
7085de41b84cSMatthew G. Knepley     PetscInt fdof;
7086de41b84cSMatthew G. Knepley 
7087de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
70884ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7089de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7090de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
7091de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
7092de41b84cSMatthew G. Knepley     }
7093de41b84cSMatthew G. Knepley     numFIndices += dof;
7094de41b84cSMatthew G. Knepley   }
7095de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
7096de41b84cSMatthew G. Knepley 
70978ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
70988ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
709969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
710069291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7101de41b84cSMatthew G. Knepley   if (numFields) {
71024acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
71034acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
71044acb8e1eSToby Isaac 
71054acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
71064acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
71074acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7108de41b84cSMatthew G. Knepley     }
71094acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
71104acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
711136fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
71124acb8e1eSToby Isaac     }
71134acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
71144acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
711536fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
71164acb8e1eSToby Isaac     }
71174acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
71184acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
71194acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7120de41b84cSMatthew G. Knepley     }
7121de41b84cSMatthew G. Knepley   } else {
71224acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
71234acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
71244acb8e1eSToby Isaac 
71254acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71264acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
71274acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
71284acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
71294acb8e1eSToby Isaac 
71304acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
713136fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
7132de41b84cSMatthew G. Knepley     }
71334acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
71344acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
71354acb8e1eSToby Isaac 
71364acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
713736fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
7138de41b84cSMatthew G. Knepley     }
71394acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71404acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
7141de41b84cSMatthew G. Knepley   }
7142de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
71434acb8e1eSToby Isaac   /* TODO: flips */
7144de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7145de41b84cSMatthew G. Knepley   if (ierr) {
7146de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
7147de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
7148de41b84cSMatthew G. Knepley 
714955b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
7150e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
7151de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
715269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
715369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
7154de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
7155de41b84cSMatthew G. Knepley   }
715669291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7157de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
715869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
715969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7160de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
7161de41b84cSMatthew G. Knepley }
7162de41b84cSMatthew G. Knepley 
71637c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
71647c927364SMatthew G. Knepley {
71657c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
71667c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
71677c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
716817c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7169412e9a14SMatthew G. Knepley   DMPolytopeType ct;
71707c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
71717c927364SMatthew G. Knepley   PetscErrorCode ierr;
71727c927364SMatthew G. Knepley 
71737c927364SMatthew G. Knepley   PetscFunctionBegin;
71747c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
71757c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
717692fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
71777c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
717892fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
71797c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7180e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
71817c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7182e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
71837c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
71847c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
71857c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7186580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7187580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
71887c927364SMatthew G. Knepley   /* Column indices */
71897c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
71907c927364SMatthew G. Knepley   maxFPoints = numCPoints;
71917c927364SMatthew G. Knepley   /* Compress out points not in the section */
71927c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
71937c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
71947c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
71957c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
71967c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
71977c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
71987c927364SMatthew G. Knepley       ++q;
71997c927364SMatthew G. Knepley     }
72007c927364SMatthew G. Knepley   }
72017c927364SMatthew G. Knepley   numCPoints = q;
72027c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
72037c927364SMatthew G. Knepley     PetscInt fdof;
72047c927364SMatthew G. Knepley 
72057c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
72067c927364SMatthew G. Knepley     if (!dof) continue;
72077c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
72087c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
72097c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
72107c927364SMatthew G. Knepley     }
72117c927364SMatthew G. Knepley     numCIndices += dof;
72127c927364SMatthew G. Knepley   }
72137c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
72147c927364SMatthew G. Knepley   /* Row indices */
7215412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7216412e9a14SMatthew G. Knepley   {
7217412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7218412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7219412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7220412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7221412e9a14SMatthew G. Knepley   }
722269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
72237c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
72247c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
72257c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
72267c927364SMatthew G. Knepley     /* Compress out points not in the section */
72277c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
72287c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
72297c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
72307c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
72317c927364SMatthew G. Knepley         if (!dof) continue;
72327c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
72337c927364SMatthew G. Knepley         if (s < q) continue;
72347c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
72357c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
72367c927364SMatthew G. Knepley         ++q;
72377c927364SMatthew G. Knepley       }
72387c927364SMatthew G. Knepley     }
72397c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
72407c927364SMatthew G. Knepley   }
72417c927364SMatthew G. Knepley   numFPoints = q;
72427c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
72437c927364SMatthew G. Knepley     PetscInt fdof;
72447c927364SMatthew G. Knepley 
72457c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
72467c927364SMatthew G. Knepley     if (!dof) continue;
72477c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
72487c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
72497c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
72507c927364SMatthew G. Knepley     }
72517c927364SMatthew G. Knepley     numFIndices += dof;
72527c927364SMatthew G. Knepley   }
72537c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
72547c927364SMatthew G. Knepley 
72558ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
72568ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
72577c927364SMatthew G. Knepley   if (numFields) {
72584acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
72594acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
72604acb8e1eSToby Isaac 
72614acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
72624acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
72634acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
72647c927364SMatthew G. Knepley     }
72654acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
72664acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
726736fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
72684acb8e1eSToby Isaac     }
72694acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
72704acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
727136fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
72724acb8e1eSToby Isaac     }
72734acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
72744acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
72754acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
72767c927364SMatthew G. Knepley     }
72777c927364SMatthew G. Knepley   } else {
72784acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
72794acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
72804acb8e1eSToby Isaac 
72814acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
72824acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
72834acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
72844acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
72854acb8e1eSToby Isaac 
72864acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
728736fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
72887c927364SMatthew G. Knepley     }
72894acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
72904acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
72914acb8e1eSToby Isaac 
72924acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
729336fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
72947c927364SMatthew G. Knepley     }
72954acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
72964acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
72977c927364SMatthew G. Knepley   }
729869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
72997c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
73007c927364SMatthew G. Knepley   PetscFunctionReturn(0);
73017c927364SMatthew G. Knepley }
73027c927364SMatthew G. Knepley 
73037cd05799SMatthew G. Knepley /*@C
73047cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
73057cd05799SMatthew G. Knepley 
73067cd05799SMatthew G. Knepley   Input Parameter:
73077cd05799SMatthew G. Knepley . dm   - The DMPlex object
73087cd05799SMatthew G. Knepley 
73097cd05799SMatthew G. Knepley   Output Parameter:
73107cd05799SMatthew G. Knepley . cellHeight - The height of a cell
73117cd05799SMatthew G. Knepley 
73127cd05799SMatthew G. Knepley   Level: developer
73137cd05799SMatthew G. Knepley 
73147cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
73157cd05799SMatthew G. Knepley @*/
7316552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7317552f7358SJed Brown {
7318552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7319552f7358SJed Brown 
7320552f7358SJed Brown   PetscFunctionBegin;
7321552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7322552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
7323552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
7324552f7358SJed Brown   PetscFunctionReturn(0);
7325552f7358SJed Brown }
7326552f7358SJed Brown 
73277cd05799SMatthew G. Knepley /*@C
73287cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
73297cd05799SMatthew G. Knepley 
73307cd05799SMatthew G. Knepley   Input Parameters:
73317cd05799SMatthew G. Knepley + dm   - The DMPlex object
73327cd05799SMatthew G. Knepley - cellHeight - The height of a cell
73337cd05799SMatthew G. Knepley 
73347cd05799SMatthew G. Knepley   Level: developer
73357cd05799SMatthew G. Knepley 
73367cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
73377cd05799SMatthew G. Knepley @*/
7338552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7339552f7358SJed Brown {
7340552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7341552f7358SJed Brown 
7342552f7358SJed Brown   PetscFunctionBegin;
7343552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7344552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7345552f7358SJed Brown   PetscFunctionReturn(0);
7346552f7358SJed Brown }
7347552f7358SJed Brown 
7348e6139122SMatthew G. Knepley /*@
7349e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7350e6139122SMatthew G. Knepley 
7351e6139122SMatthew G. Knepley   Input Parameter:
7352e6139122SMatthew G. Knepley . dm - The DMPlex object
7353e6139122SMatthew G. Knepley 
7354e6139122SMatthew G. Knepley   Output Parameters:
73552a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
73562a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7357e6139122SMatthew G. Knepley 
73582a9f31c0SMatthew G. Knepley   Level: advanced
7359e6139122SMatthew G. Knepley 
73608065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7361e6139122SMatthew G. Knepley @*/
7362e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7363e6139122SMatthew G. Knepley {
7364412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7365e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7366e6139122SMatthew G. Knepley 
7367e6139122SMatthew G. Knepley   PetscFunctionBegin;
7368e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7369412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
7370412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
7371e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
7372e6139122SMatthew G. Knepley }
7373e6139122SMatthew G. Knepley 
7374552f7358SJed Brown /* We can easily have a form that takes an IS instead */
73759886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7376552f7358SJed Brown {
7377552f7358SJed Brown   PetscSection   section, globalSection;
7378552f7358SJed Brown   PetscInt      *numbers, p;
7379552f7358SJed Brown   PetscErrorCode ierr;
7380552f7358SJed Brown 
7381552f7358SJed Brown   PetscFunctionBegin;
738282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
7383552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
7384552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7385552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
7386552f7358SJed Brown   }
7387552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
738815b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
7389854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
7390552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7391552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
7392ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7393ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
7394552f7358SJed Brown   }
739582f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
7396ef48cebcSMatthew G. Knepley   if (globalSize) {
7397ef48cebcSMatthew G. Knepley     PetscLayout layout;
7398ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
7399ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
7400ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
7401ef48cebcSMatthew G. Knepley   }
7402552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
7403552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
7404552f7358SJed Brown   PetscFunctionReturn(0);
7405552f7358SJed Brown }
7406552f7358SJed Brown 
740781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7408552f7358SJed Brown {
7409412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
7410552f7358SJed Brown   PetscErrorCode ierr;
7411552f7358SJed Brown 
7412552f7358SJed Brown   PetscFunctionBegin;
7413552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7414412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
7415412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
74169886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
741781ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
7418552f7358SJed Brown }
741981ed3555SMatthew G. Knepley 
74208dab3259SMatthew G. Knepley /*@
74217cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
74227cd05799SMatthew G. Knepley 
74237cd05799SMatthew G. Knepley   Input Parameter:
74247cd05799SMatthew G. Knepley . dm   - The DMPlex object
74257cd05799SMatthew G. Knepley 
74267cd05799SMatthew G. Knepley   Output Parameter:
74277cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
74287cd05799SMatthew G. Knepley 
74297cd05799SMatthew G. Knepley   Level: developer
74307cd05799SMatthew G. Knepley 
74317cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
74327cd05799SMatthew G. Knepley @*/
743381ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
743481ed3555SMatthew G. Knepley {
743581ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
743681ed3555SMatthew G. Knepley   PetscErrorCode ierr;
743781ed3555SMatthew G. Knepley 
743881ed3555SMatthew G. Knepley   PetscFunctionBegin;
743981ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
744081ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
7441552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
7442552f7358SJed Brown   PetscFunctionReturn(0);
7443552f7358SJed Brown }
7444552f7358SJed Brown 
744581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
744681ed3555SMatthew G. Knepley {
7447412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
744881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
744981ed3555SMatthew G. Knepley 
745081ed3555SMatthew G. Knepley   PetscFunctionBegin;
745181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
745281ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
74539886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
745481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
745581ed3555SMatthew G. Knepley }
745681ed3555SMatthew G. Knepley 
74578dab3259SMatthew G. Knepley /*@
74586aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
74597cd05799SMatthew G. Knepley 
74607cd05799SMatthew G. Knepley   Input Parameter:
74617cd05799SMatthew G. Knepley . dm   - The DMPlex object
74627cd05799SMatthew G. Knepley 
74637cd05799SMatthew G. Knepley   Output Parameter:
74647cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
74657cd05799SMatthew G. Knepley 
74667cd05799SMatthew G. Knepley   Level: developer
74677cd05799SMatthew G. Knepley 
74687cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
74697cd05799SMatthew G. Knepley @*/
7470552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7471552f7358SJed Brown {
7472552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
7473552f7358SJed Brown   PetscErrorCode ierr;
7474552f7358SJed Brown 
7475552f7358SJed Brown   PetscFunctionBegin;
7476552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
747781ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
7478552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
7479552f7358SJed Brown   PetscFunctionReturn(0);
7480552f7358SJed Brown }
7481552f7358SJed Brown 
74828dab3259SMatthew G. Knepley /*@
74837cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
74847cd05799SMatthew G. Knepley 
74857cd05799SMatthew G. Knepley   Input Parameter:
74867cd05799SMatthew G. Knepley . dm   - The DMPlex object
74877cd05799SMatthew G. Knepley 
74887cd05799SMatthew G. Knepley   Output Parameter:
74897cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
74907cd05799SMatthew G. Knepley 
74917cd05799SMatthew G. Knepley   Level: developer
74927cd05799SMatthew G. Knepley 
74937cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
74947cd05799SMatthew G. Knepley @*/
7495ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7496ef48cebcSMatthew G. Knepley {
7497ef48cebcSMatthew G. Knepley   IS             nums[4];
7498862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
7499ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
7500ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
7501ef48cebcSMatthew G. Knepley 
7502ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
7503ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7504ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
75058abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
75068abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
7507862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7508862913ffSStefano Zampini     PetscInt end;
7509862913ffSStefano Zampini 
7510862913ffSStefano Zampini     depths[d] = depth-d;
7511862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
7512862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7513862913ffSStefano Zampini   }
7514862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
7515820f2d46SBarry Smith   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
7516862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7517862913ffSStefano 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]);
7518862913ffSStefano Zampini   }
7519ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
7520ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
7521ef48cebcSMatthew G. Knepley 
7522862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
75239886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
7524ef48cebcSMatthew G. Knepley     shift += gsize;
7525ef48cebcSMatthew G. Knepley   }
7526302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7527ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7528ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7529ef48cebcSMatthew G. Knepley }
7530ef48cebcSMatthew G. Knepley 
753108a22f4bSMatthew G. Knepley /*@
753208a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
753308a22f4bSMatthew G. Knepley 
753408a22f4bSMatthew G. Knepley   Input Parameter:
753508a22f4bSMatthew G. Knepley . dm - The DMPlex object
753608a22f4bSMatthew G. Knepley 
753708a22f4bSMatthew G. Knepley   Output Parameter:
753808a22f4bSMatthew G. Knepley . ranks - The rank field
753908a22f4bSMatthew G. Knepley 
754008a22f4bSMatthew G. Knepley   Options Database Keys:
754108a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
754208a22f4bSMatthew G. Knepley 
754308a22f4bSMatthew G. Knepley   Level: intermediate
754408a22f4bSMatthew G. Knepley 
754508a22f4bSMatthew G. Knepley .seealso: DMView()
754608a22f4bSMatthew G. Knepley @*/
754708a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
754808a22f4bSMatthew G. Knepley {
754908a22f4bSMatthew G. Knepley   DM             rdm;
755008a22f4bSMatthew G. Knepley   PetscFE        fe;
755108a22f4bSMatthew G. Knepley   PetscScalar   *r;
755208a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
7553a55f9a55SMatthew G. Knepley   DMPolytopeType ct;
755408a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
7555a55f9a55SMatthew G. Knepley   PetscBool      simplex;
755608a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
755708a22f4bSMatthew G. Knepley 
755808a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7559f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7560f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
7561ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
756208a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
756308a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7564a55f9a55SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7565a55f9a55SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
7566a55f9a55SMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
7567a55f9a55SMatthew G. Knepley   ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
756808a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7569e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
757008a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7571e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
757208a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
757308a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
757408a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
757508a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
757608a22f4bSMatthew G. Knepley     PetscScalar *lr;
757708a22f4bSMatthew G. Knepley 
757808a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
757971f09efeSPierre Jolivet     if (lr) *lr = rank;
758008a22f4bSMatthew G. Knepley   }
758108a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
758208a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
758308a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
758408a22f4bSMatthew G. Knepley }
758508a22f4bSMatthew G. Knepley 
7586ca8062c8SMatthew G. Knepley /*@
758718e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
758818e14f0cSMatthew G. Knepley 
758918e14f0cSMatthew G. Knepley   Input Parameters:
759018e14f0cSMatthew G. Knepley + dm    - The DMPlex
759118e14f0cSMatthew G. Knepley - label - The DMLabel
759218e14f0cSMatthew G. Knepley 
759318e14f0cSMatthew G. Knepley   Output Parameter:
759418e14f0cSMatthew G. Knepley . val - The label value field
759518e14f0cSMatthew G. Knepley 
759618e14f0cSMatthew G. Knepley   Options Database Keys:
759718e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
759818e14f0cSMatthew G. Knepley 
759918e14f0cSMatthew G. Knepley   Level: intermediate
760018e14f0cSMatthew G. Knepley 
760118e14f0cSMatthew G. Knepley .seealso: DMView()
760218e14f0cSMatthew G. Knepley @*/
760318e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
760418e14f0cSMatthew G. Knepley {
760518e14f0cSMatthew G. Knepley   DM             rdm;
760618e14f0cSMatthew G. Knepley   PetscFE        fe;
760718e14f0cSMatthew G. Knepley   PetscScalar   *v;
760818e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
760918e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
761018e14f0cSMatthew G. Knepley 
761118e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
761218e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
761318e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
761418e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
761518e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
761618e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
761718e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
761818e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7619e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
762018e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7621e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
762218e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
762318e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7624effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
762518e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
762618e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
762718e14f0cSMatthew G. Knepley     PetscScalar *lv;
762818e14f0cSMatthew G. Knepley     PetscInt     cval;
762918e14f0cSMatthew G. Knepley 
763018e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
763118e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
763218e14f0cSMatthew G. Knepley     *lv = cval;
763318e14f0cSMatthew G. Knepley   }
763418e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
763518e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
763618e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
763718e14f0cSMatthew G. Knepley }
763818e14f0cSMatthew G. Knepley 
763918e14f0cSMatthew G. Knepley /*@
7640ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7641ca8062c8SMatthew G. Knepley 
764269916449SMatthew G. Knepley   Input Parameter:
764369916449SMatthew G. Knepley . dm - The DMPlex object
7644ca8062c8SMatthew G. Knepley 
764595eb5ee5SVaclav Hapla   Notes:
764695eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
764795eb5ee5SVaclav Hapla 
764895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7649ca8062c8SMatthew G. Knepley 
7650ca8062c8SMatthew G. Knepley   Level: developer
7651ca8062c8SMatthew G. Knepley 
765295eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7653ca8062c8SMatthew G. Knepley @*/
7654ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7655ca8062c8SMatthew G. Knepley {
7656ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7657ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7658ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
765957beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
766057beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7661ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7662ca8062c8SMatthew G. Knepley 
7663ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7664ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7665412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7666ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7667ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7668ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7669ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7670ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7671ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7672ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7673ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
767442e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
767542e66dfaSMatthew G. Knepley       PetscInt  d;
767642e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
767742e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
767842e66dfaSMatthew G. Knepley       }
7679ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7680ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7681ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7682ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7683ca8062c8SMatthew G. Knepley       }
768442e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
76858ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7686ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
76878ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7688ca8062c8SMatthew G. Knepley         }
7689302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
76908ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7691ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
76928ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7693ca8062c8SMatthew G. Knepley         }
7694302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
76958ccfff9cSToby 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]);
76968ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7697ca8062c8SMatthew G. Knepley       }
769842e66dfaSMatthew G. Knepley     }
769957beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
770057beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7701ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7702ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7703ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7704ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7705ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7706ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
770757beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
770857beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7709ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7710ca8062c8SMatthew G. Knepley       }
7711ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
77128ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7713ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
77148ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7715ca8062c8SMatthew G. Knepley         }
7716302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
77178ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7718ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
77198ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7720ca8062c8SMatthew G. Knepley         }
7721302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
77228ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7723ca8062c8SMatthew G. Knepley       }
7724ca8062c8SMatthew G. Knepley     }
7725ca8062c8SMatthew G. Knepley   }
772657beb4faSStefano Zampini   if (storagecheck) {
7727ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7728ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
77298ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
773057beb4faSStefano Zampini   }
7731ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7732ca8062c8SMatthew G. Knepley }
7733ca8062c8SMatthew G. Knepley 
7734412e9a14SMatthew G. Knepley /*
7735412e9a14SMatthew 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.
7736412e9a14SMatthew G. Knepley */
7737412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7738412e9a14SMatthew G. Knepley {
7739412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7740412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7741412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7742412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7743412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7744412e9a14SMatthew G. Knepley 
7745412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7746412e9a14SMatthew G. Knepley   *unsplit = 0;
7747412e9a14SMatthew G. Knepley   switch (ct) {
7748412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7749412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7750412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7751412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7752412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7753412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7754412e9a14SMatthew G. Knepley       }
7755412e9a14SMatthew G. Knepley       break;
7756412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7757412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7758412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7759412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7760412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7761412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7762412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7763412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7764412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7765412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7766412e9a14SMatthew G. Knepley             PetscInt p;
7767412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7768412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7769412e9a14SMatthew G. Knepley           }
7770412e9a14SMatthew G. Knepley         }
7771412e9a14SMatthew G. Knepley       }
7772412e9a14SMatthew G. Knepley       break;
7773412e9a14SMatthew G. Knepley     default: break;
7774412e9a14SMatthew G. Knepley   }
7775412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7776412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7777412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7778412e9a14SMatthew G. Knepley   }
7779412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7780412e9a14SMatthew G. Knepley }
7781412e9a14SMatthew G. Knepley 
7782ca8062c8SMatthew G. Knepley /*@
7783ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7784ca8062c8SMatthew G. Knepley 
7785ca8062c8SMatthew G. Knepley   Input Parameters:
7786ca8062c8SMatthew G. Knepley + dm - The DMPlex object
778758723a97SMatthew G. Knepley - cellHeight - Normally 0
7788ca8062c8SMatthew G. Knepley 
778995eb5ee5SVaclav Hapla   Notes:
779095eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
779125c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7792ca8062c8SMatthew G. Knepley 
779395eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
779495eb5ee5SVaclav Hapla 
7795ca8062c8SMatthew G. Knepley   Level: developer
7796ca8062c8SMatthew G. Knepley 
779795eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7798ca8062c8SMatthew G. Knepley @*/
779925c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7800ca8062c8SMatthew G. Knepley {
7801412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7802412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7803412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7804ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7805ca8062c8SMatthew G. Knepley 
7806ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7807ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7808412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
780925c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
781058723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7811412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7812412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7813412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
781458723a97SMatthew G. Knepley 
7815412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7816412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7817412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7818412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7819412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7820d4961f80SStefano 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));
7821412e9a14SMatthew G. Knepley     }
782258723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
782358723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
782458723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7825412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
782658723a97SMatthew G. Knepley     }
782758723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7828412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7829412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7830412e9a14SMatthew G. Knepley       PetscInt unsplit;
783142363296SMatthew G. Knepley 
7832412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7833412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
783442363296SMatthew G. Knepley     }
7835d4961f80SStefano 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));
783642363296SMatthew G. Knepley   }
7837ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7838ca8062c8SMatthew G. Knepley }
78399bf0dad6SMatthew G. Knepley 
78409bf0dad6SMatthew G. Knepley /*@
78419bf0dad6SMatthew 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
78429bf0dad6SMatthew G. Knepley 
7843899ea2b8SJacob Faibussowitsch   Not Collective
7844899ea2b8SJacob Faibussowitsch 
78459bf0dad6SMatthew G. Knepley   Input Parameters:
78469bf0dad6SMatthew G. Knepley + dm - The DMPlex object
78479bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
78489bf0dad6SMatthew G. Knepley 
784945da879fSVaclav Hapla   Notes:
785045da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
785145da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
785245da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
785345da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
78549bf0dad6SMatthew G. Knepley 
785595eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
785695eb5ee5SVaclav Hapla 
78579bf0dad6SMatthew G. Knepley   Level: developer
78589bf0dad6SMatthew G. Knepley 
785995eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
78609bf0dad6SMatthew G. Knepley @*/
786125c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
78629bf0dad6SMatthew G. Knepley {
7863ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
78649bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7865899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
78669bf0dad6SMatthew G. Knepley 
78679bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
78689bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7869899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
787045da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
787145da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7872899ea2b8SJacob Faibussowitsch     PetscMPIInt rank;
7873899ea2b8SJacob Faibussowitsch     MPI_Comm    comm;
7874899ea2b8SJacob Faibussowitsch 
7875899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7876ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
787745da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7878899ea2b8SJacob Faibussowitsch   }
7879899ea2b8SJacob Faibussowitsch 
7880c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7881ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
78829bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7883ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
78843554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
78853554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7886412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7887412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7888ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7889412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7890412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
78919bf0dad6SMatthew G. Knepley 
7892ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7893412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7894412e9a14SMatthew G. Knepley       if (unsplit) continue;
78959bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
78969bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
78979bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
78989bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
78999bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
79009bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
79019bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
79029bf0dad6SMatthew G. Knepley       }
7903412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
7904d4961f80SStefano 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);
79059bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
7906d4961f80SStefano Zampini         DMPolytopeType fct;
79079bf0dad6SMatthew G. Knepley         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
79089bf0dad6SMatthew G. Knepley 
7909d4961f80SStefano Zampini         ierr = DMPlexGetCellType(dm, cone[f], &fct);CHKERRQ(ierr);
79109bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
79119bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
79129bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
79139bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
79149bf0dad6SMatthew G. Knepley         }
7915d4961f80SStefano 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]);
79169bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7917d4961f80SStefano 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]);
79189bf0dad6SMatthew G. Knepley         }
79199bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7920412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
79219bf0dad6SMatthew G. Knepley       }
7922412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
79239bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
79249bf0dad6SMatthew G. Knepley     }
79253554e41dSMatthew G. Knepley   }
7926552f7358SJed Brown   PetscFunctionReturn(0);
7927552f7358SJed Brown }
79283913d7c8SMatthew G. Knepley 
7929bb6a34a8SMatthew G. Knepley /*@
7930bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7931bb6a34a8SMatthew G. Knepley 
7932bb6a34a8SMatthew G. Knepley   Input Parameter:
7933bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7934bb6a34a8SMatthew G. Knepley 
793595eb5ee5SVaclav Hapla   Notes:
793695eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
793795eb5ee5SVaclav Hapla 
793895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7939bb6a34a8SMatthew G. Knepley 
7940bb6a34a8SMatthew G. Knepley   Level: developer
7941bb6a34a8SMatthew G. Knepley 
794295eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7943bb6a34a8SMatthew G. Knepley @*/
7944bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7945bb6a34a8SMatthew G. Knepley {
7946a2a9e04cSMatthew G. Knepley   Vec            coordinates;
7947bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7948bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7949412e9a14SMatthew G. Knepley   PetscBool      periodic;
795051a74b61SMatthew G. Knepley   PetscInt       dim, depth, dE, d, cStart, cEnd, c;
7951bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7952bb6a34a8SMatthew G. Knepley 
7953bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7954bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
795551a74b61SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
795651a74b61SMatthew G. Knepley   if (dim != dE) PetscFunctionReturn(0);
7957bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7958412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7959bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7960bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7961a2a9e04cSMatthew G. Knepley   /* Make sure local coordinates are created, because that step is collective */
7962a2a9e04cSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
7963412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7964412e9a14SMatthew G. Knepley     DMPolytopeType ct;
7965412e9a14SMatthew G. Knepley     PetscInt       unsplit;
7966412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
7967412e9a14SMatthew G. Knepley 
7968412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7969412e9a14SMatthew G. Knepley     switch (ct) {
7970412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7971412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7972412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7973412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
7974412e9a14SMatthew G. Knepley       default: break;
7975412e9a14SMatthew G. Knepley     }
7976412e9a14SMatthew G. Knepley     switch (ct) {
7977412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
7978412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7979412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7980a2a9e04cSMatthew G. Knepley       case DM_POLYTOPE_PYRAMID:
7981412e9a14SMatthew G. Knepley         continue;
7982412e9a14SMatthew G. Knepley       default: break;
7983412e9a14SMatthew G. Knepley     }
7984412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7985412e9a14SMatthew G. Knepley     if (unsplit) continue;
7986bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7987d4961f80SStefano 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);
7988bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7989412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
7990bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7991d4961f80SStefano 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);
7992bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7993bb6a34a8SMatthew G. Knepley     }
7994bb6a34a8SMatthew G. Knepley   }
7995bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7996bb6a34a8SMatthew G. Knepley }
7997bb6a34a8SMatthew G. Knepley 
799803da9461SVaclav Hapla /*@
7999e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
800003da9461SVaclav Hapla 
800103da9461SVaclav Hapla   Input Parameters:
800203da9461SVaclav Hapla . dm - The DMPlex object
800303da9461SVaclav Hapla 
8004e83a0d2dSVaclav Hapla   Notes:
8005e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
80068918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
800703da9461SVaclav Hapla 
800895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
800995eb5ee5SVaclav Hapla 
801003da9461SVaclav Hapla   Level: developer
801103da9461SVaclav Hapla 
801295eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
801303da9461SVaclav Hapla @*/
801403da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
801503da9461SVaclav Hapla {
8016f0cfc026SVaclav Hapla   PetscSF         pointSF;
8017f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
8018f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
8019f0cfc026SVaclav Hapla   PetscBool       distributed;
802003da9461SVaclav Hapla   PetscErrorCode  ierr;
802103da9461SVaclav Hapla 
802203da9461SVaclav Hapla   PetscFunctionBegin;
802303da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8024f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
8025f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
8026f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
8027f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
8028f0cfc026SVaclav Hapla   if (overlap) {
80298918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
80308918e3e2SVaclav Hapla     PetscFunctionReturn(0);
80318918e3e2SVaclav Hapla   }
8032f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
8033f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
8034f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
8035f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
8036f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
803703da9461SVaclav Hapla 
8038ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
8039f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8040f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8041f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8042f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
8043f5869d18SMatthew G. Knepley 
8044f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
804503da9461SVaclav Hapla   }
8046ece87651SVaclav Hapla 
8047f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8048f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8049f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
8050f5869d18SMatthew G. Knepley     const PetscInt *cone;
8051f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
8052f5869d18SMatthew G. Knepley 
8053f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
8054f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
8055f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
8056f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
8057f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
8058f5869d18SMatthew 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]);
8059f5869d18SMatthew G. Knepley       }
8060f5869d18SMatthew G. Knepley     }
8061ece87651SVaclav Hapla   }
806203da9461SVaclav Hapla   PetscFunctionReturn(0);
806303da9461SVaclav Hapla }
806403da9461SVaclav Hapla 
8065068a5610SStefano Zampini typedef struct cell_stats
8066068a5610SStefano Zampini {
8067068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
8068068a5610SStefano Zampini   PetscInt  count;
8069068a5610SStefano Zampini } cell_stats_t;
8070068a5610SStefano Zampini 
807125befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
8072068a5610SStefano Zampini {
8073068a5610SStefano Zampini   PetscInt i, N = *len;
8074068a5610SStefano Zampini 
8075068a5610SStefano Zampini   for (i = 0; i < N; i++) {
8076068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
8077068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
8078068a5610SStefano Zampini 
8079068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
8080068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
8081068a5610SStefano Zampini     B->sum += A->sum;
8082068a5610SStefano Zampini     B->squaresum += A->squaresum;
8083068a5610SStefano Zampini     B->count += A->count;
8084068a5610SStefano Zampini   }
8085068a5610SStefano Zampini }
8086068a5610SStefano Zampini 
8087068a5610SStefano Zampini /*@
808843fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
8089068a5610SStefano Zampini 
80908261a58bSMatthew G. Knepley   Collective on dm
80918261a58bSMatthew G. Knepley 
8092068a5610SStefano Zampini   Input Parameters:
8093068a5610SStefano Zampini + dm        - The DMPlex object
809443fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
809543fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
8096068a5610SStefano Zampini 
809795eb5ee5SVaclav Hapla   Notes:
809895eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
809995eb5ee5SVaclav Hapla 
810095eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8101068a5610SStefano Zampini 
8102068a5610SStefano Zampini   Level: developer
8103068a5610SStefano Zampini 
8104f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
8105068a5610SStefano Zampini @*/
810643fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8107068a5610SStefano Zampini {
8108068a5610SStefano Zampini   DM             dmCoarse;
810943fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
811043fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
811143fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
811243fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8113412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
811443fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
8115068a5610SStefano Zampini   PetscErrorCode ierr;
8116068a5610SStefano Zampini 
8117068a5610SStefano Zampini   PetscFunctionBegin;
8118068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8119068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
8120068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
8121068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
8122068a5610SStefano Zampini   stats.count = 0;
8123068a5610SStefano Zampini 
8124ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
8125ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
812643fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
812743fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
8128412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
812943fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
8130412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
8131068a5610SStefano Zampini     PetscInt  i;
8132068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
8133068a5610SStefano Zampini 
8134068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
8135068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
813643fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
8137068a5610SStefano Zampini       frobJ    += J[i] * J[i];
8138068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
8139068a5610SStefano Zampini     }
8140068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
8141068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
8142068a5610SStefano Zampini 
8143068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
8144068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
8145068a5610SStefano Zampini     stats.sum       += cond;
8146068a5610SStefano Zampini     stats.squaresum += cond2;
8147068a5610SStefano Zampini     stats.count++;
81488261a58bSMatthew G. Knepley     if (output && cond > limit) {
814943fa8764SMatthew G. Knepley       PetscSection coordSection;
815043fa8764SMatthew G. Knepley       Vec          coordsLocal;
815143fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
815243fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
815343fa8764SMatthew G. Knepley 
815443fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
815543fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
815643fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
8157087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
815843fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
815943fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
816043fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
816143fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
816248afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
816343fa8764SMatthew G. Knepley         }
816443fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
816543fa8764SMatthew G. Knepley       }
816643fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
816743fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
816843fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
816943fa8764SMatthew G. Knepley 
817043fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
817143fa8764SMatthew G. Knepley           PetscReal len;
817243fa8764SMatthew G. Knepley 
817343fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
8174087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
817543fa8764SMatthew G. Knepley         }
817643fa8764SMatthew G. Knepley       }
817743fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
817843fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
817943fa8764SMatthew G. Knepley     }
8180068a5610SStefano Zampini   }
81818261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
8182068a5610SStefano Zampini 
8183068a5610SStefano Zampini   if (size > 1) {
8184068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
8185068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
8186068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
8187068a5610SStefano Zampini     MPI_Op        statReduce;
8188068a5610SStefano Zampini 
8189ffc4695bSBarry Smith     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRMPI(ierr);
8190ffc4695bSBarry Smith     ierr = MPI_Type_commit(&statType);CHKERRMPI(ierr);
8191ffc4695bSBarry Smith     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRMPI(ierr);
8192ffc4695bSBarry Smith     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRMPI(ierr);
8193ffc4695bSBarry Smith     ierr = MPI_Op_free(&statReduce);CHKERRMPI(ierr);
8194ffc4695bSBarry Smith     ierr = MPI_Type_free(&statType);CHKERRMPI(ierr);
8195068a5610SStefano Zampini   } else {
8196580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
8197068a5610SStefano Zampini   }
8198068a5610SStefano Zampini   if (!rank) {
8199068a5610SStefano Zampini     count = globalStats.count;
8200068a5610SStefano Zampini     min   = globalStats.min;
8201068a5610SStefano Zampini     max   = globalStats.max;
8202068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
8203068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
8204068a5610SStefano Zampini   }
8205068a5610SStefano Zampini 
8206068a5610SStefano Zampini   if (output) {
8207068a5610SStefano 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);
8208068a5610SStefano Zampini   }
8209068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
8210068a5610SStefano Zampini 
8211068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
8212068a5610SStefano Zampini   if (dmCoarse) {
8213068a5610SStefano Zampini     PetscBool isplex;
8214068a5610SStefano Zampini 
8215068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
8216068a5610SStefano Zampini     if (isplex) {
821743fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
8218068a5610SStefano Zampini     }
8219068a5610SStefano Zampini   }
8220068a5610SStefano Zampini   PetscFunctionReturn(0);
8221068a5610SStefano Zampini }
8222068a5610SStefano Zampini 
8223f108dbd7SJacob Faibussowitsch /*@
8224f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8225f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
8226f108dbd7SJacob Faibussowitsch 
82276ed19f2fSJacob Faibussowitsch   Collective on dm
8228f108dbd7SJacob Faibussowitsch 
8229f108dbd7SJacob Faibussowitsch   Input Parameters:
8230f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
8231f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
8232f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
8233f108dbd7SJacob Faibussowitsch 
8234f108dbd7SJacob Faibussowitsch   Output Parameters:
8235f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
8236f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
8237f108dbd7SJacob Faibussowitsch 
8238f108dbd7SJacob Faibussowitsch   Options Database Keys:
8239f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
8240f108dbd7SJacob Faibussowitsch supported.
8241f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
8242f108dbd7SJacob Faibussowitsch 
8243f108dbd7SJacob Faibussowitsch   Notes:
8244f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
8245f108dbd7SJacob Faibussowitsch 
8246f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
8247f108dbd7SJacob Faibussowitsch 
8248f108dbd7SJacob 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
8249f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8250f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8251f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
8252f108dbd7SJacob Faibussowitsch 
8253f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
8254f108dbd7SJacob Faibussowitsch 
8255f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
8256f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
8257f108dbd7SJacob Faibussowitsch 
8258f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
8259f108dbd7SJacob Faibussowitsch 
8260f108dbd7SJacob Faibussowitsch   Level: intermediate
8261f108dbd7SJacob Faibussowitsch 
8262f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
8263f108dbd7SJacob Faibussowitsch @*/
8264f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
8265f108dbd7SJacob Faibussowitsch {
82666ed19f2fSJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
82676ed19f2fSJacob Faibussowitsch   PetscInt                *idx;
82686ed19f2fSJacob Faibussowitsch   PetscScalar             *oqVals;
8269f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
82706ed19f2fSJacob Faibussowitsch   PetscReal               *ci, *fi, *Ai;
8271f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
8272f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
8273f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
8274f108dbd7SJacob Faibussowitsch   IS                      glob;
8275f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
8276f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
8277f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
8278f108dbd7SJacob Faibussowitsch 
8279f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
8280f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82816ed19f2fSJacob Faibussowitsch   if (fv) {PetscValidHeaderSpecific(fv, PETSCFV_CLASSID, 2);}
8282f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
82836ed19f2fSJacob Faibussowitsch   if (PetscUnlikelyDebug(atol < 0.0 || atol > 1.0)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %g not in [0,1]",(double)atol);
8284f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
8285f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
82866ed19f2fSJacob Faibussowitsch   if (nc < 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
82876ed19f2fSJacob Faibussowitsch   {
82886ed19f2fSJacob Faibussowitsch     DMPlexInterpolatedFlag interpFlag;
82896ed19f2fSJacob Faibussowitsch 
8290f108dbd7SJacob Faibussowitsch     ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
8291f108dbd7SJacob Faibussowitsch     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
8292f108dbd7SJacob Faibussowitsch       PetscMPIInt rank;
8293f108dbd7SJacob Faibussowitsch 
8294ffc4695bSBarry Smith       ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
8295f108dbd7SJacob Faibussowitsch       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
8296f108dbd7SJacob Faibussowitsch     }
82976ed19f2fSJacob Faibussowitsch   }
8298f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8299f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
8300f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
8301f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
83026ed19f2fSJacob Faibussowitsch   } else {*OrthQualLabel = NULL;}
8303f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8304f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8305f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
8306f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
8307f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
8308f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
8309f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
8310f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
8311f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
8312f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
8313f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
8314f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
8315f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
8316f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8317f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8318f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
8319f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
83206ed19f2fSJacob Faibussowitsch   ierr = PetscMalloc5(cEnd-cStart, &idx, cEnd-cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
83216ed19f2fSJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cellIter++,cell++) {
83226ed19f2fSJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
8323f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
8324f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
8325898cd552SSatish Balay     PetscReal          minvalc = 1.0, minvalf = 1.0;
8326f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
8327f108dbd7SJacob Faibussowitsch 
83286ed19f2fSJacob Faibussowitsch     idx[cellIter] = cell-cStart;
8329f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
8330f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
8331f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
8332f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
8333f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8334f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
83356ed19f2fSJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++,cellneigh++) {
83366ed19f2fSJacob Faibussowitsch       PetscInt         i;
83376ed19f2fSJacob Faibussowitsch       const PetscInt   neigh = adj[cellneigh];
8338f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
8339f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
8340f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
8341f108dbd7SJacob Faibussowitsch 
8342f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
8343f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
8344f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
8345f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
83466ed19f2fSJacob Faibussowitsch       {
83476ed19f2fSJacob Faibussowitsch         PetscInt       numcovpts;
83486ed19f2fSJacob Faibussowitsch         const PetscInt *covpts;
83496ed19f2fSJacob Faibussowitsch 
8350f108dbd7SJacob Faibussowitsch         ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8351f108dbd7SJacob Faibussowitsch         ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
8352f108dbd7SJacob Faibussowitsch         ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
83536ed19f2fSJacob Faibussowitsch       }
8354f108dbd7SJacob Faibussowitsch 
8355f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
8356f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8357f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8358f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
8359f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
8360addd1e01SJunchao Zhang         normci += PetscPowReal(ci[i], 2);
8361addd1e01SJunchao Zhang         normfi += PetscPowReal(fi[i], 2);
8362addd1e01SJunchao Zhang         normai += PetscPowReal(Ai[i], 2);
8363f108dbd7SJacob Faibussowitsch       }
8364addd1e01SJunchao Zhang       normci = PetscSqrtReal(normci);
8365addd1e01SJunchao Zhang       normfi = PetscSqrtReal(normfi);
8366addd1e01SJunchao Zhang       normai = PetscSqrtReal(normai);
8367f108dbd7SJacob Faibussowitsch 
8368f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
8369f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8370f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
8371f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
8372f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
8373f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
8374f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8375f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8376f108dbd7SJacob Faibussowitsch       }
8377f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8378f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
8379f108dbd7SJacob Faibussowitsch       }
8380f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8381f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
8382f108dbd7SJacob Faibussowitsch       }
8383f108dbd7SJacob Faibussowitsch     }
8384f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
8385f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
8386f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
83876ed19f2fSJacob Faibussowitsch     oqVals[cellIter] = PetscMin(minvalf, minvalc);
8388f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
83896ed19f2fSJacob Faibussowitsch       if (PetscRealPart(oqVals[cellIter]) <= atol) {ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);}
8390f108dbd7SJacob Faibussowitsch     }
8391f108dbd7SJacob Faibussowitsch   }
83926ed19f2fSJacob Faibussowitsch   ierr = VecSetValuesLocal(*OrthQual, cEnd-cStart, idx, oqVals, INSERT_VALUES);CHKERRQ(ierr);
8393f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
8394f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
8395f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8396f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8397f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
8398f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
83996ed19f2fSJacob Faibussowitsch     if (vwr) {ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);}
8400f108dbd7SJacob Faibussowitsch   }
84016ed19f2fSJacob Faibussowitsch   ierr = PetscFree5(idx, oqVals, ci, fi, Ai);CHKERRQ(ierr);
8402f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
8403f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
8404f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
8405f108dbd7SJacob Faibussowitsch }
8406f108dbd7SJacob Faibussowitsch 
84071eb70e55SToby Isaac /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
84081eb70e55SToby Isaac  * interpolator construction */
84091eb70e55SToby Isaac static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
84101eb70e55SToby Isaac {
84111eb70e55SToby Isaac   PetscSection   section, newSection, gsection;
84121eb70e55SToby Isaac   PetscSF        sf;
84131eb70e55SToby Isaac   PetscBool      hasConstraints, ghasConstraints;
84141eb70e55SToby Isaac   PetscErrorCode ierr;
84151eb70e55SToby Isaac 
84161eb70e55SToby Isaac   PetscFunctionBegin;
84171eb70e55SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84181eb70e55SToby Isaac   PetscValidPointer(odm,2);
84191eb70e55SToby Isaac   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
84201eb70e55SToby Isaac   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
84211eb70e55SToby Isaac   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
84221eb70e55SToby Isaac   if (!ghasConstraints) {
84231eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
84241eb70e55SToby Isaac     *odm = dm;
84251eb70e55SToby Isaac     PetscFunctionReturn(0);
84261eb70e55SToby Isaac   }
84271eb70e55SToby Isaac   ierr = DMClone(dm, odm);CHKERRQ(ierr);
84281eb70e55SToby Isaac   ierr = DMCopyFields(dm, *odm);CHKERRQ(ierr);
84291eb70e55SToby Isaac   ierr = DMGetLocalSection(*odm, &newSection);CHKERRQ(ierr);
84301eb70e55SToby Isaac   ierr = DMGetPointSF(*odm, &sf);CHKERRQ(ierr);
84311eb70e55SToby Isaac   ierr = PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
84321eb70e55SToby Isaac   ierr = DMSetGlobalSection(*odm, gsection);CHKERRQ(ierr);
84331eb70e55SToby Isaac   ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
84341eb70e55SToby Isaac   PetscFunctionReturn(0);
84351eb70e55SToby Isaac }
84361eb70e55SToby Isaac 
84371eb70e55SToby Isaac static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
84381eb70e55SToby Isaac {
84391eb70e55SToby Isaac   DM             dmco, dmfo;
84401eb70e55SToby Isaac   Mat            interpo;
84411eb70e55SToby Isaac   Vec            rscale;
84421eb70e55SToby Isaac   Vec            cglobalo, clocal;
84431eb70e55SToby Isaac   Vec            fglobal, fglobalo, flocal;
84441eb70e55SToby Isaac   PetscBool      regular;
84451eb70e55SToby Isaac   PetscErrorCode ierr;
84461eb70e55SToby Isaac 
84471eb70e55SToby Isaac   PetscFunctionBegin;
84481eb70e55SToby Isaac   ierr = DMGetFullDM(dmc, &dmco);CHKERRQ(ierr);
84491eb70e55SToby Isaac   ierr = DMGetFullDM(dmf, &dmfo);CHKERRQ(ierr);
84501eb70e55SToby Isaac   ierr = DMSetCoarseDM(dmfo, dmco);CHKERRQ(ierr);
84511eb70e55SToby Isaac   ierr = DMPlexGetRegularRefinement(dmf, &regular);CHKERRQ(ierr);
84521eb70e55SToby Isaac   ierr = DMPlexSetRegularRefinement(dmfo, regular);CHKERRQ(ierr);
84531eb70e55SToby Isaac   ierr = DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);CHKERRQ(ierr);
84541eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmco, &cglobalo);CHKERRQ(ierr);
84551eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmc, &clocal);CHKERRQ(ierr);
84561eb70e55SToby Isaac   ierr = VecSet(cglobalo, 0.);CHKERRQ(ierr);
84571eb70e55SToby Isaac   ierr = VecSet(clocal, 0.);CHKERRQ(ierr);
84581eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmf, &fglobal);CHKERRQ(ierr);
84591eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmfo, &fglobalo);CHKERRQ(ierr);
84601eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmf, &flocal);CHKERRQ(ierr);
84611eb70e55SToby Isaac   ierr = VecSet(fglobal, 0.);CHKERRQ(ierr);
84621eb70e55SToby Isaac   ierr = VecSet(fglobalo, 0.);CHKERRQ(ierr);
84631eb70e55SToby Isaac   ierr = VecSet(flocal, 0.);CHKERRQ(ierr);
84641eb70e55SToby Isaac   ierr = DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);CHKERRQ(ierr);
84651eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
84661eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
84671eb70e55SToby Isaac   ierr = MatMult(interpo, cglobalo, fglobalo);CHKERRQ(ierr);
84681eb70e55SToby Isaac   ierr = DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
84691eb70e55SToby Isaac   ierr = DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
84701eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
84711eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
84721eb70e55SToby Isaac   *shift = fglobal;
84731eb70e55SToby Isaac   ierr = VecDestroy(&flocal);CHKERRQ(ierr);
84741eb70e55SToby Isaac   ierr = VecDestroy(&fglobalo);CHKERRQ(ierr);
84751eb70e55SToby Isaac   ierr = VecDestroy(&clocal);CHKERRQ(ierr);
84761eb70e55SToby Isaac   ierr = VecDestroy(&cglobalo);CHKERRQ(ierr);
84771eb70e55SToby Isaac   ierr = VecDestroy(&rscale);CHKERRQ(ierr);
84781eb70e55SToby Isaac   ierr = MatDestroy(&interpo);CHKERRQ(ierr);
84791eb70e55SToby Isaac   ierr = DMDestroy(&dmfo);CHKERRQ(ierr);
84801eb70e55SToby Isaac   ierr = DMDestroy(&dmco);CHKERRQ(ierr);
84811eb70e55SToby Isaac   PetscFunctionReturn(0);
84821eb70e55SToby Isaac }
84831eb70e55SToby Isaac 
84841eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
84851eb70e55SToby Isaac {
84861eb70e55SToby Isaac   PetscObject    shifto;
84871eb70e55SToby Isaac   Vec            shift;
84881eb70e55SToby Isaac 
84891eb70e55SToby Isaac   PetscErrorCode ierr;
84901eb70e55SToby Isaac 
84911eb70e55SToby Isaac   PetscFunctionBegin;
84921eb70e55SToby Isaac   if (!interp) {
84931eb70e55SToby Isaac     Vec rscale;
84941eb70e55SToby Isaac 
84951eb70e55SToby Isaac     ierr = DMCreateInterpolation(coarse, fine, &interp, &rscale);CHKERRQ(ierr);
84961eb70e55SToby Isaac     ierr = VecDestroy(&rscale);CHKERRQ(ierr);
84971eb70e55SToby Isaac   } else {
84981eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)interp);CHKERRQ(ierr);
84991eb70e55SToby Isaac   }
85001eb70e55SToby Isaac   ierr = PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);CHKERRQ(ierr);
85011eb70e55SToby Isaac   if (!shifto) {
85021eb70e55SToby Isaac     ierr = DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);CHKERRQ(ierr);
85031eb70e55SToby Isaac     ierr = PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);CHKERRQ(ierr);
85041eb70e55SToby Isaac     shifto = (PetscObject) shift;
85051eb70e55SToby Isaac     ierr = VecDestroy(&shift);CHKERRQ(ierr);
85061eb70e55SToby Isaac   }
85071eb70e55SToby Isaac   shift = (Vec) shifto;
85081eb70e55SToby Isaac   ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
85091eb70e55SToby Isaac   ierr = VecAXPY(fineSol, 1.0, shift);CHKERRQ(ierr);
85101eb70e55SToby Isaac   ierr = MatDestroy(&interp);CHKERRQ(ierr);
85111eb70e55SToby Isaac   PetscFunctionReturn(0);
85121eb70e55SToby Isaac }
85131eb70e55SToby Isaac 
8514bceba477SMatthew G. Knepley /* Pointwise interpolation
8515bceba477SMatthew G. Knepley      Just code FEM for now
8516bceba477SMatthew G. Knepley      u^f = I u^c
85174ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
85184ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
85194ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
8520bceba477SMatthew G. Knepley */
8521bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8522bceba477SMatthew G. Knepley {
8523bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
8524bceba477SMatthew G. Knepley   PetscInt       m, n;
8525a063dac3SMatthew G. Knepley   void          *ctx;
852668132eb9SMatthew G. Knepley   DM             cdm;
8527cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
8528bceba477SMatthew G. Knepley   PetscErrorCode ierr;
8529bceba477SMatthew G. Knepley 
8530bceba477SMatthew G. Knepley   PetscFunctionBegin;
8531e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8532bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8533e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8534bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
853568132eb9SMatthew G. Knepley 
8536fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
8537bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
8538bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8539fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
8540a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
854168132eb9SMatthew G. Knepley 
8542a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
854368132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8544cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
854568132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
854668132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
85474db47ee9SStefano Zampini   if (scaling) {
85485d1c2e58SMatthew G. Knepley     /* Use naive scaling */
85495d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
85504db47ee9SStefano Zampini   }
8551a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
8552a063dac3SMatthew G. Knepley }
8553bceba477SMatthew G. Knepley 
85546dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8555a063dac3SMatthew G. Knepley {
855690748bafSMatthew G. Knepley   PetscErrorCode ierr;
85576dbf9973SLawrence Mitchell   VecScatter     ctx;
855890748bafSMatthew G. Knepley 
8559a063dac3SMatthew G. Knepley   PetscFunctionBegin;
85606dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
85616dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
85626dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
8563bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
8564bceba477SMatthew G. Knepley }
8565bceba477SMatthew G. Knepley 
85663e9753d6SMatthew G. Knepley static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
85673e9753d6SMatthew G. Knepley                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
85683e9753d6SMatthew G. Knepley                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
85693e9753d6SMatthew G. Knepley                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
85703e9753d6SMatthew G. Knepley {
85713e9753d6SMatthew G. Knepley   g0[0] = 1.0;
85723e9753d6SMatthew G. Knepley }
85733e9753d6SMatthew G. Knepley 
8574bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8575bd041c0cSMatthew G. Knepley {
8576bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
8577bd041c0cSMatthew G. Knepley   PetscInt       m, n;
8578bd041c0cSMatthew G. Knepley   void          *ctx;
8579bd041c0cSMatthew G. Knepley   DM             cdm;
8580bd041c0cSMatthew G. Knepley   PetscBool      regular;
8581bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
8582bd041c0cSMatthew G. Knepley 
8583bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
85843e9753d6SMatthew G. Knepley   if (dmFine == dmCoarse) {
85853e9753d6SMatthew G. Knepley     DM       dmc;
85863e9753d6SMatthew G. Knepley     PetscDS  ds;
85873e9753d6SMatthew G. Knepley     Vec      u;
85883e9753d6SMatthew G. Knepley     IS       cellIS;
858906ad1575SMatthew G. Knepley     PetscFormKey key;
85903e9753d6SMatthew G. Knepley     PetscInt depth;
85913e9753d6SMatthew G. Knepley 
85923e9753d6SMatthew G. Knepley     ierr = DMClone(dmFine, &dmc);CHKERRQ(ierr);
85933e9753d6SMatthew G. Knepley     ierr = DMCopyDisc(dmFine, dmc);CHKERRQ(ierr);
85943e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
85953e9753d6SMatthew G. Knepley     ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
85963e9753d6SMatthew G. Knepley     ierr = DMCreateMatrix(dmc, mass);CHKERRQ(ierr);
85973e9753d6SMatthew G. Knepley     ierr = DMGetGlobalVector(dmc, &u);CHKERRQ(ierr);
85983e9753d6SMatthew G. Knepley     ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
85993e9753d6SMatthew G. Knepley     ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
86003e9753d6SMatthew G. Knepley     ierr = MatZeroEntries(*mass);CHKERRQ(ierr);
86016528b96dSMatthew G. Knepley     key.label = NULL;
86026528b96dSMatthew G. Knepley     key.value = 0;
86036528b96dSMatthew G. Knepley     key.field = 0;
860406ad1575SMatthew G. Knepley     key.part  = 0;
86056528b96dSMatthew G. Knepley     ierr = DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);CHKERRQ(ierr);
86063e9753d6SMatthew G. Knepley     ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
86073e9753d6SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmc, &u);CHKERRQ(ierr);
86083e9753d6SMatthew G. Knepley     ierr = DMDestroy(&dmc);CHKERRQ(ierr);
86093e9753d6SMatthew G. Knepley   } else {
8610e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8611bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8612e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8613bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
8614bd041c0cSMatthew G. Knepley 
8615bd041c0cSMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
8616bd041c0cSMatthew G. Knepley     ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8617bd041c0cSMatthew G. Knepley     ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
8618bd041c0cSMatthew G. Knepley     ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
8619bd041c0cSMatthew G. Knepley 
8620bd041c0cSMatthew G. Knepley     ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
8621bd041c0cSMatthew G. Knepley     ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8622bd041c0cSMatthew G. Knepley     if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8623bd041c0cSMatthew G. Knepley     else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
86243e9753d6SMatthew G. Knepley   }
8625bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
8626bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
8627bd041c0cSMatthew G. Knepley }
8628bd041c0cSMatthew G. Knepley 
86290aef6b92SMatthew G. Knepley /*@
86300aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
86310aef6b92SMatthew G. Knepley 
86320aef6b92SMatthew G. Knepley   Input Parameter:
86330aef6b92SMatthew G. Knepley . dm - The DMPlex object
86340aef6b92SMatthew G. Knepley 
86350aef6b92SMatthew G. Knepley   Output Parameter:
86360aef6b92SMatthew G. Knepley . regular - The flag
86370aef6b92SMatthew G. Knepley 
86380aef6b92SMatthew G. Knepley   Level: intermediate
86390aef6b92SMatthew G. Knepley 
86400aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
86410aef6b92SMatthew G. Knepley @*/
86420aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
86430aef6b92SMatthew G. Knepley {
86440aef6b92SMatthew G. Knepley   PetscFunctionBegin;
86450aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86460aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
86470aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
86480aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
86490aef6b92SMatthew G. Knepley }
86500aef6b92SMatthew G. Knepley 
86510aef6b92SMatthew G. Knepley /*@
86520aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
86530aef6b92SMatthew G. Knepley 
86540aef6b92SMatthew G. Knepley   Input Parameters:
86550aef6b92SMatthew G. Knepley + dm - The DMPlex object
86560aef6b92SMatthew G. Knepley - regular - The flag
86570aef6b92SMatthew G. Knepley 
86580aef6b92SMatthew G. Knepley   Level: intermediate
86590aef6b92SMatthew G. Knepley 
86600aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
86610aef6b92SMatthew G. Knepley @*/
86620aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
86630aef6b92SMatthew G. Knepley {
86640aef6b92SMatthew G. Knepley   PetscFunctionBegin;
86650aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86660aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
86670aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
86680aef6b92SMatthew G. Knepley }
86690aef6b92SMatthew G. Knepley 
8670412e9a14SMatthew G. Knepley /*@
8671412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
8672412e9a14SMatthew G. Knepley 
8673412e9a14SMatthew G. Knepley   Input Parameter:
8674412e9a14SMatthew G. Knepley . dm - The DMPlex object
8675412e9a14SMatthew G. Knepley 
8676412e9a14SMatthew G. Knepley   Output Parameter:
8677412e9a14SMatthew G. Knepley . cr - The strategy number
8678412e9a14SMatthew G. Knepley 
8679412e9a14SMatthew G. Knepley   Level: intermediate
8680412e9a14SMatthew G. Knepley 
8681412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8682412e9a14SMatthew G. Knepley @*/
8683412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8684412e9a14SMatthew G. Knepley {
8685412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8686412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8687412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
8688412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8689412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8690412e9a14SMatthew G. Knepley }
8691412e9a14SMatthew G. Knepley 
8692412e9a14SMatthew G. Knepley /*@
8693412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
8694412e9a14SMatthew G. Knepley 
8695412e9a14SMatthew G. Knepley   Input Parameters:
8696412e9a14SMatthew G. Knepley + dm - The DMPlex object
8697412e9a14SMatthew G. Knepley - cr - The strategy number
8698412e9a14SMatthew G. Knepley 
8699412e9a14SMatthew G. Knepley   Level: intermediate
8700412e9a14SMatthew G. Knepley 
8701412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8702412e9a14SMatthew G. Knepley @*/
8703412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8704412e9a14SMatthew G. Knepley {
8705412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8706412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8707412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
8708412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8709412e9a14SMatthew G. Knepley }
8710412e9a14SMatthew G. Knepley 
8711f7c74593SToby Isaac /* anchors */
8712a68b90caSToby Isaac /*@
8713f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
8714f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
8715a68b90caSToby Isaac 
8716e228b242SToby Isaac   not collective
8717a68b90caSToby Isaac 
8718a68b90caSToby Isaac   Input Parameters:
8719a68b90caSToby Isaac . dm - The DMPlex object
8720a68b90caSToby Isaac 
8721a68b90caSToby Isaac   Output Parameters:
8722a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
8723a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
8724a68b90caSToby Isaac 
8725a68b90caSToby Isaac   Level: intermediate
8726a68b90caSToby Isaac 
8727f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8728a68b90caSToby Isaac @*/
8729a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8730a68b90caSToby Isaac {
8731a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
873241e6d900SToby Isaac   PetscErrorCode ierr;
8733a68b90caSToby Isaac 
8734a68b90caSToby Isaac   PetscFunctionBegin;
8735a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
873641e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
8737a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
8738a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
8739a68b90caSToby Isaac   PetscFunctionReturn(0);
8740a68b90caSToby Isaac }
8741a68b90caSToby Isaac 
8742a68b90caSToby Isaac /*@
8743f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
8744f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
8745a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
8746a68b90caSToby Isaac 
8747a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
8748f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
8749a68b90caSToby Isaac 
8750e228b242SToby Isaac   collective on dm
8751a68b90caSToby Isaac 
8752a68b90caSToby Isaac   Input Parameters:
8753a68b90caSToby Isaac + dm - The DMPlex object
8754e228b242SToby 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).
8755e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
8756a68b90caSToby Isaac 
8757a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
8758a68b90caSToby Isaac 
8759a68b90caSToby Isaac   Level: intermediate
8760a68b90caSToby Isaac 
8761f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8762a68b90caSToby Isaac @*/
8763a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8764a68b90caSToby Isaac {
8765a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8766e228b242SToby Isaac   PetscMPIInt    result;
8767a68b90caSToby Isaac   PetscErrorCode ierr;
8768a68b90caSToby Isaac 
8769a68b90caSToby Isaac   PetscFunctionBegin;
8770a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8771e228b242SToby Isaac   if (anchorSection) {
8772e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
8773ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRMPI(ierr);
8774f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8775e228b242SToby Isaac   }
8776e228b242SToby Isaac   if (anchorIS) {
8777e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
8778ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRMPI(ierr);
8779f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8780e228b242SToby Isaac   }
8781a68b90caSToby Isaac 
8782a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
8783a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
8784a68b90caSToby Isaac   plex->anchorSection = anchorSection;
8785a68b90caSToby Isaac 
8786a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
8787a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
8788a68b90caSToby Isaac   plex->anchorIS = anchorIS;
8789a68b90caSToby Isaac 
8790cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8791a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
8792a68b90caSToby Isaac     const PetscInt *anchors;
8793a68b90caSToby Isaac 
8794a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
8795a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
8796a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
8797a68b90caSToby Isaac     for (a = 0; a < size; a++) {
8798a68b90caSToby Isaac       PetscInt p;
8799a68b90caSToby Isaac 
8800a68b90caSToby Isaac       p = anchors[a];
8801a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
8802a68b90caSToby Isaac         PetscInt dof;
8803a68b90caSToby Isaac 
8804a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8805a68b90caSToby Isaac         if (dof) {
8806a68b90caSToby Isaac           PetscErrorCode ierr2;
8807a68b90caSToby Isaac 
8808a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
88098ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8810a68b90caSToby Isaac         }
8811a68b90caSToby Isaac       }
8812a68b90caSToby Isaac     }
8813a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
8814a68b90caSToby Isaac   }
8815f7c74593SToby Isaac   /* reset the generic constraints */
8816f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
8817a68b90caSToby Isaac   PetscFunctionReturn(0);
8818a68b90caSToby Isaac }
8819a68b90caSToby Isaac 
8820f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8821a68b90caSToby Isaac {
8822f7c74593SToby Isaac   PetscSection anchorSection;
88236995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
8824a68b90caSToby Isaac   PetscErrorCode ierr;
8825a68b90caSToby Isaac 
8826a68b90caSToby Isaac   PetscFunctionBegin;
8827a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8828a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
8829e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
8830a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
88316995de1eSToby Isaac   if (numFields) {
8832719ab38cSToby Isaac     PetscInt f;
8833a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
8834719ab38cSToby Isaac 
8835719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
8836719ab38cSToby Isaac       PetscInt numComp;
8837719ab38cSToby Isaac 
8838719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
8839719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
8840719ab38cSToby Isaac     }
88416995de1eSToby Isaac   }
8842a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
88436995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
88446995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
88456995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
88466995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
8847a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
8848a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
8849a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8850a68b90caSToby Isaac     if (dof) {
8851a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
8852a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
8853a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
8854a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
8855a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
8856a68b90caSToby Isaac       }
8857a68b90caSToby Isaac     }
8858a68b90caSToby Isaac   }
8859a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
8860ae65431dSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *cSec, "Constraint Section");CHKERRQ(ierr);
8861a68b90caSToby Isaac   PetscFunctionReturn(0);
8862a68b90caSToby Isaac }
8863a68b90caSToby Isaac 
8864f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8865a68b90caSToby Isaac {
8866f7c74593SToby Isaac   PetscSection   aSec;
8867ae65431dSMatthew G. Knepley   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
88680ac89760SToby Isaac   const PetscInt *anchors;
88690ac89760SToby Isaac   PetscInt       numFields, f;
887066ad2231SToby Isaac   IS             aIS;
88710ac89760SToby Isaac   PetscErrorCode ierr;
8872e19f7ee6SMark Adams   MatType        mtype;
8873e19f7ee6SMark Adams   PetscBool      iscuda,iskokkos;
88740ac89760SToby Isaac 
88750ac89760SToby Isaac   PetscFunctionBegin;
88760ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88770ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
88780ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
88790ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
88800ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
8881e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);CHKERRQ(ierr);
8882e19f7ee6SMark Adams   if (!iscuda) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);CHKERRQ(ierr); }
8883e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);CHKERRQ(ierr);
8884e19f7ee6SMark Adams   if (!iskokkos) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);CHKERRQ(ierr); }
8885e19f7ee6SMark Adams   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8886e19f7ee6SMark Adams   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8887e19f7ee6SMark Adams   else mtype = MATSEQAIJ;
8888e19f7ee6SMark Adams   ierr = MatSetType(*cMat,mtype);CHKERRQ(ierr);
8889a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
889066ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
88916995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
88926995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
8893ae65431dSMatthew G. Knepley   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
88940ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
88950ac89760SToby Isaac   i[0] = 0;
88960ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
88970ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
8898f19733c5SToby Isaac     PetscInt rDof, rOff, r;
8899f19733c5SToby Isaac 
8900f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
8901f19733c5SToby Isaac     if (!rDof) continue;
8902f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89030ac89760SToby Isaac     if (numFields) {
89040ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
89050ac89760SToby Isaac         annz = 0;
8906f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
8907f19733c5SToby Isaac           a = anchors[rOff + r];
8908ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
89090ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
89100ac89760SToby Isaac           annz += aDof;
89110ac89760SToby Isaac         }
89120ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
89130ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
89140ac89760SToby Isaac         for (q = 0; q < dof; q++) {
89150ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
89160ac89760SToby Isaac         }
89170ac89760SToby Isaac       }
89180ac89760SToby Isaac     }
89190ac89760SToby Isaac     else {
89200ac89760SToby Isaac       annz = 0;
8921326b8f31SMatthew G. Knepley       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
89220ac89760SToby Isaac       for (q = 0; q < dof; q++) {
8923ae65431dSMatthew G. Knepley         a = anchors[rOff + q];
8924ae65431dSMatthew G. Knepley         if (a < sStart || a >= sEnd) continue;
89250ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
89260ac89760SToby Isaac         annz += aDof;
89270ac89760SToby Isaac       }
89280ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
89290ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
89300ac89760SToby Isaac       for (q = 0; q < dof; q++) {
89310ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
89320ac89760SToby Isaac       }
89330ac89760SToby Isaac     }
89340ac89760SToby Isaac   }
89350ac89760SToby Isaac   nnz = i[m];
89360ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
89370ac89760SToby Isaac   offset = 0;
89380ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
89390ac89760SToby Isaac     if (numFields) {
89400ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
89410ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
89420ac89760SToby Isaac         for (q = 0; q < dof; q++) {
89430ac89760SToby Isaac           PetscInt rDof, rOff, r;
89440ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
89450ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89460ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
89470ac89760SToby Isaac             PetscInt s;
89480ac89760SToby Isaac 
89490ac89760SToby Isaac             a = anchors[rOff + r];
8950ae65431dSMatthew G. Knepley             if (a < sStart || a >= sEnd) continue;
89510ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
89520ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
89530ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
89540ac89760SToby Isaac               j[offset++] = aOff + s;
89550ac89760SToby Isaac             }
89560ac89760SToby Isaac           }
89570ac89760SToby Isaac         }
89580ac89760SToby Isaac       }
89590ac89760SToby Isaac     }
89600ac89760SToby Isaac     else {
89610ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
89620ac89760SToby Isaac       for (q = 0; q < dof; q++) {
89630ac89760SToby Isaac         PetscInt rDof, rOff, r;
89640ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
89650ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89660ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
89670ac89760SToby Isaac           PetscInt s;
89680ac89760SToby Isaac 
89690ac89760SToby Isaac           a = anchors[rOff + r];
8970ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
89710ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
89720ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
89730ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
89740ac89760SToby Isaac             j[offset++] = aOff + s;
89750ac89760SToby Isaac           }
89760ac89760SToby Isaac         }
89770ac89760SToby Isaac       }
89780ac89760SToby Isaac     }
89790ac89760SToby Isaac   }
89800ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
898125570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
898225570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
898366ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
89840ac89760SToby Isaac   PetscFunctionReturn(0);
89850ac89760SToby Isaac }
89860ac89760SToby Isaac 
898766ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
898866ad2231SToby Isaac {
8989f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8990f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
899166ad2231SToby Isaac   Mat            cMat;
899266ad2231SToby Isaac   PetscErrorCode ierr;
899366ad2231SToby Isaac 
899466ad2231SToby Isaac   PetscFunctionBegin;
899566ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8996a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
899766ad2231SToby Isaac   if (anchorSection) {
899844a7f3ddSMatthew G. Knepley     PetscInt Nf;
8999e228b242SToby Isaac 
900092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
9001f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
9002f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
900344a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
900444a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
900566ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
900666ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
900766ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
900866ad2231SToby Isaac   }
900966ad2231SToby Isaac   PetscFunctionReturn(0);
901066ad2231SToby Isaac }
9011a93c429eSMatthew G. Knepley 
9012a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
9013a93c429eSMatthew G. Knepley {
9014a93c429eSMatthew G. Knepley   IS             subis;
9015a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
9016a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
9017a93c429eSMatthew G. Knepley 
9018a93c429eSMatthew G. Knepley   PetscFunctionBegin;
901992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
9020a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
9021a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
9022a93c429eSMatthew G. Knepley   /* Create subdomain */
9023a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
9024a93c429eSMatthew G. Knepley   /* Create submodel */
902597d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
9026a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
902792fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
9028a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
9029e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
9030a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
9031a93c429eSMatthew G. Knepley   if (is) {
9032a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
9033a93c429eSMatthew G. Knepley     IS              spIS;
9034a93c429eSMatthew G. Knepley     const PetscInt *spmap;
9035a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
9036a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
9037a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
9038a93c429eSMatthew G. Knepley 
903997d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
9040a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
9041a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
90426f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90436f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
9044a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
9045a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9046a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
9047a93c429eSMatthew G. Knepley 
9048a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
9049a93c429eSMatthew G. Knepley       if (gdof > 0) {
9050a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9051a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
9052a93c429eSMatthew G. Knepley 
9053a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
9054a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
9055a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
9056a93c429eSMatthew G. Knepley         }
9057a93c429eSMatthew G. Knepley         subSize += pSubSize;
9058a93c429eSMatthew G. Knepley         if (pSubSize) {
9059a93c429eSMatthew G. Knepley           if (bs < 0) {
9060a93c429eSMatthew G. Knepley             bs = pSubSize;
9061a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
9062a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
9063a93c429eSMatthew G. Knepley             bs = 1;
9064a93c429eSMatthew G. Knepley           }
9065a93c429eSMatthew G. Knepley         }
9066a93c429eSMatthew G. Knepley       }
9067a93c429eSMatthew G. Knepley     }
9068a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
9069a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9070a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9071a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9072a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
9073a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
9074a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9075a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
9076a93c429eSMatthew G. Knepley 
9077a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
9078a93c429eSMatthew G. Knepley       if (gdof > 0) {
9079a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
9080a93c429eSMatthew G. Knepley 
9081a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
9082a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9083a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
9084a93c429eSMatthew G. Knepley 
9085a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
9086a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
9087a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
9088a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
9089a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
9090a93c429eSMatthew G. Knepley           }
9091a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
9092a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
9093a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
9094a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
9095a93c429eSMatthew G. Knepley           }
9096a93c429eSMatthew G. Knepley         }
9097a93c429eSMatthew G. Knepley       }
9098a93c429eSMatthew G. Knepley     }
9099a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
9100a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
9101a93c429eSMatthew G. Knepley     if (bs > 1) {
9102a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
9103a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
9104a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
9105a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
9106a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
9107a93c429eSMatthew G. Knepley         }
9108a93c429eSMatthew G. Knepley       }
9109a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
9110a93c429eSMatthew G. Knepley     }
9111a93c429eSMatthew G. Knepley     /* Attach nullspace */
9112a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
9113a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
9114a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
9115a93c429eSMatthew G. Knepley     }
9116a93c429eSMatthew G. Knepley     if (f < Nf) {
9117a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
91188cda7954SMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);CHKERRQ(ierr);
91196823f3c5SBlaise Bourdin 
9120a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
9121a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
9122a93c429eSMatthew G. Knepley     }
9123a93c429eSMatthew G. Knepley   }
9124a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
9125a93c429eSMatthew G. Knepley }
9126c0f0dcc3SMatthew G. Knepley 
9127c0f0dcc3SMatthew G. Knepley /*@
9128c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
9129c0f0dcc3SMatthew G. Knepley 
9130c0f0dcc3SMatthew G. Knepley   Input Parameter:
9131c0f0dcc3SMatthew G. Knepley - dm - The DM
9132c0f0dcc3SMatthew G. Knepley 
9133c0f0dcc3SMatthew G. Knepley   Level: developer
9134c0f0dcc3SMatthew G. Knepley 
9135c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9136c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
9137c0f0dcc3SMatthew G. Knepley 
9138c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
9139c0f0dcc3SMatthew G. Knepley @*/
9140c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
9141c0f0dcc3SMatthew G. Knepley {
9142e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
9143c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
9144c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
9145c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
9146c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
9147c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
9148c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
9149c0f0dcc3SMatthew G. Knepley   const char        *name;
9150c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
9151e5ed2c37SJose E. Roman #endif
9152c0f0dcc3SMatthew G. Knepley 
9153c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9154c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9155c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
9156c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
9157c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9158c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
9159c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
9160c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
9161c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
9162c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
9163c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
9164c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
9165c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
9166fae64647SBarry 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);
9167c0f0dcc3SMatthew G. Knepley #else
9168c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
9169c0f0dcc3SMatthew G. Knepley #endif
9170c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9171c0f0dcc3SMatthew G. Knepley }
9172