xref: /petsc/src/dm/impls/plex/plex.c (revision 021affd3f3ae801c639e951d286930cc42c15ed9)
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 
1556*021affd3Sksagiyam /*@
1557*021affd3Sksagiyam   DMPlexSectionView - Saves a section associated with a DMPlex
1558*021affd3Sksagiyam 
1559*021affd3Sksagiyam   Collective on DM
1560*021affd3Sksagiyam 
1561*021affd3Sksagiyam   Input Parameters:
1562*021affd3Sksagiyam + dm         - The DM that contains the topology on which the section to be saved is defined
1563*021affd3Sksagiyam . viewer     - The PetscViewer for saving
1564*021affd3Sksagiyam - sectiondm  - The DM that contains the section to be saved
1565*021affd3Sksagiyam 
1566*021affd3Sksagiyam   Level: advanced
1567*021affd3Sksagiyam 
1568*021affd3Sksagiyam   Notes:
1569*021affd3Sksagiyam   This function is a wrapper around PetscSectionView(); in addition to the raw section, it saves information that associates the section points to the topology (dm) points. When the topology (dm) and the section are later loaded with DMPlexTopologyLoad() and DMPlexSectionLoad(), respectively, this information is used to match section points with topology points.
1570*021affd3Sksagiyam 
1571*021affd3Sksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
1572*021affd3Sksagiyam 
1573*021affd3Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexCoordinatesView(), DMPlexLabelsView(), DMPlexGlobalVectorView(), DMPlexLocalVectorView(), PetscSectionView(), DMPlexSectionLoad()
1574*021affd3Sksagiyam @*/
1575*021affd3Sksagiyam PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
1576*021affd3Sksagiyam {
1577*021affd3Sksagiyam   PetscBool      ishdf5;
1578*021affd3Sksagiyam   PetscErrorCode ierr;
1579*021affd3Sksagiyam 
1580*021affd3Sksagiyam   PetscFunctionBegin;
1581*021affd3Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1582*021affd3Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1583*021affd3Sksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
1584*021affd3Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
1585*021affd3Sksagiyam   if (ishdf5) {
1586*021affd3Sksagiyam #if defined(PETSC_HAVE_HDF5)
1587*021affd3Sksagiyam     ierr = DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm);CHKERRQ(ierr);
1588*021affd3Sksagiyam #else
1589*021affd3Sksagiyam     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1590*021affd3Sksagiyam #endif
1591*021affd3Sksagiyam   }
1592*021affd3Sksagiyam   PetscFunctionReturn(0);
1593*021affd3Sksagiyam }
1594*021affd3Sksagiyam 
15952c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
15962c40f234SMatthew G. Knepley {
1597d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
15982c40f234SMatthew G. Knepley   PetscErrorCode ierr;
15992c40f234SMatthew G. Knepley 
16002c40f234SMatthew G. Knepley   PetscFunctionBegin;
16012c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16022c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
16032c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1604d4f5a9a0SVaclav Hapla   if (ishdf5) {
16052c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
16069c48423bSVaclav Hapla     PetscViewerFormat format;
16079c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
16089c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
16099c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1610509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
161139d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1612509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1613b458e8f1SJose E. Roman     PetscFunctionReturn(0);
16142c40f234SMatthew G. Knepley #else
16152c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1616552f7358SJed Brown #endif
1617b458e8f1SJose E. Roman   } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1618552f7358SJed Brown }
1619552f7358SJed Brown 
1620ea8e1828Sksagiyam /*@
1621ea8e1828Sksagiyam   DMPlexTopologyLoad - Loads a topology into a DMPlex
1622ea8e1828Sksagiyam 
1623ea8e1828Sksagiyam   Collective on DM
1624ea8e1828Sksagiyam 
1625ea8e1828Sksagiyam   Input Parameters:
1626ea8e1828Sksagiyam + dm     - The DM into which the topology is loaded
1627ea8e1828Sksagiyam - viewer - The PetscViewer for the saved topology
1628ea8e1828Sksagiyam 
1629dec9e869Sksagiyam   Output Parameters:
1630dec9e869Sksagiyam . 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
1631dec9e869Sksagiyam 
1632ea8e1828Sksagiyam   Level: advanced
1633ea8e1828Sksagiyam 
1634b08ad5deSksagiyam .seealso: DMLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1635ea8e1828Sksagiyam @*/
1636dec9e869Sksagiyam PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *sf)
1637ea8e1828Sksagiyam {
1638ea8e1828Sksagiyam   PetscBool      ishdf5;
1639ea8e1828Sksagiyam   PetscErrorCode ierr;
1640ea8e1828Sksagiyam 
1641ea8e1828Sksagiyam   PetscFunctionBegin;
1642ea8e1828Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1643ea8e1828Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1644806c51deSksagiyam   if (sf) PetscValidPointer(sf, 3);
1645ea8e1828Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1646ea8e1828Sksagiyam   if (ishdf5) {
1647ea8e1828Sksagiyam #if defined(PETSC_HAVE_HDF5)
1648ea8e1828Sksagiyam     PetscViewerFormat format;
1649ea8e1828Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1650ea8e1828Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1651dec9e869Sksagiyam       ierr = DMPlexTopologyLoad_HDF5_Internal(dm, viewer, sf);CHKERRQ(ierr);
1652ea8e1828Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1653ea8e1828Sksagiyam #else
1654ea8e1828Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1655ea8e1828Sksagiyam #endif
1656ea8e1828Sksagiyam   }
1657ea8e1828Sksagiyam   PetscFunctionReturn(0);
1658ea8e1828Sksagiyam }
1659ea8e1828Sksagiyam 
16603e701f1cSksagiyam /*@
16613e701f1cSksagiyam   DMPlexCoordinatesLoad - Loads coordinates into a DMPlex
16623e701f1cSksagiyam 
16633e701f1cSksagiyam   Collective on DM
16643e701f1cSksagiyam 
16653e701f1cSksagiyam   Input Parameters:
16663e701f1cSksagiyam + dm     - The DM into which the coordinates are loaded
16673e701f1cSksagiyam - viewer - The PetscViewer for the saved coordinates
16683e701f1cSksagiyam 
16693e701f1cSksagiyam   Level: advanced
16703e701f1cSksagiyam 
1671b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
16723e701f1cSksagiyam @*/
16733e701f1cSksagiyam PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer)
16743e701f1cSksagiyam {
16753e701f1cSksagiyam   PetscBool      ishdf5;
16763e701f1cSksagiyam   PetscErrorCode ierr;
16773e701f1cSksagiyam 
16783e701f1cSksagiyam   PetscFunctionBegin;
16793e701f1cSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16803e701f1cSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
16813e701f1cSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
16823e701f1cSksagiyam   if (ishdf5) {
16833e701f1cSksagiyam #if defined(PETSC_HAVE_HDF5)
16843e701f1cSksagiyam     PetscViewerFormat format;
16853e701f1cSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
16863e701f1cSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
16873e701f1cSksagiyam       ierr = DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
16883e701f1cSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
16893e701f1cSksagiyam #else
16903e701f1cSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
16913e701f1cSksagiyam #endif
16923e701f1cSksagiyam   }
16933e701f1cSksagiyam   PetscFunctionReturn(0);
16943e701f1cSksagiyam }
16953e701f1cSksagiyam 
1696b08ad5deSksagiyam /*@
1697b08ad5deSksagiyam   DMPlexLabelsLoad - Loads labels into a DMPlex
1698b08ad5deSksagiyam 
1699b08ad5deSksagiyam   Collective on DM
1700b08ad5deSksagiyam 
1701b08ad5deSksagiyam   Input Parameters:
1702b08ad5deSksagiyam + dm     - The DM into which the labels are loaded
1703b08ad5deSksagiyam - viewer - The PetscViewer for the saved labels
1704b08ad5deSksagiyam 
1705b08ad5deSksagiyam   Level: advanced
1706b08ad5deSksagiyam 
1707b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1708b08ad5deSksagiyam @*/
1709b08ad5deSksagiyam PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer)
1710b08ad5deSksagiyam {
1711b08ad5deSksagiyam   PetscBool      ishdf5;
1712b08ad5deSksagiyam   PetscErrorCode ierr;
1713b08ad5deSksagiyam 
1714b08ad5deSksagiyam   PetscFunctionBegin;
1715b08ad5deSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1716b08ad5deSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1717b08ad5deSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1718b08ad5deSksagiyam   if (ishdf5) {
1719b08ad5deSksagiyam #if defined(PETSC_HAVE_HDF5)
1720b08ad5deSksagiyam     PetscViewerFormat format;
1721b08ad5deSksagiyam 
1722b08ad5deSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1723b08ad5deSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1724b08ad5deSksagiyam       ierr = DMPlexLabelsLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1725b08ad5deSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1726b08ad5deSksagiyam #else
1727b08ad5deSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1728b08ad5deSksagiyam #endif
1729b08ad5deSksagiyam   }
1730b08ad5deSksagiyam   PetscFunctionReturn(0);
1731b08ad5deSksagiyam }
1732b08ad5deSksagiyam 
1733552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1734552f7358SJed Brown {
1735552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1736552f7358SJed Brown   PetscErrorCode ierr;
1737552f7358SJed Brown 
1738552f7358SJed Brown   PetscFunctionBegin;
17398135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
17408135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
174128d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
17421eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);CHKERRQ(ierr);
17430d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1744552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1745552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1746552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1747552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1748be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1749552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1750552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1751d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1752d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
175377623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1754a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
175597d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
1756552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1757552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1758a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1759a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1760d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1761d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1762d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1763d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1764d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1765d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1766fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
17670a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
1768552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1769552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1770552f7358SJed Brown   PetscFunctionReturn(0);
1771552f7358SJed Brown }
1772552f7358SJed Brown 
1773b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1774552f7358SJed Brown {
17758d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1776acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1777552f7358SJed Brown   PetscInt               localSize;
1778837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1779552f7358SJed Brown   PetscErrorCode         ierr;
1780b412c318SBarry Smith   MatType                mtype;
17811428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1782552f7358SJed Brown 
1783552f7358SJed Brown   PetscFunctionBegin;
1784607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1785b412c318SBarry Smith   mtype = dm->mattype;
1786e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1787552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1788552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
178982f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1790552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1791552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1792552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1793acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1794acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1795552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1796552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1797552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1798552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1799552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1800552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1801552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1802837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1803552f7358SJed Brown   if (!isShell) {
1804be36d101SStefano Zampini     PetscSection subSection;
1805837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
18060be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1807fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1808552f7358SJed Brown 
180900140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1810be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1811be36d101SStefano Zampini       PetscSection section;
1812be36d101SStefano Zampini       PetscInt     size;
1813be36d101SStefano Zampini 
181492fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1815be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1816be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1817be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1818be36d101SStefano Zampini     } else {
181900140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1820be36d101SStefano Zampini     }
1821552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1822be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1823a9d99c84SMatthew G. Knepley       PetscInt bdof;
1824a9d99c84SMatthew G. Knepley 
1825552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1826fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
18271d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
18281d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
18291d17a0a3SMatthew G. Knepley       if (dof) {
18301d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1831be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1832be36d101SStefano Zampini       }
1833be36d101SStefano Zampini       if (isMatIS) {
1834be36d101SStefano Zampini         PetscInt loff,c,off;
1835be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1836be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1837be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1838552f7358SJed Brown       }
18392a28c762SMatthew G Knepley     }
18402a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
18410be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
18420be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
18430be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
18440be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
18456fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
18466fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
18474fa26246SMatthew G. Knepley       PetscInt l;
18486fd5c86aSStefano Zampini 
18496fd5c86aSStefano Zampini       lsize = lsize/bs;
18506fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
18514fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1852be36d101SStefano Zampini     }
1853be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1854be36d101SStefano Zampini     if (isMatIS) {
1855be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1856be36d101SStefano Zampini     }
18571795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
18588d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1859552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1860552f7358SJed Brown   }
1861b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1862552f7358SJed Brown   PetscFunctionReturn(0);
1863552f7358SJed Brown }
1864552f7358SJed Brown 
18657cd05799SMatthew G. Knepley /*@
1866a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1867be36d101SStefano Zampini 
1868be36d101SStefano Zampini   Not collective
1869be36d101SStefano Zampini 
1870be36d101SStefano Zampini   Input Parameter:
1871be36d101SStefano Zampini . mesh - The DMPlex
1872be36d101SStefano Zampini 
1873be36d101SStefano Zampini   Output Parameters:
1874be36d101SStefano Zampini . subsection - The subdomain section
1875be36d101SStefano Zampini 
1876be36d101SStefano Zampini   Level: developer
1877be36d101SStefano Zampini 
1878be36d101SStefano Zampini .seealso:
18797cd05799SMatthew G. Knepley @*/
1880be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1881be36d101SStefano Zampini {
1882be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1883be36d101SStefano Zampini   PetscErrorCode ierr;
1884be36d101SStefano Zampini 
1885be36d101SStefano Zampini   PetscFunctionBegin;
1886be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1887be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1888be36d101SStefano Zampini     PetscSection section;
1889be36d101SStefano Zampini     PetscSF      sf;
1890be36d101SStefano Zampini 
1891be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
189292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1893be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1894be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1895be36d101SStefano Zampini   }
1896be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1897be36d101SStefano Zampini   PetscFunctionReturn(0);
1898be36d101SStefano Zampini }
1899be36d101SStefano Zampini 
1900552f7358SJed Brown /*@
1901552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1902552f7358SJed Brown 
1903552f7358SJed Brown   Not collective
1904552f7358SJed Brown 
1905552f7358SJed Brown   Input Parameter:
1906552f7358SJed Brown . mesh - The DMPlex
1907552f7358SJed Brown 
1908552f7358SJed Brown   Output Parameters:
1909552f7358SJed Brown + pStart - The first mesh point
1910552f7358SJed Brown - pEnd   - The upper bound for mesh points
1911552f7358SJed Brown 
1912552f7358SJed Brown   Level: beginner
1913552f7358SJed Brown 
1914552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1915552f7358SJed Brown @*/
1916552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1917552f7358SJed Brown {
1918552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1919552f7358SJed Brown   PetscErrorCode ierr;
1920552f7358SJed Brown 
1921552f7358SJed Brown   PetscFunctionBegin;
1922552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1923552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1924552f7358SJed Brown   PetscFunctionReturn(0);
1925552f7358SJed Brown }
1926552f7358SJed Brown 
1927552f7358SJed Brown /*@
1928552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1929552f7358SJed Brown 
1930552f7358SJed Brown   Not collective
1931552f7358SJed Brown 
1932552f7358SJed Brown   Input Parameters:
1933552f7358SJed Brown + mesh - The DMPlex
1934552f7358SJed Brown . pStart - The first mesh point
1935552f7358SJed Brown - pEnd   - The upper bound for mesh points
1936552f7358SJed Brown 
1937552f7358SJed Brown   Output Parameters:
1938552f7358SJed Brown 
1939552f7358SJed Brown   Level: beginner
1940552f7358SJed Brown 
1941552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1942552f7358SJed Brown @*/
1943552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1944552f7358SJed Brown {
1945552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1946552f7358SJed Brown   PetscErrorCode ierr;
1947552f7358SJed Brown 
1948552f7358SJed Brown   PetscFunctionBegin;
1949552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1950552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1951552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1952552f7358SJed Brown   PetscFunctionReturn(0);
1953552f7358SJed Brown }
1954552f7358SJed Brown 
1955552f7358SJed Brown /*@
1956eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1957552f7358SJed Brown 
1958552f7358SJed Brown   Not collective
1959552f7358SJed Brown 
1960552f7358SJed Brown   Input Parameters:
1961552f7358SJed Brown + mesh - The DMPlex
1962eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1963552f7358SJed Brown 
1964552f7358SJed Brown   Output Parameter:
1965552f7358SJed Brown . size - The cone size for point p
1966552f7358SJed Brown 
1967552f7358SJed Brown   Level: beginner
1968552f7358SJed Brown 
1969552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1970552f7358SJed Brown @*/
1971552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1972552f7358SJed Brown {
1973552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1974552f7358SJed Brown   PetscErrorCode ierr;
1975552f7358SJed Brown 
1976552f7358SJed Brown   PetscFunctionBegin;
1977552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1978552f7358SJed Brown   PetscValidPointer(size, 3);
1979552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1980552f7358SJed Brown   PetscFunctionReturn(0);
1981552f7358SJed Brown }
1982552f7358SJed Brown 
1983552f7358SJed Brown /*@
1984eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1985552f7358SJed Brown 
1986552f7358SJed Brown   Not collective
1987552f7358SJed Brown 
1988552f7358SJed Brown   Input Parameters:
1989552f7358SJed Brown + mesh - The DMPlex
1990eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1991552f7358SJed Brown - size - The cone size for point p
1992552f7358SJed Brown 
1993552f7358SJed Brown   Output Parameter:
1994552f7358SJed Brown 
1995552f7358SJed Brown   Note:
1996552f7358SJed Brown   This should be called after DMPlexSetChart().
1997552f7358SJed Brown 
1998552f7358SJed Brown   Level: beginner
1999552f7358SJed Brown 
2000552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
2001552f7358SJed Brown @*/
2002552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
2003552f7358SJed Brown {
2004552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2005552f7358SJed Brown   PetscErrorCode ierr;
2006552f7358SJed Brown 
2007552f7358SJed Brown   PetscFunctionBegin;
2008552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2009552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
20100d644c17SKarl Rupp 
2011552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
2012552f7358SJed Brown   PetscFunctionReturn(0);
2013552f7358SJed Brown }
2014552f7358SJed Brown 
2015f5a469b9SMatthew G. Knepley /*@
2016eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
2017f5a469b9SMatthew G. Knepley 
2018f5a469b9SMatthew G. Knepley   Not collective
2019f5a469b9SMatthew G. Knepley 
2020f5a469b9SMatthew G. Knepley   Input Parameters:
2021f5a469b9SMatthew G. Knepley + mesh - The DMPlex
2022eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2023f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
2024f5a469b9SMatthew G. Knepley 
2025f5a469b9SMatthew G. Knepley   Output Parameter:
2026f5a469b9SMatthew G. Knepley 
2027f5a469b9SMatthew G. Knepley   Note:
2028f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
2029f5a469b9SMatthew G. Knepley 
2030f5a469b9SMatthew G. Knepley   Level: beginner
2031f5a469b9SMatthew G. Knepley 
2032f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
2033f5a469b9SMatthew G. Knepley @*/
2034f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
2035f5a469b9SMatthew G. Knepley {
2036f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2037f5a469b9SMatthew G. Knepley   PetscInt       csize;
2038f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
2039f5a469b9SMatthew G. Knepley 
2040f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
2041f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2042f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
2043f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
2044f5a469b9SMatthew G. Knepley 
2045f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
2046f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
2047f5a469b9SMatthew G. Knepley }
2048f5a469b9SMatthew G. Knepley 
2049552f7358SJed Brown /*@C
2050eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
2051552f7358SJed Brown 
2052552f7358SJed Brown   Not collective
2053552f7358SJed Brown 
2054552f7358SJed Brown   Input Parameters:
2055833c876bSVaclav Hapla + dm - The DMPlex
2056eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2057552f7358SJed Brown 
2058552f7358SJed Brown   Output Parameter:
2059552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
2060552f7358SJed Brown 
2061552f7358SJed Brown   Level: beginner
2062552f7358SJed Brown 
20633813dfbdSMatthew G Knepley   Fortran Notes:
20643813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20653813dfbdSMatthew G Knepley   include petsc.h90 in your code.
2066922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
2067922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
20683813dfbdSMatthew G Knepley 
2069e45f02b0SMatthew G. Knepley .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
2070552f7358SJed Brown @*/
2071552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
2072552f7358SJed Brown {
2073552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2074552f7358SJed Brown   PetscInt       off;
2075552f7358SJed Brown   PetscErrorCode ierr;
2076552f7358SJed Brown 
2077552f7358SJed Brown   PetscFunctionBegin;
2078552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2079552f7358SJed Brown   PetscValidPointer(cone, 3);
2080552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2081552f7358SJed Brown   *cone = &mesh->cones[off];
2082552f7358SJed Brown   PetscFunctionReturn(0);
2083552f7358SJed Brown }
2084552f7358SJed Brown 
20850ce7577fSVaclav Hapla /*@C
20860ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
20870ce7577fSVaclav Hapla 
20880ce7577fSVaclav Hapla   Not collective
20890ce7577fSVaclav Hapla 
20900ce7577fSVaclav Hapla   Input Parameters:
20910ce7577fSVaclav Hapla + dm - The DMPlex
20920ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
20930ce7577fSVaclav Hapla 
20940ce7577fSVaclav Hapla   Output Parameter:
20950ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
20960ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
20970ce7577fSVaclav Hapla 
20980ce7577fSVaclav Hapla   Level: intermediate
20990ce7577fSVaclav Hapla 
2100d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
21010ce7577fSVaclav Hapla @*/
21020ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
21030ce7577fSVaclav Hapla {
21040ce7577fSVaclav Hapla   PetscSection        cs, newcs;
21050ce7577fSVaclav Hapla   PetscInt            *cones;
21060ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
21070ce7577fSVaclav Hapla   PetscInt            n;
21080ce7577fSVaclav Hapla   PetscErrorCode      ierr;
21090ce7577fSVaclav Hapla 
21100ce7577fSVaclav Hapla   PetscFunctionBegin;
21110ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
21120ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
21130ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
21140ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
21150ce7577fSVaclav Hapla   if (pCones) {
21160ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
21170ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
21180ce7577fSVaclav Hapla   }
21190ce7577fSVaclav Hapla   PetscFunctionReturn(0);
21200ce7577fSVaclav Hapla }
21210ce7577fSVaclav Hapla 
2122af9eab45SVaclav Hapla /*@
2123af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
2124d4636a37SVaclav Hapla 
2125d4636a37SVaclav Hapla   Not collective
2126d4636a37SVaclav Hapla 
2127d4636a37SVaclav Hapla   Input Parameters:
2128d4636a37SVaclav Hapla + dm - The DMPlex
2129af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2130d4636a37SVaclav Hapla 
2131d4636a37SVaclav Hapla   Output Parameter:
2132af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
2133d4636a37SVaclav Hapla 
2134d4636a37SVaclav Hapla   Level: advanced
2135d4636a37SVaclav Hapla 
2136af9eab45SVaclav Hapla   Notes:
2137af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
2138af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
2139af9eab45SVaclav Hapla 
2140af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
2141d4636a37SVaclav Hapla @*/
2142af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2143d4636a37SVaclav Hapla {
2144af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
2145af9eab45SVaclav Hapla   PetscInt            depth;
2146d4636a37SVaclav Hapla   PetscErrorCode      ierr;
2147d4636a37SVaclav Hapla 
2148d4636a37SVaclav Hapla   PetscFunctionBegin;
2149af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2150af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2151af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
2152af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2153af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
2154af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
2155af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2156af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2157af9eab45SVaclav Hapla }
2158af9eab45SVaclav Hapla 
2159af9eab45SVaclav Hapla /*@
2160af9eab45SVaclav 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).
2161af9eab45SVaclav Hapla 
2162af9eab45SVaclav Hapla   Not collective
2163af9eab45SVaclav Hapla 
2164af9eab45SVaclav Hapla   Input Parameters:
2165af9eab45SVaclav Hapla + dm - The DMPlex
2166af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2167af9eab45SVaclav Hapla 
2168af9eab45SVaclav Hapla   Output Parameter:
2169af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2170af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
2171af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2172af9eab45SVaclav Hapla 
2173af9eab45SVaclav Hapla   Level: advanced
2174af9eab45SVaclav Hapla 
2175af9eab45SVaclav Hapla   Notes:
2176af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
2177af9eab45SVaclav Hapla 
2178af9eab45SVaclav 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.
2179af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
2180af9eab45SVaclav Hapla 
2181af9eab45SVaclav 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:
2182af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
2183af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
2184af9eab45SVaclav Hapla 
2185af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2186af9eab45SVaclav Hapla @*/
2187af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2188af9eab45SVaclav Hapla {
2189af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
2190af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
2191af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
2192af9eab45SVaclav Hapla   IS                  *expandedPoints_;
2193af9eab45SVaclav Hapla   PetscSection        *sections_;
2194af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2195af9eab45SVaclav Hapla 
2196af9eab45SVaclav Hapla   PetscFunctionBegin;
2197af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2198af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2199af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
2200af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
2201af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
2202af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
2203af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
2204af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2205af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
2206af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
2207af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
2208af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
2209af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
2210af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
2211af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2212af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
2213af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
2214af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
2215af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
2216af9eab45SVaclav Hapla       } else {
2217af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
2218af9eab45SVaclav Hapla       }
2219af9eab45SVaclav Hapla     }
2220af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
2221af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
2222af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
2223af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2224af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
2225af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
2226af9eab45SVaclav Hapla       if (cn > 1) {
2227af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
2228af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
2229af9eab45SVaclav Hapla       } else {
2230af9eab45SVaclav Hapla         newarr[co] = arr[i];
2231af9eab45SVaclav Hapla       }
2232af9eab45SVaclav Hapla     }
2233af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
2234af9eab45SVaclav Hapla     arr = newarr;
2235af9eab45SVaclav Hapla     n = newn;
2236af9eab45SVaclav Hapla   }
2237ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
2238af9eab45SVaclav Hapla   *depth = depth_;
2239af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
2240af9eab45SVaclav Hapla   else {
2241af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
2242af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
2243af9eab45SVaclav Hapla   }
2244af9eab45SVaclav Hapla   if (sections) *sections = sections_;
2245af9eab45SVaclav Hapla   else {
2246af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
2247af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
2248af9eab45SVaclav Hapla   }
2249af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2250af9eab45SVaclav Hapla }
2251af9eab45SVaclav Hapla 
2252af9eab45SVaclav Hapla /*@
2253af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
2254af9eab45SVaclav Hapla 
2255af9eab45SVaclav Hapla   Not collective
2256af9eab45SVaclav Hapla 
2257af9eab45SVaclav Hapla   Input Parameters:
2258af9eab45SVaclav Hapla + dm - The DMPlex
2259af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2260af9eab45SVaclav Hapla 
2261af9eab45SVaclav Hapla   Output Parameter:
2262af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2263af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
2264af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2265af9eab45SVaclav Hapla 
2266af9eab45SVaclav Hapla   Level: advanced
2267af9eab45SVaclav Hapla 
2268af9eab45SVaclav Hapla   Notes:
2269af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
2270af9eab45SVaclav Hapla 
2271af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2272af9eab45SVaclav Hapla @*/
2273af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2274af9eab45SVaclav Hapla {
2275af9eab45SVaclav Hapla   PetscInt            d, depth_;
2276af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2277af9eab45SVaclav Hapla 
2278af9eab45SVaclav Hapla   PetscFunctionBegin;
2279af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2280af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
2281af9eab45SVaclav Hapla   if (depth) *depth = 0;
2282af9eab45SVaclav Hapla   if (expandedPoints) {
2283af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
2284af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
2285af9eab45SVaclav Hapla   }
2286af9eab45SVaclav Hapla   if (sections)  {
2287af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
2288af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
2289af9eab45SVaclav Hapla   }
2290d4636a37SVaclav Hapla   PetscFunctionReturn(0);
2291d4636a37SVaclav Hapla }
2292d4636a37SVaclav Hapla 
2293552f7358SJed Brown /*@
229492371b87SBarry 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
2295552f7358SJed Brown 
2296552f7358SJed Brown   Not collective
2297552f7358SJed Brown 
2298552f7358SJed Brown   Input Parameters:
2299552f7358SJed Brown + mesh - The DMPlex
2300eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2301552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
2302552f7358SJed Brown 
2303552f7358SJed Brown   Output Parameter:
2304552f7358SJed Brown 
2305552f7358SJed Brown   Note:
2306552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2307552f7358SJed Brown 
230892371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
230992371b87SBarry Smith 
2310552f7358SJed Brown   Level: beginner
2311552f7358SJed Brown 
231292371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
2313552f7358SJed Brown @*/
2314552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
2315552f7358SJed Brown {
2316552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2317552f7358SJed Brown   PetscInt       pStart, pEnd;
2318552f7358SJed Brown   PetscInt       dof, off, c;
2319552f7358SJed Brown   PetscErrorCode ierr;
2320552f7358SJed Brown 
2321552f7358SJed Brown   PetscFunctionBegin;
2322552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2323552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2324552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2325552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
2326552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
232782f516ccSBarry 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);
2328552f7358SJed Brown   for (c = 0; c < dof; ++c) {
232982f516ccSBarry 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);
2330552f7358SJed Brown     mesh->cones[off+c] = cone[c];
2331552f7358SJed Brown   }
2332552f7358SJed Brown   PetscFunctionReturn(0);
2333552f7358SJed Brown }
2334552f7358SJed Brown 
2335552f7358SJed Brown /*@C
2336eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
2337552f7358SJed Brown 
2338552f7358SJed Brown   Not collective
2339552f7358SJed Brown 
2340552f7358SJed Brown   Input Parameters:
2341552f7358SJed Brown + mesh - The DMPlex
2342eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2343552f7358SJed Brown 
2344552f7358SJed Brown   Output Parameter:
2345552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2346552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2347552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2348552f7358SJed Brown                     the index of the cone point on which to start.
2349552f7358SJed Brown 
2350552f7358SJed Brown   Level: beginner
2351552f7358SJed Brown 
23523813dfbdSMatthew G Knepley   Fortran Notes:
23533813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23543813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23553b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2356922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
23573813dfbdSMatthew G Knepley 
2358552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2359552f7358SJed Brown @*/
2360552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2361552f7358SJed Brown {
2362552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2363552f7358SJed Brown   PetscInt       off;
2364552f7358SJed Brown   PetscErrorCode ierr;
2365552f7358SJed Brown 
2366552f7358SJed Brown   PetscFunctionBegin;
2367552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
236876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2369552f7358SJed Brown     PetscInt dof;
2370552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2371552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2372552f7358SJed Brown   }
2373552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
23740d644c17SKarl Rupp 
2375552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2376552f7358SJed Brown   PetscFunctionReturn(0);
2377552f7358SJed Brown }
2378552f7358SJed Brown 
2379552f7358SJed Brown /*@
2380eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2381552f7358SJed Brown 
2382552f7358SJed Brown   Not collective
2383552f7358SJed Brown 
2384552f7358SJed Brown   Input Parameters:
2385552f7358SJed Brown + mesh - The DMPlex
2386eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2387552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2388552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2389552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2390552f7358SJed Brown                     the index of the cone point on which to start.
2391552f7358SJed Brown 
2392552f7358SJed Brown   Output Parameter:
2393552f7358SJed Brown 
2394552f7358SJed Brown   Note:
2395552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2396552f7358SJed Brown 
2397552f7358SJed Brown   Level: beginner
2398552f7358SJed Brown 
2399552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2400552f7358SJed Brown @*/
2401552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2402552f7358SJed Brown {
2403552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2404552f7358SJed Brown   PetscInt       pStart, pEnd;
2405552f7358SJed Brown   PetscInt       dof, off, c;
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);
2411552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2412552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2413552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
241482f516ccSBarry 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);
2415552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2416552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2417552f7358SJed Brown 
2418552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
241982f516ccSBarry 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);
2420552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2421552f7358SJed Brown   }
2422552f7358SJed Brown   PetscFunctionReturn(0);
2423552f7358SJed Brown }
2424552f7358SJed Brown 
24257cd05799SMatthew G. Knepley /*@
2426eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
24277cd05799SMatthew G. Knepley 
24287cd05799SMatthew G. Knepley   Not collective
24297cd05799SMatthew G. Knepley 
24307cd05799SMatthew G. Knepley   Input Parameters:
24317cd05799SMatthew G. Knepley + mesh - The DMPlex
2432eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24337cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
24347cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
24357cd05799SMatthew G. Knepley 
24367cd05799SMatthew G. Knepley   Level: beginner
24377cd05799SMatthew G. Knepley 
24387cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
24397cd05799SMatthew G. Knepley @*/
2440552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2441552f7358SJed Brown {
2442552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2443552f7358SJed Brown   PetscInt       pStart, pEnd;
2444552f7358SJed Brown   PetscInt       dof, off;
2445552f7358SJed Brown   PetscErrorCode ierr;
2446552f7358SJed Brown 
2447552f7358SJed Brown   PetscFunctionBegin;
2448552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2449552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
245082f516ccSBarry 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);
245182f516ccSBarry 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);
245277c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
245377c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
245477c88f5bSMatthew 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);
2455552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2456552f7358SJed Brown   PetscFunctionReturn(0);
2457552f7358SJed Brown }
2458552f7358SJed Brown 
24597cd05799SMatthew G. Knepley /*@
2460eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
24617cd05799SMatthew G. Knepley 
24627cd05799SMatthew G. Knepley   Not collective
24637cd05799SMatthew G. Knepley 
24647cd05799SMatthew G. Knepley   Input Parameters:
24657cd05799SMatthew G. Knepley + mesh - The DMPlex
2466eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24677cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
24687cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
24697cd05799SMatthew G. Knepley 
24707cd05799SMatthew G. Knepley   Level: beginner
24717cd05799SMatthew G. Knepley 
24727cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
24737cd05799SMatthew G. Knepley @*/
247477c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
247577c88f5bSMatthew G Knepley {
247677c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
247777c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
247877c88f5bSMatthew G Knepley   PetscInt       dof, off;
247977c88f5bSMatthew G Knepley   PetscErrorCode ierr;
248077c88f5bSMatthew G Knepley 
248177c88f5bSMatthew G Knepley   PetscFunctionBegin;
248277c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
248377c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
248477c88f5bSMatthew 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);
248577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
248677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
248777c88f5bSMatthew 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);
248877c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
248977c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
249077c88f5bSMatthew G Knepley }
249177c88f5bSMatthew G Knepley 
2492552f7358SJed Brown /*@
2493eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2494552f7358SJed Brown 
2495552f7358SJed Brown   Not collective
2496552f7358SJed Brown 
2497552f7358SJed Brown   Input Parameters:
2498552f7358SJed Brown + mesh - The DMPlex
2499eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2500552f7358SJed Brown 
2501552f7358SJed Brown   Output Parameter:
2502552f7358SJed Brown . size - The support size for point p
2503552f7358SJed Brown 
2504552f7358SJed Brown   Level: beginner
2505552f7358SJed Brown 
2506552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2507552f7358SJed Brown @*/
2508552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2509552f7358SJed Brown {
2510552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2511552f7358SJed Brown   PetscErrorCode ierr;
2512552f7358SJed Brown 
2513552f7358SJed Brown   PetscFunctionBegin;
2514552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2515552f7358SJed Brown   PetscValidPointer(size, 3);
2516552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2517552f7358SJed Brown   PetscFunctionReturn(0);
2518552f7358SJed Brown }
2519552f7358SJed Brown 
2520552f7358SJed Brown /*@
2521eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2522552f7358SJed Brown 
2523552f7358SJed Brown   Not collective
2524552f7358SJed Brown 
2525552f7358SJed Brown   Input Parameters:
2526552f7358SJed Brown + mesh - The DMPlex
2527eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2528552f7358SJed Brown - size - The support size for point p
2529552f7358SJed Brown 
2530552f7358SJed Brown   Output Parameter:
2531552f7358SJed Brown 
2532552f7358SJed Brown   Note:
2533552f7358SJed Brown   This should be called after DMPlexSetChart().
2534552f7358SJed Brown 
2535552f7358SJed Brown   Level: beginner
2536552f7358SJed Brown 
2537552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2538552f7358SJed Brown @*/
2539552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2540552f7358SJed Brown {
2541552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2542552f7358SJed Brown   PetscErrorCode ierr;
2543552f7358SJed Brown 
2544552f7358SJed Brown   PetscFunctionBegin;
2545552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2546552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
25470d644c17SKarl Rupp 
2548552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2549552f7358SJed Brown   PetscFunctionReturn(0);
2550552f7358SJed Brown }
2551552f7358SJed Brown 
2552552f7358SJed Brown /*@C
2553eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2554552f7358SJed Brown 
2555552f7358SJed Brown   Not collective
2556552f7358SJed Brown 
2557552f7358SJed Brown   Input Parameters:
2558552f7358SJed Brown + mesh - The DMPlex
2559eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2560552f7358SJed Brown 
2561552f7358SJed Brown   Output Parameter:
2562552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2563552f7358SJed Brown 
2564552f7358SJed Brown   Level: beginner
2565552f7358SJed Brown 
25663813dfbdSMatthew G Knepley   Fortran Notes:
25673813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25683813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25693b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2570922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
25713813dfbdSMatthew G Knepley 
2572e45f02b0SMatthew G. Knepley .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
2573552f7358SJed Brown @*/
2574552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2575552f7358SJed Brown {
2576552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2577552f7358SJed Brown   PetscInt       off;
2578552f7358SJed Brown   PetscErrorCode ierr;
2579552f7358SJed Brown 
2580552f7358SJed Brown   PetscFunctionBegin;
2581552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2582552f7358SJed Brown   PetscValidPointer(support, 3);
2583552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2584552f7358SJed Brown   *support = &mesh->supports[off];
2585552f7358SJed Brown   PetscFunctionReturn(0);
2586552f7358SJed Brown }
2587552f7358SJed Brown 
2588552f7358SJed Brown /*@
258992371b87SBarry 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
2590552f7358SJed Brown 
2591552f7358SJed Brown   Not collective
2592552f7358SJed Brown 
2593552f7358SJed Brown   Input Parameters:
2594552f7358SJed Brown + mesh - The DMPlex
2595eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
259692371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2597552f7358SJed Brown 
2598552f7358SJed Brown   Output Parameter:
2599552f7358SJed Brown 
2600552f7358SJed Brown   Note:
2601552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2602552f7358SJed Brown 
2603552f7358SJed Brown   Level: beginner
2604552f7358SJed Brown 
260592371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2606552f7358SJed Brown @*/
2607552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2608552f7358SJed Brown {
2609552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2610552f7358SJed Brown   PetscInt       pStart, pEnd;
2611552f7358SJed Brown   PetscInt       dof, off, c;
2612552f7358SJed Brown   PetscErrorCode ierr;
2613552f7358SJed Brown 
2614552f7358SJed Brown   PetscFunctionBegin;
2615552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2616552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2617552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2618552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2619552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
262082f516ccSBarry 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);
2621552f7358SJed Brown   for (c = 0; c < dof; ++c) {
262282f516ccSBarry 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);
2623552f7358SJed Brown     mesh->supports[off+c] = support[c];
2624552f7358SJed Brown   }
2625552f7358SJed Brown   PetscFunctionReturn(0);
2626552f7358SJed Brown }
2627552f7358SJed Brown 
26287cd05799SMatthew G. Knepley /*@
2629eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
26307cd05799SMatthew G. Knepley 
26317cd05799SMatthew G. Knepley   Not collective
26327cd05799SMatthew G. Knepley 
26337cd05799SMatthew G. Knepley   Input Parameters:
26347cd05799SMatthew G. Knepley + mesh - The DMPlex
2635eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
26367cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
26377cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
26387cd05799SMatthew G. Knepley 
26397cd05799SMatthew G. Knepley   Level: beginner
26407cd05799SMatthew G. Knepley 
26417cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
26427cd05799SMatthew G. Knepley @*/
2643552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2644552f7358SJed Brown {
2645552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2646552f7358SJed Brown   PetscInt       pStart, pEnd;
2647552f7358SJed Brown   PetscInt       dof, off;
2648552f7358SJed Brown   PetscErrorCode ierr;
2649552f7358SJed Brown 
2650552f7358SJed Brown   PetscFunctionBegin;
2651552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2652552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2653552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2654552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
265582f516ccSBarry 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);
265682f516ccSBarry 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);
265782f516ccSBarry 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);
2658552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2659552f7358SJed Brown   PetscFunctionReturn(0);
2660552f7358SJed Brown }
2661552f7358SJed Brown 
2662552f7358SJed Brown /*@C
2663eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2664552f7358SJed Brown 
2665552f7358SJed Brown   Not collective
2666552f7358SJed Brown 
2667552f7358SJed Brown   Input Parameters:
2668552f7358SJed Brown + mesh - The DMPlex
2669eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2670552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
26710298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2672552f7358SJed Brown 
2673552f7358SJed Brown   Output Parameters:
2674552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2675552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2676552f7358SJed Brown 
2677552f7358SJed Brown   Note:
26780298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2679552f7358SJed Brown 
26803813dfbdSMatthew G Knepley   Fortran Notes:
26813813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26823813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26833813dfbdSMatthew G Knepley 
26843813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26853813dfbdSMatthew G Knepley 
2686552f7358SJed Brown   Level: beginner
2687552f7358SJed Brown 
2688552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2689552f7358SJed Brown @*/
2690552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2691552f7358SJed Brown {
2692552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2693552f7358SJed Brown   PetscInt       *closure, *fifo;
26940298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2695552f7358SJed Brown   PetscInt        tmpSize, t;
2696552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2697552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2698552f7358SJed Brown   PetscErrorCode  ierr;
2699552f7358SJed Brown 
2700552f7358SJed Brown   PetscFunctionBegin;
2701552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2702552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2703552f7358SJed Brown   /* This is only 1-level */
2704552f7358SJed Brown   if (useCone) {
2705552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2706552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2707552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2708552f7358SJed Brown   } else {
2709552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2710552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2711552f7358SJed Brown   }
2712bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2713bfbcdd7aSMatthew G. Knepley     if (*points) {
2714bfbcdd7aSMatthew G. Knepley       closure = *points;
2715bfbcdd7aSMatthew G. Knepley     } else {
2716bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
271769291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2718bfbcdd7aSMatthew G. Knepley     }
2719bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2720bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2721bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2722bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2723bfbcdd7aSMatthew G. Knepley     }
2724bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2725bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2726bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2727bfbcdd7aSMatthew G. Knepley   }
272824c766afSToby Isaac   {
272924c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
273024c766afSToby Isaac 
273124c766afSToby Isaac     c = mesh->maxConeSize;
273224c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
273324c766afSToby Isaac     s = mesh->maxSupportSize;
273424c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
273524c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
273624c766afSToby Isaac   }
273769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2738bfbcdd7aSMatthew G. Knepley   if (*points) {
2739bfbcdd7aSMatthew G. Knepley     closure = *points;
2740bfbcdd7aSMatthew G. Knepley   } else {
274169291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2742bfbcdd7aSMatthew G. Knepley   }
2743bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2744552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2745552f7358SJed Brown     const PetscInt cp = tmp[t];
2746552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2747552f7358SJed Brown 
2748552f7358SJed Brown     closure[closureSize]   = cp;
2749552f7358SJed Brown     closure[closureSize+1] = co;
2750552f7358SJed Brown     fifo[fifoSize]         = cp;
2751552f7358SJed Brown     fifo[fifoSize+1]       = co;
2752552f7358SJed Brown   }
2753bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2754552f7358SJed Brown   while (fifoSize - fifoStart) {
2755552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2756552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2757552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2758552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2759552f7358SJed Brown 
2760552f7358SJed Brown     if (useCone) {
2761552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2762552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2763552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2764552f7358SJed Brown     } else {
2765552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2766552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
27670298fd71SBarry Smith       tmpO = NULL;
2768552f7358SJed Brown     }
2769552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2770552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2771552f7358SJed Brown       const PetscInt cp = tmp[i];
27722e1b13c2SMatthew 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. */
27732e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
27742e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
27752e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2776552f7358SJed Brown       PetscInt       c;
2777552f7358SJed Brown 
27782e1b13c2SMatthew G. Knepley       if (rev) {
27792e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
27802e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
27812e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
27822e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
27832e1b13c2SMatthew G. Knepley       }
2784552f7358SJed Brown       /* Check for duplicate */
2785552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2786552f7358SJed Brown         if (closure[c] == cp) break;
2787552f7358SJed Brown       }
2788552f7358SJed Brown       if (c == closureSize) {
2789552f7358SJed Brown         closure[closureSize]   = cp;
2790552f7358SJed Brown         closure[closureSize+1] = co;
2791552f7358SJed Brown         fifo[fifoSize]         = cp;
2792552f7358SJed Brown         fifo[fifoSize+1]       = co;
2793552f7358SJed Brown         closureSize           += 2;
2794552f7358SJed Brown         fifoSize              += 2;
2795552f7358SJed Brown       }
2796552f7358SJed Brown     }
2797552f7358SJed Brown     fifoStart += 2;
2798552f7358SJed Brown   }
2799552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2800552f7358SJed Brown   if (points)    *points    = closure;
280169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2802552f7358SJed Brown   PetscFunctionReturn(0);
2803552f7358SJed Brown }
2804552f7358SJed Brown 
28059bf0dad6SMatthew G. Knepley /*@C
2806eaf898f9SPatrick 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
28079bf0dad6SMatthew G. Knepley 
28089bf0dad6SMatthew G. Knepley   Not collective
28099bf0dad6SMatthew G. Knepley 
28109bf0dad6SMatthew G. Knepley   Input Parameters:
28119bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2812eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
28139bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
28149bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
28159bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
28169bf0dad6SMatthew G. Knepley 
28179bf0dad6SMatthew G. Knepley   Output Parameters:
28189bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
28199bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
28209bf0dad6SMatthew G. Knepley 
28219bf0dad6SMatthew G. Knepley   Note:
28229bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
28239bf0dad6SMatthew G. Knepley 
28249bf0dad6SMatthew G. Knepley   Fortran Notes:
28259bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
28269bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
28279bf0dad6SMatthew G. Knepley 
28289bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28299bf0dad6SMatthew G. Knepley 
28309bf0dad6SMatthew G. Knepley   Level: beginner
28319bf0dad6SMatthew G. Knepley 
28329bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
28339bf0dad6SMatthew G. Knepley @*/
28349bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
28359bf0dad6SMatthew G. Knepley {
28369bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
28379bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
28389bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
28399bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
28409bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
28419bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
28429bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
28439bf0dad6SMatthew G. Knepley 
28449bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
28459bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28469bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
28479bf0dad6SMatthew G. Knepley   /* This is only 1-level */
28489bf0dad6SMatthew G. Knepley   if (useCone) {
28499bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
28509bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
28519bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
28529bf0dad6SMatthew G. Knepley   } else {
28539bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
28549bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
28559bf0dad6SMatthew G. Knepley   }
28569bf0dad6SMatthew G. Knepley   if (depth == 1) {
28579bf0dad6SMatthew G. Knepley     if (*points) {
28589bf0dad6SMatthew G. Knepley       closure = *points;
28599bf0dad6SMatthew G. Knepley     } else {
28609bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
286169291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
28629bf0dad6SMatthew G. Knepley     }
28639bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
28649bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
28659bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
28669bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
28679bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
28689bf0dad6SMatthew G. Knepley     }
28699bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
28709bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
28719bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
28729bf0dad6SMatthew G. Knepley   }
287324c766afSToby Isaac   {
287424c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
287524c766afSToby Isaac 
287624c766afSToby Isaac     c = mesh->maxConeSize;
287724c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
287824c766afSToby Isaac     s = mesh->maxSupportSize;
287924c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
288024c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
288124c766afSToby Isaac   }
288269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
28839bf0dad6SMatthew G. Knepley   if (*points) {
28849bf0dad6SMatthew G. Knepley     closure = *points;
28859bf0dad6SMatthew G. Knepley   } else {
288669291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
28879bf0dad6SMatthew G. Knepley   }
28889bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
28899bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
28909bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
28919bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
28929bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
28939bf0dad6SMatthew G. Knepley 
28949bf0dad6SMatthew G. Knepley     if (ornt < 0) {
28959bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
28969bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
289786b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
28989bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
28999bf0dad6SMatthew G. Knepley     }
29009bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
29019bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
29029bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
29039bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
29049bf0dad6SMatthew G. Knepley   }
29059bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
29069bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
29079bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
29089bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
29099bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
29109bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
29119bf0dad6SMatthew G. Knepley 
29129bf0dad6SMatthew G. Knepley     if (useCone) {
29139bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
29149bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
29159bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
29169bf0dad6SMatthew G. Knepley     } else {
29179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
29189bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
29199bf0dad6SMatthew G. Knepley       tmpO = NULL;
29209bf0dad6SMatthew G. Knepley     }
29219bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
29229bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
29239bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
29249bf0dad6SMatthew 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. */
29259bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
29269bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
29279bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
29289bf0dad6SMatthew G. Knepley       PetscInt       c;
29299bf0dad6SMatthew G. Knepley 
29309bf0dad6SMatthew G. Knepley       if (rev) {
29319bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
29329bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
29339bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
29349bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
29359bf0dad6SMatthew G. Knepley       }
29369bf0dad6SMatthew G. Knepley       /* Check for duplicate */
29379bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
29389bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
29399bf0dad6SMatthew G. Knepley       }
29409bf0dad6SMatthew G. Knepley       if (c == closureSize) {
29419bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
29429bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
29439bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
29449bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
29459bf0dad6SMatthew G. Knepley         closureSize           += 2;
29469bf0dad6SMatthew G. Knepley         fifoSize              += 2;
29479bf0dad6SMatthew G. Knepley       }
29489bf0dad6SMatthew G. Knepley     }
29499bf0dad6SMatthew G. Knepley     fifoStart += 2;
29509bf0dad6SMatthew G. Knepley   }
29519bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
29529bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
295369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
29549bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
29559bf0dad6SMatthew G. Knepley }
29569bf0dad6SMatthew G. Knepley 
2957552f7358SJed Brown /*@C
2958eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2959552f7358SJed Brown 
2960552f7358SJed Brown   Not collective
2961552f7358SJed Brown 
2962552f7358SJed Brown   Input Parameters:
2963552f7358SJed Brown + mesh - The DMPlex
2964eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2965552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2966e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2967e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2968552f7358SJed Brown 
2969552f7358SJed Brown   Note:
29700298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2971552f7358SJed Brown 
29723813dfbdSMatthew G Knepley   Fortran Notes:
29733813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29743813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29753813dfbdSMatthew G Knepley 
29763813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29773813dfbdSMatthew G Knepley 
2978552f7358SJed Brown   Level: beginner
2979552f7358SJed Brown 
2980552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2981552f7358SJed Brown @*/
2982552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2983552f7358SJed Brown {
2984552f7358SJed Brown   PetscErrorCode ierr;
2985552f7358SJed Brown 
2986552f7358SJed Brown   PetscFunctionBegin;
2987552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2988e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2989e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
299069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
29914ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2992552f7358SJed Brown   PetscFunctionReturn(0);
2993552f7358SJed Brown }
2994552f7358SJed Brown 
2995552f7358SJed Brown /*@
2996eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2997552f7358SJed Brown 
2998552f7358SJed Brown   Not collective
2999552f7358SJed Brown 
3000552f7358SJed Brown   Input Parameter:
3001552f7358SJed Brown . mesh - The DMPlex
3002552f7358SJed Brown 
3003552f7358SJed Brown   Output Parameters:
3004552f7358SJed Brown + maxConeSize - The maximum number of in-edges
3005552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
3006552f7358SJed Brown 
3007552f7358SJed Brown   Level: beginner
3008552f7358SJed Brown 
3009552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
3010552f7358SJed Brown @*/
3011552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
3012552f7358SJed Brown {
3013552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3014552f7358SJed Brown 
3015552f7358SJed Brown   PetscFunctionBegin;
3016552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3017552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
3018552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
3019552f7358SJed Brown   PetscFunctionReturn(0);
3020552f7358SJed Brown }
3021552f7358SJed Brown 
3022552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
3023552f7358SJed Brown {
3024552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3025552f7358SJed Brown   PetscInt       size;
3026552f7358SJed Brown   PetscErrorCode ierr;
3027552f7358SJed Brown 
3028552f7358SJed Brown   PetscFunctionBegin;
3029552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3030552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
3031552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
30321795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
30331795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
3034ef1259faSMatthew G. Knepley   ierr = PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));CHKERRQ(ierr);
3035552f7358SJed Brown   if (mesh->maxSupportSize) {
3036552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
3037552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
30381795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
3039ef1259faSMatthew G. Knepley     ierr = PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));CHKERRQ(ierr);
3040552f7358SJed Brown   }
3041552f7358SJed Brown   PetscFunctionReturn(0);
3042552f7358SJed Brown }
3043552f7358SJed Brown 
3044276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3045552f7358SJed Brown {
3046552f7358SJed Brown   PetscErrorCode ierr;
3047552f7358SJed Brown 
3048552f7358SJed Brown   PetscFunctionBegin;
30494d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
3050792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
3051c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
3052736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
3053f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
3054f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
3055f94b4a02SBlaise Bourdin 
30563dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
30573dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
3058c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
3059f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
3060f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
3061f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
3062f94b4a02SBlaise Bourdin 
3063f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3064c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
3065f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
3066f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
3067f94b4a02SBlaise Bourdin   }
3068552f7358SJed Brown   PetscFunctionReturn(0);
3069552f7358SJed Brown }
3070552f7358SJed Brown 
30712adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
30722adcc780SMatthew G. Knepley {
30732adcc780SMatthew G. Knepley   PetscErrorCode ierr;
30743dcd263cSBlaise Bourdin   PetscInt       i = 0;
30752adcc780SMatthew G. Knepley 
30762adcc780SMatthew G. Knepley   PetscFunctionBegin;
3077435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
3078792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
3079c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
30803dcd263cSBlaise Bourdin   for (i = 0; i < len; i++) {
30813dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
30823dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
30833dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
30843dcd263cSBlaise Bourdin 
30853dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
30863dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
3087c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
308892fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
30893dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
30903dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
30913dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
30923dcd263cSBlaise Bourdin 
30933dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3094c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
30953dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
30963dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
30973dcd263cSBlaise Bourdin       break;
30983dcd263cSBlaise Bourdin     }
30993dcd263cSBlaise Bourdin   }
31002adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
31012adcc780SMatthew G. Knepley }
31022adcc780SMatthew G. Knepley 
3103552f7358SJed Brown /*@
3104eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
3105552f7358SJed Brown 
3106552f7358SJed Brown   Not collective
3107552f7358SJed Brown 
3108552f7358SJed Brown   Input Parameter:
3109552f7358SJed Brown . mesh - The DMPlex
3110552f7358SJed Brown 
3111552f7358SJed Brown   Output Parameter:
3112552f7358SJed Brown 
3113552f7358SJed Brown   Note:
3114552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
3115552f7358SJed Brown 
3116552f7358SJed Brown   Level: beginner
3117552f7358SJed Brown 
3118552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
3119552f7358SJed Brown @*/
3120552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
3121552f7358SJed Brown {
3122552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3123552f7358SJed Brown   PetscInt      *offsets;
3124552f7358SJed Brown   PetscInt       supportSize;
3125552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3126552f7358SJed Brown   PetscErrorCode ierr;
3127552f7358SJed Brown 
3128552f7358SJed Brown   PetscFunctionBegin;
3129552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
313082f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
313130b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3132552f7358SJed Brown   /* Calculate support sizes */
3133552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3134552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3135552f7358SJed Brown     PetscInt dof, off, c;
3136552f7358SJed Brown 
3137552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3138552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3139552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3140552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
3141552f7358SJed Brown     }
3142552f7358SJed Brown   }
3143552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3144552f7358SJed Brown     PetscInt dof;
3145552f7358SJed Brown 
3146552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
31470d644c17SKarl Rupp 
3148552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
3149552f7358SJed Brown   }
3150552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
3151552f7358SJed Brown   /* Calculate supports */
3152552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
31531795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
31541795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
3155552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3156552f7358SJed Brown     PetscInt dof, off, c;
3157552f7358SJed Brown 
3158552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3159552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3160552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3161552f7358SJed Brown       const PetscInt q = mesh->cones[c];
3162552f7358SJed Brown       PetscInt       offS;
3163552f7358SJed Brown 
3164552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
31650d644c17SKarl Rupp 
3166552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
3167552f7358SJed Brown       ++offsets[q];
3168552f7358SJed Brown     }
3169552f7358SJed Brown   }
3170552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
317130b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3172552f7358SJed Brown   PetscFunctionReturn(0);
3173552f7358SJed Brown }
3174552f7358SJed Brown 
3175277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3176277ea44aSLisandro Dalcin {
3177277ea44aSLisandro Dalcin   IS             stratumIS;
3178277ea44aSLisandro Dalcin   PetscErrorCode ierr;
3179277ea44aSLisandro Dalcin 
3180277ea44aSLisandro Dalcin   PetscFunctionBegin;
3181277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
318276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
3183277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
3184277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
3185277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
3186277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
3187277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3188277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
3189277ea44aSLisandro Dalcin     }
3190277ea44aSLisandro 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);
3191277ea44aSLisandro Dalcin   }
3192277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
3193277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
3194277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
3195277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
3196277ea44aSLisandro Dalcin }
3197277ea44aSLisandro Dalcin 
3198552f7358SJed Brown /*@
3199a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
32006dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
3201552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
3202552f7358SJed Brown   the DAG.
3203552f7358SJed Brown 
3204bf4602e4SToby Isaac   Collective on dm
3205552f7358SJed Brown 
3206552f7358SJed Brown   Input Parameter:
3207552f7358SJed Brown . mesh - The DMPlex
3208552f7358SJed Brown 
3209552f7358SJed Brown   Output Parameter:
3210552f7358SJed Brown 
3211552f7358SJed Brown   Notes:
3212b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3213b1bb481bSMatthew 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
3214b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
3215c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
3216150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
3217552f7358SJed Brown 
3218b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3219b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3220b1bb481bSMatthew 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
3221b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
3222b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
3223b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
3224b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
3225b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
3226b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
3227b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
3228b1bb481bSMatthew Knepley 
3229150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
3230552f7358SJed Brown 
3231552f7358SJed Brown   Level: beginner
3232552f7358SJed Brown 
3233ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
3234552f7358SJed Brown @*/
3235552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
3236552f7358SJed Brown {
3237df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3238aa50250dSMatthew G. Knepley   DMLabel        label;
3239552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3240552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
3241552f7358SJed Brown   PetscErrorCode ierr;
3242552f7358SJed Brown 
3243552f7358SJed Brown   PetscFunctionBegin;
3244552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3245552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3246277ea44aSLisandro Dalcin 
3247277ea44aSLisandro Dalcin   /* Create depth label */
3248aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3249c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
3250aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3251277ea44aSLisandro Dalcin 
3252277ea44aSLisandro Dalcin   {
3253552f7358SJed Brown     /* Initialize roots and count leaves */
3254277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3255277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3256552f7358SJed Brown     PetscInt coneSize, supportSize;
3257552f7358SJed Brown 
3258277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3259552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3260552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3261552f7358SJed Brown       if (!coneSize && supportSize) {
3262277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3263277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3264552f7358SJed Brown         ++numRoots;
3265552f7358SJed Brown       } else if (!supportSize && coneSize) {
3266552f7358SJed Brown         ++numLeaves;
3267552f7358SJed Brown       } else if (!supportSize && !coneSize) {
3268552f7358SJed Brown         /* Isolated points */
3269277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3270277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3271552f7358SJed Brown       }
3272552f7358SJed Brown     }
3273277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
3274277ea44aSLisandro Dalcin   }
3275277ea44aSLisandro Dalcin 
3276552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
3277277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3278277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3279552f7358SJed Brown     PetscInt coneSize, supportSize;
3280552f7358SJed Brown 
3281277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3282552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3283552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3284552f7358SJed Brown       if (!supportSize && coneSize) {
3285277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3286277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3287552f7358SJed Brown       }
3288552f7358SJed Brown     }
3289277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
3290552f7358SJed Brown   } else {
3291277ea44aSLisandro Dalcin     PetscInt level = 0;
3292277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
3293552f7358SJed Brown 
3294277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3295277ea44aSLisandro Dalcin     while (qEnd > qStart) {
3296277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
3297277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
329874ef644bSMatthew G. Knepley 
3299277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
330074ef644bSMatthew G. Knepley         const PetscInt *support;
330174ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
330274ef644bSMatthew G. Knepley 
3303277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
3304277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
330574ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3306277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
3307277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
3308552f7358SJed Brown         }
3309552f7358SJed Brown       }
3310277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
3311277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
3312277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
331374ef644bSMatthew G. Knepley     }
331474ef644bSMatthew G. Knepley   }
3315bf4602e4SToby Isaac   { /* just in case there is an empty process */
3316bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
3317bf4602e4SToby Isaac 
3318bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
3319ffc4695bSBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
3320bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
3321367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
3322bf4602e4SToby Isaac     }
3323bf4602e4SToby Isaac   }
3324d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
3325552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3326552f7358SJed Brown   PetscFunctionReturn(0);
3327552f7358SJed Brown }
3328552f7358SJed Brown 
3329412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
3330ba2698f1SMatthew G. Knepley {
3331412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3332412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
3333ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3334ba2698f1SMatthew G. Knepley 
3335412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
3336ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3337ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3338ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3339ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
3340ba2698f1SMatthew G. Knepley   if (depth <= 1) {
3341ba2698f1SMatthew G. Knepley     switch (pdepth) {
3342ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
3343ba2698f1SMatthew G. Knepley       case 1:
3344ba2698f1SMatthew G. Knepley         switch (coneSize) {
3345ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3346ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3347ba2698f1SMatthew G. Knepley           case 4:
3348ba2698f1SMatthew G. Knepley           switch (dim) {
3349ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3350ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3351ba2698f1SMatthew G. Knepley             default: break;
3352ba2698f1SMatthew G. Knepley           }
3353ba2698f1SMatthew G. Knepley           break;
3354da9060c4SMatthew G. Knepley         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3355ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3356ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3357ba2698f1SMatthew G. Knepley         default: break;
3358ba2698f1SMatthew G. Knepley       }
3359ba2698f1SMatthew G. Knepley     }
3360ba2698f1SMatthew G. Knepley   } else {
3361ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
3362ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
3363ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
3364ba2698f1SMatthew G. Knepley       switch (dim) {
3365ba2698f1SMatthew G. Knepley         case 1:
3366ba2698f1SMatthew G. Knepley           switch (coneSize) {
3367ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3368ba2698f1SMatthew G. Knepley             default: break;
3369ba2698f1SMatthew G. Knepley           }
3370ba2698f1SMatthew G. Knepley           break;
3371ba2698f1SMatthew G. Knepley         case 2:
3372ba2698f1SMatthew G. Knepley           switch (coneSize) {
3373ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3374ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3375ba2698f1SMatthew G. Knepley             default: break;
3376ba2698f1SMatthew G. Knepley           }
3377ba2698f1SMatthew G. Knepley           break;
3378ba2698f1SMatthew G. Knepley         case 3:
3379ba2698f1SMatthew G. Knepley           switch (coneSize) {
3380ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3381da9060c4SMatthew G. Knepley             case 5:
3382da9060c4SMatthew G. Knepley             {
3383da9060c4SMatthew G. Knepley               const PetscInt *cone;
3384da9060c4SMatthew G. Knepley               PetscInt        faceConeSize;
3385da9060c4SMatthew G. Knepley 
3386da9060c4SMatthew G. Knepley               ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
3387da9060c4SMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[0], &faceConeSize);CHKERRQ(ierr);
3388da9060c4SMatthew G. Knepley               switch (faceConeSize) {
3389da9060c4SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3390da9060c4SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
3391da9060c4SMatthew G. Knepley               }
3392da9060c4SMatthew G. Knepley             }
3393da9060c4SMatthew G. Knepley             break;
3394ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3395ba2698f1SMatthew G. Knepley             default: break;
3396ba2698f1SMatthew G. Knepley           }
3397ba2698f1SMatthew G. Knepley           break;
3398ba2698f1SMatthew G. Knepley         default: break;
3399ba2698f1SMatthew G. Knepley       }
3400ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
3401ba2698f1SMatthew G. Knepley       switch (coneSize) {
3402ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3403ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3404ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3405ba2698f1SMatthew G. Knepley         default: break;
3406ba2698f1SMatthew G. Knepley       }
3407ba2698f1SMatthew G. Knepley     }
3408ba2698f1SMatthew G. Knepley   }
3409412e9a14SMatthew G. Knepley   *pt = ct;
3410412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3411ba2698f1SMatthew G. Knepley }
3412412e9a14SMatthew G. Knepley 
3413412e9a14SMatthew G. Knepley /*@
3414412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3415412e9a14SMatthew G. Knepley 
3416412e9a14SMatthew G. Knepley   Collective on dm
3417412e9a14SMatthew G. Knepley 
3418412e9a14SMatthew G. Knepley   Input Parameter:
3419412e9a14SMatthew G. Knepley . mesh - The DMPlex
3420412e9a14SMatthew G. Knepley 
3421412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3422412e9a14SMatthew G. Knepley 
3423412e9a14SMatthew G. Knepley   Level: developer
3424412e9a14SMatthew G. Knepley 
3425412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3426412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3427412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3428412e9a14SMatthew G. Knepley 
3429412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3430412e9a14SMatthew G. Knepley @*/
3431412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3432412e9a14SMatthew G. Knepley {
3433412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3434412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3435412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3436412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3437412e9a14SMatthew G. Knepley 
3438412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3439412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3440412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3441412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3442412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3443412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3444412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3445327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3446412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3447412e9a14SMatthew G. Knepley 
3448412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3449412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3450412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3451412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3452412e9a14SMatthew G. Knepley   }
3453412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3454412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3455ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3456ba2698f1SMatthew G. Knepley }
3457ba2698f1SMatthew G. Knepley 
3458552f7358SJed Brown /*@C
3459552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3460552f7358SJed Brown 
3461552f7358SJed Brown   Not Collective
3462552f7358SJed Brown 
3463552f7358SJed Brown   Input Parameters:
3464552f7358SJed Brown + dm - The DMPlex object
3465552f7358SJed Brown . numPoints - The number of input points for the join
3466552f7358SJed Brown - points - The input points
3467552f7358SJed Brown 
3468552f7358SJed Brown   Output Parameters:
3469552f7358SJed Brown + numCoveredPoints - The number of points in the join
3470552f7358SJed Brown - coveredPoints - The points in the join
3471552f7358SJed Brown 
3472552f7358SJed Brown   Level: intermediate
3473552f7358SJed Brown 
3474552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3475552f7358SJed Brown 
34763813dfbdSMatthew G Knepley   Fortran Notes:
34773813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34783813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34793813dfbdSMatthew G Knepley 
34803813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34813813dfbdSMatthew G Knepley 
3482552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3483552f7358SJed Brown @*/
3484552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3485552f7358SJed Brown {
3486552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3487552f7358SJed Brown   PetscInt      *join[2];
3488552f7358SJed Brown   PetscInt       joinSize, i = 0;
3489552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3490552f7358SJed Brown   PetscErrorCode ierr;
3491552f7358SJed Brown 
3492552f7358SJed Brown   PetscFunctionBegin;
3493552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
349448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
349548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
349648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
349769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
349869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3499552f7358SJed Brown   /* Copy in support of first point */
3500552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3501552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3502552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3503552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3504552f7358SJed Brown   }
3505552f7358SJed Brown   /* Check each successive support */
3506552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3507552f7358SJed Brown     PetscInt newJoinSize = 0;
3508552f7358SJed Brown 
3509552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3510552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3511552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3512552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3513552f7358SJed Brown 
3514552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3515552f7358SJed Brown         if (point == join[i][m]) {
3516552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3517552f7358SJed Brown           break;
3518552f7358SJed Brown         }
3519552f7358SJed Brown       }
3520552f7358SJed Brown     }
3521552f7358SJed Brown     joinSize = newJoinSize;
3522552f7358SJed Brown     i        = 1-i;
3523552f7358SJed Brown   }
3524552f7358SJed Brown   *numCoveredPoints = joinSize;
3525552f7358SJed Brown   *coveredPoints    = join[i];
352669291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3527552f7358SJed Brown   PetscFunctionReturn(0);
3528552f7358SJed Brown }
3529552f7358SJed Brown 
3530552f7358SJed Brown /*@C
3531552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3532552f7358SJed Brown 
3533552f7358SJed Brown   Not Collective
3534552f7358SJed Brown 
3535552f7358SJed Brown   Input Parameters:
3536552f7358SJed Brown + dm - The DMPlex object
3537552f7358SJed Brown . numPoints - The number of input points for the join
3538552f7358SJed Brown - points - The input points
3539552f7358SJed Brown 
3540552f7358SJed Brown   Output Parameters:
3541552f7358SJed Brown + numCoveredPoints - The number of points in the join
3542552f7358SJed Brown - coveredPoints - The points in the join
3543552f7358SJed Brown 
35443813dfbdSMatthew G Knepley   Fortran Notes:
35453813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35463813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35473813dfbdSMatthew G Knepley 
35483813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35493813dfbdSMatthew G Knepley 
3550552f7358SJed Brown   Level: intermediate
3551552f7358SJed Brown 
3552552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3553552f7358SJed Brown @*/
3554552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3555552f7358SJed Brown {
3556552f7358SJed Brown   PetscErrorCode ierr;
3557552f7358SJed Brown 
3558552f7358SJed Brown   PetscFunctionBegin;
3559552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3560d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3561d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3562d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
356369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3564d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3565552f7358SJed Brown   PetscFunctionReturn(0);
3566552f7358SJed Brown }
3567552f7358SJed Brown 
3568552f7358SJed Brown /*@C
3569552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3570552f7358SJed Brown 
3571552f7358SJed Brown   Not Collective
3572552f7358SJed Brown 
3573552f7358SJed Brown   Input Parameters:
3574552f7358SJed Brown + dm - The DMPlex object
3575552f7358SJed Brown . numPoints - The number of input points for the join
3576552f7358SJed Brown - points - The input points
3577552f7358SJed Brown 
3578552f7358SJed Brown   Output Parameters:
3579552f7358SJed Brown + numCoveredPoints - The number of points in the join
3580552f7358SJed Brown - coveredPoints - The points in the join
3581552f7358SJed Brown 
35823813dfbdSMatthew G Knepley   Fortran Notes:
35833813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35843813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35853813dfbdSMatthew G Knepley 
35863813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35873813dfbdSMatthew G Knepley 
3588552f7358SJed Brown   Level: intermediate
3589552f7358SJed Brown 
3590552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3591552f7358SJed Brown @*/
3592552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3593552f7358SJed Brown {
3594552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3595552f7358SJed Brown   PetscInt      *offsets, **closures;
3596552f7358SJed Brown   PetscInt      *join[2];
3597552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
359824c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3599552f7358SJed Brown   PetscErrorCode ierr;
3600552f7358SJed Brown 
3601552f7358SJed Brown   PetscFunctionBegin;
3602552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
360348bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
360448bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
360548bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3606552f7358SJed Brown 
3607552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
36081795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
360969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
361024c766afSToby Isaac   ms      = mesh->maxSupportSize;
361124c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
361269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
361369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3614552f7358SJed Brown 
3615552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3616552f7358SJed Brown     PetscInt closureSize;
3617552f7358SJed Brown 
3618552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
36190d644c17SKarl Rupp 
3620552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3621552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3622552f7358SJed Brown       PetscInt pStart, pEnd, i;
3623552f7358SJed Brown 
3624552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3625552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3626552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3627552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3628552f7358SJed Brown           break;
3629552f7358SJed Brown         }
3630552f7358SJed Brown       }
3631552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3632552f7358SJed Brown     }
363382f516ccSBarry 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);
3634552f7358SJed Brown   }
3635552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3636552f7358SJed Brown     PetscInt dof;
3637552f7358SJed Brown 
3638552f7358SJed Brown     /* Copy in support of first point */
3639552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3640552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3641552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3642552f7358SJed Brown     }
3643552f7358SJed Brown     /* Check each successive cone */
3644552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3645552f7358SJed Brown       PetscInt newJoinSize = 0;
3646552f7358SJed Brown 
3647552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3648552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3649552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3650552f7358SJed Brown 
3651552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3652552f7358SJed Brown           if (point == join[i][m]) {
3653552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3654552f7358SJed Brown             break;
3655552f7358SJed Brown           }
3656552f7358SJed Brown         }
3657552f7358SJed Brown       }
3658552f7358SJed Brown       joinSize = newJoinSize;
3659552f7358SJed Brown       i        = 1-i;
3660552f7358SJed Brown     }
3661552f7358SJed Brown     if (joinSize) break;
3662552f7358SJed Brown   }
3663552f7358SJed Brown   *numCoveredPoints = joinSize;
3664552f7358SJed Brown   *coveredPoints    = join[i];
3665552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
36660298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3667552f7358SJed Brown   }
3668552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
366969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
367069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3671552f7358SJed Brown   PetscFunctionReturn(0);
3672552f7358SJed Brown }
3673552f7358SJed Brown 
3674552f7358SJed Brown /*@C
3675552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3676552f7358SJed Brown 
3677552f7358SJed Brown   Not Collective
3678552f7358SJed Brown 
3679552f7358SJed Brown   Input Parameters:
3680552f7358SJed Brown + dm - The DMPlex object
3681552f7358SJed Brown . numPoints - The number of input points for the meet
3682552f7358SJed Brown - points - The input points
3683552f7358SJed Brown 
3684552f7358SJed Brown   Output Parameters:
3685552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3686552f7358SJed Brown - coveredPoints - The points in the meet
3687552f7358SJed Brown 
3688552f7358SJed Brown   Level: intermediate
3689552f7358SJed Brown 
3690552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3691552f7358SJed Brown 
36923813dfbdSMatthew G Knepley   Fortran Notes:
36933813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
36943813dfbdSMatthew G Knepley   include petsc.h90 in your code.
36953813dfbdSMatthew G Knepley 
36963813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
36973813dfbdSMatthew G Knepley 
3698552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3699552f7358SJed Brown @*/
3700552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3701552f7358SJed Brown {
3702552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3703552f7358SJed Brown   PetscInt      *meet[2];
3704552f7358SJed Brown   PetscInt       meetSize, i = 0;
3705552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3706552f7358SJed Brown   PetscErrorCode ierr;
3707552f7358SJed Brown 
3708552f7358SJed Brown   PetscFunctionBegin;
3709552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3710064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
3711064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveringPoints, 4);
3712064a246eSJacob Faibussowitsch   PetscValidPointer(coveringPoints, 5);
371369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
371469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3715552f7358SJed Brown   /* Copy in cone of first point */
3716552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3717552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3718552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3719552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3720552f7358SJed Brown   }
3721552f7358SJed Brown   /* Check each successive cone */
3722552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3723552f7358SJed Brown     PetscInt newMeetSize = 0;
3724552f7358SJed Brown 
3725552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3726552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3727552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3728552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3729552f7358SJed Brown 
3730552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3731552f7358SJed Brown         if (point == meet[i][m]) {
3732552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3733552f7358SJed Brown           break;
3734552f7358SJed Brown         }
3735552f7358SJed Brown       }
3736552f7358SJed Brown     }
3737552f7358SJed Brown     meetSize = newMeetSize;
3738552f7358SJed Brown     i        = 1-i;
3739552f7358SJed Brown   }
3740552f7358SJed Brown   *numCoveringPoints = meetSize;
3741552f7358SJed Brown   *coveringPoints    = meet[i];
374269291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3743552f7358SJed Brown   PetscFunctionReturn(0);
3744552f7358SJed Brown }
3745552f7358SJed Brown 
3746552f7358SJed Brown /*@C
3747552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3748552f7358SJed Brown 
3749552f7358SJed Brown   Not Collective
3750552f7358SJed Brown 
3751552f7358SJed Brown   Input Parameters:
3752552f7358SJed Brown + dm - The DMPlex object
3753552f7358SJed Brown . numPoints - The number of input points for the meet
3754552f7358SJed Brown - points - The input points
3755552f7358SJed Brown 
3756552f7358SJed Brown   Output Parameters:
3757552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3758552f7358SJed Brown - coveredPoints - The points in the meet
3759552f7358SJed Brown 
3760552f7358SJed Brown   Level: intermediate
3761552f7358SJed Brown 
37623813dfbdSMatthew G Knepley   Fortran Notes:
37633813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
37643813dfbdSMatthew G Knepley   include petsc.h90 in your code.
37653813dfbdSMatthew G Knepley 
37663813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
37673813dfbdSMatthew G Knepley 
3768552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3769552f7358SJed Brown @*/
3770552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3771552f7358SJed Brown {
3772552f7358SJed Brown   PetscErrorCode ierr;
3773552f7358SJed Brown 
3774552f7358SJed Brown   PetscFunctionBegin;
3775552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3776d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3777d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3778d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
377969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3780d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3781552f7358SJed Brown   PetscFunctionReturn(0);
3782552f7358SJed Brown }
3783552f7358SJed Brown 
3784552f7358SJed Brown /*@C
3785552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3786552f7358SJed Brown 
3787552f7358SJed Brown   Not Collective
3788552f7358SJed Brown 
3789552f7358SJed Brown   Input Parameters:
3790552f7358SJed Brown + dm - The DMPlex object
3791552f7358SJed Brown . numPoints - The number of input points for the meet
3792552f7358SJed Brown - points - The input points
3793552f7358SJed Brown 
3794552f7358SJed Brown   Output Parameters:
3795552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3796552f7358SJed Brown - coveredPoints - The points in the meet
3797552f7358SJed Brown 
3798552f7358SJed Brown   Level: intermediate
3799552f7358SJed Brown 
38003813dfbdSMatthew G Knepley   Fortran Notes:
38013813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
38023813dfbdSMatthew G Knepley   include petsc.h90 in your code.
38033813dfbdSMatthew G Knepley 
38043813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
38053813dfbdSMatthew G Knepley 
3806552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3807552f7358SJed Brown @*/
3808552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3809552f7358SJed Brown {
3810552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3811552f7358SJed Brown   PetscInt      *offsets, **closures;
3812552f7358SJed Brown   PetscInt      *meet[2];
3813552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
381424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3815552f7358SJed Brown   PetscErrorCode ierr;
3816552f7358SJed Brown 
3817552f7358SJed Brown   PetscFunctionBegin;
3818552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3819064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
3820064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveredPoints, 4);
3821064a246eSJacob Faibussowitsch   PetscValidPointer(coveredPoints, 5);
3822552f7358SJed Brown 
3823552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3824785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
382569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
382624c766afSToby Isaac   mc      = mesh->maxConeSize;
382724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
382869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
382969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3830552f7358SJed Brown 
3831552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3832552f7358SJed Brown     PetscInt closureSize;
3833552f7358SJed Brown 
3834552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
38350d644c17SKarl Rupp 
3836552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3837552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3838552f7358SJed Brown       PetscInt pStart, pEnd, i;
3839552f7358SJed Brown 
3840552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3841552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3842552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3843552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3844552f7358SJed Brown           break;
3845552f7358SJed Brown         }
3846552f7358SJed Brown       }
3847552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3848552f7358SJed Brown     }
384982f516ccSBarry 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);
3850552f7358SJed Brown   }
3851552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3852552f7358SJed Brown     PetscInt dof;
3853552f7358SJed Brown 
3854552f7358SJed Brown     /* Copy in cone of first point */
3855552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3856552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3857552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3858552f7358SJed Brown     }
3859552f7358SJed Brown     /* Check each successive cone */
3860552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3861552f7358SJed Brown       PetscInt newMeetSize = 0;
3862552f7358SJed Brown 
3863552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3864552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3865552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3866552f7358SJed Brown 
3867552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3868552f7358SJed Brown           if (point == meet[i][m]) {
3869552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3870552f7358SJed Brown             break;
3871552f7358SJed Brown           }
3872552f7358SJed Brown         }
3873552f7358SJed Brown       }
3874552f7358SJed Brown       meetSize = newMeetSize;
3875552f7358SJed Brown       i        = 1-i;
3876552f7358SJed Brown     }
3877552f7358SJed Brown     if (meetSize) break;
3878552f7358SJed Brown   }
3879552f7358SJed Brown   *numCoveredPoints = meetSize;
3880552f7358SJed Brown   *coveredPoints    = meet[i];
3881552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
38820298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3883552f7358SJed Brown   }
3884552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
388569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
388669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3887552f7358SJed Brown   PetscFunctionReturn(0);
3888552f7358SJed Brown }
3889552f7358SJed Brown 
38904e3744c5SMatthew G. Knepley /*@C
38914e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
38924e3744c5SMatthew G. Knepley 
38934e3744c5SMatthew G. Knepley   Not Collective
38944e3744c5SMatthew G. Knepley 
38954e3744c5SMatthew G. Knepley   Input Parameters:
38964e3744c5SMatthew G. Knepley + dmA - A DMPlex object
38974e3744c5SMatthew G. Knepley - dmB - A DMPlex object
38984e3744c5SMatthew G. Knepley 
38994e3744c5SMatthew G. Knepley   Output Parameters:
39004e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
39014e3744c5SMatthew G. Knepley 
39024e3744c5SMatthew G. Knepley   Level: intermediate
39034e3744c5SMatthew G. Knepley 
39044e3744c5SMatthew G. Knepley   Notes:
39054e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
39064e3744c5SMatthew G. Knepley 
39074e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
39084e3744c5SMatthew G. Knepley @*/
39094e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
39104e3744c5SMatthew G. Knepley {
39114e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
39124e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
39134e3744c5SMatthew G. Knepley 
39144e3744c5SMatthew G. Knepley   PetscFunctionBegin;
39154e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
39164e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
39174e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
39184e3744c5SMatthew G. Knepley 
39194e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
39204e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
39214e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
39224e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
39234e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
39244e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
39254e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
39264e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
39274e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
39284e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
39294e3744c5SMatthew G. Knepley 
39304e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
39314e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
39324e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
39334e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
39344e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
39354e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
39364e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
39374e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
39384e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
39394e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
39404e3744c5SMatthew G. Knepley     }
39414e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
39424e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
39434e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
39444e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
39454e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
39464e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
39474e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
39484e3744c5SMatthew G. Knepley     }
39494e3744c5SMatthew G. Knepley   }
39504e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
39514e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
39524e3744c5SMatthew G. Knepley }
39534e3744c5SMatthew G. Knepley 
39547cd05799SMatthew G. Knepley /*@C
39557cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
39567cd05799SMatthew G. Knepley 
39577cd05799SMatthew G. Knepley   Not Collective
39587cd05799SMatthew G. Knepley 
39597cd05799SMatthew G. Knepley   Input Parameters:
39607cd05799SMatthew G. Knepley + dm         - The DMPlex
39617cd05799SMatthew G. Knepley . cellDim    - The cell dimension
39627cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
39637cd05799SMatthew G. Knepley 
39647cd05799SMatthew G. Knepley   Output Parameters:
39657cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
39667cd05799SMatthew G. Knepley 
39677cd05799SMatthew G. Knepley   Level: developer
39687cd05799SMatthew G. Knepley 
39697cd05799SMatthew G. Knepley   Notes:
39707cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
39717cd05799SMatthew G. Knepley 
39727cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
39737cd05799SMatthew G. Knepley @*/
397418ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3975a6dfd86eSKarl Rupp {
397682f516ccSBarry Smith   MPI_Comm       comm;
3977552f7358SJed Brown   PetscErrorCode ierr;
3978552f7358SJed Brown 
3979552f7358SJed Brown   PetscFunctionBegin;
398082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3981064a246eSJacob Faibussowitsch   PetscValidPointer(numFaceVertices,4);
3982552f7358SJed Brown   switch (cellDim) {
3983552f7358SJed Brown   case 0:
3984552f7358SJed Brown     *numFaceVertices = 0;
3985552f7358SJed Brown     break;
3986552f7358SJed Brown   case 1:
3987552f7358SJed Brown     *numFaceVertices = 1;
3988552f7358SJed Brown     break;
3989552f7358SJed Brown   case 2:
3990552f7358SJed Brown     switch (numCorners) {
399119436ca2SJed Brown     case 3: /* triangle */
399219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3993552f7358SJed Brown       break;
399419436ca2SJed Brown     case 4: /* quadrilateral */
399519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3996552f7358SJed Brown       break;
399719436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
399819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3999552f7358SJed Brown       break;
400019436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
400119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
4002552f7358SJed Brown       break;
4003552f7358SJed Brown     default:
4004f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
4005552f7358SJed Brown     }
4006552f7358SJed Brown     break;
4007552f7358SJed Brown   case 3:
4008552f7358SJed Brown     switch (numCorners) {
400919436ca2SJed Brown     case 4: /* tetradehdron */
401019436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
4011552f7358SJed Brown       break;
401219436ca2SJed Brown     case 6: /* tet cohesive cells */
401319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
4014552f7358SJed Brown       break;
401519436ca2SJed Brown     case 8: /* hexahedron */
401619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
4017552f7358SJed Brown       break;
401819436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
401919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4020552f7358SJed Brown       break;
402119436ca2SJed Brown     case 10: /* quadratic tetrahedron */
402219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4023552f7358SJed Brown       break;
402419436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
402519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4026552f7358SJed Brown       break;
402719436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
402819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4029552f7358SJed Brown       break;
403019436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
403119436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
4032552f7358SJed Brown       break;
4033552f7358SJed Brown     default:
4034f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
4035552f7358SJed Brown     }
4036552f7358SJed Brown     break;
4037552f7358SJed Brown   default:
4038f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
4039552f7358SJed Brown   }
4040552f7358SJed Brown   PetscFunctionReturn(0);
4041552f7358SJed Brown }
4042552f7358SJed Brown 
4043552f7358SJed Brown /*@
4044aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
4045552f7358SJed Brown 
4046552f7358SJed Brown   Not Collective
4047552f7358SJed Brown 
4048aa50250dSMatthew G. Knepley   Input Parameter:
4049552f7358SJed Brown . dm    - The DMPlex object
4050552f7358SJed Brown 
4051aa50250dSMatthew G. Knepley   Output Parameter:
4052aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
4053552f7358SJed Brown 
4054552f7358SJed Brown   Level: developer
4055552f7358SJed Brown 
4056dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
4057aa50250dSMatthew G. Knepley @*/
4058aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4059aa50250dSMatthew G. Knepley {
4060aa50250dSMatthew G. Knepley   PetscFunctionBegin;
4061aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4062aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
4063c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
4064aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
4065aa50250dSMatthew G. Knepley }
4066aa50250dSMatthew G. Knepley 
4067aa50250dSMatthew G. Knepley /*@
4068aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
4069aa50250dSMatthew G. Knepley 
4070aa50250dSMatthew G. Knepley   Not Collective
4071aa50250dSMatthew G. Knepley 
4072aa50250dSMatthew G. Knepley   Input Parameter:
4073aa50250dSMatthew G. Knepley . dm    - The DMPlex object
4074aa50250dSMatthew G. Knepley 
4075aa50250dSMatthew G. Knepley   Output Parameter:
4076aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
4077aa50250dSMatthew G. Knepley 
4078aa50250dSMatthew G. Knepley   Level: developer
4079552f7358SJed Brown 
4080b1bb481bSMatthew Knepley   Notes:
4081b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
4082dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
4083dc287ab2SVaclav Hapla   An empty mesh gives -1.
4084b1bb481bSMatthew Knepley 
4085dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
4086552f7358SJed Brown @*/
4087552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4088552f7358SJed Brown {
4089aa50250dSMatthew G. Knepley   DMLabel        label;
4090aa50250dSMatthew G. Knepley   PetscInt       d = 0;
4091552f7358SJed Brown   PetscErrorCode ierr;
4092552f7358SJed Brown 
4093552f7358SJed Brown   PetscFunctionBegin;
4094552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4095552f7358SJed Brown   PetscValidPointer(depth, 2);
4096aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4097aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
4098552f7358SJed Brown   *depth = d-1;
4099552f7358SJed Brown   PetscFunctionReturn(0);
4100552f7358SJed Brown }
4101552f7358SJed Brown 
4102552f7358SJed Brown /*@
4103552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
4104552f7358SJed Brown 
4105552f7358SJed Brown   Not Collective
4106552f7358SJed Brown 
4107552f7358SJed Brown   Input Parameters:
4108552f7358SJed Brown + dm           - The DMPlex object
4109552f7358SJed Brown - stratumValue - The requested depth
4110552f7358SJed Brown 
4111552f7358SJed Brown   Output Parameters:
4112552f7358SJed Brown + start - The first point at this depth
4113552f7358SJed Brown - end   - One beyond the last point at this depth
4114552f7358SJed Brown 
4115647867b2SJed Brown   Notes:
4116647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
4117647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
4118647867b2SJed Brown   higher dimension, e.g., "edges".
4119647867b2SJed Brown 
4120552f7358SJed Brown   Level: developer
4121552f7358SJed Brown 
4122dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
4123552f7358SJed Brown @*/
41240adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
41250adebc6cSBarry Smith {
4126aa50250dSMatthew G. Knepley   DMLabel        label;
412763d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
4128552f7358SJed Brown   PetscErrorCode ierr;
4129552f7358SJed Brown 
4130552f7358SJed Brown   PetscFunctionBegin;
4131552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413263d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
413363d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4134552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
41350d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
413663d1a920SMatthew G. Knepley   if (stratumValue < 0) {
413763d1a920SMatthew G. Knepley     if (start) *start = pStart;
413863d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
413963d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4140552f7358SJed Brown   }
4141aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
414263d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
414363d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
4144552f7358SJed Brown   PetscFunctionReturn(0);
4145552f7358SJed Brown }
4146552f7358SJed Brown 
4147552f7358SJed Brown /*@
4148552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
4149552f7358SJed Brown 
4150552f7358SJed Brown   Not Collective
4151552f7358SJed Brown 
4152552f7358SJed Brown   Input Parameters:
4153552f7358SJed Brown + dm           - The DMPlex object
4154552f7358SJed Brown - stratumValue - The requested height
4155552f7358SJed Brown 
4156552f7358SJed Brown   Output Parameters:
4157552f7358SJed Brown + start - The first point at this height
4158552f7358SJed Brown - end   - One beyond the last point at this height
4159552f7358SJed Brown 
4160647867b2SJed Brown   Notes:
4161647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
4162647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
4163647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
4164647867b2SJed Brown 
4165552f7358SJed Brown   Level: developer
4166552f7358SJed Brown 
41673dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
4168552f7358SJed Brown @*/
41690adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
41700adebc6cSBarry Smith {
4171aa50250dSMatthew G. Knepley   DMLabel        label;
417263d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
4173552f7358SJed Brown   PetscErrorCode ierr;
4174552f7358SJed Brown 
4175552f7358SJed Brown   PetscFunctionBegin;
4176552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
417763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
417863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4179552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
41800d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
418163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
418263d1a920SMatthew G. Knepley     if (start) *start = pStart;
418363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
418463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4185552f7358SJed Brown   }
4186aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
418713903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
418863d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
418963d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
4190552f7358SJed Brown   PetscFunctionReturn(0);
4191552f7358SJed Brown }
4192552f7358SJed Brown 
4193ba2698f1SMatthew G. Knepley /*@
4194ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
4195ba2698f1SMatthew G. Knepley 
4196ba2698f1SMatthew G. Knepley   Not Collective
4197ba2698f1SMatthew G. Knepley 
4198ba2698f1SMatthew G. Knepley   Input Parameter:
4199ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
4200ba2698f1SMatthew G. Knepley - point - The point
4201ba2698f1SMatthew G. Knepley 
4202ba2698f1SMatthew G. Knepley   Output Parameter:
4203ba2698f1SMatthew G. Knepley . depth - The depth of the point
4204ba2698f1SMatthew G. Knepley 
4205ba2698f1SMatthew G. Knepley   Level: intermediate
4206ba2698f1SMatthew G. Knepley 
42073dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
4208ba2698f1SMatthew G. Knepley @*/
4209ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4210ba2698f1SMatthew G. Knepley {
4211ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4212ba2698f1SMatthew G. Knepley 
4213ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4214ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
421540a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
4216ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
4217ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4218ba2698f1SMatthew G. Knepley }
4219ba2698f1SMatthew G. Knepley 
4220ba2698f1SMatthew G. Knepley /*@
42210c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
42220c0a32dcSVaclav Hapla 
42230c0a32dcSVaclav Hapla   Not Collective
42240c0a32dcSVaclav Hapla 
42250c0a32dcSVaclav Hapla   Input Parameter:
42260c0a32dcSVaclav Hapla + dm    - The DMPlex object
42270c0a32dcSVaclav Hapla - point - The point
42280c0a32dcSVaclav Hapla 
42290c0a32dcSVaclav Hapla   Output Parameter:
42300c0a32dcSVaclav Hapla . height - The height of the point
42310c0a32dcSVaclav Hapla 
42320c0a32dcSVaclav Hapla   Level: intermediate
42330c0a32dcSVaclav Hapla 
42343dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
42350c0a32dcSVaclav Hapla @*/
42360c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
42370c0a32dcSVaclav Hapla {
42380c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
42390c0a32dcSVaclav Hapla   PetscErrorCode ierr;
42400c0a32dcSVaclav Hapla 
42410c0a32dcSVaclav Hapla   PetscFunctionBegin;
42420c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42430c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
42440c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
42450c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
42460c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
42470c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
42480c0a32dcSVaclav Hapla }
42490c0a32dcSVaclav Hapla 
42500c0a32dcSVaclav Hapla /*@
4251ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
4252ba2698f1SMatthew G. Knepley 
4253ba2698f1SMatthew G. Knepley   Not Collective
4254ba2698f1SMatthew G. Knepley 
4255ba2698f1SMatthew G. Knepley   Input Parameter:
4256ba2698f1SMatthew G. Knepley . dm - The DMPlex object
4257ba2698f1SMatthew G. Knepley 
4258ba2698f1SMatthew G. Knepley   Output Parameter:
4259ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
4260ba2698f1SMatthew G. Knepley 
4261412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
4262412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
4263412e9a14SMatthew G. Knepley 
4264ba2698f1SMatthew G. Knepley   Level: developer
4265ba2698f1SMatthew G. Knepley 
4266dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
4267ba2698f1SMatthew G. Knepley @*/
4268ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4269ba2698f1SMatthew G. Knepley {
4270ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4271ba2698f1SMatthew G. Knepley 
4272ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4273ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4274ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
4275ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
4276ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
4277ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4278ba2698f1SMatthew G. Knepley }
4279ba2698f1SMatthew G. Knepley 
4280ba2698f1SMatthew G. Knepley /*@
4281ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
4282ba2698f1SMatthew G. Knepley 
4283ba2698f1SMatthew G. Knepley   Not Collective
4284ba2698f1SMatthew G. Knepley 
4285ba2698f1SMatthew G. Knepley   Input Parameter:
4286ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
4287ba2698f1SMatthew G. Knepley - cell - The cell
4288ba2698f1SMatthew G. Knepley 
4289ba2698f1SMatthew G. Knepley   Output Parameter:
4290ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
4291ba2698f1SMatthew G. Knepley 
4292ba2698f1SMatthew G. Knepley   Level: intermediate
4293ba2698f1SMatthew G. Knepley 
4294ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
4295ba2698f1SMatthew G. Knepley @*/
4296ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4297ba2698f1SMatthew G. Knepley {
4298ba2698f1SMatthew G. Knepley   DMLabel        label;
4299ba2698f1SMatthew G. Knepley   PetscInt       ct;
4300ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4301ba2698f1SMatthew G. Knepley 
4302ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4303ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4304ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
4305ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4306ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
43074a7ee7d0SMatthew G. Knepley   if (ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %D has not been assigned a cell type", cell);
4308ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
4309ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4310ba2698f1SMatthew G. Knepley }
4311ba2698f1SMatthew G. Knepley 
4312412e9a14SMatthew G. Knepley /*@
4313412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
4314412e9a14SMatthew G. Knepley 
4315412e9a14SMatthew G. Knepley   Not Collective
4316412e9a14SMatthew G. Knepley 
4317412e9a14SMatthew G. Knepley   Input Parameters:
4318412e9a14SMatthew G. Knepley + dm   - The DMPlex object
4319412e9a14SMatthew G. Knepley . cell - The cell
4320412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
4321412e9a14SMatthew G. Knepley 
4322412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
4323412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
4324412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
4325412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
4326412e9a14SMatthew G. Knepley 
4327412e9a14SMatthew G. Knepley   Level: advanced
4328412e9a14SMatthew G. Knepley 
4329412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
4330412e9a14SMatthew G. Knepley @*/
4331412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4332412e9a14SMatthew G. Knepley {
4333412e9a14SMatthew G. Knepley   DMLabel        label;
4334412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
4335412e9a14SMatthew G. Knepley 
4336412e9a14SMatthew G. Knepley   PetscFunctionBegin;
4337412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4338412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4339412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
4340412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
4341412e9a14SMatthew G. Knepley }
4342412e9a14SMatthew G. Knepley 
43430adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
43440adebc6cSBarry Smith {
4345efe440bfSMatthew G. Knepley   PetscSection   section, s;
4346efe440bfSMatthew G. Knepley   Mat            m;
43473e922f36SToby Isaac   PetscInt       maxHeight;
4348552f7358SJed Brown   PetscErrorCode ierr;
4349552f7358SJed Brown 
4350552f7358SJed Brown   PetscFunctionBegin;
435138221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
43523e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
43533e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
435482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
435592fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
43561d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4357efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
4358efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
4359efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
4360efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
4361efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
43628f4c458bSMatthew G. Knepley 
43638f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
43648f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
4365552f7358SJed Brown   PetscFunctionReturn(0);
4366552f7358SJed Brown }
4367552f7358SJed Brown 
4368f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4369f19dbd58SToby Isaac {
4370f19dbd58SToby Isaac   Vec            coordsLocal;
4371f19dbd58SToby Isaac   DM             coordsDM;
4372f19dbd58SToby Isaac   PetscErrorCode ierr;
4373f19dbd58SToby Isaac 
4374f19dbd58SToby Isaac   PetscFunctionBegin;
4375f19dbd58SToby Isaac   *field = NULL;
4376f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
4377f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
4378f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
4379f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
4380f19dbd58SToby Isaac   }
4381f19dbd58SToby Isaac   PetscFunctionReturn(0);
4382f19dbd58SToby Isaac }
4383f19dbd58SToby Isaac 
43847cd05799SMatthew G. Knepley /*@C
43857cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
43867cd05799SMatthew G. Knepley 
43877cd05799SMatthew G. Knepley   Not Collective
43887cd05799SMatthew G. Knepley 
43897cd05799SMatthew G. Knepley   Input Parameters:
43907cd05799SMatthew G. Knepley . dm        - The DMPlex object
43917cd05799SMatthew G. Knepley 
43927cd05799SMatthew G. Knepley   Output Parameter:
43937cd05799SMatthew G. Knepley . section - The PetscSection object
43947cd05799SMatthew G. Knepley 
43957cd05799SMatthew G. Knepley   Level: developer
43967cd05799SMatthew G. Knepley 
43977cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
43987cd05799SMatthew G. Knepley @*/
43990adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
44000adebc6cSBarry Smith {
4401552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4402552f7358SJed Brown 
4403552f7358SJed Brown   PetscFunctionBegin;
4404552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4405552f7358SJed Brown   if (section) *section = mesh->coneSection;
4406552f7358SJed Brown   PetscFunctionReturn(0);
4407552f7358SJed Brown }
4408552f7358SJed Brown 
44097cd05799SMatthew G. Knepley /*@C
44107cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
44117cd05799SMatthew G. Knepley 
44127cd05799SMatthew G. Knepley   Not Collective
44137cd05799SMatthew G. Knepley 
44147cd05799SMatthew G. Knepley   Input Parameters:
44157cd05799SMatthew G. Knepley . dm        - The DMPlex object
44167cd05799SMatthew G. Knepley 
44177cd05799SMatthew G. Knepley   Output Parameter:
44187cd05799SMatthew G. Knepley . section - The PetscSection object
44197cd05799SMatthew G. Knepley 
44207cd05799SMatthew G. Knepley   Level: developer
44217cd05799SMatthew G. Knepley 
44227cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
44237cd05799SMatthew G. Knepley @*/
44248cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
44258cb4d582SMatthew G. Knepley {
44268cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
44278cb4d582SMatthew G. Knepley 
44288cb4d582SMatthew G. Knepley   PetscFunctionBegin;
44298cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44308cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
44318cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
44328cb4d582SMatthew G. Knepley }
44338cb4d582SMatthew G. Knepley 
44347cd05799SMatthew G. Knepley /*@C
44357cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
44367cd05799SMatthew G. Knepley 
44377cd05799SMatthew G. Knepley   Not Collective
44387cd05799SMatthew G. Knepley 
44397cd05799SMatthew G. Knepley   Input Parameters:
44407cd05799SMatthew G. Knepley . dm        - The DMPlex object
44417cd05799SMatthew G. Knepley 
44427cd05799SMatthew G. Knepley   Output Parameter:
44437cd05799SMatthew G. Knepley . cones - The cone for each point
44447cd05799SMatthew G. Knepley 
44457cd05799SMatthew G. Knepley   Level: developer
44467cd05799SMatthew G. Knepley 
44477cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
44487cd05799SMatthew G. Knepley @*/
4449a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4450a6dfd86eSKarl Rupp {
4451552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4452552f7358SJed Brown 
4453552f7358SJed Brown   PetscFunctionBegin;
4454552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4455552f7358SJed Brown   if (cones) *cones = mesh->cones;
4456552f7358SJed Brown   PetscFunctionReturn(0);
4457552f7358SJed Brown }
4458552f7358SJed Brown 
44597cd05799SMatthew G. Knepley /*@C
44607cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
44617cd05799SMatthew G. Knepley 
44627cd05799SMatthew G. Knepley   Not Collective
44637cd05799SMatthew G. Knepley 
44647cd05799SMatthew G. Knepley   Input Parameters:
44657cd05799SMatthew G. Knepley . dm        - The DMPlex object
44667cd05799SMatthew G. Knepley 
44677cd05799SMatthew G. Knepley   Output Parameter:
44687cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
44697cd05799SMatthew G. Knepley 
44707cd05799SMatthew G. Knepley   Level: developer
44717cd05799SMatthew G. Knepley 
44727cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
44737cd05799SMatthew G. Knepley @*/
4474a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4475a6dfd86eSKarl Rupp {
4476552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4477552f7358SJed Brown 
4478552f7358SJed Brown   PetscFunctionBegin;
4479552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4480552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4481552f7358SJed Brown   PetscFunctionReturn(0);
4482552f7358SJed Brown }
4483552f7358SJed Brown 
4484552f7358SJed Brown /******************************** FEM Support **********************************/
4485552f7358SJed Brown 
44869e8305c2SJed Brown /*
44879e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
44889e8305c2SJed Brown  representing a line in the section.
44899e8305c2SJed Brown */
44909e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
44919e8305c2SJed Brown {
44929e8305c2SJed Brown   PetscErrorCode ierr;
44939e8305c2SJed Brown 
44949e8305c2SJed Brown   PetscFunctionBeginHot;
44959e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4496a433471fSStefano Zampini   if (line < 0) {
4497a433471fSStefano Zampini     *k = 0;
4498a433471fSStefano Zampini     *Nc = 0;
4499a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
45009e8305c2SJed Brown     *k = 1;
45019e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
45029e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
45039e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
45049e8305c2SJed Brown     *k = *k / *Nc + 1;
45059e8305c2SJed Brown   }
45069e8305c2SJed Brown   PetscFunctionReturn(0);
45079e8305c2SJed Brown }
45089e8305c2SJed Brown 
4509a4355906SMatthew Knepley /*@
4510bc1eb3faSJed Brown 
4511bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4512bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
45131bb6d2a8SBarry Smith   section provided (or the section of the DM).
4514a4355906SMatthew Knepley 
4515a4355906SMatthew Knepley   Input Parameters:
4516a4355906SMatthew Knepley + dm      - The DM
4517a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4518a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4519a4355906SMatthew Knepley 
4520a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4521a4355906SMatthew Knepley   degree of the basis.
4522a4355906SMatthew Knepley 
4523bc1eb3faSJed Brown   Example:
4524bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4525bc1eb3faSJed Brown .vb
4526bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4527bc1eb3faSJed Brown 
4528bc1eb3faSJed Brown   v4 -- e6 -- v3
4529bc1eb3faSJed Brown   |           |
4530bc1eb3faSJed Brown   e7    c0    e8
4531bc1eb3faSJed Brown   |           |
4532bc1eb3faSJed Brown   v1 -- e5 -- v2
4533bc1eb3faSJed Brown .ve
4534bc1eb3faSJed Brown 
4535bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4536bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4537bc1eb3faSJed Brown .vb
4538bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4539bc1eb3faSJed Brown     v1 -> [4]
4540bc1eb3faSJed Brown     ...
4541bc1eb3faSJed Brown     e5 -> [8, 9]
4542bc1eb3faSJed Brown .ve
4543bc1eb3faSJed Brown 
4544bc1eb3faSJed Brown   which corresponds to the dofs
4545bc1eb3faSJed Brown .vb
4546bc1eb3faSJed Brown     6   10  11  7
4547bc1eb3faSJed Brown     13  2   3   15
4548bc1eb3faSJed Brown     12  0   1   14
4549bc1eb3faSJed Brown     4   8   9   5
4550bc1eb3faSJed Brown .ve
4551bc1eb3faSJed Brown 
4552bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4553bc1eb3faSJed Brown .vb
4554bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4555bc1eb3faSJed Brown .ve
4556bc1eb3faSJed Brown 
4557bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4558bc1eb3faSJed Brown .vb
4559bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4560bc1eb3faSJed Brown .ve
4561bc1eb3faSJed Brown 
4562a4355906SMatthew Knepley   Level: developer
4563a4355906SMatthew Knepley 
45649df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4565a4355906SMatthew Knepley @*/
4566bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
45673194fc30SMatthew G. Knepley {
45687391a63aSMatthew G. Knepley   DMLabel        label;
4569bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
45709e8305c2SJed Brown   PetscBool      vertexchart;
45713194fc30SMatthew G. Knepley   PetscErrorCode ierr;
45723194fc30SMatthew G. Knepley 
45733194fc30SMatthew G. Knepley   PetscFunctionBegin;
45743194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4575a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4576a433471fSStefano Zampini   if (point < 0) {
4577a433471fSStefano Zampini     PetscInt sStart,sEnd;
4578a433471fSStefano Zampini 
4579a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4580a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4581a433471fSStefano Zampini   }
45827391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4583a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4584a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
45857391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
45867391a63aSMatthew G. Knepley   else if  (depth == dim) {
45877391a63aSMatthew G. Knepley     const PetscInt *cone;
45887391a63aSMatthew G. Knepley 
45897391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4590d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4591d4e6627bSStefano Zampini     else if (dim == 3) {
4592d4e6627bSStefano Zampini       const PetscInt *cone2;
4593d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4594d4e6627bSStefano Zampini       eStart = cone2[0];
4595d4e6627bSStefano 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);
4596a433471fSStefano 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);
45979e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
45989e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
45999e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
46009e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
46019e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
46029e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
46039e8305c2SJed Brown   }
46043194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4605bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
4606bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4607bb197d40SJed Brown     PetscInt *perm;
4608bb197d40SJed Brown 
46093194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
46109e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4611bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
46123194fc30SMatthew G. Knepley     }
46133194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
46143194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4615bb197d40SJed Brown       switch (d) {
4616babf31e0SJed Brown       case 1:
46179e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4618babf31e0SJed Brown         /*
4619babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4620babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
4621babf31e0SJed Brown          */
4622babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4623babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4624babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4625babf31e0SJed Brown         foffset = offset;
4626babf31e0SJed Brown         break;
462789eabcffSMatthew G. Knepley       case 2:
46283194fc30SMatthew 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} */
46299e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
46303194fc30SMatthew G. Knepley         /* The SEM order is
46313194fc30SMatthew G. Knepley 
46323194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
463389eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
46343194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
46353194fc30SMatthew G. Knepley          */
46363194fc30SMatthew G. Knepley         {
46373194fc30SMatthew G. Knepley           const PetscInt of   = 0;
46383194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
46393194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
46403194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
46413194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
46423194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
46433194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
46443194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
46453194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
46463194fc30SMatthew G. Knepley           PetscInt       o;
46473194fc30SMatthew G. Knepley 
46483194fc30SMatthew G. Knepley           /* bottom */
46493194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
46503194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
46513194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
46523194fc30SMatthew G. Knepley           /* middle */
46533194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
46543194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
46553194fc30SMatthew 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;
46563194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
46573194fc30SMatthew G. Knepley           }
46583194fc30SMatthew G. Knepley           /* top */
46593194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
46603194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
46613194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
46623194fc30SMatthew G. Knepley           foffset = offset;
46633194fc30SMatthew G. Knepley         }
466489eabcffSMatthew G. Knepley         break;
466589eabcffSMatthew G. Knepley       case 3:
466689eabcffSMatthew G. Knepley         /* The original hex closure is
466789eabcffSMatthew G. Knepley 
466889eabcffSMatthew G. Knepley          {c,
466989eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
467089eabcffSMatthew 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,
467189eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
467289eabcffSMatthew G. Knepley          */
46739e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
467489eabcffSMatthew G. Knepley         /* The SEM order is
467589eabcffSMatthew G. Knepley          Bottom Slice
467689eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
467789eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
467889eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
467989eabcffSMatthew G. Knepley 
468089eabcffSMatthew G. Knepley          Middle Slice (j)
468189eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
468289eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
468389eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
468489eabcffSMatthew G. Knepley 
468589eabcffSMatthew G. Knepley          Top Slice
468689eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
468789eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
468889eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
468989eabcffSMatthew G. Knepley          */
469089eabcffSMatthew G. Knepley         {
469189eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
469289eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
469389eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
469489eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
469589eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
469689eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
469789eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
469889eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
469989eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
470089eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
470189eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
470289eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
470389eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
470489eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
470589eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
470689eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
470789eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
470889eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
470989eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
471089eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
471189eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
471289eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
471389eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
471489eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
471589eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
471689eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
471789eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
471889eabcffSMatthew G. Knepley           PetscInt       o, n;
471989eabcffSMatthew G. Knepley 
472089eabcffSMatthew G. Knepley           /* Bottom Slice */
472189eabcffSMatthew G. Knepley           /*   bottom */
472289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
472389eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
472489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
472589eabcffSMatthew G. Knepley           /*   middle */
472689eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
472789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4728316b7f87SMax 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;}
472989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
47303194fc30SMatthew G. Knepley           }
473189eabcffSMatthew G. Knepley           /*   top */
473289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
473389eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
473489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
473589eabcffSMatthew G. Knepley 
473689eabcffSMatthew G. Knepley           /* Middle Slice */
473789eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
473889eabcffSMatthew G. Knepley             /*   bottom */
473989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
474089eabcffSMatthew 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;
474189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
474289eabcffSMatthew G. Knepley             /*   middle */
474389eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
474489eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
474589eabcffSMatthew 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;
474689eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
474789eabcffSMatthew G. Knepley             }
474889eabcffSMatthew G. Knepley             /*   top */
474989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
475089eabcffSMatthew 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;
475189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
475289eabcffSMatthew G. Knepley           }
475389eabcffSMatthew G. Knepley 
475489eabcffSMatthew G. Knepley           /* Top Slice */
475589eabcffSMatthew G. Knepley           /*   bottom */
475689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
475789eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
475889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
475989eabcffSMatthew G. Knepley           /*   middle */
476089eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
476189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
476289eabcffSMatthew 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;
476389eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
476489eabcffSMatthew G. Knepley           }
476589eabcffSMatthew G. Knepley           /*   top */
476689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
476789eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
476889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
476989eabcffSMatthew G. Knepley 
477089eabcffSMatthew G. Knepley           foffset = offset;
477189eabcffSMatthew G. Knepley         }
477289eabcffSMatthew G. Knepley         break;
4773bb197d40SJed Brown       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
477489eabcffSMatthew G. Knepley       }
477589eabcffSMatthew G. Knepley     }
477689eabcffSMatthew G. Knepley     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
47773194fc30SMatthew G. Knepley     /* Check permutation */
47783194fc30SMatthew G. Knepley     {
47793194fc30SMatthew G. Knepley       PetscInt *check;
47803194fc30SMatthew G. Knepley 
47813194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
47823194fc30SMatthew 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]);}
47833194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
47843194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
47853194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
47863194fc30SMatthew G. Knepley     }
4787bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
4788bb197d40SJed Brown   }
47893194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
47903194fc30SMatthew G. Knepley }
47913194fc30SMatthew G. Knepley 
4792e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4793e071409bSToby Isaac {
4794e071409bSToby Isaac   PetscDS        prob;
4795e071409bSToby Isaac   PetscInt       depth, Nf, h;
4796e071409bSToby Isaac   DMLabel        label;
4797e071409bSToby Isaac   PetscErrorCode ierr;
4798e071409bSToby Isaac 
4799e071409bSToby Isaac   PetscFunctionBeginHot;
4800e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4801e071409bSToby Isaac   Nf      = prob->Nf;
4802e071409bSToby Isaac   label   = dm->depthLabel;
4803e071409bSToby Isaac   *dspace = NULL;
4804e071409bSToby Isaac   if (field < Nf) {
4805e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4806e071409bSToby Isaac 
4807e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4808e071409bSToby Isaac       PetscDualSpace dsp;
4809e071409bSToby Isaac 
4810e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4811e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4812e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4813e071409bSToby Isaac       h    = depth - 1 - h;
4814e071409bSToby Isaac       if (h) {
4815e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4816e071409bSToby Isaac       } else {
4817e071409bSToby Isaac         *dspace = dsp;
4818e071409bSToby Isaac       }
4819e071409bSToby Isaac     }
4820e071409bSToby Isaac   }
4821e071409bSToby Isaac   PetscFunctionReturn(0);
4822e071409bSToby Isaac }
4823e071409bSToby Isaac 
48241a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4825a6dfd86eSKarl Rupp {
4826552f7358SJed Brown   PetscScalar    *array, *vArray;
4827d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
48281a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4829552f7358SJed Brown   PetscErrorCode  ierr;
4830552f7358SJed Brown 
48311b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
48322a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
48335a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
48345a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
48355a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
48363f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
48379df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
48389df71ca4SMatthew G. Knepley       PetscInt dof;
4839d9917b9dSMatthew G. Knepley 
48409df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
48419df71ca4SMatthew G. Knepley       size += dof;
48429df71ca4SMatthew G. Knepley     }
48439df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
48449df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
48452a3aaacfSMatthew G. Knepley       PetscInt       dof;
48465a1bb5cfSMatthew G. Knepley 
48475a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
48482a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
48495a1bb5cfSMatthew G. Knepley       size += dof;
48505a1bb5cfSMatthew G. Knepley     }
48513f7cbbe7SMatthew G. Knepley     if (!values) {
48523f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
48533f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
48543f7cbbe7SMatthew G. Knepley     }
485569291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4856982e9ed1SMatthew G. Knepley   } else {
4857982e9ed1SMatthew G. Knepley     array = *values;
4858982e9ed1SMatthew G. Knepley   }
48599df71ca4SMatthew G. Knepley   size = 0;
48605a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
48619df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
48629df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
48639df71ca4SMatthew G. Knepley     PetscScalar *varr;
4864d9917b9dSMatthew G. Knepley 
48659df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
48669df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
48679df71ca4SMatthew G. Knepley     varr = &vArray[off];
48681a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
48691a271a75SMatthew G. Knepley       array[offset] = varr[d];
48709df71ca4SMatthew G. Knepley     }
48719df71ca4SMatthew G. Knepley     size += dof;
48729df71ca4SMatthew G. Knepley   }
48739df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
48749df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
48759df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
48765a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
48775a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
48785a1bb5cfSMatthew G. Knepley 
487952ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
48805a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
48815a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
48825a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
48835a1bb5cfSMatthew G. Knepley     if (o >= 0) {
48841a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
48851a271a75SMatthew G. Knepley         array[offset] = varr[d];
48865a1bb5cfSMatthew G. Knepley       }
48875a1bb5cfSMatthew G. Knepley     } else {
48881a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
48891a271a75SMatthew G. Knepley         array[offset] = varr[d];
48905a1bb5cfSMatthew G. Knepley       }
48915a1bb5cfSMatthew G. Knepley     }
48929df71ca4SMatthew G. Knepley     size += dof;
48935a1bb5cfSMatthew G. Knepley   }
48945a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
48959df71ca4SMatthew G. Knepley   if (!*values) {
48965a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
48975a1bb5cfSMatthew G. Knepley     *values = array;
48989df71ca4SMatthew G. Knepley   } else {
48998ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
49008c312ff3SMatthew G. Knepley     *csize = size;
49019df71ca4SMatthew G. Knepley   }
49025a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
49035a1bb5cfSMatthew G. Knepley }
4904d9917b9dSMatthew G. Knepley 
490527f02ce8SMatthew G. Knepley /* Compress out points not in the section */
490627f02ce8SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
490727f02ce8SMatthew G. Knepley {
490827f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
490927f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
491027f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
491127f02ce8SMatthew G. Knepley 
491227f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
491327f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
491427f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
491527f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
491627f02ce8SMatthew G. Knepley       points[q*2]   = r;
491727f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
491827f02ce8SMatthew G. Knepley       ++q;
491927f02ce8SMatthew G. Knepley     }
492027f02ce8SMatthew G. Knepley   }
492127f02ce8SMatthew G. Knepley   *numPoints = q;
492227f02ce8SMatthew G. Knepley   return 0;
492327f02ce8SMatthew G. Knepley }
492427f02ce8SMatthew G. Knepley 
492527f02ce8SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
492627f02ce8SMatthew G. Knepley {
492727f02ce8SMatthew G. Knepley   const PetscInt *cone, *ornt;
492827f02ce8SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
492927f02ce8SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
493027f02ce8SMatthew G. Knepley   PetscErrorCode  ierr;
493127f02ce8SMatthew G. Knepley 
493227f02ce8SMatthew G. Knepley   PetscFunctionBeginHot;
493327f02ce8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
493427f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
493527f02ce8SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
493627f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
493727f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
493827f02ce8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, np*2, MPIU_INT, &pts);CHKERRQ(ierr);
493927f02ce8SMatthew G. Knepley   c    = 0;
494027f02ce8SMatthew G. Knepley   pts[c*2+0] = point;
494127f02ce8SMatthew G. Knepley   pts[c*2+1] = 0;
494227f02ce8SMatthew G. Knepley   ++c;
494327f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
494427f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
494527f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
494627f02ce8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
494727f02ce8SMatthew G. Knepley   if (dim >= 2) {
494827f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
494927f02ce8SMatthew G. Knepley   }
495027f02ce8SMatthew G. Knepley   if (dim >= 3) {
495127f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
495227f02ce8SMatthew G. Knepley       const PetscInt  fpoint = cone[d];
495327f02ce8SMatthew G. Knepley       const PetscInt *fcone;
495427f02ce8SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
495527f02ce8SMatthew G. Knepley 
495627f02ce8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
495727f02ce8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
495827f02ce8SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
495927f02ce8SMatthew G. Knepley         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
496027f02ce8SMatthew G. Knepley         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
496127f02ce8SMatthew G. Knepley       }
496227f02ce8SMatthew G. Knepley     }
496327f02ce8SMatthew G. Knepley   }
496427f02ce8SMatthew G. Knepley   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
496527f02ce8SMatthew G. Knepley   *numPoints = np;
496627f02ce8SMatthew G. Knepley   *points    = pts;
496727f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
496827f02ce8SMatthew G. Knepley }
496927f02ce8SMatthew G. Knepley 
497097529cf3SJed Brown /* Compressed closure does not apply closure permutation */
49711dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4972923c78e0SToby Isaac {
497327f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
4974923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4975923c78e0SToby Isaac   PetscErrorCode ierr;
4976923c78e0SToby Isaac 
4977923c78e0SToby Isaac   PetscFunctionBeginHot;
4978923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
497927f02ce8SMatthew G. Knepley   if (*clPoints) {
4980923c78e0SToby Isaac     PetscInt dof, off;
4981923c78e0SToby Isaac 
4982923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4983923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4984923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4985923c78e0SToby Isaac     np   = dof/2;
4986923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
498727f02ce8SMatthew G. Knepley   } else {
4988665f567fSMatthew G. Knepley     DMPolytopeType ct;
4989665f567fSMatthew G. Knepley 
499027f02ce8SMatthew G. Knepley     /* Do not make the label if it does not exist */
499127f02ce8SMatthew G. Knepley     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
499227f02ce8SMatthew G. Knepley     else                    {ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);}
499327f02ce8SMatthew G. Knepley     switch (ct) {
499427f02ce8SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
499527f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);CHKERRQ(ierr);
499627f02ce8SMatthew G. Knepley         break;
499727f02ce8SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
499827f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);CHKERRQ(ierr);
499927f02ce8SMatthew G. Knepley         break;
500027f02ce8SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
500127f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);CHKERRQ(ierr);
500227f02ce8SMatthew G. Knepley         break;
500327f02ce8SMatthew G. Knepley       default:
500427f02ce8SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
500527f02ce8SMatthew G. Knepley     }
500627f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
5007923c78e0SToby Isaac   }
5008923c78e0SToby Isaac   *numPoints = np;
5009923c78e0SToby Isaac   *points    = pts;
5010923c78e0SToby Isaac   *clp       = cla;
5011923c78e0SToby Isaac   PetscFunctionReturn(0);
5012923c78e0SToby Isaac }
5013923c78e0SToby Isaac 
50141dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5015923c78e0SToby Isaac {
5016923c78e0SToby Isaac   PetscErrorCode ierr;
5017923c78e0SToby Isaac 
5018923c78e0SToby Isaac   PetscFunctionBeginHot;
5019923c78e0SToby Isaac   if (!*clPoints) {
5020923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
5021923c78e0SToby Isaac   } else {
5022923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
5023923c78e0SToby Isaac   }
5024923c78e0SToby Isaac   *numPoints = 0;
5025923c78e0SToby Isaac   *points    = NULL;
5026923c78e0SToby Isaac   *clSec     = NULL;
5027923c78e0SToby Isaac   *clPoints  = NULL;
5028923c78e0SToby Isaac   *clp       = NULL;
5029923c78e0SToby Isaac   PetscFunctionReturn(0);
5030923c78e0SToby Isaac }
5031923c78e0SToby Isaac 
503297e99dd9SToby 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[])
50331a271a75SMatthew G. Knepley {
50341a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
503597e99dd9SToby Isaac   const PetscInt    **perms = NULL;
503697e99dd9SToby Isaac   const PetscScalar **flips = NULL;
50371a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
50381a271a75SMatthew G. Knepley 
50391a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5040fe02ba77SJed Brown   *size = 0;
504197e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
504297e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
504397e99dd9SToby Isaac     const PetscInt    point = points[2*p];
504497e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
504597e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
50461a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
50471a271a75SMatthew G. Knepley     const PetscScalar *varr;
50481a271a75SMatthew G. Knepley 
50491a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
50501a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
50511a271a75SMatthew G. Knepley     varr = &vArray[off];
505297e99dd9SToby Isaac     if (clperm) {
505397e99dd9SToby Isaac       if (perm) {
505497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
50551a271a75SMatthew G. Knepley       } else {
505697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
505797e99dd9SToby Isaac       }
505897e99dd9SToby Isaac       if (flip) {
505997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
506097e99dd9SToby Isaac       }
506197e99dd9SToby Isaac     } else {
506297e99dd9SToby Isaac       if (perm) {
506397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
506497e99dd9SToby Isaac       } else {
506597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
506697e99dd9SToby Isaac       }
506797e99dd9SToby Isaac       if (flip) {
506897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
50691a271a75SMatthew G. Knepley       }
50701a271a75SMatthew G. Knepley     }
507197e99dd9SToby Isaac     offset += dof;
507297e99dd9SToby Isaac   }
507397e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
50741a271a75SMatthew G. Knepley   *size = offset;
50751a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
50761a271a75SMatthew G. Knepley }
50771a271a75SMatthew G. Knepley 
507897e99dd9SToby 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[])
50791a271a75SMatthew G. Knepley {
50801a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
50811a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
50821a271a75SMatthew G. Knepley 
50831a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5084fe02ba77SJed Brown   *size = 0;
50851a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
508697e99dd9SToby Isaac     PetscInt          p;
508797e99dd9SToby Isaac     const PetscInt    **perms = NULL;
508897e99dd9SToby Isaac     const PetscScalar **flips = NULL;
50891a271a75SMatthew G. Knepley 
509097e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
509197e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
509297e99dd9SToby Isaac       const PetscInt    point = points[2*p];
509397e99dd9SToby Isaac       PetscInt          fdof, foff, b;
50941a271a75SMatthew G. Knepley       const PetscScalar *varr;
509597e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
509697e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
50971a271a75SMatthew G. Knepley 
50981a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
50991a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
51001a271a75SMatthew G. Knepley       varr = &vArray[foff];
510197e99dd9SToby Isaac       if (clperm) {
510297e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
510397e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
510497e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
51051a271a75SMatthew G. Knepley       } else {
510697e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
510797e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
510897e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
51091a271a75SMatthew G. Knepley       }
511097e99dd9SToby Isaac       offset += fdof;
51111a271a75SMatthew G. Knepley     }
511297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
51131a271a75SMatthew G. Knepley   }
51141a271a75SMatthew G. Knepley   *size = offset;
51151a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
51161a271a75SMatthew G. Knepley }
51171a271a75SMatthew G. Knepley 
5118552f7358SJed Brown /*@C
5119552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
5120552f7358SJed Brown 
5121552f7358SJed Brown   Not collective
5122552f7358SJed Brown 
5123552f7358SJed Brown   Input Parameters:
5124552f7358SJed Brown + dm - The DM
5125552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5126552f7358SJed Brown . v - The local vector
512722c1ee49SMatthew G. Knepley . point - The point in the DM
512822c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
512922c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
5130552f7358SJed Brown 
5131552f7358SJed Brown   Output Parameters:
513222c1ee49SMatthew G. Knepley + csize - The number of values in the closure
513322c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
513422c1ee49SMatthew G. Knepley 
513522c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
513622c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
513722c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
513822c1ee49SMatthew G. Knepley $
513922c1ee49SMatthew G. Knepley $ A typical use could be
514022c1ee49SMatthew G. Knepley $
514122c1ee49SMatthew G. Knepley $  values = NULL;
514222c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
514322c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
514422c1ee49SMatthew G. Knepley $    <Compute on closure>
514522c1ee49SMatthew G. Knepley $  }
514622c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
514722c1ee49SMatthew G. Knepley $
514822c1ee49SMatthew G. Knepley $ or
514922c1ee49SMatthew G. Knepley $
515022c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
515122c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
515222c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
515322c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
515422c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
515522c1ee49SMatthew G. Knepley $      <Compute on closure>
515622c1ee49SMatthew G. Knepley $    }
515722c1ee49SMatthew G. Knepley $  }
515822c1ee49SMatthew G. Knepley $  PetscFree(values);
5159552f7358SJed Brown 
5160552f7358SJed Brown   Fortran Notes:
5161552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
5162552f7358SJed Brown   include petsc.h90 in your code.
5163552f7358SJed Brown 
5164552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
5165552f7358SJed Brown 
5166552f7358SJed Brown   Level: intermediate
5167552f7358SJed Brown 
5168552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5169552f7358SJed Brown @*/
5170552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5171552f7358SJed Brown {
5172552f7358SJed Brown   PetscSection       clSection;
5173d9917b9dSMatthew G. Knepley   IS                 clPoints;
5174552f7358SJed Brown   PetscInt          *points = NULL;
5175c459fbc1SJed Brown   const PetscInt    *clp, *perm;
5176c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
5177552f7358SJed Brown   PetscErrorCode     ierr;
5178552f7358SJed Brown 
5179d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
5180552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
518192fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
51821a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
51831a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5184552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5185552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5186552f7358SJed Brown   if (depth == 1 && numFields < 2) {
51871a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5188552f7358SJed Brown     PetscFunctionReturn(0);
5189552f7358SJed Brown   }
51901a271a75SMatthew G. Knepley   /* Get points */
5191923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5192c459fbc1SJed Brown   /* Get sizes */
5193c459fbc1SJed Brown   asize = 0;
5194c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
5195c459fbc1SJed Brown     PetscInt dof;
5196552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
51971a271a75SMatthew G. Knepley     asize += dof;
5198552f7358SJed Brown   }
5199c459fbc1SJed Brown   if (values) {
5200c459fbc1SJed Brown     const PetscScalar *vArray;
5201c459fbc1SJed Brown     PetscInt          size;
5202c459fbc1SJed Brown 
5203c459fbc1SJed Brown     if (*values) {
5204c459fbc1SJed 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);
5205c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
5206c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
52078a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
52081a271a75SMatthew G. Knepley     /* Get values */
5209c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
5210c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
5211c459fbc1SJed Brown     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
52121a271a75SMatthew G. Knepley     /* Cleanup array */
52138a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5214d0f6b257SMatthew G. Knepley   }
5215c459fbc1SJed Brown   if (csize) *csize = asize;
5216c459fbc1SJed Brown   /* Cleanup points */
5217c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5218552f7358SJed Brown   PetscFunctionReturn(0);
5219552f7358SJed Brown }
5220552f7358SJed Brown 
5221e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5222e5c487bfSMatthew G. Knepley {
5223e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
5224e5c487bfSMatthew G. Knepley   PetscSection       clSection;
5225e5c487bfSMatthew G. Knepley   IS                 clPoints;
5226e5c487bfSMatthew G. Knepley   PetscScalar       *array;
5227e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
5228e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
5229c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
5230c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
5231e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
5232e5c487bfSMatthew G. Knepley 
5233e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
5234e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5235e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5236e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5237e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5238e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
5239e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
5240e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5241e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
5242e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5243e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
5244e5c487bfSMatthew G. Knepley   }
5245e5c487bfSMatthew G. Knepley   /* Get points */
5246e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5247c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
5248c459fbc1SJed Brown     PetscInt dof;
5249c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5250c459fbc1SJed Brown     clsize += dof;
5251c459fbc1SJed Brown   }
5252c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
5253e5c487bfSMatthew G. Knepley   /* Filter points */
5254e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
5255e5c487bfSMatthew G. Knepley     PetscInt dep;
5256e5c487bfSMatthew G. Knepley 
5257e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
5258e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
5259e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
5260e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
5261e5c487bfSMatthew G. Knepley     ++Np;
5262e5c487bfSMatthew G. Knepley   }
5263e5c487bfSMatthew G. Knepley   /* Get array */
5264e5c487bfSMatthew G. Knepley   if (!values || !*values) {
5265e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
5266e5c487bfSMatthew G. Knepley 
5267e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
5268e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5269e5c487bfSMatthew G. Knepley       asize += dof;
5270e5c487bfSMatthew G. Knepley     }
5271e5c487bfSMatthew G. Knepley     if (!values) {
5272e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5273e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
5274e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
5275e5c487bfSMatthew G. Knepley     }
5276e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
5277e5c487bfSMatthew G. Knepley   } else {
5278e5c487bfSMatthew G. Knepley     array = *values;
5279e5c487bfSMatthew G. Knepley   }
5280e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
5281e5c487bfSMatthew G. Knepley   /* Get values */
5282e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
5283e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
5284e5c487bfSMatthew G. Knepley   /* Cleanup points */
5285e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5286e5c487bfSMatthew G. Knepley   /* Cleanup array */
5287e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5288e5c487bfSMatthew G. Knepley   if (!*values) {
5289e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
5290e5c487bfSMatthew G. Knepley     *values = array;
5291e5c487bfSMatthew G. Knepley   } else {
5292e5c487bfSMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
5293e5c487bfSMatthew G. Knepley     *csize = size;
5294e5c487bfSMatthew G. Knepley   }
5295e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
5296e5c487bfSMatthew G. Knepley }
5297e5c487bfSMatthew G. Knepley 
5298552f7358SJed Brown /*@C
5299552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5300552f7358SJed Brown 
5301552f7358SJed Brown   Not collective
5302552f7358SJed Brown 
5303552f7358SJed Brown   Input Parameters:
5304552f7358SJed Brown + dm - The DM
53050298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
5306552f7358SJed Brown . v - The local vector
5307eaf898f9SPatrick Sanan . point - The point in the DM
53080298fd71SBarry Smith . csize - The number of values in the closure, or NULL
5309552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
5310552f7358SJed Brown 
531122c1ee49SMatthew 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()
531222c1ee49SMatthew G. Knepley 
53133813dfbdSMatthew G Knepley   Fortran Notes:
53143813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
53153813dfbdSMatthew G Knepley   include petsc.h90 in your code.
53163813dfbdSMatthew G Knepley 
53173813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
53183813dfbdSMatthew G Knepley 
5319552f7358SJed Brown   Level: intermediate
5320552f7358SJed Brown 
5321552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5322552f7358SJed Brown @*/
53237c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5324a6dfd86eSKarl Rupp {
5325552f7358SJed Brown   PetscInt       size = 0;
5326552f7358SJed Brown   PetscErrorCode ierr;
5327552f7358SJed Brown 
5328552f7358SJed Brown   PetscFunctionBegin;
5329552f7358SJed Brown   /* Should work without recalculating size */
533069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
5331c9fdaa05SMatthew G. Knepley   *values = NULL;
5332552f7358SJed Brown   PetscFunctionReturn(0);
5333552f7358SJed Brown }
5334552f7358SJed Brown 
5335552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
5336552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
5337552f7358SJed Brown 
533897e99dd9SToby 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[])
5339552f7358SJed Brown {
5340552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5341552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5342552f7358SJed Brown   PetscScalar    *a;
5343552f7358SJed Brown   PetscInt        off, cind = 0, k;
5344552f7358SJed Brown   PetscErrorCode  ierr;
5345552f7358SJed Brown 
5346552f7358SJed Brown   PetscFunctionBegin;
5347552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5348552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5349552f7358SJed Brown   a    = &array[off];
5350552f7358SJed Brown   if (!cdof || setBC) {
535197e99dd9SToby Isaac     if (clperm) {
535297e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
535397e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5354552f7358SJed Brown     } else {
535597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
535697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5357552f7358SJed Brown     }
5358552f7358SJed Brown   } else {
5359552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
536097e99dd9SToby Isaac     if (clperm) {
536197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
5362552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
536397e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5364552f7358SJed Brown         }
5365552f7358SJed Brown       } else {
5366552f7358SJed Brown         for (k = 0; k < dof; ++k) {
5367552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
536897e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
536997e99dd9SToby Isaac         }
537097e99dd9SToby Isaac       }
537197e99dd9SToby Isaac     } else {
537297e99dd9SToby Isaac       if (perm) {
537397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
537497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
537597e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
537697e99dd9SToby Isaac         }
537797e99dd9SToby Isaac       } else {
537897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
537997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
538097e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
538197e99dd9SToby Isaac         }
5382552f7358SJed Brown       }
5383552f7358SJed Brown     }
5384552f7358SJed Brown   }
5385552f7358SJed Brown   PetscFunctionReturn(0);
5386552f7358SJed Brown }
5387552f7358SJed Brown 
538897e99dd9SToby 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[])
5389a5e93ea8SMatthew G. Knepley {
5390a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5391a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5392a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5393a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5394a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5395a5e93ea8SMatthew G. Knepley 
5396a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5397a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5398a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5399a5e93ea8SMatthew G. Knepley   a    = &array[off];
5400a5e93ea8SMatthew G. Knepley   if (cdof) {
5401a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
540297e99dd9SToby Isaac     if (clperm) {
540397e99dd9SToby Isaac       if (perm) {
5404a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5405a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
540697e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
540797e99dd9SToby Isaac             cind++;
5408a5e93ea8SMatthew G. Knepley           }
5409a5e93ea8SMatthew G. Knepley         }
5410a5e93ea8SMatthew G. Knepley       } else {
5411a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5412a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
541397e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
541497e99dd9SToby Isaac             cind++;
541597e99dd9SToby Isaac           }
541697e99dd9SToby Isaac         }
541797e99dd9SToby Isaac       }
541897e99dd9SToby Isaac     } else {
541997e99dd9SToby Isaac       if (perm) {
542097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
542197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
542297e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
542397e99dd9SToby Isaac             cind++;
542497e99dd9SToby Isaac           }
542597e99dd9SToby Isaac         }
542697e99dd9SToby Isaac       } else {
542797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
542897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
542997e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
543097e99dd9SToby Isaac             cind++;
543197e99dd9SToby Isaac           }
5432a5e93ea8SMatthew G. Knepley         }
5433a5e93ea8SMatthew G. Knepley       }
5434a5e93ea8SMatthew G. Knepley     }
5435a5e93ea8SMatthew G. Knepley   }
5436a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5437a5e93ea8SMatthew G. Knepley }
5438a5e93ea8SMatthew G. Knepley 
543997e99dd9SToby 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[])
5440a6dfd86eSKarl Rupp {
5441552f7358SJed Brown   PetscScalar    *a;
54421a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
54431a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
544497e99dd9SToby Isaac   PetscInt        cind = 0, b;
5445552f7358SJed Brown   PetscErrorCode  ierr;
5446552f7358SJed Brown 
5447552f7358SJed Brown   PetscFunctionBegin;
5448552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5449552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
54501a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
54511a271a75SMatthew G. Knepley   a    = &array[foff];
5452552f7358SJed Brown   if (!fcdof || setBC) {
545397e99dd9SToby Isaac     if (clperm) {
545497e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
545597e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5456552f7358SJed Brown     } else {
545797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
545897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5459552f7358SJed Brown     }
5460552f7358SJed Brown   } else {
5461552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
546297e99dd9SToby Isaac     if (clperm) {
546397e99dd9SToby Isaac       if (perm) {
546497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
546597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
546697e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5467552f7358SJed Brown         }
5468552f7358SJed Brown       } else {
546997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
547097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
547197e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
547297e99dd9SToby Isaac         }
547397e99dd9SToby Isaac       }
547497e99dd9SToby Isaac     } else {
547597e99dd9SToby Isaac       if (perm) {
547697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
547797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
547897e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
547997e99dd9SToby Isaac         }
548097e99dd9SToby Isaac       } else {
548197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
548297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
548397e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5484552f7358SJed Brown         }
5485552f7358SJed Brown       }
5486552f7358SJed Brown     }
5487552f7358SJed Brown   }
54881a271a75SMatthew G. Knepley   *offset += fdof;
5489552f7358SJed Brown   PetscFunctionReturn(0);
5490552f7358SJed Brown }
5491552f7358SJed Brown 
5492ba322698SMatthew 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[])
5493a5e93ea8SMatthew G. Knepley {
5494a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
54951a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
54961a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
54975da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
5498ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
5499a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5500a5e93ea8SMatthew G. Knepley 
5501a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
55025da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
5503a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5504a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
55051a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
55061a271a75SMatthew G. Knepley   a    = &array[foff];
5507a5e93ea8SMatthew G. Knepley   if (fcdof) {
5508ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
5509a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
551097e99dd9SToby Isaac     if (clperm) {
551197e99dd9SToby Isaac       if (perm) {
5512ba322698SMatthew G. Knepley         if (comps) {
5513ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5514ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
55155da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5516ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5517ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5518ba322698SMatthew G. Knepley           }
5519ba322698SMatthew G. Knepley         } else {
552097e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
552197e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
552297e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5523a5e93ea8SMatthew G. Knepley               ++cind;
5524a5e93ea8SMatthew G. Knepley             }
5525a5e93ea8SMatthew G. Knepley           }
5526ba322698SMatthew G. Knepley         }
5527ba322698SMatthew G. Knepley       } else {
5528ba322698SMatthew G. Knepley         if (comps) {
5529ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5530ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
55315da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5532ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5533ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5534ba322698SMatthew G. Knepley           }
5535a5e93ea8SMatthew G. Knepley         } else {
553697e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
553797e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
553897e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
553997e99dd9SToby Isaac               ++cind;
554097e99dd9SToby Isaac             }
554197e99dd9SToby Isaac           }
554297e99dd9SToby Isaac         }
5543ba322698SMatthew G. Knepley       }
554497e99dd9SToby Isaac     } else {
554597e99dd9SToby Isaac       if (perm) {
5546ba322698SMatthew G. Knepley         if (comps) {
5547ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5548ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
55495da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5550ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5551ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5552ba322698SMatthew G. Knepley           }
5553ba322698SMatthew G. Knepley         } else {
555497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
555597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
555697e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
555797e99dd9SToby Isaac               ++cind;
555897e99dd9SToby Isaac             }
555997e99dd9SToby Isaac           }
5560ba322698SMatthew G. Knepley         }
5561ba322698SMatthew G. Knepley       } else {
5562ba322698SMatthew G. Knepley         if (comps) {
5563ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5564ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
55655da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5566ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5567ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5568ba322698SMatthew G. Knepley           }
556997e99dd9SToby Isaac         } else {
557097e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
557197e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
557297e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5573a5e93ea8SMatthew G. Knepley               ++cind;
5574a5e93ea8SMatthew G. Knepley             }
5575a5e93ea8SMatthew G. Knepley           }
5576a5e93ea8SMatthew G. Knepley         }
5577a5e93ea8SMatthew G. Knepley       }
5578a5e93ea8SMatthew G. Knepley     }
5579ba322698SMatthew G. Knepley   }
55801a271a75SMatthew G. Knepley   *offset += fdof;
5581a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5582a5e93ea8SMatthew G. Knepley }
5583a5e93ea8SMatthew G. Knepley 
55848f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5585a6dfd86eSKarl Rupp {
5586552f7358SJed Brown   PetscScalar    *array;
55871b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
55881b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5589552f7358SJed Brown   PetscErrorCode  ierr;
5590552f7358SJed Brown 
55911b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5592b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5593b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5594b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5595b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5596b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5597b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5598b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5599b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5600b6ebb6e6SMatthew G. Knepley 
5601b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5602b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5603b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5604b6ebb6e6SMatthew G. Knepley     {
5605b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5606b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5607b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5608b6ebb6e6SMatthew G. Knepley 
5609b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5610b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5611b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5612b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5613b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5614b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5615b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5616b6ebb6e6SMatthew G. Knepley           }
5617b6ebb6e6SMatthew G. Knepley         } else {
5618b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5619b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5620b6ebb6e6SMatthew G. Knepley           }
5621b6ebb6e6SMatthew G. Knepley         }
5622b6ebb6e6SMatthew G. Knepley       } else {
5623b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5624b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5625b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5626b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5627b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5628b6ebb6e6SMatthew G. Knepley           }
5629b6ebb6e6SMatthew G. Knepley         } else {
5630b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5631b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5632b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5633b6ebb6e6SMatthew G. Knepley           }
5634b6ebb6e6SMatthew G. Knepley         }
5635b6ebb6e6SMatthew G. Knepley       }
5636b6ebb6e6SMatthew G. Knepley     }
5637b6ebb6e6SMatthew G. Knepley   }
5638b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5639b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5640b6ebb6e6SMatthew G. Knepley }
56411b406b76SMatthew G. Knepley 
56421b406b76SMatthew G. Knepley /*@C
56431b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
56441b406b76SMatthew G. Knepley 
56451b406b76SMatthew G. Knepley   Not collective
56461b406b76SMatthew G. Knepley 
56471b406b76SMatthew G. Knepley   Input Parameters:
56481b406b76SMatthew G. Knepley + dm - The DM
56491b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
56501b406b76SMatthew G. Knepley . v - The local vector
5651eaf898f9SPatrick Sanan . point - The point in the DM
56521b406b76SMatthew G. Knepley . values - The array of values
565322c1ee49SMatthew 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,
565422c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
56551b406b76SMatthew G. Knepley 
56561b406b76SMatthew G. Knepley   Fortran Notes:
56571b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
56581b406b76SMatthew G. Knepley 
56591b406b76SMatthew G. Knepley   Level: intermediate
56601b406b76SMatthew G. Knepley 
56611b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
56621b406b76SMatthew G. Knepley @*/
56631b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
56641b406b76SMatthew G. Knepley {
56651b406b76SMatthew G. Knepley   PetscSection    clSection;
56661b406b76SMatthew G. Knepley   IS              clPoints;
56671b406b76SMatthew G. Knepley   PetscScalar    *array;
56681b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
566927f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
5670c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
56711b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
56721b406b76SMatthew G. Knepley 
56731a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
56741b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
567592fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
56761a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
56771a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
56781b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
56791b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
56801b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
56818f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
56821b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
56831b406b76SMatthew G. Knepley   }
56841a271a75SMatthew G. Knepley   /* Get points */
5685923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5686c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
5687c459fbc1SJed Brown     PetscInt dof;
5688c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5689c459fbc1SJed Brown     clsize += dof;
5690c459fbc1SJed Brown   }
5691c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
56921a271a75SMatthew G. Knepley   /* Get array */
5693552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
56941a271a75SMatthew G. Knepley   /* Get values */
5695ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
569697e99dd9SToby Isaac     PetscInt offset = 0, f;
5697552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
569897e99dd9SToby Isaac       const PetscInt    **perms = NULL;
569997e99dd9SToby Isaac       const PetscScalar **flips = NULL;
570097e99dd9SToby Isaac 
570197e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5702552f7358SJed Brown       switch (mode) {
5703552f7358SJed Brown       case INSERT_VALUES:
570497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
570597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
570697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
570797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
570897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5709552f7358SJed Brown         } break;
5710552f7358SJed Brown       case INSERT_ALL_VALUES:
571197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
571297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
571397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
571497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
571597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5716552f7358SJed Brown         } break;
5717a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
571897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
571997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
572097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
572197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5722ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5723a5e93ea8SMatthew G. Knepley         } break;
5724552f7358SJed Brown       case ADD_VALUES:
572597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
572697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
572797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
572897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
572997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5730552f7358SJed Brown         } break;
5731552f7358SJed Brown       case ADD_ALL_VALUES:
573297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
573397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
573497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
573597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
573697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5737552f7358SJed Brown         } break;
5738304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
573997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
574097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
574197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
574297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5743ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5744304ab55fSMatthew G. Knepley         } break;
5745552f7358SJed Brown       default:
5746f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5747552f7358SJed Brown       }
574897e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
57491a271a75SMatthew G. Knepley     }
5750552f7358SJed Brown   } else {
57511a271a75SMatthew G. Knepley     PetscInt dof, off;
575297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
575397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
57541a271a75SMatthew G. Knepley 
575597e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5756552f7358SJed Brown     switch (mode) {
5757552f7358SJed Brown     case INSERT_VALUES:
575897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
575997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
576097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
576197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
576297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
576397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5764552f7358SJed Brown       } break;
5765552f7358SJed Brown     case INSERT_ALL_VALUES:
576697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
576797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
576897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
576997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
577097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
577197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5772552f7358SJed Brown       } break;
5773a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
577497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
577597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
577697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
577797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
577897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
577997e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5780a5e93ea8SMatthew G. Knepley       } break;
5781552f7358SJed Brown     case ADD_VALUES:
578297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
578397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
578497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
578597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
578697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
578797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5788552f7358SJed Brown       } break;
5789552f7358SJed Brown     case ADD_ALL_VALUES:
579097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
579197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
579297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
579397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
579497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
579597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5796552f7358SJed Brown       } break;
5797304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
579897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
579997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
580097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
580197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
580297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
580397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5804304ab55fSMatthew G. Knepley       } break;
5805552f7358SJed Brown     default:
5806f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5807552f7358SJed Brown     }
580897e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5809552f7358SJed Brown   }
58101a271a75SMatthew G. Knepley   /* Cleanup points */
5811923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
58121a271a75SMatthew G. Knepley   /* Cleanup array */
5813552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5814552f7358SJed Brown   PetscFunctionReturn(0);
5815552f7358SJed Brown }
5816552f7358SJed Brown 
58175f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
58185f790a90SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
58195f790a90SMatthew G. Knepley {
58205f790a90SMatthew G. Knepley   PetscFunctionBegin;
58215f790a90SMatthew G. Knepley   if (label) {
58225f790a90SMatthew G. Knepley     PetscInt       val, fdof;
58235f790a90SMatthew G. Knepley     PetscErrorCode ierr;
58245f790a90SMatthew G. Knepley 
58255f790a90SMatthew G. Knepley     /* There is a problem with this:
58265f790a90SMatthew 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
58275f790a90SMatthew 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.
58285f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
58295f790a90SMatthew G. Knepley     */
58305f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
58315f790a90SMatthew G. Knepley     if (val < 0) {
58325f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
58335f790a90SMatthew G. Knepley       *offset += fdof;
58345f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
58355f790a90SMatthew G. Knepley     }
58365f790a90SMatthew G. Knepley   }
58375f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
58385f790a90SMatthew G. Knepley }
58395f790a90SMatthew G. Knepley 
584097529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
58415f790a90SMatthew 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)
5842e07394fbSMatthew G. Knepley {
5843e07394fbSMatthew G. Knepley   PetscSection      clSection;
5844e07394fbSMatthew G. Knepley   IS                clPoints;
5845e07394fbSMatthew G. Knepley   PetscScalar       *array;
5846e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
584797529cf3SJed Brown   const PetscInt    *clp;
5848e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
584997e99dd9SToby Isaac   PetscInt          offset = 0, f;
5850e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5851e07394fbSMatthew G. Knepley 
5852e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5853e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
585492fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5855e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5856e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5857e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5858e07394fbSMatthew G. Knepley   /* Get points */
5859923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5860e07394fbSMatthew G. Knepley   /* Get array */
5861e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5862e07394fbSMatthew G. Knepley   /* Get values */
5863e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
586497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
586597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
586697e99dd9SToby Isaac 
5867e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5868e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5869e07394fbSMatthew G. Knepley         PetscInt fdof;
5870e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5871e07394fbSMatthew G. Knepley         offset += fdof;
5872e07394fbSMatthew G. Knepley       }
5873e07394fbSMatthew G. Knepley       continue;
5874e07394fbSMatthew G. Knepley     }
587597e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5876e07394fbSMatthew G. Knepley     switch (mode) {
5877e07394fbSMatthew G. Knepley     case INSERT_VALUES:
587897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
587997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
588097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
588197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58825f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
588397529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5884e07394fbSMatthew G. Knepley       } break;
5885e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
588697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
588797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
588897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
588997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58905f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
589197529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5892e07394fbSMatthew G. Knepley       } break;
5893e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
589497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
589597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
589697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
589797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58985f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
589997529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5900e07394fbSMatthew G. Knepley       } break;
5901e07394fbSMatthew G. Knepley     case ADD_VALUES:
590297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
590397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
590497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
590597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
59065f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
590797529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5908e07394fbSMatthew G. Knepley       } break;
5909e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
591097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
591197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
591297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
591397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
59145f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
591597529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5916e07394fbSMatthew G. Knepley       } break;
5917e07394fbSMatthew G. Knepley     default:
5918f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5919e07394fbSMatthew G. Knepley     }
592097e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5921e07394fbSMatthew G. Knepley   }
5922e07394fbSMatthew G. Knepley   /* Cleanup points */
5923923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5924e07394fbSMatthew G. Knepley   /* Cleanup array */
5925e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5926e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5927e07394fbSMatthew G. Knepley }
5928e07394fbSMatthew G. Knepley 
59297cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5930552f7358SJed Brown {
5931552f7358SJed Brown   PetscMPIInt    rank;
5932552f7358SJed Brown   PetscInt       i, j;
5933552f7358SJed Brown   PetscErrorCode ierr;
5934552f7358SJed Brown 
5935552f7358SJed Brown   PetscFunctionBegin;
5936ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr);
5937eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5938e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5939e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5940b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5941557cf195SMatthew G. Knepley   if (!values) PetscFunctionReturn(0);
5942b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5943e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5944b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5945519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
59467eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5947552f7358SJed Brown #else
5948b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5949552f7358SJed Brown #endif
5950552f7358SJed Brown     }
595177a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5952552f7358SJed Brown   }
5953552f7358SJed Brown   PetscFunctionReturn(0);
5954552f7358SJed Brown }
5955552f7358SJed Brown 
595605586334SMatthew G. Knepley /*
595705586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
595805586334SMatthew G. Knepley 
595905586334SMatthew G. Knepley   Input Parameters:
596005586334SMatthew G. Knepley + section - The section for this data layout
596136fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
596205586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
596305586334SMatthew G. Knepley . off     - The global offset of this point
596405586334SMatthew G. Knepley . loff    - The local offset of each field
596505586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
596605586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
596705586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
596805586334SMatthew G. Knepley 
596905586334SMatthew G. Knepley   Output Parameter:
597005586334SMatthew G. Knepley . indices - Indices for dofs on this point
597105586334SMatthew G. Knepley 
597205586334SMatthew G. Knepley   Level: developer
597305586334SMatthew G. Knepley 
597405586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
597505586334SMatthew G. Knepley */
597636fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5977a6dfd86eSKarl Rupp {
5978e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5979552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5980552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5981552f7358SJed Brown   PetscInt        cind = 0, k;
5982552f7358SJed Brown   PetscErrorCode  ierr;
5983552f7358SJed Brown 
5984552f7358SJed Brown   PetscFunctionBegin;
598536fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5986552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5987552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5988552f7358SJed Brown   if (!cdof || setBC) {
598905586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
599005586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
599105586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
599205586334SMatthew G. Knepley 
599305586334SMatthew G. Knepley       indices[ind] = off + k;
5994552f7358SJed Brown     }
5995552f7358SJed Brown   } else {
5996552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
59974acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
599805586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
599905586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
600005586334SMatthew G. Knepley 
60014acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
60024acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
600305586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
60044acb8e1eSToby Isaac         ++cind;
60054acb8e1eSToby Isaac       } else {
600636fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
6007552f7358SJed Brown       }
6008552f7358SJed Brown     }
6009552f7358SJed Brown   }
6010e6ccafaeSMatthew G Knepley   *loff += dof;
6011552f7358SJed Brown   PetscFunctionReturn(0);
6012552f7358SJed Brown }
6013552f7358SJed Brown 
60147e29afd2SMatthew G. Knepley /*
601536fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
60167e29afd2SMatthew G. Knepley 
601736fa2b79SJed Brown  Input Parameters:
601836fa2b79SJed Brown + section - a section (global or local)
601936fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
602036fa2b79SJed Brown . point - point within section
602136fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
602236fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
602336fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
602436fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
602536fa2b79SJed Brown . permsoff - offset
602636fa2b79SJed Brown - indperm - index permutation
602736fa2b79SJed Brown 
602836fa2b79SJed Brown  Output Parameter:
602936fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
603036fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
603136fa2b79SJed Brown 
603236fa2b79SJed Brown  Notes:
603336fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
603436fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
603536fa2b79SJed Brown  in the local vector.
603636fa2b79SJed Brown 
603736fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
603836fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
603936fa2b79SJed Brown 
604036fa2b79SJed Brown  Developer Note:
604136fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
604236fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
604336fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
604436fa2b79SJed Brown 
604536fa2b79SJed Brown  Example:
604636fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
604736fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
604836fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
604936fa2b79SJed 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.
605036fa2b79SJed Brown 
605136fa2b79SJed Brown  Level: developer
60527e29afd2SMatthew G. Knepley */
605336fa2b79SJed 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[])
6054a6dfd86eSKarl Rupp {
6055552f7358SJed Brown   PetscInt       numFields, foff, f;
6056552f7358SJed Brown   PetscErrorCode ierr;
6057552f7358SJed Brown 
6058552f7358SJed Brown   PetscFunctionBegin;
605936fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
6060552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6061552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
60624acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
6063552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
60644acb8e1eSToby Isaac     PetscInt        cind = 0, b;
60654acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
6066552f7358SJed Brown 
6067552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
6068552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
6069552f7358SJed Brown     if (!cfdof || setBC) {
607005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
607105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
607205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
607305586334SMatthew G. Knepley 
607405586334SMatthew G. Knepley         indices[ind] = off+foff+b;
607505586334SMatthew G. Knepley       }
6076552f7358SJed Brown     } else {
6077552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
607805586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
607905586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
608005586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
608105586334SMatthew G. Knepley 
60824acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
608305586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
6084552f7358SJed Brown           ++cind;
6085552f7358SJed Brown         } else {
608636fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
6087552f7358SJed Brown         }
6088552f7358SJed Brown       }
6089552f7358SJed Brown     }
609036fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
6091552f7358SJed Brown     foffs[f] += fdof;
6092552f7358SJed Brown   }
6093552f7358SJed Brown   PetscFunctionReturn(0);
6094552f7358SJed Brown }
6095552f7358SJed Brown 
60967e29afd2SMatthew G. Knepley /*
60977e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
60987e29afd2SMatthew G. Knepley 
60997e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
6100645102dcSJed Brown 
6101645102dcSJed Brown  Notes:
6102645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6103645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
61047e29afd2SMatthew G. Knepley */
6105645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
61067e29afd2SMatthew G. Knepley {
61077e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
61087e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
61097e29afd2SMatthew G. Knepley 
61107e29afd2SMatthew G. Knepley   PetscFunctionBegin;
61117e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
61127e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
61137e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
61147e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
61157e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
61167e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
61177e29afd2SMatthew G. Knepley 
61187e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
61197e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
61207e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
6121645102dcSJed Brown     if (!cfdof) {
612205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
612305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
612405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
612505586334SMatthew G. Knepley 
612605586334SMatthew G. Knepley         indices[ind] = foff+b;
612705586334SMatthew G. Knepley       }
61287e29afd2SMatthew G. Knepley     } else {
61297e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
613005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
613105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
613205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
613305586334SMatthew G. Knepley 
61347e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
613505586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
61367e29afd2SMatthew G. Knepley           ++cind;
61377e29afd2SMatthew G. Knepley         } else {
613805586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
61397e29afd2SMatthew G. Knepley         }
61407e29afd2SMatthew G. Knepley       }
61417e29afd2SMatthew G. Knepley     }
61427e29afd2SMatthew G. Knepley     foffs[f] += fdof;
61437e29afd2SMatthew G. Knepley   }
61447e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
61457e29afd2SMatthew G. Knepley }
61467e29afd2SMatthew G. Knepley 
61474acb8e1eSToby 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)
6148d3d1a6afSToby Isaac {
6149d3d1a6afSToby Isaac   Mat             cMat;
6150d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
6151d3d1a6afSToby Isaac   IS              aIS;
6152d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
6153d3d1a6afSToby Isaac   const PetscInt  *anchors;
6154e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
6155d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
6156d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
6157d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
6158d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
6159d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
6160d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
6161d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
61626ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
6163d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
6164d3d1a6afSToby Isaac   PetscErrorCode  ierr;
6165d3d1a6afSToby Isaac 
6166d3d1a6afSToby Isaac   PetscFunctionBegin;
6167d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6168d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6169d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6170d3d1a6afSToby Isaac 
6171a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
6172d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
6173d3d1a6afSToby Isaac   if (aSec) {
6174580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
6175d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
6176d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
6177d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
6178d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
6179d3d1a6afSToby Isaac      * into the global matrix anyway) */
6180d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
6181d3d1a6afSToby Isaac       PetscInt b    = points[p];
61824b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6183d3d1a6afSToby Isaac 
61844b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
61854b2f2278SToby Isaac       if (!bSecDof) {
61864b2f2278SToby Isaac         continue;
61874b2f2278SToby Isaac       }
6188d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6189d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
6190d3d1a6afSToby Isaac       }
6191d3d1a6afSToby Isaac       if (bDof) {
6192d3d1a6afSToby Isaac         /* this point is constrained */
6193d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
6194d3d1a6afSToby Isaac         PetscInt bOff, q;
6195d3d1a6afSToby Isaac 
6196d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
6197d3d1a6afSToby Isaac         newNumPoints  += bDof;
6198d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
6199d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6200d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
6201d3d1a6afSToby Isaac           PetscInt aDof;
6202d3d1a6afSToby Isaac 
6203d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
6204d3d1a6afSToby Isaac           newNumIndices += aDof;
6205d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
6206d3d1a6afSToby Isaac             PetscInt fDof;
6207d3d1a6afSToby Isaac 
6208d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
6209d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
6210d3d1a6afSToby Isaac           }
6211d3d1a6afSToby Isaac         }
6212d3d1a6afSToby Isaac       }
6213d3d1a6afSToby Isaac       else {
6214d3d1a6afSToby Isaac         /* this point is not constrained */
6215d3d1a6afSToby Isaac         newNumPoints++;
62164b2f2278SToby Isaac         newNumIndices += bSecDof;
6217d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
6218d3d1a6afSToby Isaac           PetscInt fDof;
6219d3d1a6afSToby Isaac 
6220d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6221d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
6222d3d1a6afSToby Isaac         }
6223d3d1a6afSToby Isaac       }
6224d3d1a6afSToby Isaac     }
6225d3d1a6afSToby Isaac   }
6226d3d1a6afSToby Isaac   if (!anyConstrained) {
622772b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
622872b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
622972b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
623072b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
623172b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
6232d3d1a6afSToby Isaac     PetscFunctionReturn(0);
6233d3d1a6afSToby Isaac   }
6234d3d1a6afSToby Isaac 
62356ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
62366ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
62376ecaa68aSToby Isaac 
6238f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
6239d3d1a6afSToby Isaac 
62406ecaa68aSToby Isaac   if (!outPoints && !outValues) {
62416ecaa68aSToby Isaac     if (offsets) {
62426ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
62436ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
62446ecaa68aSToby Isaac       }
62456ecaa68aSToby Isaac     }
62466ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
62476ecaa68aSToby Isaac     PetscFunctionReturn(0);
62486ecaa68aSToby Isaac   }
62496ecaa68aSToby Isaac 
6250f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
6251d3d1a6afSToby Isaac 
6252f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
6253d3d1a6afSToby Isaac 
6254d3d1a6afSToby Isaac   /* workspaces */
6255d3d1a6afSToby Isaac   if (numFields) {
6256d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
625769291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
625869291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6259d3d1a6afSToby Isaac     }
6260d3d1a6afSToby Isaac   }
6261d3d1a6afSToby Isaac   else {
626269291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
626369291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6264d3d1a6afSToby Isaac   }
6265d3d1a6afSToby Isaac 
6266d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
6267d3d1a6afSToby Isaac   if (numFields) {
62684b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
62694b2f2278SToby Isaac 
6270d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6271d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
62724b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6273d3d1a6afSToby Isaac 
62744b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
62754b2f2278SToby Isaac       if (!bSecDof) {
62764b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
62774b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
62784b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
62794b2f2278SToby Isaac         }
62804b2f2278SToby Isaac         continue;
62814b2f2278SToby Isaac       }
6282d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6283d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6284d3d1a6afSToby Isaac       }
6285d3d1a6afSToby Isaac       if (bDof) {
6286d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6287d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
6288d3d1a6afSToby Isaac 
6289d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6290d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6291d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
6292d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
6293d3d1a6afSToby Isaac             PetscInt aFDof;
6294d3d1a6afSToby Isaac 
6295d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
6296d3d1a6afSToby Isaac             allFDof += aFDof;
6297d3d1a6afSToby Isaac           }
6298d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
6299d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
6300d3d1a6afSToby Isaac         }
6301d3d1a6afSToby Isaac       }
6302d3d1a6afSToby Isaac       else {
6303d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6304d3d1a6afSToby Isaac           PetscInt fDof;
6305d3d1a6afSToby Isaac 
6306d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6307d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
6308d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
6309d3d1a6afSToby Isaac         }
6310d3d1a6afSToby Isaac       }
6311d3d1a6afSToby Isaac     }
63124b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
63134b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
63144b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
6315d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6316d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
6317d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
6318d3d1a6afSToby Isaac       }
631919f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
632019f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
632169291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
6322d3d1a6afSToby Isaac     }
6323d3d1a6afSToby Isaac   }
6324d3d1a6afSToby Isaac   else {
6325d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6326d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
63274b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6328d3d1a6afSToby Isaac 
63294b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
63304b2f2278SToby Isaac       if (!bSecDof) {
63314b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
63324b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
63334b2f2278SToby Isaac         continue;
63344b2f2278SToby Isaac       }
6335d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6336d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6337d3d1a6afSToby Isaac       }
6338d3d1a6afSToby Isaac       if (bDof) {
63394b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
6340d3d1a6afSToby Isaac 
6341d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6342d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6343d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
6344d3d1a6afSToby Isaac 
6345d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
6346d3d1a6afSToby Isaac           allDof += aDof;
6347d3d1a6afSToby Isaac         }
6348d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
63494b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
6350d3d1a6afSToby Isaac       }
6351d3d1a6afSToby Isaac       else {
63524b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
6353d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
6354d3d1a6afSToby Isaac       }
6355d3d1a6afSToby Isaac     }
6356d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
6357d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
6358d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6359d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6360d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6361d3d1a6afSToby Isaac     }
636269291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
6363d3d1a6afSToby Isaac   }
6364d3d1a6afSToby Isaac 
63656ecaa68aSToby Isaac   /* output arrays */
636669291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
63676ecaa68aSToby Isaac 
6368d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
6369d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
6370d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
637169291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
637269291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
6373d3d1a6afSToby Isaac   if (numFields) {
6374d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6375d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6376d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
63774b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6378d3d1a6afSToby Isaac 
63794b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
63804b2f2278SToby Isaac       if (!bSecDof) {
63814b2f2278SToby Isaac         continue;
63824b2f2278SToby Isaac       }
6383d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6384d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6385d3d1a6afSToby Isaac       }
6386d3d1a6afSToby Isaac       if (bDof) {
6387d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6388d3d1a6afSToby Isaac 
6389d3d1a6afSToby Isaac         fStart[0] = 0;
6390d3d1a6afSToby Isaac         fEnd[0]   = 0;
6391d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6392d3d1a6afSToby Isaac           PetscInt fDof;
6393d3d1a6afSToby Isaac 
6394d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6395d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6396d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6397d3d1a6afSToby Isaac         }
6398d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
639936fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6400d3d1a6afSToby Isaac 
6401d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6402d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6403d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6404d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6405d3d1a6afSToby Isaac 
6406d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6407d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6408d3d1a6afSToby Isaac         }
6409d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6410d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6411d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6412d3d1a6afSToby Isaac 
6413d3d1a6afSToby 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 */
6414d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6415d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6416302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
641736fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
6418d3d1a6afSToby Isaac         }
6419d3d1a6afSToby Isaac         newP += bDof;
6420d3d1a6afSToby Isaac 
64216ecaa68aSToby Isaac         if (outValues) {
6422d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
6423d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
6424d3d1a6afSToby 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);
6425d3d1a6afSToby Isaac           }
6426d3d1a6afSToby Isaac         }
64276ecaa68aSToby Isaac       }
6428d3d1a6afSToby Isaac       else {
6429d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6430d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6431d3d1a6afSToby Isaac         newP++;
6432d3d1a6afSToby Isaac       }
6433d3d1a6afSToby Isaac     }
6434d3d1a6afSToby Isaac   } else {
6435d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6436d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6437d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
64384b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6439d3d1a6afSToby Isaac 
64404b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
64414b2f2278SToby Isaac       if (!bSecDof) {
64424b2f2278SToby Isaac         continue;
64434b2f2278SToby Isaac       }
6444d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6445d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6446d3d1a6afSToby Isaac       }
6447d3d1a6afSToby Isaac       if (bDof) {
6448d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
6449d3d1a6afSToby Isaac 
6450d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
645136fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
6452d3d1a6afSToby Isaac 
6453d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
6454d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6455d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6456d3d1a6afSToby Isaac 
6457d3d1a6afSToby 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 */
6458d3d1a6afSToby Isaac 
6459d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6460d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6461302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
646236fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
6463d3d1a6afSToby Isaac         }
6464d3d1a6afSToby Isaac         newP += bDof;
6465d3d1a6afSToby Isaac 
6466d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
64676ecaa68aSToby Isaac         if (outValues) {
6468d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
6469d3d1a6afSToby Isaac         }
64706ecaa68aSToby Isaac       }
6471d3d1a6afSToby Isaac       else {
6472d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6473d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6474d3d1a6afSToby Isaac         newP++;
6475d3d1a6afSToby Isaac       }
6476d3d1a6afSToby Isaac     }
6477d3d1a6afSToby Isaac   }
6478d3d1a6afSToby Isaac 
64796ecaa68aSToby Isaac   if (outValues) {
648069291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
6481580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
6482d3d1a6afSToby Isaac     /* multiply constraints on the right */
6483d3d1a6afSToby Isaac     if (numFields) {
6484d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
6485d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
6486d3d1a6afSToby Isaac 
6487d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6488d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
6489d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6490d3d1a6afSToby Isaac           PetscInt c, r, k;
6491d3d1a6afSToby Isaac           PetscInt dof;
6492d3d1a6afSToby Isaac 
6493d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
64944b2f2278SToby Isaac           if (!dof) {
64954b2f2278SToby Isaac             continue;
64964b2f2278SToby Isaac           }
6497d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6498d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6499d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
6500d3d1a6afSToby Isaac 
6501d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6502d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
6503d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
65044acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6505d3d1a6afSToby Isaac                 }
6506d3d1a6afSToby Isaac               }
6507d3d1a6afSToby Isaac             }
6508d3d1a6afSToby Isaac           }
6509d3d1a6afSToby Isaac           else {
6510d3d1a6afSToby Isaac             /* copy this column as is */
6511d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6512d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
6513d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6514d3d1a6afSToby Isaac               }
6515d3d1a6afSToby Isaac             }
6516d3d1a6afSToby Isaac           }
6517d3d1a6afSToby Isaac           oldOff += dof;
6518d3d1a6afSToby Isaac         }
6519d3d1a6afSToby Isaac       }
6520d3d1a6afSToby Isaac     }
6521d3d1a6afSToby Isaac     else {
6522d3d1a6afSToby Isaac       PetscInt oldOff = 0;
6523d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6524d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
6525d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
6526d3d1a6afSToby Isaac         PetscInt c, r, k;
6527d3d1a6afSToby Isaac         PetscInt dof;
6528d3d1a6afSToby Isaac 
6529d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
65304b2f2278SToby Isaac         if (!dof) {
65314b2f2278SToby Isaac           continue;
65324b2f2278SToby Isaac         }
6533d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6534d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6535d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
6536d3d1a6afSToby Isaac 
6537d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6538d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
6539d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
6540d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6541d3d1a6afSToby Isaac               }
6542d3d1a6afSToby Isaac             }
6543d3d1a6afSToby Isaac           }
6544d3d1a6afSToby Isaac         }
6545d3d1a6afSToby Isaac         else {
6546d3d1a6afSToby Isaac           /* copy this column as is */
6547d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6548d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
6549d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6550d3d1a6afSToby Isaac             }
6551d3d1a6afSToby Isaac           }
6552d3d1a6afSToby Isaac         }
6553d3d1a6afSToby Isaac         oldOff += dof;
6554d3d1a6afSToby Isaac       }
6555d3d1a6afSToby Isaac     }
6556d3d1a6afSToby Isaac 
65576ecaa68aSToby Isaac     if (multiplyLeft) {
655869291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
6559580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
6560d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
6561d3d1a6afSToby Isaac       if (numFields) {
6562d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6563d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
6564d3d1a6afSToby Isaac 
6565d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
6566d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
6567d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
6568d3d1a6afSToby Isaac             PetscInt c, r, k;
6569d3d1a6afSToby Isaac             PetscInt dof;
6570d3d1a6afSToby Isaac 
6571d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
6572d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6573d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
6574d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
6575d3d1a6afSToby Isaac 
6576d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
6577d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6578d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
6579d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6580d3d1a6afSToby Isaac                   }
6581d3d1a6afSToby Isaac                 }
6582d3d1a6afSToby Isaac               }
6583d3d1a6afSToby Isaac             }
6584d3d1a6afSToby Isaac             else {
6585d3d1a6afSToby Isaac               /* copy this row as is */
6586d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6587d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6588d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6589d3d1a6afSToby Isaac                 }
6590d3d1a6afSToby Isaac               }
6591d3d1a6afSToby Isaac             }
6592d3d1a6afSToby Isaac             oldOff += dof;
6593d3d1a6afSToby Isaac           }
6594d3d1a6afSToby Isaac         }
6595d3d1a6afSToby Isaac       }
6596d3d1a6afSToby Isaac       else {
6597d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6598d3d1a6afSToby Isaac 
6599d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6600d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6601d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6602d3d1a6afSToby Isaac           PetscInt c, r, k;
6603d3d1a6afSToby Isaac           PetscInt dof;
6604d3d1a6afSToby Isaac 
6605d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6606d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6607d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6608d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6609d3d1a6afSToby Isaac 
6610d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6611d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6612d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6613d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6614d3d1a6afSToby Isaac                 }
6615d3d1a6afSToby Isaac               }
6616d3d1a6afSToby Isaac             }
6617d3d1a6afSToby Isaac           }
6618d3d1a6afSToby Isaac           else {
6619d3d1a6afSToby Isaac             /* copy this row as is */
66209fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6621d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6622d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6623d3d1a6afSToby Isaac               }
6624d3d1a6afSToby Isaac             }
6625d3d1a6afSToby Isaac           }
6626d3d1a6afSToby Isaac           oldOff += dof;
6627d3d1a6afSToby Isaac         }
6628d3d1a6afSToby Isaac       }
6629d3d1a6afSToby Isaac 
663069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
66316ecaa68aSToby Isaac     }
66326ecaa68aSToby Isaac     else {
66336ecaa68aSToby Isaac       newValues = tmpValues;
66346ecaa68aSToby Isaac     }
66356ecaa68aSToby Isaac   }
66366ecaa68aSToby Isaac 
6637d3d1a6afSToby Isaac   /* clean up */
663869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
663969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
66406ecaa68aSToby Isaac 
6641d3d1a6afSToby Isaac   if (numFields) {
6642d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
664369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
664469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
664569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6646d3d1a6afSToby Isaac     }
6647d3d1a6afSToby Isaac   }
6648d3d1a6afSToby Isaac   else {
664969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
665069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
665169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6652d3d1a6afSToby Isaac   }
6653d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6654d3d1a6afSToby Isaac 
6655d3d1a6afSToby Isaac   /* output */
66566ecaa68aSToby Isaac   if (outPoints) {
6657d3d1a6afSToby Isaac     *outPoints = newPoints;
66586ecaa68aSToby Isaac   }
66596ecaa68aSToby Isaac   else {
666069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
66616ecaa68aSToby Isaac   }
666231620726SToby Isaac   if (outValues) {
6663d3d1a6afSToby Isaac     *outValues = newValues;
66646ecaa68aSToby Isaac   }
66656ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6666d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6667d3d1a6afSToby Isaac   }
6668d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6669d3d1a6afSToby Isaac }
6670d3d1a6afSToby Isaac 
66714a1e0b3eSMatthew G. Knepley /*@C
667271f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
66737cd05799SMatthew G. Knepley 
66747cd05799SMatthew G. Knepley   Not collective
66757cd05799SMatthew G. Knepley 
66767cd05799SMatthew G. Knepley   Input Parameters:
66777cd05799SMatthew G. Knepley + dm         - The DM
667871f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
667971f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
668071f0bbf9SMatthew G. Knepley . point      - The point defining the closure
668171f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
66827cd05799SMatthew G. Knepley 
668371f0bbf9SMatthew G. Knepley   Output Parameters:
668471f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
668571f0bbf9SMatthew G. Knepley . indices    - The dof indices
668671f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
668771f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
66887cd05799SMatthew G. Knepley 
668936fa2b79SJed Brown   Notes:
669036fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
669136fa2b79SJed Brown 
669236fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
669336fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
669436fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
669536fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
669636fa2b79SJed Brown   indices (with the above semantics) are implied.
66977cd05799SMatthew G. Knepley 
66987cd05799SMatthew G. Knepley   Level: advanced
66997cd05799SMatthew G. Knepley 
670036fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
67014a1e0b3eSMatthew G. Knepley @*/
670271f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
670371f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
67047773e69fSMatthew G. Knepley {
670571f0bbf9SMatthew G. Knepley   /* Closure ordering */
67067773e69fSMatthew G. Knepley   PetscSection        clSection;
67077773e69fSMatthew G. Knepley   IS                  clPoints;
670871f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
670971f0bbf9SMatthew G. Knepley   PetscInt           *points;
671071f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
671171f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
67124acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
671371f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
671471f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
671571f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
671671f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
671771f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
671871f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
671971f0bbf9SMatthew G. Knepley 
672071f0bbf9SMatthew G. Knepley   PetscInt           *idx;
672171f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
672271f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
67237773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
67247773e69fSMatthew G. Knepley 
672571f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
67267773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67277773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
672836fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
672971f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
673071f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
673171f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
673271f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
67337773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
67347773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6735580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
673671f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
673771f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
6738c459fbc1SJed Brown   if (useClPerm) {
6739c459fbc1SJed Brown     PetscInt depth, clsize;
6740c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
6741c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
6742c459fbc1SJed Brown       PetscInt dof;
6743c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6744c459fbc1SJed Brown       clsize += dof;
6745c459fbc1SJed Brown     }
6746c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
6747c459fbc1SJed Brown   }
674871f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
674971f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
67507773e69fSMatthew G. Knepley     PetscInt dof, fdof;
67517773e69fSMatthew G. Knepley 
67527773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
67537773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
67547773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
67557773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
67567773e69fSMatthew G. Knepley     }
675771f0bbf9SMatthew G. Knepley     Ni += dof;
67587773e69fSMatthew G. Knepley   }
67597773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
676071f0bbf9SMatthew 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);
676171f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
676271f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
676371f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
676471f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
676571f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
676671f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
676771f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
67686ecaa68aSToby Isaac 
676971f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
677071f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
677171f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
677271f0bbf9SMatthew G. Knepley 
677371f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
677471f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
677571f0bbf9SMatthew G. Knepley         if (flip) {
677671f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
677771f0bbf9SMatthew G. Knepley 
677871f0bbf9SMatthew G. Knepley           if (!valCopy) {
677971f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
678071f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
678171f0bbf9SMatthew G. Knepley             *values = valCopy;
678271f0bbf9SMatthew G. Knepley           }
678371f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
678471f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
678571f0bbf9SMatthew G. Knepley 
678671f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
678771f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
678871f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
67896ecaa68aSToby Isaac             }
67906ecaa68aSToby Isaac           }
679171f0bbf9SMatthew G. Knepley         }
679271f0bbf9SMatthew G. Knepley         foffset += fdof;
679371f0bbf9SMatthew G. Knepley       }
679471f0bbf9SMatthew G. Knepley     }
679571f0bbf9SMatthew G. Knepley   }
679671f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
679771f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
679871f0bbf9SMatthew G. Knepley   if (NclC) {
679971f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
680071f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
680171f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
680271f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
680371f0bbf9SMatthew G. Knepley     }
680471f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
680571f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
680671f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
680771f0bbf9SMatthew G. Knepley     }
680871f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
680971f0bbf9SMatthew G. Knepley     Ncl     = NclC;
681071f0bbf9SMatthew G. Knepley     Ni      = NiC;
681171f0bbf9SMatthew G. Knepley     points  = pointsC;
681271f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
681371f0bbf9SMatthew G. Knepley   }
681471f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
681571f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
681671f0bbf9SMatthew G. Knepley   if (Nf) {
681771f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
681871f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
681971f0bbf9SMatthew G. Knepley 
682071f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
682171f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
682271f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
682371f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
682471f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
682571f0bbf9SMatthew G. Knepley 
682671f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
68277773e69fSMatthew G. Knepley       }
68287773e69fSMatthew G. Knepley     } else {
682971f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
683071f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
683171f0bbf9SMatthew G. Knepley 
683271f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
683371f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
683471f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
683571f0bbf9SMatthew G. Knepley          * global section. */
683671f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
683771f0bbf9SMatthew G. Knepley       }
683871f0bbf9SMatthew G. Knepley     }
683971f0bbf9SMatthew G. Knepley   } else {
684071f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
684171f0bbf9SMatthew G. Knepley 
684271f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
684371f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
68444acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
68454acb8e1eSToby Isaac 
684671f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
684771f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
684871f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
684971f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
68507773e69fSMatthew G. Knepley     }
68517773e69fSMatthew G. Knepley   }
685271f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
685371f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
685471f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
685571f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
68564acb8e1eSToby Isaac   }
685771f0bbf9SMatthew G. Knepley   if (NclC) {
685871f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
68597773e69fSMatthew G. Knepley   } else {
686071f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
68617773e69fSMatthew G. Knepley   }
686271f0bbf9SMatthew G. Knepley 
686371f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
686471f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
68657773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
68667773e69fSMatthew G. Knepley }
68677773e69fSMatthew G. Knepley 
68687cd05799SMatthew G. Knepley /*@C
686971f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
68707cd05799SMatthew G. Knepley 
68717cd05799SMatthew G. Knepley   Not collective
68727cd05799SMatthew G. Knepley 
68737cd05799SMatthew G. Knepley   Input Parameters:
68747cd05799SMatthew G. Knepley + dm         - The DM
687571f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
687671f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
687771f0bbf9SMatthew G. Knepley . point      - The point defining the closure
687871f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
687971f0bbf9SMatthew G. Knepley 
688071f0bbf9SMatthew G. Knepley   Output Parameters:
688171f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
688271f0bbf9SMatthew G. Knepley . indices    - The dof indices
688371f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
688471f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
688571f0bbf9SMatthew G. Knepley 
688671f0bbf9SMatthew G. Knepley   Notes:
688771f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
688871f0bbf9SMatthew G. Knepley 
688971f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
689071f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
689171f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
689271f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
689371f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
68947cd05799SMatthew G. Knepley 
68957cd05799SMatthew G. Knepley   Level: advanced
68967cd05799SMatthew G. Knepley 
689771f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
68987cd05799SMatthew G. Knepley @*/
689971f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
690071f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
69017773e69fSMatthew G. Knepley {
69027773e69fSMatthew G. Knepley   PetscErrorCode ierr;
69037773e69fSMatthew G. Knepley 
69047773e69fSMatthew G. Knepley   PetscFunctionBegin;
69057773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6906064a246eSJacob Faibussowitsch   PetscValidPointer(indices, 7);
690769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
69087773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
69097773e69fSMatthew G. Knepley }
69107773e69fSMatthew G. Knepley 
69117f5d1fdeSMatthew G. Knepley /*@C
69127f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
69137f5d1fdeSMatthew G. Knepley 
69147f5d1fdeSMatthew G. Knepley   Not collective
69157f5d1fdeSMatthew G. Knepley 
69167f5d1fdeSMatthew G. Knepley   Input Parameters:
69177f5d1fdeSMatthew G. Knepley + dm - The DM
6918ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6919ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
69207f5d1fdeSMatthew G. Knepley . A - The matrix
6921eaf898f9SPatrick Sanan . point - The point in the DM
69227f5d1fdeSMatthew G. Knepley . values - The array of values
69237f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
69247f5d1fdeSMatthew G. Knepley 
69257f5d1fdeSMatthew G. Knepley   Fortran Notes:
69267f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
69277f5d1fdeSMatthew G. Knepley 
69287f5d1fdeSMatthew G. Knepley   Level: intermediate
69297f5d1fdeSMatthew G. Knepley 
69304a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
69317f5d1fdeSMatthew G. Knepley @*/
69327c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6933552f7358SJed Brown {
6934552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
6935552f7358SJed Brown   PetscInt          *indices;
693671f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
693771f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
6938552f7358SJed Brown   PetscErrorCode     ierr;
6939552f7358SJed Brown 
6940552f7358SJed Brown   PetscFunctionBegin;
6941552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
694292fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
69433dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6944e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
69453dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
69463dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6947552f7358SJed Brown 
694871f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
69490d644c17SKarl Rupp 
6950b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
69514a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6952552f7358SJed Brown   if (ierr) {
6953552f7358SJed Brown     PetscMPIInt    rank;
6954552f7358SJed Brown     PetscErrorCode ierr2;
6955552f7358SJed Brown 
695655b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6957e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6958b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
695971f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
696071f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6961552f7358SJed Brown     CHKERRQ(ierr);
6962552f7358SJed Brown   }
69634a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
69644a1e0b3eSMatthew G. Knepley     PetscInt i;
69654a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
69664a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
69674a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
69684a1e0b3eSMatthew G. Knepley   }
696971f0bbf9SMatthew G. Knepley 
697071f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
697171f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
697271f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
69734acb8e1eSToby Isaac }
697471f0bbf9SMatthew G. Knepley 
69754a1e0b3eSMatthew G. Knepley /*@C
69764a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
69774a1e0b3eSMatthew G. Knepley 
69784a1e0b3eSMatthew G. Knepley   Not collective
69794a1e0b3eSMatthew G. Knepley 
69804a1e0b3eSMatthew G. Knepley   Input Parameters:
69814a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
69824a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
69834a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
69844a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
69854a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
69864a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
69874a1e0b3eSMatthew G. Knepley . A - The matrix
69884a1e0b3eSMatthew G. Knepley . point - The point in the DMs
69894a1e0b3eSMatthew G. Knepley . values - The array of values
69904a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
69914a1e0b3eSMatthew G. Knepley 
69924a1e0b3eSMatthew G. Knepley   Level: intermediate
69934a1e0b3eSMatthew G. Knepley 
69944a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
69954a1e0b3eSMatthew G. Knepley @*/
699671f0bbf9SMatthew 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)
699771f0bbf9SMatthew G. Knepley {
699871f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
699971f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
700071f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
700171f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
700271f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
700371f0bbf9SMatthew G. Knepley 
700471f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
700571f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
700671f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
700771f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
700871f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
700971f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
701071f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
701171f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
701271f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
701371f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
701471f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
701571f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
701671f0bbf9SMatthew G. Knepley 
701771f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
701871f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
701971f0bbf9SMatthew G. Knepley 
702071f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
70214a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
702271f0bbf9SMatthew G. Knepley   if (ierr) {
702371f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
702471f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
702571f0bbf9SMatthew G. Knepley 
702655b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
702771f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
702871f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
702971f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
703071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
703171f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
703271f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
7033d3d1a6afSToby Isaac   }
703471f0bbf9SMatthew G. Knepley 
703571f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
703671f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
703771f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
7038552f7358SJed Brown   PetscFunctionReturn(0);
7039552f7358SJed Brown }
7040552f7358SJed Brown 
7041de41b84cSMatthew 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)
7042de41b84cSMatthew G. Knepley {
7043de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
7044de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
7045de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
7046de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
704717c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7048de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
7049412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
70504ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7051de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
7052de41b84cSMatthew G. Knepley 
7053de41b84cSMatthew G. Knepley   PetscFunctionBegin;
7054de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
7055de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
705692fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
7057de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
705892fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
7059de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7060e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
7061de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7062e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
7063de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
7064de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
7065de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
7066de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7067580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7068580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
7069de41b84cSMatthew G. Knepley   /* Column indices */
7070de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
70714ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
7072de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
7073de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
7074de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
7075de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
7076de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7077de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
7078de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
7079de41b84cSMatthew G. Knepley       ++q;
7080de41b84cSMatthew G. Knepley     }
7081de41b84cSMatthew G. Knepley   }
7082de41b84cSMatthew G. Knepley   numCPoints = q;
7083de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
7084de41b84cSMatthew G. Knepley     PetscInt fdof;
7085de41b84cSMatthew G. Knepley 
7086de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
70874ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7088de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7089de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
7090de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
7091de41b84cSMatthew G. Knepley     }
7092de41b84cSMatthew G. Knepley     numCIndices += dof;
7093de41b84cSMatthew G. Knepley   }
7094de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
7095de41b84cSMatthew G. Knepley   /* Row indices */
7096412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7097412e9a14SMatthew G. Knepley   {
7098412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7099412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7100412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7101412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7102412e9a14SMatthew G. Knepley   }
710369291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7104de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
7105de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
7106de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7107de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
7108de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
7109de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
7110de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
71114ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
71124ca5e9f5SMatthew G. Knepley         if (!dof) continue;
71134ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
71144ca5e9f5SMatthew G. Knepley         if (s < q) continue;
7115de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
7116de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
7117de41b84cSMatthew G. Knepley         ++q;
7118de41b84cSMatthew G. Knepley       }
7119de41b84cSMatthew G. Knepley     }
7120de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7121de41b84cSMatthew G. Knepley   }
7122de41b84cSMatthew G. Knepley   numFPoints = q;
7123de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
7124de41b84cSMatthew G. Knepley     PetscInt fdof;
7125de41b84cSMatthew G. Knepley 
7126de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
71274ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7128de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7129de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
7130de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
7131de41b84cSMatthew G. Knepley     }
7132de41b84cSMatthew G. Knepley     numFIndices += dof;
7133de41b84cSMatthew G. Knepley   }
7134de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
7135de41b84cSMatthew G. Knepley 
71368ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
71378ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
713869291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
713969291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7140de41b84cSMatthew G. Knepley   if (numFields) {
71414acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
71424acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
71434acb8e1eSToby Isaac 
71444acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
71454acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
71464acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7147de41b84cSMatthew G. Knepley     }
71484acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
71494acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
715036fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
71514acb8e1eSToby Isaac     }
71524acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
71534acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
715436fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
71554acb8e1eSToby Isaac     }
71564acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
71574acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
71584acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7159de41b84cSMatthew G. Knepley     }
7160de41b84cSMatthew G. Knepley   } else {
71614acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
71624acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
71634acb8e1eSToby Isaac 
71644acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71654acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
71664acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
71674acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
71684acb8e1eSToby Isaac 
71694acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
717036fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
7171de41b84cSMatthew G. Knepley     }
71724acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
71734acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
71744acb8e1eSToby Isaac 
71754acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
717636fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
7177de41b84cSMatthew G. Knepley     }
71784acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71794acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
7180de41b84cSMatthew G. Knepley   }
7181de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
71824acb8e1eSToby Isaac   /* TODO: flips */
7183de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7184de41b84cSMatthew G. Knepley   if (ierr) {
7185de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
7186de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
7187de41b84cSMatthew G. Knepley 
718855b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
7189e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
7190de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
719169291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
719269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
7193de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
7194de41b84cSMatthew G. Knepley   }
719569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7196de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
719769291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
719869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7199de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
7200de41b84cSMatthew G. Knepley }
7201de41b84cSMatthew G. Knepley 
72027c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
72037c927364SMatthew G. Knepley {
72047c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
72057c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
72067c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
720717c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7208412e9a14SMatthew G. Knepley   DMPolytopeType ct;
72097c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
72107c927364SMatthew G. Knepley   PetscErrorCode ierr;
72117c927364SMatthew G. Knepley 
72127c927364SMatthew G. Knepley   PetscFunctionBegin;
72137c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
72147c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
721592fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
72167c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
721792fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
72187c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7219e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
72207c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7221e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
72227c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
72237c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
72247c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7225580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7226580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
72277c927364SMatthew G. Knepley   /* Column indices */
72287c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
72297c927364SMatthew G. Knepley   maxFPoints = numCPoints;
72307c927364SMatthew G. Knepley   /* Compress out points not in the section */
72317c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
72327c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
72337c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
72347c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
72357c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
72367c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
72377c927364SMatthew G. Knepley       ++q;
72387c927364SMatthew G. Knepley     }
72397c927364SMatthew G. Knepley   }
72407c927364SMatthew G. Knepley   numCPoints = q;
72417c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
72427c927364SMatthew G. Knepley     PetscInt fdof;
72437c927364SMatthew G. Knepley 
72447c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
72457c927364SMatthew G. Knepley     if (!dof) continue;
72467c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
72477c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
72487c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
72497c927364SMatthew G. Knepley     }
72507c927364SMatthew G. Knepley     numCIndices += dof;
72517c927364SMatthew G. Knepley   }
72527c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
72537c927364SMatthew G. Knepley   /* Row indices */
7254412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7255412e9a14SMatthew G. Knepley   {
7256412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7257412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7258412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7259412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7260412e9a14SMatthew G. Knepley   }
726169291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
72627c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
72637c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
72647c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
72657c927364SMatthew G. Knepley     /* Compress out points not in the section */
72667c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
72677c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
72687c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
72697c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
72707c927364SMatthew G. Knepley         if (!dof) continue;
72717c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
72727c927364SMatthew G. Knepley         if (s < q) continue;
72737c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
72747c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
72757c927364SMatthew G. Knepley         ++q;
72767c927364SMatthew G. Knepley       }
72777c927364SMatthew G. Knepley     }
72787c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
72797c927364SMatthew G. Knepley   }
72807c927364SMatthew G. Knepley   numFPoints = q;
72817c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
72827c927364SMatthew G. Knepley     PetscInt fdof;
72837c927364SMatthew G. Knepley 
72847c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
72857c927364SMatthew G. Knepley     if (!dof) continue;
72867c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
72877c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
72887c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
72897c927364SMatthew G. Knepley     }
72907c927364SMatthew G. Knepley     numFIndices += dof;
72917c927364SMatthew G. Knepley   }
72927c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
72937c927364SMatthew G. Knepley 
72948ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
72958ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
72967c927364SMatthew G. Knepley   if (numFields) {
72974acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
72984acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
72994acb8e1eSToby Isaac 
73004acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
73014acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
73024acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
73037c927364SMatthew G. Knepley     }
73044acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
73054acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
730636fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
73074acb8e1eSToby Isaac     }
73084acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
73094acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
731036fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
73114acb8e1eSToby Isaac     }
73124acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
73134acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
73144acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
73157c927364SMatthew G. Knepley     }
73167c927364SMatthew G. Knepley   } else {
73174acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
73184acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
73194acb8e1eSToby Isaac 
73204acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
73214acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
73224acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
73234acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
73244acb8e1eSToby Isaac 
73254acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
732636fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
73277c927364SMatthew G. Knepley     }
73284acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
73294acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
73304acb8e1eSToby Isaac 
73314acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
733236fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
73337c927364SMatthew G. Knepley     }
73344acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
73354acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
73367c927364SMatthew G. Knepley   }
733769291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
73387c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
73397c927364SMatthew G. Knepley   PetscFunctionReturn(0);
73407c927364SMatthew G. Knepley }
73417c927364SMatthew G. Knepley 
73427cd05799SMatthew G. Knepley /*@C
73437cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
73447cd05799SMatthew G. Knepley 
73457cd05799SMatthew G. Knepley   Input Parameter:
73467cd05799SMatthew G. Knepley . dm   - The DMPlex object
73477cd05799SMatthew G. Knepley 
73487cd05799SMatthew G. Knepley   Output Parameter:
73497cd05799SMatthew G. Knepley . cellHeight - The height of a cell
73507cd05799SMatthew G. Knepley 
73517cd05799SMatthew G. Knepley   Level: developer
73527cd05799SMatthew G. Knepley 
73537cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
73547cd05799SMatthew G. Knepley @*/
7355552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7356552f7358SJed Brown {
7357552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7358552f7358SJed Brown 
7359552f7358SJed Brown   PetscFunctionBegin;
7360552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7361552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
7362552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
7363552f7358SJed Brown   PetscFunctionReturn(0);
7364552f7358SJed Brown }
7365552f7358SJed Brown 
73667cd05799SMatthew G. Knepley /*@C
73677cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
73687cd05799SMatthew G. Knepley 
73697cd05799SMatthew G. Knepley   Input Parameters:
73707cd05799SMatthew G. Knepley + dm   - The DMPlex object
73717cd05799SMatthew G. Knepley - cellHeight - The height of a cell
73727cd05799SMatthew G. Knepley 
73737cd05799SMatthew G. Knepley   Level: developer
73747cd05799SMatthew G. Knepley 
73757cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
73767cd05799SMatthew G. Knepley @*/
7377552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7378552f7358SJed Brown {
7379552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7380552f7358SJed Brown 
7381552f7358SJed Brown   PetscFunctionBegin;
7382552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7383552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7384552f7358SJed Brown   PetscFunctionReturn(0);
7385552f7358SJed Brown }
7386552f7358SJed Brown 
7387e6139122SMatthew G. Knepley /*@
7388e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7389e6139122SMatthew G. Knepley 
7390e6139122SMatthew G. Knepley   Input Parameter:
7391e6139122SMatthew G. Knepley . dm - The DMPlex object
7392e6139122SMatthew G. Knepley 
7393e6139122SMatthew G. Knepley   Output Parameters:
73942a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
73952a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7396e6139122SMatthew G. Knepley 
73972a9f31c0SMatthew G. Knepley   Level: advanced
7398e6139122SMatthew G. Knepley 
73998065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7400e6139122SMatthew G. Knepley @*/
7401e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7402e6139122SMatthew G. Knepley {
7403412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7404e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7405e6139122SMatthew G. Knepley 
7406e6139122SMatthew G. Knepley   PetscFunctionBegin;
7407e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7408412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
7409412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
7410e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
7411e6139122SMatthew G. Knepley }
7412e6139122SMatthew G. Knepley 
7413552f7358SJed Brown /* We can easily have a form that takes an IS instead */
74149886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7415552f7358SJed Brown {
7416552f7358SJed Brown   PetscSection   section, globalSection;
7417552f7358SJed Brown   PetscInt      *numbers, p;
7418552f7358SJed Brown   PetscErrorCode ierr;
7419552f7358SJed Brown 
7420552f7358SJed Brown   PetscFunctionBegin;
742182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
7422552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
7423552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7424552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
7425552f7358SJed Brown   }
7426552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
742715b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
7428854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
7429552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7430552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
7431ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7432ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
7433552f7358SJed Brown   }
743482f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
7435ef48cebcSMatthew G. Knepley   if (globalSize) {
7436ef48cebcSMatthew G. Knepley     PetscLayout layout;
7437ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
7438ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
7439ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
7440ef48cebcSMatthew G. Knepley   }
7441552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
7442552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
7443552f7358SJed Brown   PetscFunctionReturn(0);
7444552f7358SJed Brown }
7445552f7358SJed Brown 
744681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7447552f7358SJed Brown {
7448412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
7449552f7358SJed Brown   PetscErrorCode ierr;
7450552f7358SJed Brown 
7451552f7358SJed Brown   PetscFunctionBegin;
7452552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7453412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
7454412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
74559886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
745681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
7457552f7358SJed Brown }
745881ed3555SMatthew G. Knepley 
74598dab3259SMatthew G. Knepley /*@
74607cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
74617cd05799SMatthew G. Knepley 
74627cd05799SMatthew G. Knepley   Input Parameter:
74637cd05799SMatthew G. Knepley . dm   - The DMPlex object
74647cd05799SMatthew G. Knepley 
74657cd05799SMatthew G. Knepley   Output Parameter:
74667cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
74677cd05799SMatthew G. Knepley 
74687cd05799SMatthew G. Knepley   Level: developer
74697cd05799SMatthew G. Knepley 
74707cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
74717cd05799SMatthew G. Knepley @*/
747281ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
747381ed3555SMatthew G. Knepley {
747481ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
747581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
747681ed3555SMatthew G. Knepley 
747781ed3555SMatthew G. Knepley   PetscFunctionBegin;
747881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
747981ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
7480552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
7481552f7358SJed Brown   PetscFunctionReturn(0);
7482552f7358SJed Brown }
7483552f7358SJed Brown 
748481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
748581ed3555SMatthew G. Knepley {
7486412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
748781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
748881ed3555SMatthew G. Knepley 
748981ed3555SMatthew G. Knepley   PetscFunctionBegin;
749081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
749181ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
74929886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
749381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
749481ed3555SMatthew G. Knepley }
749581ed3555SMatthew G. Knepley 
74968dab3259SMatthew G. Knepley /*@
74976aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
74987cd05799SMatthew G. Knepley 
74997cd05799SMatthew G. Knepley   Input Parameter:
75007cd05799SMatthew G. Knepley . dm   - The DMPlex object
75017cd05799SMatthew G. Knepley 
75027cd05799SMatthew G. Knepley   Output Parameter:
75037cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
75047cd05799SMatthew G. Knepley 
75057cd05799SMatthew G. Knepley   Level: developer
75067cd05799SMatthew G. Knepley 
75077cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
75087cd05799SMatthew G. Knepley @*/
7509552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7510552f7358SJed Brown {
7511552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
7512552f7358SJed Brown   PetscErrorCode ierr;
7513552f7358SJed Brown 
7514552f7358SJed Brown   PetscFunctionBegin;
7515552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
751681ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
7517552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
7518552f7358SJed Brown   PetscFunctionReturn(0);
7519552f7358SJed Brown }
7520552f7358SJed Brown 
75218dab3259SMatthew G. Knepley /*@
75227cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
75237cd05799SMatthew G. Knepley 
75247cd05799SMatthew G. Knepley   Input Parameter:
75257cd05799SMatthew G. Knepley . dm   - The DMPlex object
75267cd05799SMatthew G. Knepley 
75277cd05799SMatthew G. Knepley   Output Parameter:
75287cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
75297cd05799SMatthew G. Knepley 
75307cd05799SMatthew G. Knepley   Level: developer
75317cd05799SMatthew G. Knepley 
75327cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
75337cd05799SMatthew G. Knepley @*/
7534ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7535ef48cebcSMatthew G. Knepley {
7536ef48cebcSMatthew G. Knepley   IS             nums[4];
7537862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
7538ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
7539ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
7540ef48cebcSMatthew G. Knepley 
7541ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
7542ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7543ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
75448abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
75458abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
7546862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7547862913ffSStefano Zampini     PetscInt end;
7548862913ffSStefano Zampini 
7549862913ffSStefano Zampini     depths[d] = depth-d;
7550862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
7551862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7552862913ffSStefano Zampini   }
7553862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
7554820f2d46SBarry Smith   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
7555862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7556862913ffSStefano 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]);
7557862913ffSStefano Zampini   }
7558ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
7559ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
7560ef48cebcSMatthew G. Knepley 
7561862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
75629886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
7563ef48cebcSMatthew G. Knepley     shift += gsize;
7564ef48cebcSMatthew G. Knepley   }
7565302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7566ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7567ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7568ef48cebcSMatthew G. Knepley }
7569ef48cebcSMatthew G. Knepley 
757008a22f4bSMatthew G. Knepley /*@
757108a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
757208a22f4bSMatthew G. Knepley 
757308a22f4bSMatthew G. Knepley   Input Parameter:
757408a22f4bSMatthew G. Knepley . dm - The DMPlex object
757508a22f4bSMatthew G. Knepley 
757608a22f4bSMatthew G. Knepley   Output Parameter:
757708a22f4bSMatthew G. Knepley . ranks - The rank field
757808a22f4bSMatthew G. Knepley 
757908a22f4bSMatthew G. Knepley   Options Database Keys:
758008a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
758108a22f4bSMatthew G. Knepley 
758208a22f4bSMatthew G. Knepley   Level: intermediate
758308a22f4bSMatthew G. Knepley 
758408a22f4bSMatthew G. Knepley .seealso: DMView()
758508a22f4bSMatthew G. Knepley @*/
758608a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
758708a22f4bSMatthew G. Knepley {
758808a22f4bSMatthew G. Knepley   DM             rdm;
758908a22f4bSMatthew G. Knepley   PetscFE        fe;
759008a22f4bSMatthew G. Knepley   PetscScalar   *r;
759108a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
7592a55f9a55SMatthew G. Knepley   DMPolytopeType ct;
759308a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
7594a55f9a55SMatthew G. Knepley   PetscBool      simplex;
759508a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
759608a22f4bSMatthew G. Knepley 
759708a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7598f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7599f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
7600ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
760108a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
760208a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7603a55f9a55SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7604a55f9a55SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
7605a55f9a55SMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
7606a55f9a55SMatthew G. Knepley   ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
760708a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7608e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
760908a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7610e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
761108a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
761208a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
761308a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
761408a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
761508a22f4bSMatthew G. Knepley     PetscScalar *lr;
761608a22f4bSMatthew G. Knepley 
761708a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
761871f09efeSPierre Jolivet     if (lr) *lr = rank;
761908a22f4bSMatthew G. Knepley   }
762008a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
762108a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
762208a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
762308a22f4bSMatthew G. Knepley }
762408a22f4bSMatthew G. Knepley 
7625ca8062c8SMatthew G. Knepley /*@
762618e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
762718e14f0cSMatthew G. Knepley 
762818e14f0cSMatthew G. Knepley   Input Parameters:
762918e14f0cSMatthew G. Knepley + dm    - The DMPlex
763018e14f0cSMatthew G. Knepley - label - The DMLabel
763118e14f0cSMatthew G. Knepley 
763218e14f0cSMatthew G. Knepley   Output Parameter:
763318e14f0cSMatthew G. Knepley . val - The label value field
763418e14f0cSMatthew G. Knepley 
763518e14f0cSMatthew G. Knepley   Options Database Keys:
763618e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
763718e14f0cSMatthew G. Knepley 
763818e14f0cSMatthew G. Knepley   Level: intermediate
763918e14f0cSMatthew G. Knepley 
764018e14f0cSMatthew G. Knepley .seealso: DMView()
764118e14f0cSMatthew G. Knepley @*/
764218e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
764318e14f0cSMatthew G. Knepley {
764418e14f0cSMatthew G. Knepley   DM             rdm;
764518e14f0cSMatthew G. Knepley   PetscFE        fe;
764618e14f0cSMatthew G. Knepley   PetscScalar   *v;
764718e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
764818e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
764918e14f0cSMatthew G. Knepley 
765018e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
765118e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
765218e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
765318e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
765418e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
765518e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
765618e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
765718e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7658e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
765918e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7660e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
766118e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
766218e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7663effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
766418e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
766518e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
766618e14f0cSMatthew G. Knepley     PetscScalar *lv;
766718e14f0cSMatthew G. Knepley     PetscInt     cval;
766818e14f0cSMatthew G. Knepley 
766918e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
767018e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
767118e14f0cSMatthew G. Knepley     *lv = cval;
767218e14f0cSMatthew G. Knepley   }
767318e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
767418e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
767518e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
767618e14f0cSMatthew G. Knepley }
767718e14f0cSMatthew G. Knepley 
767818e14f0cSMatthew G. Knepley /*@
7679ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7680ca8062c8SMatthew G. Knepley 
768169916449SMatthew G. Knepley   Input Parameter:
768269916449SMatthew G. Knepley . dm - The DMPlex object
7683ca8062c8SMatthew G. Knepley 
768495eb5ee5SVaclav Hapla   Notes:
768595eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
768695eb5ee5SVaclav Hapla 
768795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7688ca8062c8SMatthew G. Knepley 
7689ca8062c8SMatthew G. Knepley   Level: developer
7690ca8062c8SMatthew G. Knepley 
769195eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7692ca8062c8SMatthew G. Knepley @*/
7693ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7694ca8062c8SMatthew G. Knepley {
7695ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7696ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7697ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
769857beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
769957beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7700ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7701ca8062c8SMatthew G. Knepley 
7702ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7703ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7704412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7705ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7706ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7707ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7708ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7709ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7710ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7711ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7712ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
771342e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
771442e66dfaSMatthew G. Knepley       PetscInt  d;
771542e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
771642e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
771742e66dfaSMatthew G. Knepley       }
7718ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7719ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7720ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7721ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7722ca8062c8SMatthew G. Knepley       }
772342e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
77248ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7725ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
77268ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7727ca8062c8SMatthew G. Knepley         }
7728302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
77298ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7730ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
77318ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7732ca8062c8SMatthew G. Knepley         }
7733302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
77348ccfff9cSToby 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]);
77358ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7736ca8062c8SMatthew G. Knepley       }
773742e66dfaSMatthew G. Knepley     }
773857beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
773957beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7740ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7741ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7742ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7743ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7744ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7745ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
774657beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
774757beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7748ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7749ca8062c8SMatthew G. Knepley       }
7750ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
77518ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7752ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
77538ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7754ca8062c8SMatthew G. Knepley         }
7755302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
77568ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7757ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
77588ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7759ca8062c8SMatthew G. Knepley         }
7760302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
77618ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7762ca8062c8SMatthew G. Knepley       }
7763ca8062c8SMatthew G. Knepley     }
7764ca8062c8SMatthew G. Knepley   }
776557beb4faSStefano Zampini   if (storagecheck) {
7766ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7767ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
77688ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
776957beb4faSStefano Zampini   }
7770ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7771ca8062c8SMatthew G. Knepley }
7772ca8062c8SMatthew G. Knepley 
7773412e9a14SMatthew G. Knepley /*
7774412e9a14SMatthew 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.
7775412e9a14SMatthew G. Knepley */
7776412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7777412e9a14SMatthew G. Knepley {
7778412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7779412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7780412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7781412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7782412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7783412e9a14SMatthew G. Knepley 
7784412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7785412e9a14SMatthew G. Knepley   *unsplit = 0;
7786412e9a14SMatthew G. Knepley   switch (ct) {
7787412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7788412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7789412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7790412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7791412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7792412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7793412e9a14SMatthew G. Knepley       }
7794412e9a14SMatthew G. Knepley       break;
7795412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7796412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7797412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7798412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7799412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7800412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7801412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7802412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7803412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7804412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7805412e9a14SMatthew G. Knepley             PetscInt p;
7806412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7807412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7808412e9a14SMatthew G. Knepley           }
7809412e9a14SMatthew G. Knepley         }
7810412e9a14SMatthew G. Knepley       }
7811412e9a14SMatthew G. Knepley       break;
7812412e9a14SMatthew G. Knepley     default: break;
7813412e9a14SMatthew G. Knepley   }
7814412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7815412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7816412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7817412e9a14SMatthew G. Knepley   }
7818412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7819412e9a14SMatthew G. Knepley }
7820412e9a14SMatthew G. Knepley 
7821ca8062c8SMatthew G. Knepley /*@
7822ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7823ca8062c8SMatthew G. Knepley 
7824ca8062c8SMatthew G. Knepley   Input Parameters:
7825ca8062c8SMatthew G. Knepley + dm - The DMPlex object
782658723a97SMatthew G. Knepley - cellHeight - Normally 0
7827ca8062c8SMatthew G. Knepley 
782895eb5ee5SVaclav Hapla   Notes:
782995eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
783025c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7831ca8062c8SMatthew G. Knepley 
783295eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
783395eb5ee5SVaclav Hapla 
7834ca8062c8SMatthew G. Knepley   Level: developer
7835ca8062c8SMatthew G. Knepley 
783695eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7837ca8062c8SMatthew G. Knepley @*/
783825c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7839ca8062c8SMatthew G. Knepley {
7840412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7841412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7842412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7843ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7844ca8062c8SMatthew G. Knepley 
7845ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7846ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7847412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
784825c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
784958723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7850412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7851412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7852412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
785358723a97SMatthew G. Knepley 
7854412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7855412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7856412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7857412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7858412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7859d4961f80SStefano 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));
7860412e9a14SMatthew G. Knepley     }
786158723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
786258723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
786358723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7864412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
786558723a97SMatthew G. Knepley     }
786658723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7867412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7868412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7869412e9a14SMatthew G. Knepley       PetscInt unsplit;
787042363296SMatthew G. Knepley 
7871412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7872412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
787342363296SMatthew G. Knepley     }
7874d4961f80SStefano 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));
787542363296SMatthew G. Knepley   }
7876ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7877ca8062c8SMatthew G. Knepley }
78789bf0dad6SMatthew G. Knepley 
78799bf0dad6SMatthew G. Knepley /*@
78809bf0dad6SMatthew 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
78819bf0dad6SMatthew G. Knepley 
7882899ea2b8SJacob Faibussowitsch   Not Collective
7883899ea2b8SJacob Faibussowitsch 
78849bf0dad6SMatthew G. Knepley   Input Parameters:
78859bf0dad6SMatthew G. Knepley + dm - The DMPlex object
78869bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
78879bf0dad6SMatthew G. Knepley 
788845da879fSVaclav Hapla   Notes:
788945da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
789045da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
789145da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
789245da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
78939bf0dad6SMatthew G. Knepley 
789495eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
789595eb5ee5SVaclav Hapla 
78969bf0dad6SMatthew G. Knepley   Level: developer
78979bf0dad6SMatthew G. Knepley 
789895eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
78999bf0dad6SMatthew G. Knepley @*/
790025c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
79019bf0dad6SMatthew G. Knepley {
7902ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
79039bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7904899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
79059bf0dad6SMatthew G. Knepley 
79069bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
79079bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7908899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
790945da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
791045da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7911899ea2b8SJacob Faibussowitsch     PetscMPIInt rank;
7912899ea2b8SJacob Faibussowitsch     MPI_Comm    comm;
7913899ea2b8SJacob Faibussowitsch 
7914899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7915ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
791645da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7917899ea2b8SJacob Faibussowitsch   }
7918899ea2b8SJacob Faibussowitsch 
7919c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7920ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
79219bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7922ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
79233554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
79243554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7925412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7926412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7927ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7928412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7929412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
79309bf0dad6SMatthew G. Knepley 
7931ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7932412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7933412e9a14SMatthew G. Knepley       if (unsplit) continue;
79349bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
79359bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
79369bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
79379bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
79389bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
79399bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
79409bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
79419bf0dad6SMatthew G. Knepley       }
7942412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
7943d4961f80SStefano 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);
79449bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
7945d4961f80SStefano Zampini         DMPolytopeType fct;
79469bf0dad6SMatthew G. Knepley         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
79479bf0dad6SMatthew G. Knepley 
7948d4961f80SStefano Zampini         ierr = DMPlexGetCellType(dm, cone[f], &fct);CHKERRQ(ierr);
79499bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
79509bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
79519bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
79529bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
79539bf0dad6SMatthew G. Knepley         }
7954d4961f80SStefano 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]);
79559bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7956d4961f80SStefano 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]);
79579bf0dad6SMatthew G. Knepley         }
79589bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7959412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
79609bf0dad6SMatthew G. Knepley       }
7961412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
79629bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
79639bf0dad6SMatthew G. Knepley     }
79643554e41dSMatthew G. Knepley   }
7965552f7358SJed Brown   PetscFunctionReturn(0);
7966552f7358SJed Brown }
79673913d7c8SMatthew G. Knepley 
7968bb6a34a8SMatthew G. Knepley /*@
7969bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7970bb6a34a8SMatthew G. Knepley 
7971bb6a34a8SMatthew G. Knepley   Input Parameter:
7972bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7973bb6a34a8SMatthew G. Knepley 
797495eb5ee5SVaclav Hapla   Notes:
797595eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
797695eb5ee5SVaclav Hapla 
797795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7978bb6a34a8SMatthew G. Knepley 
7979bb6a34a8SMatthew G. Knepley   Level: developer
7980bb6a34a8SMatthew G. Knepley 
798195eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7982bb6a34a8SMatthew G. Knepley @*/
7983bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7984bb6a34a8SMatthew G. Knepley {
7985a2a9e04cSMatthew G. Knepley   Vec            coordinates;
7986bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7987bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7988412e9a14SMatthew G. Knepley   PetscBool      periodic;
798951a74b61SMatthew G. Knepley   PetscInt       dim, depth, dE, d, cStart, cEnd, c;
7990bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7991bb6a34a8SMatthew G. Knepley 
7992bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7993bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
799451a74b61SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
799551a74b61SMatthew G. Knepley   if (dim != dE) PetscFunctionReturn(0);
7996bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7997412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7998bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7999bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8000a2a9e04cSMatthew G. Knepley   /* Make sure local coordinates are created, because that step is collective */
8001a2a9e04cSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
8002412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
8003412e9a14SMatthew G. Knepley     DMPolytopeType ct;
8004412e9a14SMatthew G. Knepley     PetscInt       unsplit;
8005412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
8006412e9a14SMatthew G. Knepley 
8007412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
8008412e9a14SMatthew G. Knepley     switch (ct) {
8009412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
8010412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
8011412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8012412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
8013412e9a14SMatthew G. Knepley       default: break;
8014412e9a14SMatthew G. Knepley     }
8015412e9a14SMatthew G. Knepley     switch (ct) {
8016412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
8017412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
8018412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8019a2a9e04cSMatthew G. Knepley       case DM_POLYTOPE_PYRAMID:
8020412e9a14SMatthew G. Knepley         continue;
8021412e9a14SMatthew G. Knepley       default: break;
8022412e9a14SMatthew G. Knepley     }
8023412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
8024412e9a14SMatthew G. Knepley     if (unsplit) continue;
8025bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
8026d4961f80SStefano 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);
8027bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
8028412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
8029bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
8030d4961f80SStefano 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);
8031bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
8032bb6a34a8SMatthew G. Knepley     }
8033bb6a34a8SMatthew G. Knepley   }
8034bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
8035bb6a34a8SMatthew G. Knepley }
8036bb6a34a8SMatthew G. Knepley 
803703da9461SVaclav Hapla /*@
8038e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
803903da9461SVaclav Hapla 
804003da9461SVaclav Hapla   Input Parameters:
804103da9461SVaclav Hapla . dm - The DMPlex object
804203da9461SVaclav Hapla 
8043e83a0d2dSVaclav Hapla   Notes:
8044e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
80458918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
804603da9461SVaclav Hapla 
804795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
804895eb5ee5SVaclav Hapla 
804903da9461SVaclav Hapla   Level: developer
805003da9461SVaclav Hapla 
805195eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
805203da9461SVaclav Hapla @*/
805303da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
805403da9461SVaclav Hapla {
8055f0cfc026SVaclav Hapla   PetscSF         pointSF;
8056f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
8057f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
8058f0cfc026SVaclav Hapla   PetscBool       distributed;
805903da9461SVaclav Hapla   PetscErrorCode  ierr;
806003da9461SVaclav Hapla 
806103da9461SVaclav Hapla   PetscFunctionBegin;
806203da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8063f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
8064f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
8065f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
8066f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
8067f0cfc026SVaclav Hapla   if (overlap) {
80688918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
80698918e3e2SVaclav Hapla     PetscFunctionReturn(0);
80708918e3e2SVaclav Hapla   }
8071f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
8072f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
8073f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
8074f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
8075f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
807603da9461SVaclav Hapla 
8077ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
8078f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8079f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8080f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8081f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
8082f5869d18SMatthew G. Knepley 
8083f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
808403da9461SVaclav Hapla   }
8085ece87651SVaclav Hapla 
8086f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8087f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8088f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
8089f5869d18SMatthew G. Knepley     const PetscInt *cone;
8090f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
8091f5869d18SMatthew G. Knepley 
8092f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
8093f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
8094f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
8095f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
8096f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
8097f5869d18SMatthew 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]);
8098f5869d18SMatthew G. Knepley       }
8099f5869d18SMatthew G. Knepley     }
8100ece87651SVaclav Hapla   }
810103da9461SVaclav Hapla   PetscFunctionReturn(0);
810203da9461SVaclav Hapla }
810303da9461SVaclav Hapla 
8104068a5610SStefano Zampini typedef struct cell_stats
8105068a5610SStefano Zampini {
8106068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
8107068a5610SStefano Zampini   PetscInt  count;
8108068a5610SStefano Zampini } cell_stats_t;
8109068a5610SStefano Zampini 
811025befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
8111068a5610SStefano Zampini {
8112068a5610SStefano Zampini   PetscInt i, N = *len;
8113068a5610SStefano Zampini 
8114068a5610SStefano Zampini   for (i = 0; i < N; i++) {
8115068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
8116068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
8117068a5610SStefano Zampini 
8118068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
8119068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
8120068a5610SStefano Zampini     B->sum += A->sum;
8121068a5610SStefano Zampini     B->squaresum += A->squaresum;
8122068a5610SStefano Zampini     B->count += A->count;
8123068a5610SStefano Zampini   }
8124068a5610SStefano Zampini }
8125068a5610SStefano Zampini 
8126068a5610SStefano Zampini /*@
812743fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
8128068a5610SStefano Zampini 
81298261a58bSMatthew G. Knepley   Collective on dm
81308261a58bSMatthew G. Knepley 
8131068a5610SStefano Zampini   Input Parameters:
8132068a5610SStefano Zampini + dm        - The DMPlex object
813343fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
813443fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
8135068a5610SStefano Zampini 
813695eb5ee5SVaclav Hapla   Notes:
813795eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
813895eb5ee5SVaclav Hapla 
813995eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8140068a5610SStefano Zampini 
8141068a5610SStefano Zampini   Level: developer
8142068a5610SStefano Zampini 
8143f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
8144068a5610SStefano Zampini @*/
814543fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8146068a5610SStefano Zampini {
8147068a5610SStefano Zampini   DM             dmCoarse;
814843fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
814943fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
815043fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
815143fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8152412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
815343fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
8154068a5610SStefano Zampini   PetscErrorCode ierr;
8155068a5610SStefano Zampini 
8156068a5610SStefano Zampini   PetscFunctionBegin;
8157068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8158068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
8159068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
8160068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
8161068a5610SStefano Zampini   stats.count = 0;
8162068a5610SStefano Zampini 
8163ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
8164ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
816543fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
816643fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
8167412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
816843fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
8169412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
8170068a5610SStefano Zampini     PetscInt  i;
8171068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
8172068a5610SStefano Zampini 
8173068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
8174068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
817543fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
8176068a5610SStefano Zampini       frobJ    += J[i] * J[i];
8177068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
8178068a5610SStefano Zampini     }
8179068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
8180068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
8181068a5610SStefano Zampini 
8182068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
8183068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
8184068a5610SStefano Zampini     stats.sum       += cond;
8185068a5610SStefano Zampini     stats.squaresum += cond2;
8186068a5610SStefano Zampini     stats.count++;
81878261a58bSMatthew G. Knepley     if (output && cond > limit) {
818843fa8764SMatthew G. Knepley       PetscSection coordSection;
818943fa8764SMatthew G. Knepley       Vec          coordsLocal;
819043fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
819143fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
819243fa8764SMatthew G. Knepley 
819343fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
819443fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
819543fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
8196087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
819743fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
819843fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
819943fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
820043fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
820148afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
820243fa8764SMatthew G. Knepley         }
820343fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
820443fa8764SMatthew G. Knepley       }
820543fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
820643fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
820743fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
820843fa8764SMatthew G. Knepley 
820943fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
821043fa8764SMatthew G. Knepley           PetscReal len;
821143fa8764SMatthew G. Knepley 
821243fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
8213087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
821443fa8764SMatthew G. Knepley         }
821543fa8764SMatthew G. Knepley       }
821643fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
821743fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
821843fa8764SMatthew G. Knepley     }
8219068a5610SStefano Zampini   }
82208261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
8221068a5610SStefano Zampini 
8222068a5610SStefano Zampini   if (size > 1) {
8223068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
8224068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
8225068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
8226068a5610SStefano Zampini     MPI_Op        statReduce;
8227068a5610SStefano Zampini 
8228ffc4695bSBarry Smith     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRMPI(ierr);
8229ffc4695bSBarry Smith     ierr = MPI_Type_commit(&statType);CHKERRMPI(ierr);
8230ffc4695bSBarry Smith     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRMPI(ierr);
8231ffc4695bSBarry Smith     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRMPI(ierr);
8232ffc4695bSBarry Smith     ierr = MPI_Op_free(&statReduce);CHKERRMPI(ierr);
8233ffc4695bSBarry Smith     ierr = MPI_Type_free(&statType);CHKERRMPI(ierr);
8234068a5610SStefano Zampini   } else {
8235580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
8236068a5610SStefano Zampini   }
8237068a5610SStefano Zampini   if (!rank) {
8238068a5610SStefano Zampini     count = globalStats.count;
8239068a5610SStefano Zampini     min   = globalStats.min;
8240068a5610SStefano Zampini     max   = globalStats.max;
8241068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
8242068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
8243068a5610SStefano Zampini   }
8244068a5610SStefano Zampini 
8245068a5610SStefano Zampini   if (output) {
8246068a5610SStefano 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);
8247068a5610SStefano Zampini   }
8248068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
8249068a5610SStefano Zampini 
8250068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
8251068a5610SStefano Zampini   if (dmCoarse) {
8252068a5610SStefano Zampini     PetscBool isplex;
8253068a5610SStefano Zampini 
8254068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
8255068a5610SStefano Zampini     if (isplex) {
825643fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
8257068a5610SStefano Zampini     }
8258068a5610SStefano Zampini   }
8259068a5610SStefano Zampini   PetscFunctionReturn(0);
8260068a5610SStefano Zampini }
8261068a5610SStefano Zampini 
8262f108dbd7SJacob Faibussowitsch /*@
8263f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8264f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
8265f108dbd7SJacob Faibussowitsch 
82666ed19f2fSJacob Faibussowitsch   Collective on dm
8267f108dbd7SJacob Faibussowitsch 
8268f108dbd7SJacob Faibussowitsch   Input Parameters:
8269f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
8270f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
8271f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
8272f108dbd7SJacob Faibussowitsch 
8273f108dbd7SJacob Faibussowitsch   Output Parameters:
8274f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
8275f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
8276f108dbd7SJacob Faibussowitsch 
8277f108dbd7SJacob Faibussowitsch   Options Database Keys:
8278f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
8279f108dbd7SJacob Faibussowitsch supported.
8280f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
8281f108dbd7SJacob Faibussowitsch 
8282f108dbd7SJacob Faibussowitsch   Notes:
8283f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
8284f108dbd7SJacob Faibussowitsch 
8285f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
8286f108dbd7SJacob Faibussowitsch 
8287f108dbd7SJacob 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
8288f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8289f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8290f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
8291f108dbd7SJacob Faibussowitsch 
8292f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
8293f108dbd7SJacob Faibussowitsch 
8294f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
8295f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
8296f108dbd7SJacob Faibussowitsch 
8297f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
8298f108dbd7SJacob Faibussowitsch 
8299f108dbd7SJacob Faibussowitsch   Level: intermediate
8300f108dbd7SJacob Faibussowitsch 
8301f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
8302f108dbd7SJacob Faibussowitsch @*/
8303f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
8304f108dbd7SJacob Faibussowitsch {
83056ed19f2fSJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
83066ed19f2fSJacob Faibussowitsch   PetscInt                *idx;
83076ed19f2fSJacob Faibussowitsch   PetscScalar             *oqVals;
8308f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
83096ed19f2fSJacob Faibussowitsch   PetscReal               *ci, *fi, *Ai;
8310f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
8311f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
8312f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
8313f108dbd7SJacob Faibussowitsch   IS                      glob;
8314f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
8315f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
8316f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
8317f108dbd7SJacob Faibussowitsch 
8318f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
8319f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83206ed19f2fSJacob Faibussowitsch   if (fv) {PetscValidHeaderSpecific(fv, PETSCFV_CLASSID, 2);}
8321f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
83226ed19f2fSJacob 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);
8323f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
8324f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
83256ed19f2fSJacob Faibussowitsch   if (nc < 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
83266ed19f2fSJacob Faibussowitsch   {
83276ed19f2fSJacob Faibussowitsch     DMPlexInterpolatedFlag interpFlag;
83286ed19f2fSJacob Faibussowitsch 
8329f108dbd7SJacob Faibussowitsch     ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
8330f108dbd7SJacob Faibussowitsch     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
8331f108dbd7SJacob Faibussowitsch       PetscMPIInt rank;
8332f108dbd7SJacob Faibussowitsch 
8333ffc4695bSBarry Smith       ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
8334f108dbd7SJacob Faibussowitsch       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
8335f108dbd7SJacob Faibussowitsch     }
83366ed19f2fSJacob Faibussowitsch   }
8337f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8338f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
8339f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
8340f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
83416ed19f2fSJacob Faibussowitsch   } else {*OrthQualLabel = NULL;}
8342f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8343f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8344f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
8345f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
8346f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
8347f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
8348f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
8349f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
8350f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
8351f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
8352f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
8353f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
8354f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
8355f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8356f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8357f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
8358f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
83596ed19f2fSJacob Faibussowitsch   ierr = PetscMalloc5(cEnd-cStart, &idx, cEnd-cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
83606ed19f2fSJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cellIter++,cell++) {
83616ed19f2fSJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
8362f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
8363f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
8364898cd552SSatish Balay     PetscReal          minvalc = 1.0, minvalf = 1.0;
8365f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
8366f108dbd7SJacob Faibussowitsch 
83676ed19f2fSJacob Faibussowitsch     idx[cellIter] = cell-cStart;
8368f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
8369f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
8370f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
8371f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
8372f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8373f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
83746ed19f2fSJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++,cellneigh++) {
83756ed19f2fSJacob Faibussowitsch       PetscInt         i;
83766ed19f2fSJacob Faibussowitsch       const PetscInt   neigh = adj[cellneigh];
8377f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
8378f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
8379f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
8380f108dbd7SJacob Faibussowitsch 
8381f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
8382f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
8383f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
8384f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
83856ed19f2fSJacob Faibussowitsch       {
83866ed19f2fSJacob Faibussowitsch         PetscInt       numcovpts;
83876ed19f2fSJacob Faibussowitsch         const PetscInt *covpts;
83886ed19f2fSJacob Faibussowitsch 
8389f108dbd7SJacob Faibussowitsch         ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8390f108dbd7SJacob Faibussowitsch         ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
8391f108dbd7SJacob Faibussowitsch         ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
83926ed19f2fSJacob Faibussowitsch       }
8393f108dbd7SJacob Faibussowitsch 
8394f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
8395f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8396f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8397f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
8398f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
8399addd1e01SJunchao Zhang         normci += PetscPowReal(ci[i], 2);
8400addd1e01SJunchao Zhang         normfi += PetscPowReal(fi[i], 2);
8401addd1e01SJunchao Zhang         normai += PetscPowReal(Ai[i], 2);
8402f108dbd7SJacob Faibussowitsch       }
8403addd1e01SJunchao Zhang       normci = PetscSqrtReal(normci);
8404addd1e01SJunchao Zhang       normfi = PetscSqrtReal(normfi);
8405addd1e01SJunchao Zhang       normai = PetscSqrtReal(normai);
8406f108dbd7SJacob Faibussowitsch 
8407f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
8408f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8409f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
8410f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
8411f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
8412f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
8413f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8414f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8415f108dbd7SJacob Faibussowitsch       }
8416f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8417f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
8418f108dbd7SJacob Faibussowitsch       }
8419f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8420f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
8421f108dbd7SJacob Faibussowitsch       }
8422f108dbd7SJacob Faibussowitsch     }
8423f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
8424f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
8425f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
84266ed19f2fSJacob Faibussowitsch     oqVals[cellIter] = PetscMin(minvalf, minvalc);
8427f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
84286ed19f2fSJacob Faibussowitsch       if (PetscRealPart(oqVals[cellIter]) <= atol) {ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);}
8429f108dbd7SJacob Faibussowitsch     }
8430f108dbd7SJacob Faibussowitsch   }
84316ed19f2fSJacob Faibussowitsch   ierr = VecSetValuesLocal(*OrthQual, cEnd-cStart, idx, oqVals, INSERT_VALUES);CHKERRQ(ierr);
8432f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
8433f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
8434f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8435f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8436f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
8437f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
84386ed19f2fSJacob Faibussowitsch     if (vwr) {ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);}
8439f108dbd7SJacob Faibussowitsch   }
84406ed19f2fSJacob Faibussowitsch   ierr = PetscFree5(idx, oqVals, ci, fi, Ai);CHKERRQ(ierr);
8441f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
8442f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
8443f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
8444f108dbd7SJacob Faibussowitsch }
8445f108dbd7SJacob Faibussowitsch 
84461eb70e55SToby Isaac /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
84471eb70e55SToby Isaac  * interpolator construction */
84481eb70e55SToby Isaac static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
84491eb70e55SToby Isaac {
84501eb70e55SToby Isaac   PetscSection   section, newSection, gsection;
84511eb70e55SToby Isaac   PetscSF        sf;
84521eb70e55SToby Isaac   PetscBool      hasConstraints, ghasConstraints;
84531eb70e55SToby Isaac   PetscErrorCode ierr;
84541eb70e55SToby Isaac 
84551eb70e55SToby Isaac   PetscFunctionBegin;
84561eb70e55SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84571eb70e55SToby Isaac   PetscValidPointer(odm,2);
84581eb70e55SToby Isaac   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
84591eb70e55SToby Isaac   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
84601eb70e55SToby Isaac   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
84611eb70e55SToby Isaac   if (!ghasConstraints) {
84621eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
84631eb70e55SToby Isaac     *odm = dm;
84641eb70e55SToby Isaac     PetscFunctionReturn(0);
84651eb70e55SToby Isaac   }
84661eb70e55SToby Isaac   ierr = DMClone(dm, odm);CHKERRQ(ierr);
84671eb70e55SToby Isaac   ierr = DMCopyFields(dm, *odm);CHKERRQ(ierr);
84681eb70e55SToby Isaac   ierr = DMGetLocalSection(*odm, &newSection);CHKERRQ(ierr);
84691eb70e55SToby Isaac   ierr = DMGetPointSF(*odm, &sf);CHKERRQ(ierr);
84701eb70e55SToby Isaac   ierr = PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
84711eb70e55SToby Isaac   ierr = DMSetGlobalSection(*odm, gsection);CHKERRQ(ierr);
84721eb70e55SToby Isaac   ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
84731eb70e55SToby Isaac   PetscFunctionReturn(0);
84741eb70e55SToby Isaac }
84751eb70e55SToby Isaac 
84761eb70e55SToby Isaac static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
84771eb70e55SToby Isaac {
84781eb70e55SToby Isaac   DM             dmco, dmfo;
84791eb70e55SToby Isaac   Mat            interpo;
84801eb70e55SToby Isaac   Vec            rscale;
84811eb70e55SToby Isaac   Vec            cglobalo, clocal;
84821eb70e55SToby Isaac   Vec            fglobal, fglobalo, flocal;
84831eb70e55SToby Isaac   PetscBool      regular;
84841eb70e55SToby Isaac   PetscErrorCode ierr;
84851eb70e55SToby Isaac 
84861eb70e55SToby Isaac   PetscFunctionBegin;
84871eb70e55SToby Isaac   ierr = DMGetFullDM(dmc, &dmco);CHKERRQ(ierr);
84881eb70e55SToby Isaac   ierr = DMGetFullDM(dmf, &dmfo);CHKERRQ(ierr);
84891eb70e55SToby Isaac   ierr = DMSetCoarseDM(dmfo, dmco);CHKERRQ(ierr);
84901eb70e55SToby Isaac   ierr = DMPlexGetRegularRefinement(dmf, &regular);CHKERRQ(ierr);
84911eb70e55SToby Isaac   ierr = DMPlexSetRegularRefinement(dmfo, regular);CHKERRQ(ierr);
84921eb70e55SToby Isaac   ierr = DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);CHKERRQ(ierr);
84931eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmco, &cglobalo);CHKERRQ(ierr);
84941eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmc, &clocal);CHKERRQ(ierr);
84951eb70e55SToby Isaac   ierr = VecSet(cglobalo, 0.);CHKERRQ(ierr);
84961eb70e55SToby Isaac   ierr = VecSet(clocal, 0.);CHKERRQ(ierr);
84971eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmf, &fglobal);CHKERRQ(ierr);
84981eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmfo, &fglobalo);CHKERRQ(ierr);
84991eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmf, &flocal);CHKERRQ(ierr);
85001eb70e55SToby Isaac   ierr = VecSet(fglobal, 0.);CHKERRQ(ierr);
85011eb70e55SToby Isaac   ierr = VecSet(fglobalo, 0.);CHKERRQ(ierr);
85021eb70e55SToby Isaac   ierr = VecSet(flocal, 0.);CHKERRQ(ierr);
85031eb70e55SToby Isaac   ierr = DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);CHKERRQ(ierr);
85041eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
85051eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
85061eb70e55SToby Isaac   ierr = MatMult(interpo, cglobalo, fglobalo);CHKERRQ(ierr);
85071eb70e55SToby Isaac   ierr = DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
85081eb70e55SToby Isaac   ierr = DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
85091eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
85101eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
85111eb70e55SToby Isaac   *shift = fglobal;
85121eb70e55SToby Isaac   ierr = VecDestroy(&flocal);CHKERRQ(ierr);
85131eb70e55SToby Isaac   ierr = VecDestroy(&fglobalo);CHKERRQ(ierr);
85141eb70e55SToby Isaac   ierr = VecDestroy(&clocal);CHKERRQ(ierr);
85151eb70e55SToby Isaac   ierr = VecDestroy(&cglobalo);CHKERRQ(ierr);
85161eb70e55SToby Isaac   ierr = VecDestroy(&rscale);CHKERRQ(ierr);
85171eb70e55SToby Isaac   ierr = MatDestroy(&interpo);CHKERRQ(ierr);
85181eb70e55SToby Isaac   ierr = DMDestroy(&dmfo);CHKERRQ(ierr);
85191eb70e55SToby Isaac   ierr = DMDestroy(&dmco);CHKERRQ(ierr);
85201eb70e55SToby Isaac   PetscFunctionReturn(0);
85211eb70e55SToby Isaac }
85221eb70e55SToby Isaac 
85231eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
85241eb70e55SToby Isaac {
85251eb70e55SToby Isaac   PetscObject    shifto;
85261eb70e55SToby Isaac   Vec            shift;
85271eb70e55SToby Isaac 
85281eb70e55SToby Isaac   PetscErrorCode ierr;
85291eb70e55SToby Isaac 
85301eb70e55SToby Isaac   PetscFunctionBegin;
85311eb70e55SToby Isaac   if (!interp) {
85321eb70e55SToby Isaac     Vec rscale;
85331eb70e55SToby Isaac 
85341eb70e55SToby Isaac     ierr = DMCreateInterpolation(coarse, fine, &interp, &rscale);CHKERRQ(ierr);
85351eb70e55SToby Isaac     ierr = VecDestroy(&rscale);CHKERRQ(ierr);
85361eb70e55SToby Isaac   } else {
85371eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)interp);CHKERRQ(ierr);
85381eb70e55SToby Isaac   }
85391eb70e55SToby Isaac   ierr = PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);CHKERRQ(ierr);
85401eb70e55SToby Isaac   if (!shifto) {
85411eb70e55SToby Isaac     ierr = DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);CHKERRQ(ierr);
85421eb70e55SToby Isaac     ierr = PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);CHKERRQ(ierr);
85431eb70e55SToby Isaac     shifto = (PetscObject) shift;
85441eb70e55SToby Isaac     ierr = VecDestroy(&shift);CHKERRQ(ierr);
85451eb70e55SToby Isaac   }
85461eb70e55SToby Isaac   shift = (Vec) shifto;
85471eb70e55SToby Isaac   ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
85481eb70e55SToby Isaac   ierr = VecAXPY(fineSol, 1.0, shift);CHKERRQ(ierr);
85491eb70e55SToby Isaac   ierr = MatDestroy(&interp);CHKERRQ(ierr);
85501eb70e55SToby Isaac   PetscFunctionReturn(0);
85511eb70e55SToby Isaac }
85521eb70e55SToby Isaac 
8553bceba477SMatthew G. Knepley /* Pointwise interpolation
8554bceba477SMatthew G. Knepley      Just code FEM for now
8555bceba477SMatthew G. Knepley      u^f = I u^c
85564ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
85574ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
85584ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
8559bceba477SMatthew G. Knepley */
8560bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8561bceba477SMatthew G. Knepley {
8562bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
8563bceba477SMatthew G. Knepley   PetscInt       m, n;
8564a063dac3SMatthew G. Knepley   void          *ctx;
856568132eb9SMatthew G. Knepley   DM             cdm;
8566cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
8567bceba477SMatthew G. Knepley   PetscErrorCode ierr;
8568bceba477SMatthew G. Knepley 
8569bceba477SMatthew G. Knepley   PetscFunctionBegin;
8570e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8571bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8572e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8573bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
857468132eb9SMatthew G. Knepley 
8575fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
8576bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
8577bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8578fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
8579a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
858068132eb9SMatthew G. Knepley 
8581a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
858268132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8583cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
858468132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
858568132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
85864db47ee9SStefano Zampini   if (scaling) {
85875d1c2e58SMatthew G. Knepley     /* Use naive scaling */
85885d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
85894db47ee9SStefano Zampini   }
8590a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
8591a063dac3SMatthew G. Knepley }
8592bceba477SMatthew G. Knepley 
85936dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8594a063dac3SMatthew G. Knepley {
859590748bafSMatthew G. Knepley   PetscErrorCode ierr;
85966dbf9973SLawrence Mitchell   VecScatter     ctx;
859790748bafSMatthew G. Knepley 
8598a063dac3SMatthew G. Knepley   PetscFunctionBegin;
85996dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
86006dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
86016dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
8602bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
8603bceba477SMatthew G. Knepley }
8604bceba477SMatthew G. Knepley 
86053e9753d6SMatthew G. Knepley static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
86063e9753d6SMatthew G. Knepley                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
86073e9753d6SMatthew G. Knepley                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
86083e9753d6SMatthew G. Knepley                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
86093e9753d6SMatthew G. Knepley {
86103e9753d6SMatthew G. Knepley   g0[0] = 1.0;
86113e9753d6SMatthew G. Knepley }
86123e9753d6SMatthew G. Knepley 
8613bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8614bd041c0cSMatthew G. Knepley {
8615bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
8616bd041c0cSMatthew G. Knepley   PetscInt       m, n;
8617bd041c0cSMatthew G. Knepley   void          *ctx;
8618bd041c0cSMatthew G. Knepley   DM             cdm;
8619bd041c0cSMatthew G. Knepley   PetscBool      regular;
8620bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
8621bd041c0cSMatthew G. Knepley 
8622bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
86233e9753d6SMatthew G. Knepley   if (dmFine == dmCoarse) {
86243e9753d6SMatthew G. Knepley     DM       dmc;
86253e9753d6SMatthew G. Knepley     PetscDS  ds;
86263e9753d6SMatthew G. Knepley     Vec      u;
86273e9753d6SMatthew G. Knepley     IS       cellIS;
862806ad1575SMatthew G. Knepley     PetscFormKey key;
86293e9753d6SMatthew G. Knepley     PetscInt depth;
86303e9753d6SMatthew G. Knepley 
86313e9753d6SMatthew G. Knepley     ierr = DMClone(dmFine, &dmc);CHKERRQ(ierr);
86323e9753d6SMatthew G. Knepley     ierr = DMCopyDisc(dmFine, dmc);CHKERRQ(ierr);
86333e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
86343e9753d6SMatthew G. Knepley     ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
86353e9753d6SMatthew G. Knepley     ierr = DMCreateMatrix(dmc, mass);CHKERRQ(ierr);
86363e9753d6SMatthew G. Knepley     ierr = DMGetGlobalVector(dmc, &u);CHKERRQ(ierr);
86373e9753d6SMatthew G. Knepley     ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
86383e9753d6SMatthew G. Knepley     ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
86393e9753d6SMatthew G. Knepley     ierr = MatZeroEntries(*mass);CHKERRQ(ierr);
86406528b96dSMatthew G. Knepley     key.label = NULL;
86416528b96dSMatthew G. Knepley     key.value = 0;
86426528b96dSMatthew G. Knepley     key.field = 0;
864306ad1575SMatthew G. Knepley     key.part  = 0;
86446528b96dSMatthew G. Knepley     ierr = DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);CHKERRQ(ierr);
86453e9753d6SMatthew G. Knepley     ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
86463e9753d6SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmc, &u);CHKERRQ(ierr);
86473e9753d6SMatthew G. Knepley     ierr = DMDestroy(&dmc);CHKERRQ(ierr);
86483e9753d6SMatthew G. Knepley   } else {
8649e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8650bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8651e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8652bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
8653bd041c0cSMatthew G. Knepley 
8654bd041c0cSMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
8655bd041c0cSMatthew G. Knepley     ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8656bd041c0cSMatthew G. Knepley     ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
8657bd041c0cSMatthew G. Knepley     ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
8658bd041c0cSMatthew G. Knepley 
8659bd041c0cSMatthew G. Knepley     ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
8660bd041c0cSMatthew G. Knepley     ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8661bd041c0cSMatthew G. Knepley     if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8662bd041c0cSMatthew G. Knepley     else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
86633e9753d6SMatthew G. Knepley   }
8664bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
8665bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
8666bd041c0cSMatthew G. Knepley }
8667bd041c0cSMatthew G. Knepley 
86680aef6b92SMatthew G. Knepley /*@
86690aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
86700aef6b92SMatthew G. Knepley 
86710aef6b92SMatthew G. Knepley   Input Parameter:
86720aef6b92SMatthew G. Knepley . dm - The DMPlex object
86730aef6b92SMatthew G. Knepley 
86740aef6b92SMatthew G. Knepley   Output Parameter:
86750aef6b92SMatthew G. Knepley . regular - The flag
86760aef6b92SMatthew G. Knepley 
86770aef6b92SMatthew G. Knepley   Level: intermediate
86780aef6b92SMatthew G. Knepley 
86790aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
86800aef6b92SMatthew G. Knepley @*/
86810aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
86820aef6b92SMatthew G. Knepley {
86830aef6b92SMatthew G. Knepley   PetscFunctionBegin;
86840aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86850aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
86860aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
86870aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
86880aef6b92SMatthew G. Knepley }
86890aef6b92SMatthew G. Knepley 
86900aef6b92SMatthew G. Knepley /*@
86910aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
86920aef6b92SMatthew G. Knepley 
86930aef6b92SMatthew G. Knepley   Input Parameters:
86940aef6b92SMatthew G. Knepley + dm - The DMPlex object
86950aef6b92SMatthew G. Knepley - regular - The flag
86960aef6b92SMatthew G. Knepley 
86970aef6b92SMatthew G. Knepley   Level: intermediate
86980aef6b92SMatthew G. Knepley 
86990aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
87000aef6b92SMatthew G. Knepley @*/
87010aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
87020aef6b92SMatthew G. Knepley {
87030aef6b92SMatthew G. Knepley   PetscFunctionBegin;
87040aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87050aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
87060aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
87070aef6b92SMatthew G. Knepley }
87080aef6b92SMatthew G. Knepley 
8709412e9a14SMatthew G. Knepley /*@
8710412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
8711412e9a14SMatthew G. Knepley 
8712412e9a14SMatthew G. Knepley   Input Parameter:
8713412e9a14SMatthew G. Knepley . dm - The DMPlex object
8714412e9a14SMatthew G. Knepley 
8715412e9a14SMatthew G. Knepley   Output Parameter:
8716412e9a14SMatthew G. Knepley . cr - The strategy number
8717412e9a14SMatthew G. Knepley 
8718412e9a14SMatthew G. Knepley   Level: intermediate
8719412e9a14SMatthew G. Knepley 
8720412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8721412e9a14SMatthew G. Knepley @*/
8722412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8723412e9a14SMatthew G. Knepley {
8724412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8725412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8726412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
8727412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8728412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8729412e9a14SMatthew G. Knepley }
8730412e9a14SMatthew G. Knepley 
8731412e9a14SMatthew G. Knepley /*@
8732412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
8733412e9a14SMatthew G. Knepley 
8734412e9a14SMatthew G. Knepley   Input Parameters:
8735412e9a14SMatthew G. Knepley + dm - The DMPlex object
8736412e9a14SMatthew G. Knepley - cr - The strategy number
8737412e9a14SMatthew G. Knepley 
8738412e9a14SMatthew G. Knepley   Level: intermediate
8739412e9a14SMatthew G. Knepley 
8740412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8741412e9a14SMatthew G. Knepley @*/
8742412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8743412e9a14SMatthew G. Knepley {
8744412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8745412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8746412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
8747412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8748412e9a14SMatthew G. Knepley }
8749412e9a14SMatthew G. Knepley 
8750f7c74593SToby Isaac /* anchors */
8751a68b90caSToby Isaac /*@
8752f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
8753f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
8754a68b90caSToby Isaac 
8755e228b242SToby Isaac   not collective
8756a68b90caSToby Isaac 
8757a68b90caSToby Isaac   Input Parameters:
8758a68b90caSToby Isaac . dm - The DMPlex object
8759a68b90caSToby Isaac 
8760a68b90caSToby Isaac   Output Parameters:
8761a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
8762a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
8763a68b90caSToby Isaac 
8764a68b90caSToby Isaac   Level: intermediate
8765a68b90caSToby Isaac 
8766f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8767a68b90caSToby Isaac @*/
8768a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8769a68b90caSToby Isaac {
8770a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
877141e6d900SToby Isaac   PetscErrorCode ierr;
8772a68b90caSToby Isaac 
8773a68b90caSToby Isaac   PetscFunctionBegin;
8774a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
877541e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
8776a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
8777a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
8778a68b90caSToby Isaac   PetscFunctionReturn(0);
8779a68b90caSToby Isaac }
8780a68b90caSToby Isaac 
8781a68b90caSToby Isaac /*@
8782f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
8783f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
8784a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
8785a68b90caSToby Isaac 
8786a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
8787f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
8788a68b90caSToby Isaac 
8789e228b242SToby Isaac   collective on dm
8790a68b90caSToby Isaac 
8791a68b90caSToby Isaac   Input Parameters:
8792a68b90caSToby Isaac + dm - The DMPlex object
8793e228b242SToby 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).
8794e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
8795a68b90caSToby Isaac 
8796a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
8797a68b90caSToby Isaac 
8798a68b90caSToby Isaac   Level: intermediate
8799a68b90caSToby Isaac 
8800f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8801a68b90caSToby Isaac @*/
8802a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8803a68b90caSToby Isaac {
8804a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8805e228b242SToby Isaac   PetscMPIInt    result;
8806a68b90caSToby Isaac   PetscErrorCode ierr;
8807a68b90caSToby Isaac 
8808a68b90caSToby Isaac   PetscFunctionBegin;
8809a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8810e228b242SToby Isaac   if (anchorSection) {
8811e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
8812ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRMPI(ierr);
8813f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8814e228b242SToby Isaac   }
8815e228b242SToby Isaac   if (anchorIS) {
8816e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
8817ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRMPI(ierr);
8818f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8819e228b242SToby Isaac   }
8820a68b90caSToby Isaac 
8821a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
8822a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
8823a68b90caSToby Isaac   plex->anchorSection = anchorSection;
8824a68b90caSToby Isaac 
8825a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
8826a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
8827a68b90caSToby Isaac   plex->anchorIS = anchorIS;
8828a68b90caSToby Isaac 
8829cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8830a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
8831a68b90caSToby Isaac     const PetscInt *anchors;
8832a68b90caSToby Isaac 
8833a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
8834a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
8835a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
8836a68b90caSToby Isaac     for (a = 0; a < size; a++) {
8837a68b90caSToby Isaac       PetscInt p;
8838a68b90caSToby Isaac 
8839a68b90caSToby Isaac       p = anchors[a];
8840a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
8841a68b90caSToby Isaac         PetscInt dof;
8842a68b90caSToby Isaac 
8843a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8844a68b90caSToby Isaac         if (dof) {
8845a68b90caSToby Isaac           PetscErrorCode ierr2;
8846a68b90caSToby Isaac 
8847a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
88488ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8849a68b90caSToby Isaac         }
8850a68b90caSToby Isaac       }
8851a68b90caSToby Isaac     }
8852a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
8853a68b90caSToby Isaac   }
8854f7c74593SToby Isaac   /* reset the generic constraints */
8855f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
8856a68b90caSToby Isaac   PetscFunctionReturn(0);
8857a68b90caSToby Isaac }
8858a68b90caSToby Isaac 
8859f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8860a68b90caSToby Isaac {
8861f7c74593SToby Isaac   PetscSection anchorSection;
88626995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
8863a68b90caSToby Isaac   PetscErrorCode ierr;
8864a68b90caSToby Isaac 
8865a68b90caSToby Isaac   PetscFunctionBegin;
8866a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8867a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
8868e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
8869a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
88706995de1eSToby Isaac   if (numFields) {
8871719ab38cSToby Isaac     PetscInt f;
8872a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
8873719ab38cSToby Isaac 
8874719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
8875719ab38cSToby Isaac       PetscInt numComp;
8876719ab38cSToby Isaac 
8877719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
8878719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
8879719ab38cSToby Isaac     }
88806995de1eSToby Isaac   }
8881a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
88826995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
88836995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
88846995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
88856995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
8886a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
8887a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
8888a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8889a68b90caSToby Isaac     if (dof) {
8890a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
8891a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
8892a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
8893a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
8894a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
8895a68b90caSToby Isaac       }
8896a68b90caSToby Isaac     }
8897a68b90caSToby Isaac   }
8898a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
8899ae65431dSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *cSec, "Constraint Section");CHKERRQ(ierr);
8900a68b90caSToby Isaac   PetscFunctionReturn(0);
8901a68b90caSToby Isaac }
8902a68b90caSToby Isaac 
8903f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8904a68b90caSToby Isaac {
8905f7c74593SToby Isaac   PetscSection   aSec;
8906ae65431dSMatthew G. Knepley   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
89070ac89760SToby Isaac   const PetscInt *anchors;
89080ac89760SToby Isaac   PetscInt       numFields, f;
890966ad2231SToby Isaac   IS             aIS;
89100ac89760SToby Isaac   PetscErrorCode ierr;
8911e19f7ee6SMark Adams   MatType        mtype;
8912e19f7ee6SMark Adams   PetscBool      iscuda,iskokkos;
89130ac89760SToby Isaac 
89140ac89760SToby Isaac   PetscFunctionBegin;
89150ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89160ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
89170ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
89180ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
89190ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
8920e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);CHKERRQ(ierr);
8921e19f7ee6SMark Adams   if (!iscuda) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);CHKERRQ(ierr); }
8922e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);CHKERRQ(ierr);
8923e19f7ee6SMark Adams   if (!iskokkos) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);CHKERRQ(ierr); }
8924e19f7ee6SMark Adams   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8925e19f7ee6SMark Adams   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8926e19f7ee6SMark Adams   else mtype = MATSEQAIJ;
8927e19f7ee6SMark Adams   ierr = MatSetType(*cMat,mtype);CHKERRQ(ierr);
8928a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
892966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
89306995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
89316995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
8932ae65431dSMatthew G. Knepley   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
89330ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
89340ac89760SToby Isaac   i[0] = 0;
89350ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
89360ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
8937f19733c5SToby Isaac     PetscInt rDof, rOff, r;
8938f19733c5SToby Isaac 
8939f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
8940f19733c5SToby Isaac     if (!rDof) continue;
8941f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89420ac89760SToby Isaac     if (numFields) {
89430ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
89440ac89760SToby Isaac         annz = 0;
8945f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
8946f19733c5SToby Isaac           a = anchors[rOff + r];
8947ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
89480ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
89490ac89760SToby Isaac           annz += aDof;
89500ac89760SToby Isaac         }
89510ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
89520ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
89530ac89760SToby Isaac         for (q = 0; q < dof; q++) {
89540ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
89550ac89760SToby Isaac         }
89560ac89760SToby Isaac       }
89570ac89760SToby Isaac     }
89580ac89760SToby Isaac     else {
89590ac89760SToby Isaac       annz = 0;
8960326b8f31SMatthew G. Knepley       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
89610ac89760SToby Isaac       for (q = 0; q < dof; q++) {
8962ae65431dSMatthew G. Knepley         a = anchors[rOff + q];
8963ae65431dSMatthew G. Knepley         if (a < sStart || a >= sEnd) continue;
89640ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
89650ac89760SToby Isaac         annz += aDof;
89660ac89760SToby Isaac       }
89670ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
89680ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
89690ac89760SToby Isaac       for (q = 0; q < dof; q++) {
89700ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
89710ac89760SToby Isaac       }
89720ac89760SToby Isaac     }
89730ac89760SToby Isaac   }
89740ac89760SToby Isaac   nnz = i[m];
89750ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
89760ac89760SToby Isaac   offset = 0;
89770ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
89780ac89760SToby Isaac     if (numFields) {
89790ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
89800ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
89810ac89760SToby Isaac         for (q = 0; q < dof; q++) {
89820ac89760SToby Isaac           PetscInt rDof, rOff, r;
89830ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
89840ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89850ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
89860ac89760SToby Isaac             PetscInt s;
89870ac89760SToby Isaac 
89880ac89760SToby Isaac             a = anchors[rOff + r];
8989ae65431dSMatthew G. Knepley             if (a < sStart || a >= sEnd) continue;
89900ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
89910ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
89920ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
89930ac89760SToby Isaac               j[offset++] = aOff + s;
89940ac89760SToby Isaac             }
89950ac89760SToby Isaac           }
89960ac89760SToby Isaac         }
89970ac89760SToby Isaac       }
89980ac89760SToby Isaac     }
89990ac89760SToby Isaac     else {
90000ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
90010ac89760SToby Isaac       for (q = 0; q < dof; q++) {
90020ac89760SToby Isaac         PetscInt rDof, rOff, r;
90030ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
90040ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
90050ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
90060ac89760SToby Isaac           PetscInt s;
90070ac89760SToby Isaac 
90080ac89760SToby Isaac           a = anchors[rOff + r];
9009ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
90100ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
90110ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
90120ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
90130ac89760SToby Isaac             j[offset++] = aOff + s;
90140ac89760SToby Isaac           }
90150ac89760SToby Isaac         }
90160ac89760SToby Isaac       }
90170ac89760SToby Isaac     }
90180ac89760SToby Isaac   }
90190ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
902025570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
902125570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
902266ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
90230ac89760SToby Isaac   PetscFunctionReturn(0);
90240ac89760SToby Isaac }
90250ac89760SToby Isaac 
902666ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
902766ad2231SToby Isaac {
9028f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
9029f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
903066ad2231SToby Isaac   Mat            cMat;
903166ad2231SToby Isaac   PetscErrorCode ierr;
903266ad2231SToby Isaac 
903366ad2231SToby Isaac   PetscFunctionBegin;
903466ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9035a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
903666ad2231SToby Isaac   if (anchorSection) {
903744a7f3ddSMatthew G. Knepley     PetscInt Nf;
9038e228b242SToby Isaac 
903992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
9040f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
9041f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
904244a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
904344a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
904466ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
904566ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
904666ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
904766ad2231SToby Isaac   }
904866ad2231SToby Isaac   PetscFunctionReturn(0);
904966ad2231SToby Isaac }
9050a93c429eSMatthew G. Knepley 
9051a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
9052a93c429eSMatthew G. Knepley {
9053a93c429eSMatthew G. Knepley   IS             subis;
9054a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
9055a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
9056a93c429eSMatthew G. Knepley 
9057a93c429eSMatthew G. Knepley   PetscFunctionBegin;
905892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
9059a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
9060a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
9061a93c429eSMatthew G. Knepley   /* Create subdomain */
9062a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
9063a93c429eSMatthew G. Knepley   /* Create submodel */
906497d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
9065a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
906692fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
9067a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
9068e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
9069a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
9070a93c429eSMatthew G. Knepley   if (is) {
9071a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
9072a93c429eSMatthew G. Knepley     IS              spIS;
9073a93c429eSMatthew G. Knepley     const PetscInt *spmap;
9074a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
9075a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
9076a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
9077a93c429eSMatthew G. Knepley 
907897d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
9079a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
9080a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
90816f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90826f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
9083a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
9084a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9085a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
9086a93c429eSMatthew G. Knepley 
9087a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
9088a93c429eSMatthew G. Knepley       if (gdof > 0) {
9089a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9090a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
9091a93c429eSMatthew G. Knepley 
9092a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
9093a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
9094a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
9095a93c429eSMatthew G. Knepley         }
9096a93c429eSMatthew G. Knepley         subSize += pSubSize;
9097a93c429eSMatthew G. Knepley         if (pSubSize) {
9098a93c429eSMatthew G. Knepley           if (bs < 0) {
9099a93c429eSMatthew G. Knepley             bs = pSubSize;
9100a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
9101a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
9102a93c429eSMatthew G. Knepley             bs = 1;
9103a93c429eSMatthew G. Knepley           }
9104a93c429eSMatthew G. Knepley         }
9105a93c429eSMatthew G. Knepley       }
9106a93c429eSMatthew G. Knepley     }
9107a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
9108a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9109a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9110a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9111a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
9112a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
9113a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9114a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
9115a93c429eSMatthew G. Knepley 
9116a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
9117a93c429eSMatthew G. Knepley       if (gdof > 0) {
9118a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
9119a93c429eSMatthew G. Knepley 
9120a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
9121a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9122a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
9123a93c429eSMatthew G. Knepley 
9124a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
9125a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
9126a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
9127a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
9128a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
9129a93c429eSMatthew G. Knepley           }
9130a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
9131a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
9132a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
9133a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
9134a93c429eSMatthew G. Knepley           }
9135a93c429eSMatthew G. Knepley         }
9136a93c429eSMatthew G. Knepley       }
9137a93c429eSMatthew G. Knepley     }
9138a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
9139a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
9140a93c429eSMatthew G. Knepley     if (bs > 1) {
9141a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
9142a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
9143a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
9144a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
9145a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
9146a93c429eSMatthew G. Knepley         }
9147a93c429eSMatthew G. Knepley       }
9148a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
9149a93c429eSMatthew G. Knepley     }
9150a93c429eSMatthew G. Knepley     /* Attach nullspace */
9151a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
9152a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
9153a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
9154a93c429eSMatthew G. Knepley     }
9155a93c429eSMatthew G. Knepley     if (f < Nf) {
9156a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
91578cda7954SMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);CHKERRQ(ierr);
91586823f3c5SBlaise Bourdin 
9159a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
9160a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
9161a93c429eSMatthew G. Knepley     }
9162a93c429eSMatthew G. Knepley   }
9163a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
9164a93c429eSMatthew G. Knepley }
9165c0f0dcc3SMatthew G. Knepley 
9166c0f0dcc3SMatthew G. Knepley /*@
9167c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
9168c0f0dcc3SMatthew G. Knepley 
9169c0f0dcc3SMatthew G. Knepley   Input Parameter:
9170c0f0dcc3SMatthew G. Knepley - dm - The DM
9171c0f0dcc3SMatthew G. Knepley 
9172c0f0dcc3SMatthew G. Knepley   Level: developer
9173c0f0dcc3SMatthew G. Knepley 
9174c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9175c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
9176c0f0dcc3SMatthew G. Knepley 
9177c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
9178c0f0dcc3SMatthew G. Knepley @*/
9179c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
9180c0f0dcc3SMatthew G. Knepley {
9181e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
9182c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
9183c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
9184c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
9185c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
9186c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
9187c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
9188c0f0dcc3SMatthew G. Knepley   const char        *name;
9189c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
9190e5ed2c37SJose E. Roman #endif
9191c0f0dcc3SMatthew G. Knepley 
9192c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9193c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9194c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
9195c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
9196c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9197c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
9198c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
9199c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
9200c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
9201c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
9202c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
9203c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
9204c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
9205fae64647SBarry 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);
9206c0f0dcc3SMatthew G. Knepley #else
9207c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
9208c0f0dcc3SMatthew G. Knepley #endif
9209c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9210c0f0dcc3SMatthew G. Knepley }
9211