xref: /petsc/src/dm/impls/plex/plex.c (revision 77b8e257d517dda0b147809a58fca67b80460076)
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 
1480*77b8e257Sksagiyam /*@
1481*77b8e257Sksagiyam   DMPlexCoordinatesView - Saves DMPlex coordinates into a file
1482*77b8e257Sksagiyam 
1483*77b8e257Sksagiyam   Collective on DM
1484*77b8e257Sksagiyam 
1485*77b8e257Sksagiyam   Input Parameters:
1486*77b8e257Sksagiyam + dm     - The DM whose coordinates are to be saved
1487*77b8e257Sksagiyam - viewer - The PetscViewer for saving
1488*77b8e257Sksagiyam 
1489*77b8e257Sksagiyam   Level: advanced
1490*77b8e257Sksagiyam 
1491*77b8e257Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexLabelsView(), DMPlexCoordinatesLoad()
1492*77b8e257Sksagiyam @*/
1493*77b8e257Sksagiyam PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
1494*77b8e257Sksagiyam {
1495*77b8e257Sksagiyam   PetscBool      ishdf5;
1496*77b8e257Sksagiyam   PetscErrorCode ierr;
1497*77b8e257Sksagiyam 
1498*77b8e257Sksagiyam   PetscFunctionBegin;
1499*77b8e257Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1500*77b8e257Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1501*77b8e257Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1502*77b8e257Sksagiyam   if (ishdf5) {
1503*77b8e257Sksagiyam #if defined(PETSC_HAVE_HDF5)
1504*77b8e257Sksagiyam     PetscViewerFormat format;
1505*77b8e257Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1506*77b8e257Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1507*77b8e257Sksagiyam       ierr = DMPlexCoordinatesView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1508*77b8e257Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1509*77b8e257Sksagiyam #else
1510*77b8e257Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1511*77b8e257Sksagiyam #endif
1512*77b8e257Sksagiyam   }
1513*77b8e257Sksagiyam   PetscFunctionReturn(0);
1514*77b8e257Sksagiyam }
1515*77b8e257Sksagiyam 
15162c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
15172c40f234SMatthew G. Knepley {
1518d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
15192c40f234SMatthew G. Knepley   PetscErrorCode ierr;
15202c40f234SMatthew G. Knepley 
15212c40f234SMatthew G. Knepley   PetscFunctionBegin;
15222c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15232c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
15242c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1525d4f5a9a0SVaclav Hapla   if (ishdf5) {
15262c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
15279c48423bSVaclav Hapla     PetscViewerFormat format;
15289c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
15299c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
15309c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1531509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
153239d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1533509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1534b458e8f1SJose E. Roman     PetscFunctionReturn(0);
15352c40f234SMatthew G. Knepley #else
15362c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1537552f7358SJed Brown #endif
1538b458e8f1SJose E. Roman   } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1539552f7358SJed Brown }
1540552f7358SJed Brown 
1541ea8e1828Sksagiyam /*@
1542ea8e1828Sksagiyam   DMPlexTopologyLoad - Loads a topology into a DMPlex
1543ea8e1828Sksagiyam 
1544ea8e1828Sksagiyam   Collective on DM
1545ea8e1828Sksagiyam 
1546ea8e1828Sksagiyam   Input Parameters:
1547ea8e1828Sksagiyam + dm     - The DM into which the topology is loaded
1548ea8e1828Sksagiyam - viewer - The PetscViewer for the saved topology
1549ea8e1828Sksagiyam 
1550dec9e869Sksagiyam   Output Parameters:
1551dec9e869Sksagiyam . 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
1552dec9e869Sksagiyam 
1553ea8e1828Sksagiyam   Level: advanced
1554ea8e1828Sksagiyam 
1555b08ad5deSksagiyam .seealso: DMLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1556ea8e1828Sksagiyam @*/
1557dec9e869Sksagiyam PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *sf)
1558ea8e1828Sksagiyam {
1559ea8e1828Sksagiyam   PetscBool      ishdf5;
1560ea8e1828Sksagiyam   PetscErrorCode ierr;
1561ea8e1828Sksagiyam 
1562ea8e1828Sksagiyam   PetscFunctionBegin;
1563ea8e1828Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1564ea8e1828Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1565ea8e1828Sksagiyam   PetscValidPointer(sf, 3);
1566ea8e1828Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1567ea8e1828Sksagiyam   if (ishdf5) {
1568ea8e1828Sksagiyam #if defined(PETSC_HAVE_HDF5)
1569ea8e1828Sksagiyam     PetscViewerFormat format;
1570ea8e1828Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1571ea8e1828Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1572dec9e869Sksagiyam       ierr = DMPlexTopologyLoad_HDF5_Internal(dm, viewer, sf);CHKERRQ(ierr);
1573ea8e1828Sksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1574ea8e1828Sksagiyam #else
1575ea8e1828Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1576ea8e1828Sksagiyam #endif
1577ea8e1828Sksagiyam   }
1578ea8e1828Sksagiyam   PetscFunctionReturn(0);
1579ea8e1828Sksagiyam }
1580ea8e1828Sksagiyam 
15813e701f1cSksagiyam /*@
15823e701f1cSksagiyam   DMPlexCoordinatesLoad - Loads coordinates into a DMPlex
15833e701f1cSksagiyam 
15843e701f1cSksagiyam   Collective on DM
15853e701f1cSksagiyam 
15863e701f1cSksagiyam   Input Parameters:
15873e701f1cSksagiyam + dm     - The DM into which the coordinates are loaded
15883e701f1cSksagiyam - viewer - The PetscViewer for the saved coordinates
15893e701f1cSksagiyam 
15903e701f1cSksagiyam   Level: advanced
15913e701f1cSksagiyam 
1592b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
15933e701f1cSksagiyam @*/
15943e701f1cSksagiyam PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer)
15953e701f1cSksagiyam {
15963e701f1cSksagiyam   PetscBool      ishdf5;
15973e701f1cSksagiyam   PetscErrorCode ierr;
15983e701f1cSksagiyam 
15993e701f1cSksagiyam   PetscFunctionBegin;
16003e701f1cSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16013e701f1cSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
16023e701f1cSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
16033e701f1cSksagiyam   if (ishdf5) {
16043e701f1cSksagiyam #if defined(PETSC_HAVE_HDF5)
16053e701f1cSksagiyam     PetscViewerFormat format;
16063e701f1cSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
16073e701f1cSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
16083e701f1cSksagiyam       ierr = DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
16093e701f1cSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
16103e701f1cSksagiyam #else
16113e701f1cSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
16123e701f1cSksagiyam #endif
16133e701f1cSksagiyam   }
16143e701f1cSksagiyam   PetscFunctionReturn(0);
16153e701f1cSksagiyam }
16163e701f1cSksagiyam 
1617b08ad5deSksagiyam /*@
1618b08ad5deSksagiyam   DMPlexLabelsLoad - Loads labels into a DMPlex
1619b08ad5deSksagiyam 
1620b08ad5deSksagiyam   Collective on DM
1621b08ad5deSksagiyam 
1622b08ad5deSksagiyam   Input Parameters:
1623b08ad5deSksagiyam + dm     - The DM into which the labels are loaded
1624b08ad5deSksagiyam - viewer - The PetscViewer for the saved labels
1625b08ad5deSksagiyam 
1626b08ad5deSksagiyam   Level: advanced
1627b08ad5deSksagiyam 
1628b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
1629b08ad5deSksagiyam @*/
1630b08ad5deSksagiyam PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer)
1631b08ad5deSksagiyam {
1632b08ad5deSksagiyam   PetscBool      ishdf5;
1633b08ad5deSksagiyam   PetscErrorCode ierr;
1634b08ad5deSksagiyam 
1635b08ad5deSksagiyam   PetscFunctionBegin;
1636b08ad5deSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1637b08ad5deSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1638b08ad5deSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1639b08ad5deSksagiyam   if (ishdf5) {
1640b08ad5deSksagiyam #if defined(PETSC_HAVE_HDF5)
1641b08ad5deSksagiyam     PetscViewerFormat format;
1642b08ad5deSksagiyam 
1643b08ad5deSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1644b08ad5deSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1645b08ad5deSksagiyam       ierr = DMPlexLabelsLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1646b08ad5deSksagiyam     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1647b08ad5deSksagiyam #else
1648b08ad5deSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1649b08ad5deSksagiyam #endif
1650b08ad5deSksagiyam   }
1651b08ad5deSksagiyam   PetscFunctionReturn(0);
1652b08ad5deSksagiyam }
1653b08ad5deSksagiyam 
1654552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1655552f7358SJed Brown {
1656552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1657552f7358SJed Brown   PetscErrorCode ierr;
1658552f7358SJed Brown 
1659552f7358SJed Brown   PetscFunctionBegin;
16608135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
16618135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
166228d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
16631eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);CHKERRQ(ierr);
16640d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1665552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1666552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1667552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1668552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1669be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1670552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1671552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1672d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1673d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
167477623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1675a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
167697d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
1677552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1678552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1679a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1680a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1681d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1682d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1683d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1684d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1685d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1686d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1687fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
16880a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
1689552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1690552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1691552f7358SJed Brown   PetscFunctionReturn(0);
1692552f7358SJed Brown }
1693552f7358SJed Brown 
1694b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1695552f7358SJed Brown {
16968d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1697acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1698552f7358SJed Brown   PetscInt               localSize;
1699837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1700552f7358SJed Brown   PetscErrorCode         ierr;
1701b412c318SBarry Smith   MatType                mtype;
17021428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1703552f7358SJed Brown 
1704552f7358SJed Brown   PetscFunctionBegin;
1705607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1706b412c318SBarry Smith   mtype = dm->mattype;
1707e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1708552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1709552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
171082f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1711552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1712552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1713552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1714acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1715acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1716552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1717552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1718552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1719552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1720552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1721552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1722552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1723837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1724552f7358SJed Brown   if (!isShell) {
1725be36d101SStefano Zampini     PetscSection subSection;
1726837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
17270be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1728fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1729552f7358SJed Brown 
173000140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1731be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1732be36d101SStefano Zampini       PetscSection section;
1733be36d101SStefano Zampini       PetscInt     size;
1734be36d101SStefano Zampini 
173592fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1736be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1737be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1738be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1739be36d101SStefano Zampini     } else {
174000140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1741be36d101SStefano Zampini     }
1742552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1743be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1744a9d99c84SMatthew G. Knepley       PetscInt bdof;
1745a9d99c84SMatthew G. Knepley 
1746552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1747fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
17481d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
17491d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
17501d17a0a3SMatthew G. Knepley       if (dof) {
17511d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1752be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1753be36d101SStefano Zampini       }
1754be36d101SStefano Zampini       if (isMatIS) {
1755be36d101SStefano Zampini         PetscInt loff,c,off;
1756be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1757be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1758be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1759552f7358SJed Brown       }
17602a28c762SMatthew G Knepley     }
17612a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
17620be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
17630be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
17640be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
17650be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
17666fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
17676fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
17684fa26246SMatthew G. Knepley       PetscInt l;
17696fd5c86aSStefano Zampini 
17706fd5c86aSStefano Zampini       lsize = lsize/bs;
17716fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
17724fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1773be36d101SStefano Zampini     }
1774be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1775be36d101SStefano Zampini     if (isMatIS) {
1776be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1777be36d101SStefano Zampini     }
17781795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
17798d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1780552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1781552f7358SJed Brown   }
1782b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1783552f7358SJed Brown   PetscFunctionReturn(0);
1784552f7358SJed Brown }
1785552f7358SJed Brown 
17867cd05799SMatthew G. Knepley /*@
1787a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1788be36d101SStefano Zampini 
1789be36d101SStefano Zampini   Not collective
1790be36d101SStefano Zampini 
1791be36d101SStefano Zampini   Input Parameter:
1792be36d101SStefano Zampini . mesh - The DMPlex
1793be36d101SStefano Zampini 
1794be36d101SStefano Zampini   Output Parameters:
1795be36d101SStefano Zampini . subsection - The subdomain section
1796be36d101SStefano Zampini 
1797be36d101SStefano Zampini   Level: developer
1798be36d101SStefano Zampini 
1799be36d101SStefano Zampini .seealso:
18007cd05799SMatthew G. Knepley @*/
1801be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1802be36d101SStefano Zampini {
1803be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1804be36d101SStefano Zampini   PetscErrorCode ierr;
1805be36d101SStefano Zampini 
1806be36d101SStefano Zampini   PetscFunctionBegin;
1807be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1808be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1809be36d101SStefano Zampini     PetscSection section;
1810be36d101SStefano Zampini     PetscSF      sf;
1811be36d101SStefano Zampini 
1812be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
181392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1814be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1815be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1816be36d101SStefano Zampini   }
1817be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1818be36d101SStefano Zampini   PetscFunctionReturn(0);
1819be36d101SStefano Zampini }
1820be36d101SStefano Zampini 
1821552f7358SJed Brown /*@
1822552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1823552f7358SJed Brown 
1824552f7358SJed Brown   Not collective
1825552f7358SJed Brown 
1826552f7358SJed Brown   Input Parameter:
1827552f7358SJed Brown . mesh - The DMPlex
1828552f7358SJed Brown 
1829552f7358SJed Brown   Output Parameters:
1830552f7358SJed Brown + pStart - The first mesh point
1831552f7358SJed Brown - pEnd   - The upper bound for mesh points
1832552f7358SJed Brown 
1833552f7358SJed Brown   Level: beginner
1834552f7358SJed Brown 
1835552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1836552f7358SJed Brown @*/
1837552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1838552f7358SJed Brown {
1839552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1840552f7358SJed Brown   PetscErrorCode ierr;
1841552f7358SJed Brown 
1842552f7358SJed Brown   PetscFunctionBegin;
1843552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1844552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1845552f7358SJed Brown   PetscFunctionReturn(0);
1846552f7358SJed Brown }
1847552f7358SJed Brown 
1848552f7358SJed Brown /*@
1849552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1850552f7358SJed Brown 
1851552f7358SJed Brown   Not collective
1852552f7358SJed Brown 
1853552f7358SJed Brown   Input Parameters:
1854552f7358SJed Brown + mesh - The DMPlex
1855552f7358SJed Brown . pStart - The first mesh point
1856552f7358SJed Brown - pEnd   - The upper bound for mesh points
1857552f7358SJed Brown 
1858552f7358SJed Brown   Output Parameters:
1859552f7358SJed Brown 
1860552f7358SJed Brown   Level: beginner
1861552f7358SJed Brown 
1862552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1863552f7358SJed Brown @*/
1864552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1865552f7358SJed Brown {
1866552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1867552f7358SJed Brown   PetscErrorCode ierr;
1868552f7358SJed Brown 
1869552f7358SJed Brown   PetscFunctionBegin;
1870552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1871552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1872552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1873552f7358SJed Brown   PetscFunctionReturn(0);
1874552f7358SJed Brown }
1875552f7358SJed Brown 
1876552f7358SJed Brown /*@
1877eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1878552f7358SJed Brown 
1879552f7358SJed Brown   Not collective
1880552f7358SJed Brown 
1881552f7358SJed Brown   Input Parameters:
1882552f7358SJed Brown + mesh - The DMPlex
1883eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1884552f7358SJed Brown 
1885552f7358SJed Brown   Output Parameter:
1886552f7358SJed Brown . size - The cone size for point p
1887552f7358SJed Brown 
1888552f7358SJed Brown   Level: beginner
1889552f7358SJed Brown 
1890552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1891552f7358SJed Brown @*/
1892552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1893552f7358SJed Brown {
1894552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1895552f7358SJed Brown   PetscErrorCode ierr;
1896552f7358SJed Brown 
1897552f7358SJed Brown   PetscFunctionBegin;
1898552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1899552f7358SJed Brown   PetscValidPointer(size, 3);
1900552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1901552f7358SJed Brown   PetscFunctionReturn(0);
1902552f7358SJed Brown }
1903552f7358SJed Brown 
1904552f7358SJed Brown /*@
1905eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1906552f7358SJed Brown 
1907552f7358SJed Brown   Not collective
1908552f7358SJed Brown 
1909552f7358SJed Brown   Input Parameters:
1910552f7358SJed Brown + mesh - The DMPlex
1911eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1912552f7358SJed Brown - size - The cone size for point p
1913552f7358SJed Brown 
1914552f7358SJed Brown   Output Parameter:
1915552f7358SJed Brown 
1916552f7358SJed Brown   Note:
1917552f7358SJed Brown   This should be called after DMPlexSetChart().
1918552f7358SJed Brown 
1919552f7358SJed Brown   Level: beginner
1920552f7358SJed Brown 
1921552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1922552f7358SJed Brown @*/
1923552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1924552f7358SJed Brown {
1925552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1926552f7358SJed Brown   PetscErrorCode ierr;
1927552f7358SJed Brown 
1928552f7358SJed Brown   PetscFunctionBegin;
1929552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1930552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
19310d644c17SKarl Rupp 
1932552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1933552f7358SJed Brown   PetscFunctionReturn(0);
1934552f7358SJed Brown }
1935552f7358SJed Brown 
1936f5a469b9SMatthew G. Knepley /*@
1937eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1938f5a469b9SMatthew G. Knepley 
1939f5a469b9SMatthew G. Knepley   Not collective
1940f5a469b9SMatthew G. Knepley 
1941f5a469b9SMatthew G. Knepley   Input Parameters:
1942f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1943eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1944f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1945f5a469b9SMatthew G. Knepley 
1946f5a469b9SMatthew G. Knepley   Output Parameter:
1947f5a469b9SMatthew G. Knepley 
1948f5a469b9SMatthew G. Knepley   Note:
1949f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1950f5a469b9SMatthew G. Knepley 
1951f5a469b9SMatthew G. Knepley   Level: beginner
1952f5a469b9SMatthew G. Knepley 
1953f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1954f5a469b9SMatthew G. Knepley @*/
1955f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1956f5a469b9SMatthew G. Knepley {
1957f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1958f5a469b9SMatthew G. Knepley   PetscInt       csize;
1959f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1960f5a469b9SMatthew G. Knepley 
1961f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1962f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1963f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1964f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1965f5a469b9SMatthew G. Knepley 
1966f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1967f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1968f5a469b9SMatthew G. Knepley }
1969f5a469b9SMatthew G. Knepley 
1970552f7358SJed Brown /*@C
1971eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1972552f7358SJed Brown 
1973552f7358SJed Brown   Not collective
1974552f7358SJed Brown 
1975552f7358SJed Brown   Input Parameters:
1976833c876bSVaclav Hapla + dm - The DMPlex
1977eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1978552f7358SJed Brown 
1979552f7358SJed Brown   Output Parameter:
1980552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1981552f7358SJed Brown 
1982552f7358SJed Brown   Level: beginner
1983552f7358SJed Brown 
19843813dfbdSMatthew G Knepley   Fortran Notes:
19853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1987922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1988922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
19893813dfbdSMatthew G Knepley 
1990e45f02b0SMatthew G. Knepley .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1991552f7358SJed Brown @*/
1992552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1993552f7358SJed Brown {
1994552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1995552f7358SJed Brown   PetscInt       off;
1996552f7358SJed Brown   PetscErrorCode ierr;
1997552f7358SJed Brown 
1998552f7358SJed Brown   PetscFunctionBegin;
1999552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2000552f7358SJed Brown   PetscValidPointer(cone, 3);
2001552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2002552f7358SJed Brown   *cone = &mesh->cones[off];
2003552f7358SJed Brown   PetscFunctionReturn(0);
2004552f7358SJed Brown }
2005552f7358SJed Brown 
20060ce7577fSVaclav Hapla /*@C
20070ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
20080ce7577fSVaclav Hapla 
20090ce7577fSVaclav Hapla   Not collective
20100ce7577fSVaclav Hapla 
20110ce7577fSVaclav Hapla   Input Parameters:
20120ce7577fSVaclav Hapla + dm - The DMPlex
20130ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
20140ce7577fSVaclav Hapla 
20150ce7577fSVaclav Hapla   Output Parameter:
20160ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
20170ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
20180ce7577fSVaclav Hapla 
20190ce7577fSVaclav Hapla   Level: intermediate
20200ce7577fSVaclav Hapla 
2021d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
20220ce7577fSVaclav Hapla @*/
20230ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
20240ce7577fSVaclav Hapla {
20250ce7577fSVaclav Hapla   PetscSection        cs, newcs;
20260ce7577fSVaclav Hapla   PetscInt            *cones;
20270ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
20280ce7577fSVaclav Hapla   PetscInt            n;
20290ce7577fSVaclav Hapla   PetscErrorCode      ierr;
20300ce7577fSVaclav Hapla 
20310ce7577fSVaclav Hapla   PetscFunctionBegin;
20320ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
20330ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
20340ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
20350ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
20360ce7577fSVaclav Hapla   if (pCones) {
20370ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
20380ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
20390ce7577fSVaclav Hapla   }
20400ce7577fSVaclav Hapla   PetscFunctionReturn(0);
20410ce7577fSVaclav Hapla }
20420ce7577fSVaclav Hapla 
2043af9eab45SVaclav Hapla /*@
2044af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
2045d4636a37SVaclav Hapla 
2046d4636a37SVaclav Hapla   Not collective
2047d4636a37SVaclav Hapla 
2048d4636a37SVaclav Hapla   Input Parameters:
2049d4636a37SVaclav Hapla + dm - The DMPlex
2050af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2051d4636a37SVaclav Hapla 
2052d4636a37SVaclav Hapla   Output Parameter:
2053af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
2054d4636a37SVaclav Hapla 
2055d4636a37SVaclav Hapla   Level: advanced
2056d4636a37SVaclav Hapla 
2057af9eab45SVaclav Hapla   Notes:
2058af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
2059af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
2060af9eab45SVaclav Hapla 
2061af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
2062d4636a37SVaclav Hapla @*/
2063af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2064d4636a37SVaclav Hapla {
2065af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
2066af9eab45SVaclav Hapla   PetscInt            depth;
2067d4636a37SVaclav Hapla   PetscErrorCode      ierr;
2068d4636a37SVaclav Hapla 
2069d4636a37SVaclav Hapla   PetscFunctionBegin;
2070af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2071af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2072af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
2073af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2074af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
2075af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
2076af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2077af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2078af9eab45SVaclav Hapla }
2079af9eab45SVaclav Hapla 
2080af9eab45SVaclav Hapla /*@
2081af9eab45SVaclav 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).
2082af9eab45SVaclav Hapla 
2083af9eab45SVaclav Hapla   Not collective
2084af9eab45SVaclav Hapla 
2085af9eab45SVaclav Hapla   Input Parameters:
2086af9eab45SVaclav Hapla + dm - The DMPlex
2087af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2088af9eab45SVaclav Hapla 
2089af9eab45SVaclav Hapla   Output Parameter:
2090af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2091af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
2092af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2093af9eab45SVaclav Hapla 
2094af9eab45SVaclav Hapla   Level: advanced
2095af9eab45SVaclav Hapla 
2096af9eab45SVaclav Hapla   Notes:
2097af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
2098af9eab45SVaclav Hapla 
2099af9eab45SVaclav 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.
2100af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
2101af9eab45SVaclav Hapla 
2102af9eab45SVaclav 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:
2103af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
2104af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
2105af9eab45SVaclav Hapla 
2106af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2107af9eab45SVaclav Hapla @*/
2108af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2109af9eab45SVaclav Hapla {
2110af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
2111af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
2112af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
2113af9eab45SVaclav Hapla   IS                  *expandedPoints_;
2114af9eab45SVaclav Hapla   PetscSection        *sections_;
2115af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2116af9eab45SVaclav Hapla 
2117af9eab45SVaclav Hapla   PetscFunctionBegin;
2118af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2119af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2120af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
2121af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
2122af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
2123af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
2124af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
2125af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2126af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
2127af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
2128af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
2129af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
2130af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
2131af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
2132af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2133af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
2134af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
2135af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
2136af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
2137af9eab45SVaclav Hapla       } else {
2138af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
2139af9eab45SVaclav Hapla       }
2140af9eab45SVaclav Hapla     }
2141af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
2142af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
2143af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
2144af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2145af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
2146af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
2147af9eab45SVaclav Hapla       if (cn > 1) {
2148af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
2149af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
2150af9eab45SVaclav Hapla       } else {
2151af9eab45SVaclav Hapla         newarr[co] = arr[i];
2152af9eab45SVaclav Hapla       }
2153af9eab45SVaclav Hapla     }
2154af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
2155af9eab45SVaclav Hapla     arr = newarr;
2156af9eab45SVaclav Hapla     n = newn;
2157af9eab45SVaclav Hapla   }
2158ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
2159af9eab45SVaclav Hapla   *depth = depth_;
2160af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
2161af9eab45SVaclav Hapla   else {
2162af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
2163af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
2164af9eab45SVaclav Hapla   }
2165af9eab45SVaclav Hapla   if (sections) *sections = sections_;
2166af9eab45SVaclav Hapla   else {
2167af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
2168af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
2169af9eab45SVaclav Hapla   }
2170af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2171af9eab45SVaclav Hapla }
2172af9eab45SVaclav Hapla 
2173af9eab45SVaclav Hapla /*@
2174af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
2175af9eab45SVaclav Hapla 
2176af9eab45SVaclav Hapla   Not collective
2177af9eab45SVaclav Hapla 
2178af9eab45SVaclav Hapla   Input Parameters:
2179af9eab45SVaclav Hapla + dm - The DMPlex
2180af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2181af9eab45SVaclav Hapla 
2182af9eab45SVaclav Hapla   Output Parameter:
2183af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2184af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
2185af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2186af9eab45SVaclav Hapla 
2187af9eab45SVaclav Hapla   Level: advanced
2188af9eab45SVaclav Hapla 
2189af9eab45SVaclav Hapla   Notes:
2190af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
2191af9eab45SVaclav Hapla 
2192af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2193af9eab45SVaclav Hapla @*/
2194af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2195af9eab45SVaclav Hapla {
2196af9eab45SVaclav Hapla   PetscInt            d, depth_;
2197af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2198af9eab45SVaclav Hapla 
2199af9eab45SVaclav Hapla   PetscFunctionBegin;
2200af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2201af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
2202af9eab45SVaclav Hapla   if (depth) *depth = 0;
2203af9eab45SVaclav Hapla   if (expandedPoints) {
2204af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
2205af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
2206af9eab45SVaclav Hapla   }
2207af9eab45SVaclav Hapla   if (sections)  {
2208af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
2209af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
2210af9eab45SVaclav Hapla   }
2211d4636a37SVaclav Hapla   PetscFunctionReturn(0);
2212d4636a37SVaclav Hapla }
2213d4636a37SVaclav Hapla 
2214552f7358SJed Brown /*@
221592371b87SBarry 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
2216552f7358SJed Brown 
2217552f7358SJed Brown   Not collective
2218552f7358SJed Brown 
2219552f7358SJed Brown   Input Parameters:
2220552f7358SJed Brown + mesh - The DMPlex
2221eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2222552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
2223552f7358SJed Brown 
2224552f7358SJed Brown   Output Parameter:
2225552f7358SJed Brown 
2226552f7358SJed Brown   Note:
2227552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2228552f7358SJed Brown 
222992371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
223092371b87SBarry Smith 
2231552f7358SJed Brown   Level: beginner
2232552f7358SJed Brown 
223392371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
2234552f7358SJed Brown @*/
2235552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
2236552f7358SJed Brown {
2237552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2238552f7358SJed Brown   PetscInt       pStart, pEnd;
2239552f7358SJed Brown   PetscInt       dof, off, c;
2240552f7358SJed Brown   PetscErrorCode ierr;
2241552f7358SJed Brown 
2242552f7358SJed Brown   PetscFunctionBegin;
2243552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2244552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2245552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2246552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
2247552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
224882f516ccSBarry 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);
2249552f7358SJed Brown   for (c = 0; c < dof; ++c) {
225082f516ccSBarry 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);
2251552f7358SJed Brown     mesh->cones[off+c] = cone[c];
2252552f7358SJed Brown   }
2253552f7358SJed Brown   PetscFunctionReturn(0);
2254552f7358SJed Brown }
2255552f7358SJed Brown 
2256552f7358SJed Brown /*@C
2257eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
2258552f7358SJed Brown 
2259552f7358SJed Brown   Not collective
2260552f7358SJed Brown 
2261552f7358SJed Brown   Input Parameters:
2262552f7358SJed Brown + mesh - The DMPlex
2263eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2264552f7358SJed Brown 
2265552f7358SJed Brown   Output Parameter:
2266552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2267552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2268552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2269552f7358SJed Brown                     the index of the cone point on which to start.
2270552f7358SJed Brown 
2271552f7358SJed Brown   Level: beginner
2272552f7358SJed Brown 
22733813dfbdSMatthew G Knepley   Fortran Notes:
22743813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22753813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22763b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2277922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
22783813dfbdSMatthew G Knepley 
2279552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2280552f7358SJed Brown @*/
2281552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2282552f7358SJed Brown {
2283552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2284552f7358SJed Brown   PetscInt       off;
2285552f7358SJed Brown   PetscErrorCode ierr;
2286552f7358SJed Brown 
2287552f7358SJed Brown   PetscFunctionBegin;
2288552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
228976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2290552f7358SJed Brown     PetscInt dof;
2291552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2292552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2293552f7358SJed Brown   }
2294552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
22950d644c17SKarl Rupp 
2296552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2297552f7358SJed Brown   PetscFunctionReturn(0);
2298552f7358SJed Brown }
2299552f7358SJed Brown 
2300552f7358SJed Brown /*@
2301eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2302552f7358SJed Brown 
2303552f7358SJed Brown   Not collective
2304552f7358SJed Brown 
2305552f7358SJed Brown   Input Parameters:
2306552f7358SJed Brown + mesh - The DMPlex
2307eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2308552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2309552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2310552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2311552f7358SJed Brown                     the index of the cone point on which to start.
2312552f7358SJed Brown 
2313552f7358SJed Brown   Output Parameter:
2314552f7358SJed Brown 
2315552f7358SJed Brown   Note:
2316552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2317552f7358SJed Brown 
2318552f7358SJed Brown   Level: beginner
2319552f7358SJed Brown 
2320552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2321552f7358SJed Brown @*/
2322552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2323552f7358SJed Brown {
2324552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2325552f7358SJed Brown   PetscInt       pStart, pEnd;
2326552f7358SJed Brown   PetscInt       dof, off, c;
2327552f7358SJed Brown   PetscErrorCode ierr;
2328552f7358SJed Brown 
2329552f7358SJed Brown   PetscFunctionBegin;
2330552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2331552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2332552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2333552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2334552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
233582f516ccSBarry 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);
2336552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2337552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2338552f7358SJed Brown 
2339552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
234082f516ccSBarry 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);
2341552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2342552f7358SJed Brown   }
2343552f7358SJed Brown   PetscFunctionReturn(0);
2344552f7358SJed Brown }
2345552f7358SJed Brown 
23467cd05799SMatthew G. Knepley /*@
2347eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
23487cd05799SMatthew G. Knepley 
23497cd05799SMatthew G. Knepley   Not collective
23507cd05799SMatthew G. Knepley 
23517cd05799SMatthew G. Knepley   Input Parameters:
23527cd05799SMatthew G. Knepley + mesh - The DMPlex
2353eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
23547cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
23557cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
23567cd05799SMatthew G. Knepley 
23577cd05799SMatthew G. Knepley   Level: beginner
23587cd05799SMatthew G. Knepley 
23597cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
23607cd05799SMatthew G. Knepley @*/
2361552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2362552f7358SJed Brown {
2363552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2364552f7358SJed Brown   PetscInt       pStart, pEnd;
2365552f7358SJed Brown   PetscInt       dof, off;
2366552f7358SJed Brown   PetscErrorCode ierr;
2367552f7358SJed Brown 
2368552f7358SJed Brown   PetscFunctionBegin;
2369552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2370552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
237182f516ccSBarry 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);
237282f516ccSBarry 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);
237377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
237477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
237577c88f5bSMatthew 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);
2376552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2377552f7358SJed Brown   PetscFunctionReturn(0);
2378552f7358SJed Brown }
2379552f7358SJed Brown 
23807cd05799SMatthew G. Knepley /*@
2381eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
23827cd05799SMatthew G. Knepley 
23837cd05799SMatthew G. Knepley   Not collective
23847cd05799SMatthew G. Knepley 
23857cd05799SMatthew G. Knepley   Input Parameters:
23867cd05799SMatthew G. Knepley + mesh - The DMPlex
2387eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
23887cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
23897cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
23907cd05799SMatthew G. Knepley 
23917cd05799SMatthew G. Knepley   Level: beginner
23927cd05799SMatthew G. Knepley 
23937cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
23947cd05799SMatthew G. Knepley @*/
239577c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
239677c88f5bSMatthew G Knepley {
239777c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
239877c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
239977c88f5bSMatthew G Knepley   PetscInt       dof, off;
240077c88f5bSMatthew G Knepley   PetscErrorCode ierr;
240177c88f5bSMatthew G Knepley 
240277c88f5bSMatthew G Knepley   PetscFunctionBegin;
240377c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
240477c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
240577c88f5bSMatthew 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);
240677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
240777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
240877c88f5bSMatthew 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);
240977c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
241077c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
241177c88f5bSMatthew G Knepley }
241277c88f5bSMatthew G Knepley 
2413552f7358SJed Brown /*@
2414eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2415552f7358SJed Brown 
2416552f7358SJed Brown   Not collective
2417552f7358SJed Brown 
2418552f7358SJed Brown   Input Parameters:
2419552f7358SJed Brown + mesh - The DMPlex
2420eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2421552f7358SJed Brown 
2422552f7358SJed Brown   Output Parameter:
2423552f7358SJed Brown . size - The support size for point p
2424552f7358SJed Brown 
2425552f7358SJed Brown   Level: beginner
2426552f7358SJed Brown 
2427552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2428552f7358SJed Brown @*/
2429552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2430552f7358SJed Brown {
2431552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2432552f7358SJed Brown   PetscErrorCode ierr;
2433552f7358SJed Brown 
2434552f7358SJed Brown   PetscFunctionBegin;
2435552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2436552f7358SJed Brown   PetscValidPointer(size, 3);
2437552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2438552f7358SJed Brown   PetscFunctionReturn(0);
2439552f7358SJed Brown }
2440552f7358SJed Brown 
2441552f7358SJed Brown /*@
2442eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2443552f7358SJed Brown 
2444552f7358SJed Brown   Not collective
2445552f7358SJed Brown 
2446552f7358SJed Brown   Input Parameters:
2447552f7358SJed Brown + mesh - The DMPlex
2448eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2449552f7358SJed Brown - size - The support size for point p
2450552f7358SJed Brown 
2451552f7358SJed Brown   Output Parameter:
2452552f7358SJed Brown 
2453552f7358SJed Brown   Note:
2454552f7358SJed Brown   This should be called after DMPlexSetChart().
2455552f7358SJed Brown 
2456552f7358SJed Brown   Level: beginner
2457552f7358SJed Brown 
2458552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2459552f7358SJed Brown @*/
2460552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2461552f7358SJed Brown {
2462552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2463552f7358SJed Brown   PetscErrorCode ierr;
2464552f7358SJed Brown 
2465552f7358SJed Brown   PetscFunctionBegin;
2466552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2467552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
24680d644c17SKarl Rupp 
2469552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2470552f7358SJed Brown   PetscFunctionReturn(0);
2471552f7358SJed Brown }
2472552f7358SJed Brown 
2473552f7358SJed Brown /*@C
2474eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2475552f7358SJed Brown 
2476552f7358SJed Brown   Not collective
2477552f7358SJed Brown 
2478552f7358SJed Brown   Input Parameters:
2479552f7358SJed Brown + mesh - The DMPlex
2480eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2481552f7358SJed Brown 
2482552f7358SJed Brown   Output Parameter:
2483552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2484552f7358SJed Brown 
2485552f7358SJed Brown   Level: beginner
2486552f7358SJed Brown 
24873813dfbdSMatthew G Knepley   Fortran Notes:
24883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24903b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2491922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
24923813dfbdSMatthew G Knepley 
2493e45f02b0SMatthew G. Knepley .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
2494552f7358SJed Brown @*/
2495552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2496552f7358SJed Brown {
2497552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2498552f7358SJed Brown   PetscInt       off;
2499552f7358SJed Brown   PetscErrorCode ierr;
2500552f7358SJed Brown 
2501552f7358SJed Brown   PetscFunctionBegin;
2502552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2503552f7358SJed Brown   PetscValidPointer(support, 3);
2504552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2505552f7358SJed Brown   *support = &mesh->supports[off];
2506552f7358SJed Brown   PetscFunctionReturn(0);
2507552f7358SJed Brown }
2508552f7358SJed Brown 
2509552f7358SJed Brown /*@
251092371b87SBarry 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
2511552f7358SJed Brown 
2512552f7358SJed Brown   Not collective
2513552f7358SJed Brown 
2514552f7358SJed Brown   Input Parameters:
2515552f7358SJed Brown + mesh - The DMPlex
2516eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
251792371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2518552f7358SJed Brown 
2519552f7358SJed Brown   Output Parameter:
2520552f7358SJed Brown 
2521552f7358SJed Brown   Note:
2522552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2523552f7358SJed Brown 
2524552f7358SJed Brown   Level: beginner
2525552f7358SJed Brown 
252692371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2527552f7358SJed Brown @*/
2528552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2529552f7358SJed Brown {
2530552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2531552f7358SJed Brown   PetscInt       pStart, pEnd;
2532552f7358SJed Brown   PetscInt       dof, off, c;
2533552f7358SJed Brown   PetscErrorCode ierr;
2534552f7358SJed Brown 
2535552f7358SJed Brown   PetscFunctionBegin;
2536552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2537552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2538552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2539552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2540552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
254182f516ccSBarry 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);
2542552f7358SJed Brown   for (c = 0; c < dof; ++c) {
254382f516ccSBarry 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);
2544552f7358SJed Brown     mesh->supports[off+c] = support[c];
2545552f7358SJed Brown   }
2546552f7358SJed Brown   PetscFunctionReturn(0);
2547552f7358SJed Brown }
2548552f7358SJed Brown 
25497cd05799SMatthew G. Knepley /*@
2550eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
25517cd05799SMatthew G. Knepley 
25527cd05799SMatthew G. Knepley   Not collective
25537cd05799SMatthew G. Knepley 
25547cd05799SMatthew G. Knepley   Input Parameters:
25557cd05799SMatthew G. Knepley + mesh - The DMPlex
2556eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
25577cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
25587cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
25597cd05799SMatthew G. Knepley 
25607cd05799SMatthew G. Knepley   Level: beginner
25617cd05799SMatthew G. Knepley 
25627cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
25637cd05799SMatthew G. Knepley @*/
2564552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2565552f7358SJed Brown {
2566552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2567552f7358SJed Brown   PetscInt       pStart, pEnd;
2568552f7358SJed Brown   PetscInt       dof, off;
2569552f7358SJed Brown   PetscErrorCode ierr;
2570552f7358SJed Brown 
2571552f7358SJed Brown   PetscFunctionBegin;
2572552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2573552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2574552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2575552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
257682f516ccSBarry 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);
257782f516ccSBarry 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);
257882f516ccSBarry 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);
2579552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2580552f7358SJed Brown   PetscFunctionReturn(0);
2581552f7358SJed Brown }
2582552f7358SJed Brown 
2583552f7358SJed Brown /*@C
2584eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2585552f7358SJed Brown 
2586552f7358SJed Brown   Not collective
2587552f7358SJed Brown 
2588552f7358SJed Brown   Input Parameters:
2589552f7358SJed Brown + mesh - The DMPlex
2590eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2591552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
25920298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2593552f7358SJed Brown 
2594552f7358SJed Brown   Output Parameters:
2595552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2596552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2597552f7358SJed Brown 
2598552f7358SJed Brown   Note:
25990298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2600552f7358SJed Brown 
26013813dfbdSMatthew G Knepley   Fortran Notes:
26023813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26033813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26043813dfbdSMatthew G Knepley 
26053813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26063813dfbdSMatthew G Knepley 
2607552f7358SJed Brown   Level: beginner
2608552f7358SJed Brown 
2609552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2610552f7358SJed Brown @*/
2611552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2612552f7358SJed Brown {
2613552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2614552f7358SJed Brown   PetscInt       *closure, *fifo;
26150298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2616552f7358SJed Brown   PetscInt        tmpSize, t;
2617552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2618552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2619552f7358SJed Brown   PetscErrorCode  ierr;
2620552f7358SJed Brown 
2621552f7358SJed Brown   PetscFunctionBegin;
2622552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2623552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2624552f7358SJed Brown   /* This is only 1-level */
2625552f7358SJed Brown   if (useCone) {
2626552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2627552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2628552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2629552f7358SJed Brown   } else {
2630552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2631552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2632552f7358SJed Brown   }
2633bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2634bfbcdd7aSMatthew G. Knepley     if (*points) {
2635bfbcdd7aSMatthew G. Knepley       closure = *points;
2636bfbcdd7aSMatthew G. Knepley     } else {
2637bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
263869291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2639bfbcdd7aSMatthew G. Knepley     }
2640bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2641bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2642bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2643bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2644bfbcdd7aSMatthew G. Knepley     }
2645bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2646bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2647bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2648bfbcdd7aSMatthew G. Knepley   }
264924c766afSToby Isaac   {
265024c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
265124c766afSToby Isaac 
265224c766afSToby Isaac     c = mesh->maxConeSize;
265324c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
265424c766afSToby Isaac     s = mesh->maxSupportSize;
265524c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
265624c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
265724c766afSToby Isaac   }
265869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2659bfbcdd7aSMatthew G. Knepley   if (*points) {
2660bfbcdd7aSMatthew G. Knepley     closure = *points;
2661bfbcdd7aSMatthew G. Knepley   } else {
266269291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2663bfbcdd7aSMatthew G. Knepley   }
2664bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2665552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2666552f7358SJed Brown     const PetscInt cp = tmp[t];
2667552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2668552f7358SJed Brown 
2669552f7358SJed Brown     closure[closureSize]   = cp;
2670552f7358SJed Brown     closure[closureSize+1] = co;
2671552f7358SJed Brown     fifo[fifoSize]         = cp;
2672552f7358SJed Brown     fifo[fifoSize+1]       = co;
2673552f7358SJed Brown   }
2674bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2675552f7358SJed Brown   while (fifoSize - fifoStart) {
2676552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2677552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2678552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2679552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2680552f7358SJed Brown 
2681552f7358SJed Brown     if (useCone) {
2682552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2683552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2684552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2685552f7358SJed Brown     } else {
2686552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2687552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
26880298fd71SBarry Smith       tmpO = NULL;
2689552f7358SJed Brown     }
2690552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2691552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2692552f7358SJed Brown       const PetscInt cp = tmp[i];
26932e1b13c2SMatthew 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. */
26942e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
26952e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
26962e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2697552f7358SJed Brown       PetscInt       c;
2698552f7358SJed Brown 
26992e1b13c2SMatthew G. Knepley       if (rev) {
27002e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
27012e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
27022e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
27032e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
27042e1b13c2SMatthew G. Knepley       }
2705552f7358SJed Brown       /* Check for duplicate */
2706552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2707552f7358SJed Brown         if (closure[c] == cp) break;
2708552f7358SJed Brown       }
2709552f7358SJed Brown       if (c == closureSize) {
2710552f7358SJed Brown         closure[closureSize]   = cp;
2711552f7358SJed Brown         closure[closureSize+1] = co;
2712552f7358SJed Brown         fifo[fifoSize]         = cp;
2713552f7358SJed Brown         fifo[fifoSize+1]       = co;
2714552f7358SJed Brown         closureSize           += 2;
2715552f7358SJed Brown         fifoSize              += 2;
2716552f7358SJed Brown       }
2717552f7358SJed Brown     }
2718552f7358SJed Brown     fifoStart += 2;
2719552f7358SJed Brown   }
2720552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2721552f7358SJed Brown   if (points)    *points    = closure;
272269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2723552f7358SJed Brown   PetscFunctionReturn(0);
2724552f7358SJed Brown }
2725552f7358SJed Brown 
27269bf0dad6SMatthew G. Knepley /*@C
2727eaf898f9SPatrick 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
27289bf0dad6SMatthew G. Knepley 
27299bf0dad6SMatthew G. Knepley   Not collective
27309bf0dad6SMatthew G. Knepley 
27319bf0dad6SMatthew G. Knepley   Input Parameters:
27329bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2733eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
27349bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
27359bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
27369bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
27379bf0dad6SMatthew G. Knepley 
27389bf0dad6SMatthew G. Knepley   Output Parameters:
27399bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
27409bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
27419bf0dad6SMatthew G. Knepley 
27429bf0dad6SMatthew G. Knepley   Note:
27439bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
27449bf0dad6SMatthew G. Knepley 
27459bf0dad6SMatthew G. Knepley   Fortran Notes:
27469bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27479bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
27489bf0dad6SMatthew G. Knepley 
27499bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27509bf0dad6SMatthew G. Knepley 
27519bf0dad6SMatthew G. Knepley   Level: beginner
27529bf0dad6SMatthew G. Knepley 
27539bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
27549bf0dad6SMatthew G. Knepley @*/
27559bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
27569bf0dad6SMatthew G. Knepley {
27579bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
27589bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
27599bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
27609bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
27619bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
27629bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
27639bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
27649bf0dad6SMatthew G. Knepley 
27659bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
27669bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27679bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
27689bf0dad6SMatthew G. Knepley   /* This is only 1-level */
27699bf0dad6SMatthew G. Knepley   if (useCone) {
27709bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
27719bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
27729bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
27739bf0dad6SMatthew G. Knepley   } else {
27749bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
27759bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
27769bf0dad6SMatthew G. Knepley   }
27779bf0dad6SMatthew G. Knepley   if (depth == 1) {
27789bf0dad6SMatthew G. Knepley     if (*points) {
27799bf0dad6SMatthew G. Knepley       closure = *points;
27809bf0dad6SMatthew G. Knepley     } else {
27819bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
278269291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
27839bf0dad6SMatthew G. Knepley     }
27849bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
27859bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
27869bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
27879bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
27889bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
27899bf0dad6SMatthew G. Knepley     }
27909bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
27919bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
27929bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
27939bf0dad6SMatthew G. Knepley   }
279424c766afSToby Isaac   {
279524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
279624c766afSToby Isaac 
279724c766afSToby Isaac     c = mesh->maxConeSize;
279824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
279924c766afSToby Isaac     s = mesh->maxSupportSize;
280024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
280124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
280224c766afSToby Isaac   }
280369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
28049bf0dad6SMatthew G. Knepley   if (*points) {
28059bf0dad6SMatthew G. Knepley     closure = *points;
28069bf0dad6SMatthew G. Knepley   } else {
280769291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
28089bf0dad6SMatthew G. Knepley   }
28099bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
28109bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
28119bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
28129bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
28139bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
28149bf0dad6SMatthew G. Knepley 
28159bf0dad6SMatthew G. Knepley     if (ornt < 0) {
28169bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
28179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
281886b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
28199bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
28209bf0dad6SMatthew G. Knepley     }
28219bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
28229bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
28239bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
28249bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
28259bf0dad6SMatthew G. Knepley   }
28269bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
28279bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
28289bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
28299bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
28309bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
28319bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
28329bf0dad6SMatthew G. Knepley 
28339bf0dad6SMatthew G. Knepley     if (useCone) {
28349bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
28359bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
28369bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
28379bf0dad6SMatthew G. Knepley     } else {
28389bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
28399bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
28409bf0dad6SMatthew G. Knepley       tmpO = NULL;
28419bf0dad6SMatthew G. Knepley     }
28429bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
28439bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
28449bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
28459bf0dad6SMatthew 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. */
28469bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
28479bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
28489bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
28499bf0dad6SMatthew G. Knepley       PetscInt       c;
28509bf0dad6SMatthew G. Knepley 
28519bf0dad6SMatthew G. Knepley       if (rev) {
28529bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
28539bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
28549bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
28559bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
28569bf0dad6SMatthew G. Knepley       }
28579bf0dad6SMatthew G. Knepley       /* Check for duplicate */
28589bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
28599bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
28609bf0dad6SMatthew G. Knepley       }
28619bf0dad6SMatthew G. Knepley       if (c == closureSize) {
28629bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
28639bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
28649bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
28659bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
28669bf0dad6SMatthew G. Knepley         closureSize           += 2;
28679bf0dad6SMatthew G. Knepley         fifoSize              += 2;
28689bf0dad6SMatthew G. Knepley       }
28699bf0dad6SMatthew G. Knepley     }
28709bf0dad6SMatthew G. Knepley     fifoStart += 2;
28719bf0dad6SMatthew G. Knepley   }
28729bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
28739bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
287469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
28759bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
28769bf0dad6SMatthew G. Knepley }
28779bf0dad6SMatthew G. Knepley 
2878552f7358SJed Brown /*@C
2879eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2880552f7358SJed Brown 
2881552f7358SJed Brown   Not collective
2882552f7358SJed Brown 
2883552f7358SJed Brown   Input Parameters:
2884552f7358SJed Brown + mesh - The DMPlex
2885eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2886552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2887e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2888e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2889552f7358SJed Brown 
2890552f7358SJed Brown   Note:
28910298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2892552f7358SJed Brown 
28933813dfbdSMatthew G Knepley   Fortran Notes:
28943813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
28953813dfbdSMatthew G Knepley   include petsc.h90 in your code.
28963813dfbdSMatthew G Knepley 
28973813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28983813dfbdSMatthew G Knepley 
2899552f7358SJed Brown   Level: beginner
2900552f7358SJed Brown 
2901552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2902552f7358SJed Brown @*/
2903552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2904552f7358SJed Brown {
2905552f7358SJed Brown   PetscErrorCode ierr;
2906552f7358SJed Brown 
2907552f7358SJed Brown   PetscFunctionBegin;
2908552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2909e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2910e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
291169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
29124ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2913552f7358SJed Brown   PetscFunctionReturn(0);
2914552f7358SJed Brown }
2915552f7358SJed Brown 
2916552f7358SJed Brown /*@
2917eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2918552f7358SJed Brown 
2919552f7358SJed Brown   Not collective
2920552f7358SJed Brown 
2921552f7358SJed Brown   Input Parameter:
2922552f7358SJed Brown . mesh - The DMPlex
2923552f7358SJed Brown 
2924552f7358SJed Brown   Output Parameters:
2925552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2926552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2927552f7358SJed Brown 
2928552f7358SJed Brown   Level: beginner
2929552f7358SJed Brown 
2930552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2931552f7358SJed Brown @*/
2932552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2933552f7358SJed Brown {
2934552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2935552f7358SJed Brown 
2936552f7358SJed Brown   PetscFunctionBegin;
2937552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2938552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2939552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2940552f7358SJed Brown   PetscFunctionReturn(0);
2941552f7358SJed Brown }
2942552f7358SJed Brown 
2943552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2944552f7358SJed Brown {
2945552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2946552f7358SJed Brown   PetscInt       size;
2947552f7358SJed Brown   PetscErrorCode ierr;
2948552f7358SJed Brown 
2949552f7358SJed Brown   PetscFunctionBegin;
2950552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2951552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2952552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
29531795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
29541795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2955ef1259faSMatthew G. Knepley   ierr = PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));CHKERRQ(ierr);
2956552f7358SJed Brown   if (mesh->maxSupportSize) {
2957552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2958552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
29591795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2960ef1259faSMatthew G. Knepley     ierr = PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));CHKERRQ(ierr);
2961552f7358SJed Brown   }
2962552f7358SJed Brown   PetscFunctionReturn(0);
2963552f7358SJed Brown }
2964552f7358SJed Brown 
2965276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2966552f7358SJed Brown {
2967552f7358SJed Brown   PetscErrorCode ierr;
2968552f7358SJed Brown 
2969552f7358SJed Brown   PetscFunctionBegin;
29704d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2971792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2972c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2973736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2974f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2975f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2976f94b4a02SBlaise Bourdin 
29773dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
29783dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2979c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
2980f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2981f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2982f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2983f94b4a02SBlaise Bourdin 
2984f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2985c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2986f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2987f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2988f94b4a02SBlaise Bourdin   }
2989552f7358SJed Brown   PetscFunctionReturn(0);
2990552f7358SJed Brown }
2991552f7358SJed Brown 
29922adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
29932adcc780SMatthew G. Knepley {
29942adcc780SMatthew G. Knepley   PetscErrorCode ierr;
29953dcd263cSBlaise Bourdin   PetscInt       i = 0;
29962adcc780SMatthew G. Knepley 
29972adcc780SMatthew G. Knepley   PetscFunctionBegin;
2998435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2999792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
3000c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
30013dcd263cSBlaise Bourdin   for (i = 0; i < len; i++) {
30023dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
30033dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
30043dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
30053dcd263cSBlaise Bourdin 
30063dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
30073dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
3008c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
300992fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
30103dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
30113dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
30123dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
30133dcd263cSBlaise Bourdin 
30143dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3015c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
30163dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
30173dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
30183dcd263cSBlaise Bourdin       break;
30193dcd263cSBlaise Bourdin     }
30203dcd263cSBlaise Bourdin   }
30212adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
30222adcc780SMatthew G. Knepley }
30232adcc780SMatthew G. Knepley 
3024552f7358SJed Brown /*@
3025eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
3026552f7358SJed Brown 
3027552f7358SJed Brown   Not collective
3028552f7358SJed Brown 
3029552f7358SJed Brown   Input Parameter:
3030552f7358SJed Brown . mesh - The DMPlex
3031552f7358SJed Brown 
3032552f7358SJed Brown   Output Parameter:
3033552f7358SJed Brown 
3034552f7358SJed Brown   Note:
3035552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
3036552f7358SJed Brown 
3037552f7358SJed Brown   Level: beginner
3038552f7358SJed Brown 
3039552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
3040552f7358SJed Brown @*/
3041552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
3042552f7358SJed Brown {
3043552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3044552f7358SJed Brown   PetscInt      *offsets;
3045552f7358SJed Brown   PetscInt       supportSize;
3046552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3047552f7358SJed Brown   PetscErrorCode ierr;
3048552f7358SJed Brown 
3049552f7358SJed Brown   PetscFunctionBegin;
3050552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
305182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
305230b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3053552f7358SJed Brown   /* Calculate support sizes */
3054552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3055552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3056552f7358SJed Brown     PetscInt dof, off, c;
3057552f7358SJed Brown 
3058552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3059552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3060552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3061552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
3062552f7358SJed Brown     }
3063552f7358SJed Brown   }
3064552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3065552f7358SJed Brown     PetscInt dof;
3066552f7358SJed Brown 
3067552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
30680d644c17SKarl Rupp 
3069552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
3070552f7358SJed Brown   }
3071552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
3072552f7358SJed Brown   /* Calculate supports */
3073552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
30741795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
30751795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
3076552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3077552f7358SJed Brown     PetscInt dof, off, c;
3078552f7358SJed Brown 
3079552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3080552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3081552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3082552f7358SJed Brown       const PetscInt q = mesh->cones[c];
3083552f7358SJed Brown       PetscInt       offS;
3084552f7358SJed Brown 
3085552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
30860d644c17SKarl Rupp 
3087552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
3088552f7358SJed Brown       ++offsets[q];
3089552f7358SJed Brown     }
3090552f7358SJed Brown   }
3091552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
309230b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3093552f7358SJed Brown   PetscFunctionReturn(0);
3094552f7358SJed Brown }
3095552f7358SJed Brown 
3096277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3097277ea44aSLisandro Dalcin {
3098277ea44aSLisandro Dalcin   IS             stratumIS;
3099277ea44aSLisandro Dalcin   PetscErrorCode ierr;
3100277ea44aSLisandro Dalcin 
3101277ea44aSLisandro Dalcin   PetscFunctionBegin;
3102277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
310376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
3104277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
3105277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
3106277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
3107277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
3108277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3109277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
3110277ea44aSLisandro Dalcin     }
3111277ea44aSLisandro 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);
3112277ea44aSLisandro Dalcin   }
3113277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
3114277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
3115277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
3116277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
3117277ea44aSLisandro Dalcin }
3118277ea44aSLisandro Dalcin 
3119552f7358SJed Brown /*@
3120a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
31216dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
3122552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
3123552f7358SJed Brown   the DAG.
3124552f7358SJed Brown 
3125bf4602e4SToby Isaac   Collective on dm
3126552f7358SJed Brown 
3127552f7358SJed Brown   Input Parameter:
3128552f7358SJed Brown . mesh - The DMPlex
3129552f7358SJed Brown 
3130552f7358SJed Brown   Output Parameter:
3131552f7358SJed Brown 
3132552f7358SJed Brown   Notes:
3133b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3134b1bb481bSMatthew 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
3135b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
3136c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
3137150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
3138552f7358SJed Brown 
3139b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3140b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3141b1bb481bSMatthew 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
3142b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
3143b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
3144b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
3145b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
3146b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
3147b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
3148b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
3149b1bb481bSMatthew Knepley 
3150150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
3151552f7358SJed Brown 
3152552f7358SJed Brown   Level: beginner
3153552f7358SJed Brown 
3154ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
3155552f7358SJed Brown @*/
3156552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
3157552f7358SJed Brown {
3158df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3159aa50250dSMatthew G. Knepley   DMLabel        label;
3160552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3161552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
3162552f7358SJed Brown   PetscErrorCode ierr;
3163552f7358SJed Brown 
3164552f7358SJed Brown   PetscFunctionBegin;
3165552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3166552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3167277ea44aSLisandro Dalcin 
3168277ea44aSLisandro Dalcin   /* Create depth label */
3169aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3170c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
3171aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3172277ea44aSLisandro Dalcin 
3173277ea44aSLisandro Dalcin   {
3174552f7358SJed Brown     /* Initialize roots and count leaves */
3175277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3176277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3177552f7358SJed Brown     PetscInt coneSize, supportSize;
3178552f7358SJed Brown 
3179277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3180552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3181552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3182552f7358SJed Brown       if (!coneSize && supportSize) {
3183277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3184277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3185552f7358SJed Brown         ++numRoots;
3186552f7358SJed Brown       } else if (!supportSize && coneSize) {
3187552f7358SJed Brown         ++numLeaves;
3188552f7358SJed Brown       } else if (!supportSize && !coneSize) {
3189552f7358SJed Brown         /* Isolated points */
3190277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3191277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3192552f7358SJed Brown       }
3193552f7358SJed Brown     }
3194277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
3195277ea44aSLisandro Dalcin   }
3196277ea44aSLisandro Dalcin 
3197552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
3198277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3199277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3200552f7358SJed Brown     PetscInt coneSize, supportSize;
3201552f7358SJed Brown 
3202277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3203552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3204552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3205552f7358SJed Brown       if (!supportSize && coneSize) {
3206277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3207277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3208552f7358SJed Brown       }
3209552f7358SJed Brown     }
3210277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
3211552f7358SJed Brown   } else {
3212277ea44aSLisandro Dalcin     PetscInt level = 0;
3213277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
3214552f7358SJed Brown 
3215277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3216277ea44aSLisandro Dalcin     while (qEnd > qStart) {
3217277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
3218277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
321974ef644bSMatthew G. Knepley 
3220277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
322174ef644bSMatthew G. Knepley         const PetscInt *support;
322274ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
322374ef644bSMatthew G. Knepley 
3224277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
3225277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
322674ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3227277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
3228277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
3229552f7358SJed Brown         }
3230552f7358SJed Brown       }
3231277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
3232277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
3233277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
323474ef644bSMatthew G. Knepley     }
323574ef644bSMatthew G. Knepley   }
3236bf4602e4SToby Isaac   { /* just in case there is an empty process */
3237bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
3238bf4602e4SToby Isaac 
3239bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
3240ffc4695bSBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
3241bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
3242367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
3243bf4602e4SToby Isaac     }
3244bf4602e4SToby Isaac   }
3245d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
3246552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3247552f7358SJed Brown   PetscFunctionReturn(0);
3248552f7358SJed Brown }
3249552f7358SJed Brown 
3250412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
3251ba2698f1SMatthew G. Knepley {
3252412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3253412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
3254ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3255ba2698f1SMatthew G. Knepley 
3256412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
3257ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3258ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3259ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3260ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
3261ba2698f1SMatthew G. Knepley   if (depth <= 1) {
3262ba2698f1SMatthew G. Knepley     switch (pdepth) {
3263ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
3264ba2698f1SMatthew G. Knepley       case 1:
3265ba2698f1SMatthew G. Knepley         switch (coneSize) {
3266ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3267ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3268ba2698f1SMatthew G. Knepley           case 4:
3269ba2698f1SMatthew G. Knepley           switch (dim) {
3270ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3271ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3272ba2698f1SMatthew G. Knepley             default: break;
3273ba2698f1SMatthew G. Knepley           }
3274ba2698f1SMatthew G. Knepley           break;
3275da9060c4SMatthew G. Knepley         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3276ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3277ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3278ba2698f1SMatthew G. Knepley         default: break;
3279ba2698f1SMatthew G. Knepley       }
3280ba2698f1SMatthew G. Knepley     }
3281ba2698f1SMatthew G. Knepley   } else {
3282ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
3283ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
3284ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
3285ba2698f1SMatthew G. Knepley       switch (dim) {
3286ba2698f1SMatthew G. Knepley         case 1:
3287ba2698f1SMatthew G. Knepley           switch (coneSize) {
3288ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3289ba2698f1SMatthew G. Knepley             default: break;
3290ba2698f1SMatthew G. Knepley           }
3291ba2698f1SMatthew G. Knepley           break;
3292ba2698f1SMatthew G. Knepley         case 2:
3293ba2698f1SMatthew G. Knepley           switch (coneSize) {
3294ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3295ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3296ba2698f1SMatthew G. Knepley             default: break;
3297ba2698f1SMatthew G. Knepley           }
3298ba2698f1SMatthew G. Knepley           break;
3299ba2698f1SMatthew G. Knepley         case 3:
3300ba2698f1SMatthew G. Knepley           switch (coneSize) {
3301ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3302da9060c4SMatthew G. Knepley             case 5:
3303da9060c4SMatthew G. Knepley             {
3304da9060c4SMatthew G. Knepley               const PetscInt *cone;
3305da9060c4SMatthew G. Knepley               PetscInt        faceConeSize;
3306da9060c4SMatthew G. Knepley 
3307da9060c4SMatthew G. Knepley               ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
3308da9060c4SMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[0], &faceConeSize);CHKERRQ(ierr);
3309da9060c4SMatthew G. Knepley               switch (faceConeSize) {
3310da9060c4SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3311da9060c4SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
3312da9060c4SMatthew G. Knepley               }
3313da9060c4SMatthew G. Knepley             }
3314da9060c4SMatthew G. Knepley             break;
3315ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3316ba2698f1SMatthew G. Knepley             default: break;
3317ba2698f1SMatthew G. Knepley           }
3318ba2698f1SMatthew G. Knepley           break;
3319ba2698f1SMatthew G. Knepley         default: break;
3320ba2698f1SMatthew G. Knepley       }
3321ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
3322ba2698f1SMatthew G. Knepley       switch (coneSize) {
3323ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3324ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3325ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3326ba2698f1SMatthew G. Knepley         default: break;
3327ba2698f1SMatthew G. Knepley       }
3328ba2698f1SMatthew G. Knepley     }
3329ba2698f1SMatthew G. Knepley   }
3330412e9a14SMatthew G. Knepley   *pt = ct;
3331412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3332ba2698f1SMatthew G. Knepley }
3333412e9a14SMatthew G. Knepley 
3334412e9a14SMatthew G. Knepley /*@
3335412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3336412e9a14SMatthew G. Knepley 
3337412e9a14SMatthew G. Knepley   Collective on dm
3338412e9a14SMatthew G. Knepley 
3339412e9a14SMatthew G. Knepley   Input Parameter:
3340412e9a14SMatthew G. Knepley . mesh - The DMPlex
3341412e9a14SMatthew G. Knepley 
3342412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3343412e9a14SMatthew G. Knepley 
3344412e9a14SMatthew G. Knepley   Level: developer
3345412e9a14SMatthew G. Knepley 
3346412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3347412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3348412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3349412e9a14SMatthew G. Knepley 
3350412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3351412e9a14SMatthew G. Knepley @*/
3352412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3353412e9a14SMatthew G. Knepley {
3354412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3355412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3356412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3357412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3358412e9a14SMatthew G. Knepley 
3359412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3360412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3361412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3362412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3363412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3364412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3365412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3366327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3367412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3368412e9a14SMatthew G. Knepley 
3369412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3370412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3371412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3372412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3373412e9a14SMatthew G. Knepley   }
3374412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3375412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3376ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3377ba2698f1SMatthew G. Knepley }
3378ba2698f1SMatthew G. Knepley 
3379552f7358SJed Brown /*@C
3380552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3381552f7358SJed Brown 
3382552f7358SJed Brown   Not Collective
3383552f7358SJed Brown 
3384552f7358SJed Brown   Input Parameters:
3385552f7358SJed Brown + dm - The DMPlex object
3386552f7358SJed Brown . numPoints - The number of input points for the join
3387552f7358SJed Brown - points - The input points
3388552f7358SJed Brown 
3389552f7358SJed Brown   Output Parameters:
3390552f7358SJed Brown + numCoveredPoints - The number of points in the join
3391552f7358SJed Brown - coveredPoints - The points in the join
3392552f7358SJed Brown 
3393552f7358SJed Brown   Level: intermediate
3394552f7358SJed Brown 
3395552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3396552f7358SJed Brown 
33973813dfbdSMatthew G Knepley   Fortran Notes:
33983813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33993813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34003813dfbdSMatthew G Knepley 
34013813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34023813dfbdSMatthew G Knepley 
3403552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3404552f7358SJed Brown @*/
3405552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3406552f7358SJed Brown {
3407552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3408552f7358SJed Brown   PetscInt      *join[2];
3409552f7358SJed Brown   PetscInt       joinSize, i = 0;
3410552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3411552f7358SJed Brown   PetscErrorCode ierr;
3412552f7358SJed Brown 
3413552f7358SJed Brown   PetscFunctionBegin;
3414552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
341548bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
341648bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
341748bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
341869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
341969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3420552f7358SJed Brown   /* Copy in support of first point */
3421552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3422552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3423552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3424552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3425552f7358SJed Brown   }
3426552f7358SJed Brown   /* Check each successive support */
3427552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3428552f7358SJed Brown     PetscInt newJoinSize = 0;
3429552f7358SJed Brown 
3430552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3431552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3432552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3433552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3434552f7358SJed Brown 
3435552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3436552f7358SJed Brown         if (point == join[i][m]) {
3437552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3438552f7358SJed Brown           break;
3439552f7358SJed Brown         }
3440552f7358SJed Brown       }
3441552f7358SJed Brown     }
3442552f7358SJed Brown     joinSize = newJoinSize;
3443552f7358SJed Brown     i        = 1-i;
3444552f7358SJed Brown   }
3445552f7358SJed Brown   *numCoveredPoints = joinSize;
3446552f7358SJed Brown   *coveredPoints    = join[i];
344769291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3448552f7358SJed Brown   PetscFunctionReturn(0);
3449552f7358SJed Brown }
3450552f7358SJed Brown 
3451552f7358SJed Brown /*@C
3452552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3453552f7358SJed Brown 
3454552f7358SJed Brown   Not Collective
3455552f7358SJed Brown 
3456552f7358SJed Brown   Input Parameters:
3457552f7358SJed Brown + dm - The DMPlex object
3458552f7358SJed Brown . numPoints - The number of input points for the join
3459552f7358SJed Brown - points - The input points
3460552f7358SJed Brown 
3461552f7358SJed Brown   Output Parameters:
3462552f7358SJed Brown + numCoveredPoints - The number of points in the join
3463552f7358SJed Brown - coveredPoints - The points in the join
3464552f7358SJed Brown 
34653813dfbdSMatthew G Knepley   Fortran Notes:
34663813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34673813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34683813dfbdSMatthew G Knepley 
34693813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34703813dfbdSMatthew G Knepley 
3471552f7358SJed Brown   Level: intermediate
3472552f7358SJed Brown 
3473552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3474552f7358SJed Brown @*/
3475552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3476552f7358SJed Brown {
3477552f7358SJed Brown   PetscErrorCode ierr;
3478552f7358SJed Brown 
3479552f7358SJed Brown   PetscFunctionBegin;
3480552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3481d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3482d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3483d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
348469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3485d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3486552f7358SJed Brown   PetscFunctionReturn(0);
3487552f7358SJed Brown }
3488552f7358SJed Brown 
3489552f7358SJed Brown /*@C
3490552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3491552f7358SJed Brown 
3492552f7358SJed Brown   Not Collective
3493552f7358SJed Brown 
3494552f7358SJed Brown   Input Parameters:
3495552f7358SJed Brown + dm - The DMPlex object
3496552f7358SJed Brown . numPoints - The number of input points for the join
3497552f7358SJed Brown - points - The input points
3498552f7358SJed Brown 
3499552f7358SJed Brown   Output Parameters:
3500552f7358SJed Brown + numCoveredPoints - The number of points in the join
3501552f7358SJed Brown - coveredPoints - The points in the join
3502552f7358SJed Brown 
35033813dfbdSMatthew G Knepley   Fortran Notes:
35043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
35053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
35063813dfbdSMatthew G Knepley 
35073813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35083813dfbdSMatthew G Knepley 
3509552f7358SJed Brown   Level: intermediate
3510552f7358SJed Brown 
3511552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3512552f7358SJed Brown @*/
3513552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3514552f7358SJed Brown {
3515552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3516552f7358SJed Brown   PetscInt      *offsets, **closures;
3517552f7358SJed Brown   PetscInt      *join[2];
3518552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
351924c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3520552f7358SJed Brown   PetscErrorCode ierr;
3521552f7358SJed Brown 
3522552f7358SJed Brown   PetscFunctionBegin;
3523552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
352448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
352548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
352648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3527552f7358SJed Brown 
3528552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
35291795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
353069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
353124c766afSToby Isaac   ms      = mesh->maxSupportSize;
353224c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
353369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
353469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3535552f7358SJed Brown 
3536552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3537552f7358SJed Brown     PetscInt closureSize;
3538552f7358SJed Brown 
3539552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
35400d644c17SKarl Rupp 
3541552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3542552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3543552f7358SJed Brown       PetscInt pStart, pEnd, i;
3544552f7358SJed Brown 
3545552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3546552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3547552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3548552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3549552f7358SJed Brown           break;
3550552f7358SJed Brown         }
3551552f7358SJed Brown       }
3552552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3553552f7358SJed Brown     }
355482f516ccSBarry 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);
3555552f7358SJed Brown   }
3556552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3557552f7358SJed Brown     PetscInt dof;
3558552f7358SJed Brown 
3559552f7358SJed Brown     /* Copy in support of first point */
3560552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3561552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3562552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3563552f7358SJed Brown     }
3564552f7358SJed Brown     /* Check each successive cone */
3565552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3566552f7358SJed Brown       PetscInt newJoinSize = 0;
3567552f7358SJed Brown 
3568552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3569552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3570552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3571552f7358SJed Brown 
3572552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3573552f7358SJed Brown           if (point == join[i][m]) {
3574552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3575552f7358SJed Brown             break;
3576552f7358SJed Brown           }
3577552f7358SJed Brown         }
3578552f7358SJed Brown       }
3579552f7358SJed Brown       joinSize = newJoinSize;
3580552f7358SJed Brown       i        = 1-i;
3581552f7358SJed Brown     }
3582552f7358SJed Brown     if (joinSize) break;
3583552f7358SJed Brown   }
3584552f7358SJed Brown   *numCoveredPoints = joinSize;
3585552f7358SJed Brown   *coveredPoints    = join[i];
3586552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
35870298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3588552f7358SJed Brown   }
3589552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
359069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
359169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3592552f7358SJed Brown   PetscFunctionReturn(0);
3593552f7358SJed Brown }
3594552f7358SJed Brown 
3595552f7358SJed Brown /*@C
3596552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3597552f7358SJed Brown 
3598552f7358SJed Brown   Not Collective
3599552f7358SJed Brown 
3600552f7358SJed Brown   Input Parameters:
3601552f7358SJed Brown + dm - The DMPlex object
3602552f7358SJed Brown . numPoints - The number of input points for the meet
3603552f7358SJed Brown - points - The input points
3604552f7358SJed Brown 
3605552f7358SJed Brown   Output Parameters:
3606552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3607552f7358SJed Brown - coveredPoints - The points in the meet
3608552f7358SJed Brown 
3609552f7358SJed Brown   Level: intermediate
3610552f7358SJed Brown 
3611552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3612552f7358SJed Brown 
36133813dfbdSMatthew G Knepley   Fortran Notes:
36143813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
36153813dfbdSMatthew G Knepley   include petsc.h90 in your code.
36163813dfbdSMatthew G Knepley 
36173813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
36183813dfbdSMatthew G Knepley 
3619552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3620552f7358SJed Brown @*/
3621552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3622552f7358SJed Brown {
3623552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3624552f7358SJed Brown   PetscInt      *meet[2];
3625552f7358SJed Brown   PetscInt       meetSize, i = 0;
3626552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3627552f7358SJed Brown   PetscErrorCode ierr;
3628552f7358SJed Brown 
3629552f7358SJed Brown   PetscFunctionBegin;
3630552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3631064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
3632064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveringPoints, 4);
3633064a246eSJacob Faibussowitsch   PetscValidPointer(coveringPoints, 5);
363469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
363569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3636552f7358SJed Brown   /* Copy in cone of first point */
3637552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3638552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3639552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3640552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3641552f7358SJed Brown   }
3642552f7358SJed Brown   /* Check each successive cone */
3643552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3644552f7358SJed Brown     PetscInt newMeetSize = 0;
3645552f7358SJed Brown 
3646552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3647552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3648552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3649552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3650552f7358SJed Brown 
3651552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3652552f7358SJed Brown         if (point == meet[i][m]) {
3653552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3654552f7358SJed Brown           break;
3655552f7358SJed Brown         }
3656552f7358SJed Brown       }
3657552f7358SJed Brown     }
3658552f7358SJed Brown     meetSize = newMeetSize;
3659552f7358SJed Brown     i        = 1-i;
3660552f7358SJed Brown   }
3661552f7358SJed Brown   *numCoveringPoints = meetSize;
3662552f7358SJed Brown   *coveringPoints    = meet[i];
366369291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3664552f7358SJed Brown   PetscFunctionReturn(0);
3665552f7358SJed Brown }
3666552f7358SJed Brown 
3667552f7358SJed Brown /*@C
3668552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3669552f7358SJed Brown 
3670552f7358SJed Brown   Not Collective
3671552f7358SJed Brown 
3672552f7358SJed Brown   Input Parameters:
3673552f7358SJed Brown + dm - The DMPlex object
3674552f7358SJed Brown . numPoints - The number of input points for the meet
3675552f7358SJed Brown - points - The input points
3676552f7358SJed Brown 
3677552f7358SJed Brown   Output Parameters:
3678552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3679552f7358SJed Brown - coveredPoints - The points in the meet
3680552f7358SJed Brown 
3681552f7358SJed Brown   Level: intermediate
3682552f7358SJed Brown 
36833813dfbdSMatthew G Knepley   Fortran Notes:
36843813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
36853813dfbdSMatthew G Knepley   include petsc.h90 in your code.
36863813dfbdSMatthew G Knepley 
36873813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
36883813dfbdSMatthew G Knepley 
3689552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3690552f7358SJed Brown @*/
3691552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3692552f7358SJed Brown {
3693552f7358SJed Brown   PetscErrorCode ierr;
3694552f7358SJed Brown 
3695552f7358SJed Brown   PetscFunctionBegin;
3696552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3697d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3698d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3699d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
370069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3701d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3702552f7358SJed Brown   PetscFunctionReturn(0);
3703552f7358SJed Brown }
3704552f7358SJed Brown 
3705552f7358SJed Brown /*@C
3706552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3707552f7358SJed Brown 
3708552f7358SJed Brown   Not Collective
3709552f7358SJed Brown 
3710552f7358SJed Brown   Input Parameters:
3711552f7358SJed Brown + dm - The DMPlex object
3712552f7358SJed Brown . numPoints - The number of input points for the meet
3713552f7358SJed Brown - points - The input points
3714552f7358SJed Brown 
3715552f7358SJed Brown   Output Parameters:
3716552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3717552f7358SJed Brown - coveredPoints - The points in the meet
3718552f7358SJed Brown 
3719552f7358SJed Brown   Level: intermediate
3720552f7358SJed Brown 
37213813dfbdSMatthew G Knepley   Fortran Notes:
37223813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
37233813dfbdSMatthew G Knepley   include petsc.h90 in your code.
37243813dfbdSMatthew G Knepley 
37253813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
37263813dfbdSMatthew G Knepley 
3727552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3728552f7358SJed Brown @*/
3729552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3730552f7358SJed Brown {
3731552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3732552f7358SJed Brown   PetscInt      *offsets, **closures;
3733552f7358SJed Brown   PetscInt      *meet[2];
3734552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
373524c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3736552f7358SJed Brown   PetscErrorCode ierr;
3737552f7358SJed Brown 
3738552f7358SJed Brown   PetscFunctionBegin;
3739552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3740064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
3741064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveredPoints, 4);
3742064a246eSJacob Faibussowitsch   PetscValidPointer(coveredPoints, 5);
3743552f7358SJed Brown 
3744552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3745785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
374669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
374724c766afSToby Isaac   mc      = mesh->maxConeSize;
374824c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
374969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
375069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3751552f7358SJed Brown 
3752552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3753552f7358SJed Brown     PetscInt closureSize;
3754552f7358SJed Brown 
3755552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
37560d644c17SKarl Rupp 
3757552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3758552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3759552f7358SJed Brown       PetscInt pStart, pEnd, i;
3760552f7358SJed Brown 
3761552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3762552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3763552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3764552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3765552f7358SJed Brown           break;
3766552f7358SJed Brown         }
3767552f7358SJed Brown       }
3768552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3769552f7358SJed Brown     }
377082f516ccSBarry 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);
3771552f7358SJed Brown   }
3772552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3773552f7358SJed Brown     PetscInt dof;
3774552f7358SJed Brown 
3775552f7358SJed Brown     /* Copy in cone of first point */
3776552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3777552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3778552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3779552f7358SJed Brown     }
3780552f7358SJed Brown     /* Check each successive cone */
3781552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3782552f7358SJed Brown       PetscInt newMeetSize = 0;
3783552f7358SJed Brown 
3784552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3785552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3786552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3787552f7358SJed Brown 
3788552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3789552f7358SJed Brown           if (point == meet[i][m]) {
3790552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3791552f7358SJed Brown             break;
3792552f7358SJed Brown           }
3793552f7358SJed Brown         }
3794552f7358SJed Brown       }
3795552f7358SJed Brown       meetSize = newMeetSize;
3796552f7358SJed Brown       i        = 1-i;
3797552f7358SJed Brown     }
3798552f7358SJed Brown     if (meetSize) break;
3799552f7358SJed Brown   }
3800552f7358SJed Brown   *numCoveredPoints = meetSize;
3801552f7358SJed Brown   *coveredPoints    = meet[i];
3802552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
38030298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3804552f7358SJed Brown   }
3805552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
380669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
380769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3808552f7358SJed Brown   PetscFunctionReturn(0);
3809552f7358SJed Brown }
3810552f7358SJed Brown 
38114e3744c5SMatthew G. Knepley /*@C
38124e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
38134e3744c5SMatthew G. Knepley 
38144e3744c5SMatthew G. Knepley   Not Collective
38154e3744c5SMatthew G. Knepley 
38164e3744c5SMatthew G. Knepley   Input Parameters:
38174e3744c5SMatthew G. Knepley + dmA - A DMPlex object
38184e3744c5SMatthew G. Knepley - dmB - A DMPlex object
38194e3744c5SMatthew G. Knepley 
38204e3744c5SMatthew G. Knepley   Output Parameters:
38214e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
38224e3744c5SMatthew G. Knepley 
38234e3744c5SMatthew G. Knepley   Level: intermediate
38244e3744c5SMatthew G. Knepley 
38254e3744c5SMatthew G. Knepley   Notes:
38264e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
38274e3744c5SMatthew G. Knepley 
38284e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
38294e3744c5SMatthew G. Knepley @*/
38304e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
38314e3744c5SMatthew G. Knepley {
38324e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
38334e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
38344e3744c5SMatthew G. Knepley 
38354e3744c5SMatthew G. Knepley   PetscFunctionBegin;
38364e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
38374e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
38384e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
38394e3744c5SMatthew G. Knepley 
38404e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
38414e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
38424e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
38434e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
38444e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
38454e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
38464e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
38474e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
38484e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
38494e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
38504e3744c5SMatthew G. Knepley 
38514e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
38524e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
38534e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
38544e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
38554e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
38564e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
38574e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
38584e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
38594e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
38604e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
38614e3744c5SMatthew G. Knepley     }
38624e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
38634e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
38644e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
38654e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
38664e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
38674e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
38684e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
38694e3744c5SMatthew G. Knepley     }
38704e3744c5SMatthew G. Knepley   }
38714e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
38724e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
38734e3744c5SMatthew G. Knepley }
38744e3744c5SMatthew G. Knepley 
38757cd05799SMatthew G. Knepley /*@C
38767cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
38777cd05799SMatthew G. Knepley 
38787cd05799SMatthew G. Knepley   Not Collective
38797cd05799SMatthew G. Knepley 
38807cd05799SMatthew G. Knepley   Input Parameters:
38817cd05799SMatthew G. Knepley + dm         - The DMPlex
38827cd05799SMatthew G. Knepley . cellDim    - The cell dimension
38837cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
38847cd05799SMatthew G. Knepley 
38857cd05799SMatthew G. Knepley   Output Parameters:
38867cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
38877cd05799SMatthew G. Knepley 
38887cd05799SMatthew G. Knepley   Level: developer
38897cd05799SMatthew G. Knepley 
38907cd05799SMatthew G. Knepley   Notes:
38917cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
38927cd05799SMatthew G. Knepley 
38937cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
38947cd05799SMatthew G. Knepley @*/
389518ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3896a6dfd86eSKarl Rupp {
389782f516ccSBarry Smith   MPI_Comm       comm;
3898552f7358SJed Brown   PetscErrorCode ierr;
3899552f7358SJed Brown 
3900552f7358SJed Brown   PetscFunctionBegin;
390182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3902064a246eSJacob Faibussowitsch   PetscValidPointer(numFaceVertices,4);
3903552f7358SJed Brown   switch (cellDim) {
3904552f7358SJed Brown   case 0:
3905552f7358SJed Brown     *numFaceVertices = 0;
3906552f7358SJed Brown     break;
3907552f7358SJed Brown   case 1:
3908552f7358SJed Brown     *numFaceVertices = 1;
3909552f7358SJed Brown     break;
3910552f7358SJed Brown   case 2:
3911552f7358SJed Brown     switch (numCorners) {
391219436ca2SJed Brown     case 3: /* triangle */
391319436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3914552f7358SJed Brown       break;
391519436ca2SJed Brown     case 4: /* quadrilateral */
391619436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3917552f7358SJed Brown       break;
391819436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
391919436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3920552f7358SJed Brown       break;
392119436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
392219436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3923552f7358SJed Brown       break;
3924552f7358SJed Brown     default:
3925f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3926552f7358SJed Brown     }
3927552f7358SJed Brown     break;
3928552f7358SJed Brown   case 3:
3929552f7358SJed Brown     switch (numCorners) {
393019436ca2SJed Brown     case 4: /* tetradehdron */
393119436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3932552f7358SJed Brown       break;
393319436ca2SJed Brown     case 6: /* tet cohesive cells */
393419436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3935552f7358SJed Brown       break;
393619436ca2SJed Brown     case 8: /* hexahedron */
393719436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3938552f7358SJed Brown       break;
393919436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
394019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3941552f7358SJed Brown       break;
394219436ca2SJed Brown     case 10: /* quadratic tetrahedron */
394319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3944552f7358SJed Brown       break;
394519436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
394619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3947552f7358SJed Brown       break;
394819436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
394919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3950552f7358SJed Brown       break;
395119436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
395219436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3953552f7358SJed Brown       break;
3954552f7358SJed Brown     default:
3955f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3956552f7358SJed Brown     }
3957552f7358SJed Brown     break;
3958552f7358SJed Brown   default:
3959f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3960552f7358SJed Brown   }
3961552f7358SJed Brown   PetscFunctionReturn(0);
3962552f7358SJed Brown }
3963552f7358SJed Brown 
3964552f7358SJed Brown /*@
3965aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3966552f7358SJed Brown 
3967552f7358SJed Brown   Not Collective
3968552f7358SJed Brown 
3969aa50250dSMatthew G. Knepley   Input Parameter:
3970552f7358SJed Brown . dm    - The DMPlex object
3971552f7358SJed Brown 
3972aa50250dSMatthew G. Knepley   Output Parameter:
3973aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3974552f7358SJed Brown 
3975552f7358SJed Brown   Level: developer
3976552f7358SJed Brown 
3977dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
3978aa50250dSMatthew G. Knepley @*/
3979aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3980aa50250dSMatthew G. Knepley {
3981aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3982aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3983aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3984c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3985aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3986aa50250dSMatthew G. Knepley }
3987aa50250dSMatthew G. Knepley 
3988aa50250dSMatthew G. Knepley /*@
3989aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3990aa50250dSMatthew G. Knepley 
3991aa50250dSMatthew G. Knepley   Not Collective
3992aa50250dSMatthew G. Knepley 
3993aa50250dSMatthew G. Knepley   Input Parameter:
3994aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3995aa50250dSMatthew G. Knepley 
3996aa50250dSMatthew G. Knepley   Output Parameter:
3997aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3998aa50250dSMatthew G. Knepley 
3999aa50250dSMatthew G. Knepley   Level: developer
4000552f7358SJed Brown 
4001b1bb481bSMatthew Knepley   Notes:
4002b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
4003dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
4004dc287ab2SVaclav Hapla   An empty mesh gives -1.
4005b1bb481bSMatthew Knepley 
4006dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
4007552f7358SJed Brown @*/
4008552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4009552f7358SJed Brown {
4010aa50250dSMatthew G. Knepley   DMLabel        label;
4011aa50250dSMatthew G. Knepley   PetscInt       d = 0;
4012552f7358SJed Brown   PetscErrorCode ierr;
4013552f7358SJed Brown 
4014552f7358SJed Brown   PetscFunctionBegin;
4015552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4016552f7358SJed Brown   PetscValidPointer(depth, 2);
4017aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4018aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
4019552f7358SJed Brown   *depth = d-1;
4020552f7358SJed Brown   PetscFunctionReturn(0);
4021552f7358SJed Brown }
4022552f7358SJed Brown 
4023552f7358SJed Brown /*@
4024552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
4025552f7358SJed Brown 
4026552f7358SJed Brown   Not Collective
4027552f7358SJed Brown 
4028552f7358SJed Brown   Input Parameters:
4029552f7358SJed Brown + dm           - The DMPlex object
4030552f7358SJed Brown - stratumValue - The requested depth
4031552f7358SJed Brown 
4032552f7358SJed Brown   Output Parameters:
4033552f7358SJed Brown + start - The first point at this depth
4034552f7358SJed Brown - end   - One beyond the last point at this depth
4035552f7358SJed Brown 
4036647867b2SJed Brown   Notes:
4037647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
4038647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
4039647867b2SJed Brown   higher dimension, e.g., "edges".
4040647867b2SJed Brown 
4041552f7358SJed Brown   Level: developer
4042552f7358SJed Brown 
4043dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
4044552f7358SJed Brown @*/
40450adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
40460adebc6cSBarry Smith {
4047aa50250dSMatthew G. Knepley   DMLabel        label;
404863d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
4049552f7358SJed Brown   PetscErrorCode ierr;
4050552f7358SJed Brown 
4051552f7358SJed Brown   PetscFunctionBegin;
4052552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
405363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
405463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4055552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
40560d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
405763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
405863d1a920SMatthew G. Knepley     if (start) *start = pStart;
405963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
406063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4061552f7358SJed Brown   }
4062aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
406363d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
406463d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
4065552f7358SJed Brown   PetscFunctionReturn(0);
4066552f7358SJed Brown }
4067552f7358SJed Brown 
4068552f7358SJed Brown /*@
4069552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
4070552f7358SJed Brown 
4071552f7358SJed Brown   Not Collective
4072552f7358SJed Brown 
4073552f7358SJed Brown   Input Parameters:
4074552f7358SJed Brown + dm           - The DMPlex object
4075552f7358SJed Brown - stratumValue - The requested height
4076552f7358SJed Brown 
4077552f7358SJed Brown   Output Parameters:
4078552f7358SJed Brown + start - The first point at this height
4079552f7358SJed Brown - end   - One beyond the last point at this height
4080552f7358SJed Brown 
4081647867b2SJed Brown   Notes:
4082647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
4083647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
4084647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
4085647867b2SJed Brown 
4086552f7358SJed Brown   Level: developer
4087552f7358SJed Brown 
40883dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
4089552f7358SJed Brown @*/
40900adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
40910adebc6cSBarry Smith {
4092aa50250dSMatthew G. Knepley   DMLabel        label;
409363d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
4094552f7358SJed Brown   PetscErrorCode ierr;
4095552f7358SJed Brown 
4096552f7358SJed Brown   PetscFunctionBegin;
4097552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409863d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
409963d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4100552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
41010d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
410263d1a920SMatthew G. Knepley   if (stratumValue < 0) {
410363d1a920SMatthew G. Knepley     if (start) *start = pStart;
410463d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
410563d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4106552f7358SJed Brown   }
4107aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
410813903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
410963d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
411063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
4111552f7358SJed Brown   PetscFunctionReturn(0);
4112552f7358SJed Brown }
4113552f7358SJed Brown 
4114ba2698f1SMatthew G. Knepley /*@
4115ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
4116ba2698f1SMatthew G. Knepley 
4117ba2698f1SMatthew G. Knepley   Not Collective
4118ba2698f1SMatthew G. Knepley 
4119ba2698f1SMatthew G. Knepley   Input Parameter:
4120ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
4121ba2698f1SMatthew G. Knepley - point - The point
4122ba2698f1SMatthew G. Knepley 
4123ba2698f1SMatthew G. Knepley   Output Parameter:
4124ba2698f1SMatthew G. Knepley . depth - The depth of the point
4125ba2698f1SMatthew G. Knepley 
4126ba2698f1SMatthew G. Knepley   Level: intermediate
4127ba2698f1SMatthew G. Knepley 
41283dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
4129ba2698f1SMatthew G. Knepley @*/
4130ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4131ba2698f1SMatthew G. Knepley {
4132ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4133ba2698f1SMatthew G. Knepley 
4134ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4135ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413640a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
4137ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
4138ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4139ba2698f1SMatthew G. Knepley }
4140ba2698f1SMatthew G. Knepley 
4141ba2698f1SMatthew G. Knepley /*@
41420c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
41430c0a32dcSVaclav Hapla 
41440c0a32dcSVaclav Hapla   Not Collective
41450c0a32dcSVaclav Hapla 
41460c0a32dcSVaclav Hapla   Input Parameter:
41470c0a32dcSVaclav Hapla + dm    - The DMPlex object
41480c0a32dcSVaclav Hapla - point - The point
41490c0a32dcSVaclav Hapla 
41500c0a32dcSVaclav Hapla   Output Parameter:
41510c0a32dcSVaclav Hapla . height - The height of the point
41520c0a32dcSVaclav Hapla 
41530c0a32dcSVaclav Hapla   Level: intermediate
41540c0a32dcSVaclav Hapla 
41553dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
41560c0a32dcSVaclav Hapla @*/
41570c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
41580c0a32dcSVaclav Hapla {
41590c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
41600c0a32dcSVaclav Hapla   PetscErrorCode ierr;
41610c0a32dcSVaclav Hapla 
41620c0a32dcSVaclav Hapla   PetscFunctionBegin;
41630c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41640c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
41650c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
41660c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
41670c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
41680c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
41690c0a32dcSVaclav Hapla }
41700c0a32dcSVaclav Hapla 
41710c0a32dcSVaclav Hapla /*@
4172ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
4173ba2698f1SMatthew G. Knepley 
4174ba2698f1SMatthew G. Knepley   Not Collective
4175ba2698f1SMatthew G. Knepley 
4176ba2698f1SMatthew G. Knepley   Input Parameter:
4177ba2698f1SMatthew G. Knepley . dm - The DMPlex object
4178ba2698f1SMatthew G. Knepley 
4179ba2698f1SMatthew G. Knepley   Output Parameter:
4180ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
4181ba2698f1SMatthew G. Knepley 
4182412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
4183412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
4184412e9a14SMatthew G. Knepley 
4185ba2698f1SMatthew G. Knepley   Level: developer
4186ba2698f1SMatthew G. Knepley 
4187dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
4188ba2698f1SMatthew G. Knepley @*/
4189ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4190ba2698f1SMatthew G. Knepley {
4191ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4192ba2698f1SMatthew G. Knepley 
4193ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4194ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4195ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
4196ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
4197ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
4198ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4199ba2698f1SMatthew G. Knepley }
4200ba2698f1SMatthew G. Knepley 
4201ba2698f1SMatthew G. Knepley /*@
4202ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
4203ba2698f1SMatthew G. Knepley 
4204ba2698f1SMatthew G. Knepley   Not Collective
4205ba2698f1SMatthew G. Knepley 
4206ba2698f1SMatthew G. Knepley   Input Parameter:
4207ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
4208ba2698f1SMatthew G. Knepley - cell - The cell
4209ba2698f1SMatthew G. Knepley 
4210ba2698f1SMatthew G. Knepley   Output Parameter:
4211ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
4212ba2698f1SMatthew G. Knepley 
4213ba2698f1SMatthew G. Knepley   Level: intermediate
4214ba2698f1SMatthew G. Knepley 
4215ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
4216ba2698f1SMatthew G. Knepley @*/
4217ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4218ba2698f1SMatthew G. Knepley {
4219ba2698f1SMatthew G. Knepley   DMLabel        label;
4220ba2698f1SMatthew G. Knepley   PetscInt       ct;
4221ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4222ba2698f1SMatthew G. Knepley 
4223ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4224ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4225ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
4226ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4227ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
42284a7ee7d0SMatthew G. Knepley   if (ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %D has not been assigned a cell type", cell);
4229ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
4230ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4231ba2698f1SMatthew G. Knepley }
4232ba2698f1SMatthew G. Knepley 
4233412e9a14SMatthew G. Knepley /*@
4234412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
4235412e9a14SMatthew G. Knepley 
4236412e9a14SMatthew G. Knepley   Not Collective
4237412e9a14SMatthew G. Knepley 
4238412e9a14SMatthew G. Knepley   Input Parameters:
4239412e9a14SMatthew G. Knepley + dm   - The DMPlex object
4240412e9a14SMatthew G. Knepley . cell - The cell
4241412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
4242412e9a14SMatthew G. Knepley 
4243412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
4244412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
4245412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
4246412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
4247412e9a14SMatthew G. Knepley 
4248412e9a14SMatthew G. Knepley   Level: advanced
4249412e9a14SMatthew G. Knepley 
4250412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
4251412e9a14SMatthew G. Knepley @*/
4252412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4253412e9a14SMatthew G. Knepley {
4254412e9a14SMatthew G. Knepley   DMLabel        label;
4255412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
4256412e9a14SMatthew G. Knepley 
4257412e9a14SMatthew G. Knepley   PetscFunctionBegin;
4258412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4259412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4260412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
4261412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
4262412e9a14SMatthew G. Knepley }
4263412e9a14SMatthew G. Knepley 
42640adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
42650adebc6cSBarry Smith {
4266efe440bfSMatthew G. Knepley   PetscSection   section, s;
4267efe440bfSMatthew G. Knepley   Mat            m;
42683e922f36SToby Isaac   PetscInt       maxHeight;
4269552f7358SJed Brown   PetscErrorCode ierr;
4270552f7358SJed Brown 
4271552f7358SJed Brown   PetscFunctionBegin;
427238221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
42733e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
42743e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
427582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
427692fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
42771d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4278efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
4279efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
4280efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
4281efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
4282efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
42838f4c458bSMatthew G. Knepley 
42848f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
42858f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
4286552f7358SJed Brown   PetscFunctionReturn(0);
4287552f7358SJed Brown }
4288552f7358SJed Brown 
4289f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4290f19dbd58SToby Isaac {
4291f19dbd58SToby Isaac   Vec            coordsLocal;
4292f19dbd58SToby Isaac   DM             coordsDM;
4293f19dbd58SToby Isaac   PetscErrorCode ierr;
4294f19dbd58SToby Isaac 
4295f19dbd58SToby Isaac   PetscFunctionBegin;
4296f19dbd58SToby Isaac   *field = NULL;
4297f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
4298f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
4299f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
4300f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
4301f19dbd58SToby Isaac   }
4302f19dbd58SToby Isaac   PetscFunctionReturn(0);
4303f19dbd58SToby Isaac }
4304f19dbd58SToby Isaac 
43057cd05799SMatthew G. Knepley /*@C
43067cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
43077cd05799SMatthew G. Knepley 
43087cd05799SMatthew G. Knepley   Not Collective
43097cd05799SMatthew G. Knepley 
43107cd05799SMatthew G. Knepley   Input Parameters:
43117cd05799SMatthew G. Knepley . dm        - The DMPlex object
43127cd05799SMatthew G. Knepley 
43137cd05799SMatthew G. Knepley   Output Parameter:
43147cd05799SMatthew G. Knepley . section - The PetscSection object
43157cd05799SMatthew G. Knepley 
43167cd05799SMatthew G. Knepley   Level: developer
43177cd05799SMatthew G. Knepley 
43187cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
43197cd05799SMatthew G. Knepley @*/
43200adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
43210adebc6cSBarry Smith {
4322552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4323552f7358SJed Brown 
4324552f7358SJed Brown   PetscFunctionBegin;
4325552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4326552f7358SJed Brown   if (section) *section = mesh->coneSection;
4327552f7358SJed Brown   PetscFunctionReturn(0);
4328552f7358SJed Brown }
4329552f7358SJed Brown 
43307cd05799SMatthew G. Knepley /*@C
43317cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
43327cd05799SMatthew G. Knepley 
43337cd05799SMatthew G. Knepley   Not Collective
43347cd05799SMatthew G. Knepley 
43357cd05799SMatthew G. Knepley   Input Parameters:
43367cd05799SMatthew G. Knepley . dm        - The DMPlex object
43377cd05799SMatthew G. Knepley 
43387cd05799SMatthew G. Knepley   Output Parameter:
43397cd05799SMatthew G. Knepley . section - The PetscSection object
43407cd05799SMatthew G. Knepley 
43417cd05799SMatthew G. Knepley   Level: developer
43427cd05799SMatthew G. Knepley 
43437cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
43447cd05799SMatthew G. Knepley @*/
43458cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
43468cb4d582SMatthew G. Knepley {
43478cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
43488cb4d582SMatthew G. Knepley 
43498cb4d582SMatthew G. Knepley   PetscFunctionBegin;
43508cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43518cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
43528cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
43538cb4d582SMatthew G. Knepley }
43548cb4d582SMatthew G. Knepley 
43557cd05799SMatthew G. Knepley /*@C
43567cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
43577cd05799SMatthew G. Knepley 
43587cd05799SMatthew G. Knepley   Not Collective
43597cd05799SMatthew G. Knepley 
43607cd05799SMatthew G. Knepley   Input Parameters:
43617cd05799SMatthew G. Knepley . dm        - The DMPlex object
43627cd05799SMatthew G. Knepley 
43637cd05799SMatthew G. Knepley   Output Parameter:
43647cd05799SMatthew G. Knepley . cones - The cone for each point
43657cd05799SMatthew G. Knepley 
43667cd05799SMatthew G. Knepley   Level: developer
43677cd05799SMatthew G. Knepley 
43687cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
43697cd05799SMatthew G. Knepley @*/
4370a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4371a6dfd86eSKarl Rupp {
4372552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4373552f7358SJed Brown 
4374552f7358SJed Brown   PetscFunctionBegin;
4375552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4376552f7358SJed Brown   if (cones) *cones = mesh->cones;
4377552f7358SJed Brown   PetscFunctionReturn(0);
4378552f7358SJed Brown }
4379552f7358SJed Brown 
43807cd05799SMatthew G. Knepley /*@C
43817cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
43827cd05799SMatthew G. Knepley 
43837cd05799SMatthew G. Knepley   Not Collective
43847cd05799SMatthew G. Knepley 
43857cd05799SMatthew G. Knepley   Input Parameters:
43867cd05799SMatthew G. Knepley . dm        - The DMPlex object
43877cd05799SMatthew G. Knepley 
43887cd05799SMatthew G. Knepley   Output Parameter:
43897cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
43907cd05799SMatthew G. Knepley 
43917cd05799SMatthew G. Knepley   Level: developer
43927cd05799SMatthew G. Knepley 
43937cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
43947cd05799SMatthew G. Knepley @*/
4395a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4396a6dfd86eSKarl Rupp {
4397552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4398552f7358SJed Brown 
4399552f7358SJed Brown   PetscFunctionBegin;
4400552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4401552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4402552f7358SJed Brown   PetscFunctionReturn(0);
4403552f7358SJed Brown }
4404552f7358SJed Brown 
4405552f7358SJed Brown /******************************** FEM Support **********************************/
4406552f7358SJed Brown 
44079e8305c2SJed Brown /*
44089e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
44099e8305c2SJed Brown  representing a line in the section.
44109e8305c2SJed Brown */
44119e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
44129e8305c2SJed Brown {
44139e8305c2SJed Brown   PetscErrorCode ierr;
44149e8305c2SJed Brown 
44159e8305c2SJed Brown   PetscFunctionBeginHot;
44169e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4417a433471fSStefano Zampini   if (line < 0) {
4418a433471fSStefano Zampini     *k = 0;
4419a433471fSStefano Zampini     *Nc = 0;
4420a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
44219e8305c2SJed Brown     *k = 1;
44229e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
44239e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
44249e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
44259e8305c2SJed Brown     *k = *k / *Nc + 1;
44269e8305c2SJed Brown   }
44279e8305c2SJed Brown   PetscFunctionReturn(0);
44289e8305c2SJed Brown }
44299e8305c2SJed Brown 
4430a4355906SMatthew Knepley /*@
4431bc1eb3faSJed Brown 
4432bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4433bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
44341bb6d2a8SBarry Smith   section provided (or the section of the DM).
4435a4355906SMatthew Knepley 
4436a4355906SMatthew Knepley   Input Parameters:
4437a4355906SMatthew Knepley + dm      - The DM
4438a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4439a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4440a4355906SMatthew Knepley 
4441a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4442a4355906SMatthew Knepley   degree of the basis.
4443a4355906SMatthew Knepley 
4444bc1eb3faSJed Brown   Example:
4445bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4446bc1eb3faSJed Brown .vb
4447bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4448bc1eb3faSJed Brown 
4449bc1eb3faSJed Brown   v4 -- e6 -- v3
4450bc1eb3faSJed Brown   |           |
4451bc1eb3faSJed Brown   e7    c0    e8
4452bc1eb3faSJed Brown   |           |
4453bc1eb3faSJed Brown   v1 -- e5 -- v2
4454bc1eb3faSJed Brown .ve
4455bc1eb3faSJed Brown 
4456bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4457bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4458bc1eb3faSJed Brown .vb
4459bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4460bc1eb3faSJed Brown     v1 -> [4]
4461bc1eb3faSJed Brown     ...
4462bc1eb3faSJed Brown     e5 -> [8, 9]
4463bc1eb3faSJed Brown .ve
4464bc1eb3faSJed Brown 
4465bc1eb3faSJed Brown   which corresponds to the dofs
4466bc1eb3faSJed Brown .vb
4467bc1eb3faSJed Brown     6   10  11  7
4468bc1eb3faSJed Brown     13  2   3   15
4469bc1eb3faSJed Brown     12  0   1   14
4470bc1eb3faSJed Brown     4   8   9   5
4471bc1eb3faSJed Brown .ve
4472bc1eb3faSJed Brown 
4473bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4474bc1eb3faSJed Brown .vb
4475bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4476bc1eb3faSJed Brown .ve
4477bc1eb3faSJed Brown 
4478bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4479bc1eb3faSJed Brown .vb
4480bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4481bc1eb3faSJed Brown .ve
4482bc1eb3faSJed Brown 
4483a4355906SMatthew Knepley   Level: developer
4484a4355906SMatthew Knepley 
44859df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4486a4355906SMatthew Knepley @*/
4487bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
44883194fc30SMatthew G. Knepley {
44897391a63aSMatthew G. Knepley   DMLabel        label;
4490bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
44919e8305c2SJed Brown   PetscBool      vertexchart;
44923194fc30SMatthew G. Knepley   PetscErrorCode ierr;
44933194fc30SMatthew G. Knepley 
44943194fc30SMatthew G. Knepley   PetscFunctionBegin;
44953194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4496a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4497a433471fSStefano Zampini   if (point < 0) {
4498a433471fSStefano Zampini     PetscInt sStart,sEnd;
4499a433471fSStefano Zampini 
4500a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4501a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4502a433471fSStefano Zampini   }
45037391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4504a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4505a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
45067391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
45077391a63aSMatthew G. Knepley   else if  (depth == dim) {
45087391a63aSMatthew G. Knepley     const PetscInt *cone;
45097391a63aSMatthew G. Knepley 
45107391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4511d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4512d4e6627bSStefano Zampini     else if (dim == 3) {
4513d4e6627bSStefano Zampini       const PetscInt *cone2;
4514d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4515d4e6627bSStefano Zampini       eStart = cone2[0];
4516d4e6627bSStefano 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);
4517a433471fSStefano 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);
45189e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
45199e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
45209e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
45219e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
45229e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
45239e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
45249e8305c2SJed Brown   }
45253194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4526bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
4527bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4528bb197d40SJed Brown     PetscInt *perm;
4529bb197d40SJed Brown 
45303194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
45319e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4532bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
45333194fc30SMatthew G. Knepley     }
45343194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
45353194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4536bb197d40SJed Brown       switch (d) {
4537babf31e0SJed Brown       case 1:
45389e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4539babf31e0SJed Brown         /*
4540babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4541babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
4542babf31e0SJed Brown          */
4543babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4544babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4545babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4546babf31e0SJed Brown         foffset = offset;
4547babf31e0SJed Brown         break;
454889eabcffSMatthew G. Knepley       case 2:
45493194fc30SMatthew 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} */
45509e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
45513194fc30SMatthew G. Knepley         /* The SEM order is
45523194fc30SMatthew G. Knepley 
45533194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
455489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
45553194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
45563194fc30SMatthew G. Knepley          */
45573194fc30SMatthew G. Knepley         {
45583194fc30SMatthew G. Knepley           const PetscInt of   = 0;
45593194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
45603194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
45613194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
45623194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
45633194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
45643194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
45653194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
45663194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
45673194fc30SMatthew G. Knepley           PetscInt       o;
45683194fc30SMatthew G. Knepley 
45693194fc30SMatthew G. Knepley           /* bottom */
45703194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
45713194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
45723194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
45733194fc30SMatthew G. Knepley           /* middle */
45743194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
45753194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
45763194fc30SMatthew 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;
45773194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
45783194fc30SMatthew G. Knepley           }
45793194fc30SMatthew G. Knepley           /* top */
45803194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
45813194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
45823194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
45833194fc30SMatthew G. Knepley           foffset = offset;
45843194fc30SMatthew G. Knepley         }
458589eabcffSMatthew G. Knepley         break;
458689eabcffSMatthew G. Knepley       case 3:
458789eabcffSMatthew G. Knepley         /* The original hex closure is
458889eabcffSMatthew G. Knepley 
458989eabcffSMatthew G. Knepley          {c,
459089eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
459189eabcffSMatthew 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,
459289eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
459389eabcffSMatthew G. Knepley          */
45949e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
459589eabcffSMatthew G. Knepley         /* The SEM order is
459689eabcffSMatthew G. Knepley          Bottom Slice
459789eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
459889eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
459989eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
460089eabcffSMatthew G. Knepley 
460189eabcffSMatthew G. Knepley          Middle Slice (j)
460289eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
460389eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
460489eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
460589eabcffSMatthew G. Knepley 
460689eabcffSMatthew G. Knepley          Top Slice
460789eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
460889eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
460989eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
461089eabcffSMatthew G. Knepley          */
461189eabcffSMatthew G. Knepley         {
461289eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
461389eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
461489eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
461589eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
461689eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
461789eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
461889eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
461989eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
462089eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
462189eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
462289eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
462389eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
462489eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
462589eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
462689eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
462789eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
462889eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
462989eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
463089eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
463189eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
463289eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
463389eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
463489eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
463589eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
463689eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
463789eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
463889eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
463989eabcffSMatthew G. Knepley           PetscInt       o, n;
464089eabcffSMatthew G. Knepley 
464189eabcffSMatthew G. Knepley           /* Bottom Slice */
464289eabcffSMatthew G. Knepley           /*   bottom */
464389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
464489eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
464589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
464689eabcffSMatthew G. Knepley           /*   middle */
464789eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
464889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4649316b7f87SMax 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;}
465089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
46513194fc30SMatthew G. Knepley           }
465289eabcffSMatthew G. Knepley           /*   top */
465389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
465489eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
465589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
465689eabcffSMatthew G. Knepley 
465789eabcffSMatthew G. Knepley           /* Middle Slice */
465889eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
465989eabcffSMatthew G. Knepley             /*   bottom */
466089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
466189eabcffSMatthew 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;
466289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
466389eabcffSMatthew G. Knepley             /*   middle */
466489eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
466589eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
466689eabcffSMatthew 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;
466789eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
466889eabcffSMatthew G. Knepley             }
466989eabcffSMatthew G. Knepley             /*   top */
467089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
467189eabcffSMatthew 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;
467289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
467389eabcffSMatthew G. Knepley           }
467489eabcffSMatthew G. Knepley 
467589eabcffSMatthew G. Knepley           /* Top Slice */
467689eabcffSMatthew G. Knepley           /*   bottom */
467789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
467889eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
467989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
468089eabcffSMatthew G. Knepley           /*   middle */
468189eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
468289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
468389eabcffSMatthew 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;
468489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
468589eabcffSMatthew G. Knepley           }
468689eabcffSMatthew G. Knepley           /*   top */
468789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
468889eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
468989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
469089eabcffSMatthew G. Knepley 
469189eabcffSMatthew G. Knepley           foffset = offset;
469289eabcffSMatthew G. Knepley         }
469389eabcffSMatthew G. Knepley         break;
4694bb197d40SJed Brown       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
469589eabcffSMatthew G. Knepley       }
469689eabcffSMatthew G. Knepley     }
469789eabcffSMatthew G. Knepley     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
46983194fc30SMatthew G. Knepley     /* Check permutation */
46993194fc30SMatthew G. Knepley     {
47003194fc30SMatthew G. Knepley       PetscInt *check;
47013194fc30SMatthew G. Knepley 
47023194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
47033194fc30SMatthew 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]);}
47043194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
47053194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
47063194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
47073194fc30SMatthew G. Knepley     }
4708bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
4709bb197d40SJed Brown   }
47103194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
47113194fc30SMatthew G. Knepley }
47123194fc30SMatthew G. Knepley 
4713e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4714e071409bSToby Isaac {
4715e071409bSToby Isaac   PetscDS        prob;
4716e071409bSToby Isaac   PetscInt       depth, Nf, h;
4717e071409bSToby Isaac   DMLabel        label;
4718e071409bSToby Isaac   PetscErrorCode ierr;
4719e071409bSToby Isaac 
4720e071409bSToby Isaac   PetscFunctionBeginHot;
4721e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4722e071409bSToby Isaac   Nf      = prob->Nf;
4723e071409bSToby Isaac   label   = dm->depthLabel;
4724e071409bSToby Isaac   *dspace = NULL;
4725e071409bSToby Isaac   if (field < Nf) {
4726e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4727e071409bSToby Isaac 
4728e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4729e071409bSToby Isaac       PetscDualSpace dsp;
4730e071409bSToby Isaac 
4731e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4732e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4733e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4734e071409bSToby Isaac       h    = depth - 1 - h;
4735e071409bSToby Isaac       if (h) {
4736e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4737e071409bSToby Isaac       } else {
4738e071409bSToby Isaac         *dspace = dsp;
4739e071409bSToby Isaac       }
4740e071409bSToby Isaac     }
4741e071409bSToby Isaac   }
4742e071409bSToby Isaac   PetscFunctionReturn(0);
4743e071409bSToby Isaac }
4744e071409bSToby Isaac 
47451a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4746a6dfd86eSKarl Rupp {
4747552f7358SJed Brown   PetscScalar    *array, *vArray;
4748d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
47491a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4750552f7358SJed Brown   PetscErrorCode  ierr;
4751552f7358SJed Brown 
47521b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
47532a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
47545a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
47555a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
47565a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
47573f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
47589df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
47599df71ca4SMatthew G. Knepley       PetscInt dof;
4760d9917b9dSMatthew G. Knepley 
47619df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
47629df71ca4SMatthew G. Knepley       size += dof;
47639df71ca4SMatthew G. Knepley     }
47649df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
47659df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
47662a3aaacfSMatthew G. Knepley       PetscInt       dof;
47675a1bb5cfSMatthew G. Knepley 
47685a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
47692a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
47705a1bb5cfSMatthew G. Knepley       size += dof;
47715a1bb5cfSMatthew G. Knepley     }
47723f7cbbe7SMatthew G. Knepley     if (!values) {
47733f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
47743f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
47753f7cbbe7SMatthew G. Knepley     }
477669291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4777982e9ed1SMatthew G. Knepley   } else {
4778982e9ed1SMatthew G. Knepley     array = *values;
4779982e9ed1SMatthew G. Knepley   }
47809df71ca4SMatthew G. Knepley   size = 0;
47815a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
47829df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
47839df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
47849df71ca4SMatthew G. Knepley     PetscScalar *varr;
4785d9917b9dSMatthew G. Knepley 
47869df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
47879df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
47889df71ca4SMatthew G. Knepley     varr = &vArray[off];
47891a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
47901a271a75SMatthew G. Knepley       array[offset] = varr[d];
47919df71ca4SMatthew G. Knepley     }
47929df71ca4SMatthew G. Knepley     size += dof;
47939df71ca4SMatthew G. Knepley   }
47949df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
47959df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
47969df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
47975a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
47985a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
47995a1bb5cfSMatthew G. Knepley 
480052ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
48015a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
48025a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
48035a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
48045a1bb5cfSMatthew G. Knepley     if (o >= 0) {
48051a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
48061a271a75SMatthew G. Knepley         array[offset] = varr[d];
48075a1bb5cfSMatthew G. Knepley       }
48085a1bb5cfSMatthew G. Knepley     } else {
48091a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
48101a271a75SMatthew G. Knepley         array[offset] = varr[d];
48115a1bb5cfSMatthew G. Knepley       }
48125a1bb5cfSMatthew G. Knepley     }
48139df71ca4SMatthew G. Knepley     size += dof;
48145a1bb5cfSMatthew G. Knepley   }
48155a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
48169df71ca4SMatthew G. Knepley   if (!*values) {
48175a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
48185a1bb5cfSMatthew G. Knepley     *values = array;
48199df71ca4SMatthew G. Knepley   } else {
48208ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
48218c312ff3SMatthew G. Knepley     *csize = size;
48229df71ca4SMatthew G. Knepley   }
48235a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
48245a1bb5cfSMatthew G. Knepley }
4825d9917b9dSMatthew G. Knepley 
482627f02ce8SMatthew G. Knepley /* Compress out points not in the section */
482727f02ce8SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
482827f02ce8SMatthew G. Knepley {
482927f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
483027f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
483127f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
483227f02ce8SMatthew G. Knepley 
483327f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
483427f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
483527f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
483627f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
483727f02ce8SMatthew G. Knepley       points[q*2]   = r;
483827f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
483927f02ce8SMatthew G. Knepley       ++q;
484027f02ce8SMatthew G. Knepley     }
484127f02ce8SMatthew G. Knepley   }
484227f02ce8SMatthew G. Knepley   *numPoints = q;
484327f02ce8SMatthew G. Knepley   return 0;
484427f02ce8SMatthew G. Knepley }
484527f02ce8SMatthew G. Knepley 
484627f02ce8SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
484727f02ce8SMatthew G. Knepley {
484827f02ce8SMatthew G. Knepley   const PetscInt *cone, *ornt;
484927f02ce8SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
485027f02ce8SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
485127f02ce8SMatthew G. Knepley   PetscErrorCode  ierr;
485227f02ce8SMatthew G. Knepley 
485327f02ce8SMatthew G. Knepley   PetscFunctionBeginHot;
485427f02ce8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
485527f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
485627f02ce8SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
485727f02ce8SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
485827f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
485927f02ce8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, np*2, MPIU_INT, &pts);CHKERRQ(ierr);
486027f02ce8SMatthew G. Knepley   c    = 0;
486127f02ce8SMatthew G. Knepley   pts[c*2+0] = point;
486227f02ce8SMatthew G. Knepley   pts[c*2+1] = 0;
486327f02ce8SMatthew G. Knepley   ++c;
486427f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
486527f02ce8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
486627f02ce8SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
486727f02ce8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
486827f02ce8SMatthew G. Knepley   if (dim >= 2) {
486927f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
487027f02ce8SMatthew G. Knepley   }
487127f02ce8SMatthew G. Knepley   if (dim >= 3) {
487227f02ce8SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
487327f02ce8SMatthew G. Knepley       const PetscInt  fpoint = cone[d];
487427f02ce8SMatthew G. Knepley       const PetscInt *fcone;
487527f02ce8SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
487627f02ce8SMatthew G. Knepley 
487727f02ce8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
487827f02ce8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
487927f02ce8SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
488027f02ce8SMatthew G. Knepley         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
488127f02ce8SMatthew G. Knepley         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
488227f02ce8SMatthew G. Knepley       }
488327f02ce8SMatthew G. Knepley     }
488427f02ce8SMatthew G. Knepley   }
488527f02ce8SMatthew G. Knepley   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
488627f02ce8SMatthew G. Knepley   *numPoints = np;
488727f02ce8SMatthew G. Knepley   *points    = pts;
488827f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
488927f02ce8SMatthew G. Knepley }
489027f02ce8SMatthew G. Knepley 
489197529cf3SJed Brown /* Compressed closure does not apply closure permutation */
48921dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4893923c78e0SToby Isaac {
489427f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
4895923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4896923c78e0SToby Isaac   PetscErrorCode ierr;
4897923c78e0SToby Isaac 
4898923c78e0SToby Isaac   PetscFunctionBeginHot;
4899923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
490027f02ce8SMatthew G. Knepley   if (*clPoints) {
4901923c78e0SToby Isaac     PetscInt dof, off;
4902923c78e0SToby Isaac 
4903923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4904923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4905923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4906923c78e0SToby Isaac     np   = dof/2;
4907923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
490827f02ce8SMatthew G. Knepley   } else {
4909665f567fSMatthew G. Knepley     DMPolytopeType ct;
4910665f567fSMatthew G. Knepley 
491127f02ce8SMatthew G. Knepley     /* Do not make the label if it does not exist */
491227f02ce8SMatthew G. Knepley     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
491327f02ce8SMatthew G. Knepley     else                    {ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);}
491427f02ce8SMatthew G. Knepley     switch (ct) {
491527f02ce8SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
491627f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);CHKERRQ(ierr);
491727f02ce8SMatthew G. Knepley         break;
491827f02ce8SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
491927f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);CHKERRQ(ierr);
492027f02ce8SMatthew G. Knepley         break;
492127f02ce8SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
492227f02ce8SMatthew G. Knepley         ierr = DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);CHKERRQ(ierr);
492327f02ce8SMatthew G. Knepley         break;
492427f02ce8SMatthew G. Knepley       default:
492527f02ce8SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
492627f02ce8SMatthew G. Knepley     }
492727f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
4928923c78e0SToby Isaac   }
4929923c78e0SToby Isaac   *numPoints = np;
4930923c78e0SToby Isaac   *points    = pts;
4931923c78e0SToby Isaac   *clp       = cla;
4932923c78e0SToby Isaac   PetscFunctionReturn(0);
4933923c78e0SToby Isaac }
4934923c78e0SToby Isaac 
49351dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4936923c78e0SToby Isaac {
4937923c78e0SToby Isaac   PetscErrorCode ierr;
4938923c78e0SToby Isaac 
4939923c78e0SToby Isaac   PetscFunctionBeginHot;
4940923c78e0SToby Isaac   if (!*clPoints) {
4941923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4942923c78e0SToby Isaac   } else {
4943923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4944923c78e0SToby Isaac   }
4945923c78e0SToby Isaac   *numPoints = 0;
4946923c78e0SToby Isaac   *points    = NULL;
4947923c78e0SToby Isaac   *clSec     = NULL;
4948923c78e0SToby Isaac   *clPoints  = NULL;
4949923c78e0SToby Isaac   *clp       = NULL;
4950923c78e0SToby Isaac   PetscFunctionReturn(0);
4951923c78e0SToby Isaac }
4952923c78e0SToby Isaac 
495397e99dd9SToby 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[])
49541a271a75SMatthew G. Knepley {
49551a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
495697e99dd9SToby Isaac   const PetscInt    **perms = NULL;
495797e99dd9SToby Isaac   const PetscScalar **flips = NULL;
49581a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
49591a271a75SMatthew G. Knepley 
49601a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4961fe02ba77SJed Brown   *size = 0;
496297e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
496397e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
496497e99dd9SToby Isaac     const PetscInt    point = points[2*p];
496597e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
496697e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
49671a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
49681a271a75SMatthew G. Knepley     const PetscScalar *varr;
49691a271a75SMatthew G. Knepley 
49701a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
49711a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
49721a271a75SMatthew G. Knepley     varr = &vArray[off];
497397e99dd9SToby Isaac     if (clperm) {
497497e99dd9SToby Isaac       if (perm) {
497597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
49761a271a75SMatthew G. Knepley       } else {
497797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
497897e99dd9SToby Isaac       }
497997e99dd9SToby Isaac       if (flip) {
498097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
498197e99dd9SToby Isaac       }
498297e99dd9SToby Isaac     } else {
498397e99dd9SToby Isaac       if (perm) {
498497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
498597e99dd9SToby Isaac       } else {
498697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
498797e99dd9SToby Isaac       }
498897e99dd9SToby Isaac       if (flip) {
498997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
49901a271a75SMatthew G. Knepley       }
49911a271a75SMatthew G. Knepley     }
499297e99dd9SToby Isaac     offset += dof;
499397e99dd9SToby Isaac   }
499497e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
49951a271a75SMatthew G. Knepley   *size = offset;
49961a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
49971a271a75SMatthew G. Knepley }
49981a271a75SMatthew G. Knepley 
499997e99dd9SToby 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[])
50001a271a75SMatthew G. Knepley {
50011a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
50021a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
50031a271a75SMatthew G. Knepley 
50041a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5005fe02ba77SJed Brown   *size = 0;
50061a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
500797e99dd9SToby Isaac     PetscInt          p;
500897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
500997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
50101a271a75SMatthew G. Knepley 
501197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
501297e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
501397e99dd9SToby Isaac       const PetscInt    point = points[2*p];
501497e99dd9SToby Isaac       PetscInt          fdof, foff, b;
50151a271a75SMatthew G. Knepley       const PetscScalar *varr;
501697e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
501797e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
50181a271a75SMatthew G. Knepley 
50191a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
50201a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
50211a271a75SMatthew G. Knepley       varr = &vArray[foff];
502297e99dd9SToby Isaac       if (clperm) {
502397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
502497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
502597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
50261a271a75SMatthew G. Knepley       } else {
502797e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
502897e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
502997e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
50301a271a75SMatthew G. Knepley       }
503197e99dd9SToby Isaac       offset += fdof;
50321a271a75SMatthew G. Knepley     }
503397e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
50341a271a75SMatthew G. Knepley   }
50351a271a75SMatthew G. Knepley   *size = offset;
50361a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
50371a271a75SMatthew G. Knepley }
50381a271a75SMatthew G. Knepley 
5039552f7358SJed Brown /*@C
5040552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
5041552f7358SJed Brown 
5042552f7358SJed Brown   Not collective
5043552f7358SJed Brown 
5044552f7358SJed Brown   Input Parameters:
5045552f7358SJed Brown + dm - The DM
5046552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5047552f7358SJed Brown . v - The local vector
504822c1ee49SMatthew G. Knepley . point - The point in the DM
504922c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
505022c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
5051552f7358SJed Brown 
5052552f7358SJed Brown   Output Parameters:
505322c1ee49SMatthew G. Knepley + csize - The number of values in the closure
505422c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
505522c1ee49SMatthew G. Knepley 
505622c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
505722c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
505822c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
505922c1ee49SMatthew G. Knepley $
506022c1ee49SMatthew G. Knepley $ A typical use could be
506122c1ee49SMatthew G. Knepley $
506222c1ee49SMatthew G. Knepley $  values = NULL;
506322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
506422c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
506522c1ee49SMatthew G. Knepley $    <Compute on closure>
506622c1ee49SMatthew G. Knepley $  }
506722c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
506822c1ee49SMatthew G. Knepley $
506922c1ee49SMatthew G. Knepley $ or
507022c1ee49SMatthew G. Knepley $
507122c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
507222c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
507322c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
507422c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
507522c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
507622c1ee49SMatthew G. Knepley $      <Compute on closure>
507722c1ee49SMatthew G. Knepley $    }
507822c1ee49SMatthew G. Knepley $  }
507922c1ee49SMatthew G. Knepley $  PetscFree(values);
5080552f7358SJed Brown 
5081552f7358SJed Brown   Fortran Notes:
5082552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
5083552f7358SJed Brown   include petsc.h90 in your code.
5084552f7358SJed Brown 
5085552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
5086552f7358SJed Brown 
5087552f7358SJed Brown   Level: intermediate
5088552f7358SJed Brown 
5089552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5090552f7358SJed Brown @*/
5091552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5092552f7358SJed Brown {
5093552f7358SJed Brown   PetscSection       clSection;
5094d9917b9dSMatthew G. Knepley   IS                 clPoints;
5095552f7358SJed Brown   PetscInt          *points = NULL;
5096c459fbc1SJed Brown   const PetscInt    *clp, *perm;
5097c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
5098552f7358SJed Brown   PetscErrorCode     ierr;
5099552f7358SJed Brown 
5100d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
5101552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
510292fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
51031a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
51041a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5105552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5106552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5107552f7358SJed Brown   if (depth == 1 && numFields < 2) {
51081a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5109552f7358SJed Brown     PetscFunctionReturn(0);
5110552f7358SJed Brown   }
51111a271a75SMatthew G. Knepley   /* Get points */
5112923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5113c459fbc1SJed Brown   /* Get sizes */
5114c459fbc1SJed Brown   asize = 0;
5115c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
5116c459fbc1SJed Brown     PetscInt dof;
5117552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
51181a271a75SMatthew G. Knepley     asize += dof;
5119552f7358SJed Brown   }
5120c459fbc1SJed Brown   if (values) {
5121c459fbc1SJed Brown     const PetscScalar *vArray;
5122c459fbc1SJed Brown     PetscInt          size;
5123c459fbc1SJed Brown 
5124c459fbc1SJed Brown     if (*values) {
5125c459fbc1SJed 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);
5126c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
5127c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
51288a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
51291a271a75SMatthew G. Knepley     /* Get values */
5130c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
5131c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
5132c459fbc1SJed Brown     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
51331a271a75SMatthew G. Knepley     /* Cleanup array */
51348a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5135d0f6b257SMatthew G. Knepley   }
5136c459fbc1SJed Brown   if (csize) *csize = asize;
5137c459fbc1SJed Brown   /* Cleanup points */
5138c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5139552f7358SJed Brown   PetscFunctionReturn(0);
5140552f7358SJed Brown }
5141552f7358SJed Brown 
5142e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5143e5c487bfSMatthew G. Knepley {
5144e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
5145e5c487bfSMatthew G. Knepley   PetscSection       clSection;
5146e5c487bfSMatthew G. Knepley   IS                 clPoints;
5147e5c487bfSMatthew G. Knepley   PetscScalar       *array;
5148e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
5149e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
5150c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
5151c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
5152e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
5153e5c487bfSMatthew G. Knepley 
5154e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
5155e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5156e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5157e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5158e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5159e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
5160e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
5161e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5162e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
5163e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5164e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
5165e5c487bfSMatthew G. Knepley   }
5166e5c487bfSMatthew G. Knepley   /* Get points */
5167e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5168c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
5169c459fbc1SJed Brown     PetscInt dof;
5170c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5171c459fbc1SJed Brown     clsize += dof;
5172c459fbc1SJed Brown   }
5173c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
5174e5c487bfSMatthew G. Knepley   /* Filter points */
5175e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
5176e5c487bfSMatthew G. Knepley     PetscInt dep;
5177e5c487bfSMatthew G. Knepley 
5178e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
5179e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
5180e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
5181e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
5182e5c487bfSMatthew G. Knepley     ++Np;
5183e5c487bfSMatthew G. Knepley   }
5184e5c487bfSMatthew G. Knepley   /* Get array */
5185e5c487bfSMatthew G. Knepley   if (!values || !*values) {
5186e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
5187e5c487bfSMatthew G. Knepley 
5188e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
5189e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5190e5c487bfSMatthew G. Knepley       asize += dof;
5191e5c487bfSMatthew G. Knepley     }
5192e5c487bfSMatthew G. Knepley     if (!values) {
5193e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5194e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
5195e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
5196e5c487bfSMatthew G. Knepley     }
5197e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
5198e5c487bfSMatthew G. Knepley   } else {
5199e5c487bfSMatthew G. Knepley     array = *values;
5200e5c487bfSMatthew G. Knepley   }
5201e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
5202e5c487bfSMatthew G. Knepley   /* Get values */
5203e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
5204e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
5205e5c487bfSMatthew G. Knepley   /* Cleanup points */
5206e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5207e5c487bfSMatthew G. Knepley   /* Cleanup array */
5208e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5209e5c487bfSMatthew G. Knepley   if (!*values) {
5210e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
5211e5c487bfSMatthew G. Knepley     *values = array;
5212e5c487bfSMatthew G. Knepley   } else {
5213e5c487bfSMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
5214e5c487bfSMatthew G. Knepley     *csize = size;
5215e5c487bfSMatthew G. Knepley   }
5216e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
5217e5c487bfSMatthew G. Knepley }
5218e5c487bfSMatthew G. Knepley 
5219552f7358SJed Brown /*@C
5220552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5221552f7358SJed Brown 
5222552f7358SJed Brown   Not collective
5223552f7358SJed Brown 
5224552f7358SJed Brown   Input Parameters:
5225552f7358SJed Brown + dm - The DM
52260298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
5227552f7358SJed Brown . v - The local vector
5228eaf898f9SPatrick Sanan . point - The point in the DM
52290298fd71SBarry Smith . csize - The number of values in the closure, or NULL
5230552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
5231552f7358SJed Brown 
523222c1ee49SMatthew 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()
523322c1ee49SMatthew G. Knepley 
52343813dfbdSMatthew G Knepley   Fortran Notes:
52353813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
52363813dfbdSMatthew G Knepley   include petsc.h90 in your code.
52373813dfbdSMatthew G Knepley 
52383813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
52393813dfbdSMatthew G Knepley 
5240552f7358SJed Brown   Level: intermediate
5241552f7358SJed Brown 
5242552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5243552f7358SJed Brown @*/
52447c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5245a6dfd86eSKarl Rupp {
5246552f7358SJed Brown   PetscInt       size = 0;
5247552f7358SJed Brown   PetscErrorCode ierr;
5248552f7358SJed Brown 
5249552f7358SJed Brown   PetscFunctionBegin;
5250552f7358SJed Brown   /* Should work without recalculating size */
525169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
5252c9fdaa05SMatthew G. Knepley   *values = NULL;
5253552f7358SJed Brown   PetscFunctionReturn(0);
5254552f7358SJed Brown }
5255552f7358SJed Brown 
5256552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
5257552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
5258552f7358SJed Brown 
525997e99dd9SToby 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[])
5260552f7358SJed Brown {
5261552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5262552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5263552f7358SJed Brown   PetscScalar    *a;
5264552f7358SJed Brown   PetscInt        off, cind = 0, k;
5265552f7358SJed Brown   PetscErrorCode  ierr;
5266552f7358SJed Brown 
5267552f7358SJed Brown   PetscFunctionBegin;
5268552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5269552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5270552f7358SJed Brown   a    = &array[off];
5271552f7358SJed Brown   if (!cdof || setBC) {
527297e99dd9SToby Isaac     if (clperm) {
527397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
527497e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5275552f7358SJed Brown     } else {
527697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
527797e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5278552f7358SJed Brown     }
5279552f7358SJed Brown   } else {
5280552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
528197e99dd9SToby Isaac     if (clperm) {
528297e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
5283552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
528497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5285552f7358SJed Brown         }
5286552f7358SJed Brown       } else {
5287552f7358SJed Brown         for (k = 0; k < dof; ++k) {
5288552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
528997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
529097e99dd9SToby Isaac         }
529197e99dd9SToby Isaac       }
529297e99dd9SToby Isaac     } else {
529397e99dd9SToby Isaac       if (perm) {
529497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
529597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
529697e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
529797e99dd9SToby Isaac         }
529897e99dd9SToby Isaac       } else {
529997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
530097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
530197e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
530297e99dd9SToby Isaac         }
5303552f7358SJed Brown       }
5304552f7358SJed Brown     }
5305552f7358SJed Brown   }
5306552f7358SJed Brown   PetscFunctionReturn(0);
5307552f7358SJed Brown }
5308552f7358SJed Brown 
530997e99dd9SToby 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[])
5310a5e93ea8SMatthew G. Knepley {
5311a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5312a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5313a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5314a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5315a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5316a5e93ea8SMatthew G. Knepley 
5317a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5318a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5319a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5320a5e93ea8SMatthew G. Knepley   a    = &array[off];
5321a5e93ea8SMatthew G. Knepley   if (cdof) {
5322a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
532397e99dd9SToby Isaac     if (clperm) {
532497e99dd9SToby Isaac       if (perm) {
5325a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5326a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
532797e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
532897e99dd9SToby Isaac             cind++;
5329a5e93ea8SMatthew G. Knepley           }
5330a5e93ea8SMatthew G. Knepley         }
5331a5e93ea8SMatthew G. Knepley       } else {
5332a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5333a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
533497e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
533597e99dd9SToby Isaac             cind++;
533697e99dd9SToby Isaac           }
533797e99dd9SToby Isaac         }
533897e99dd9SToby Isaac       }
533997e99dd9SToby Isaac     } else {
534097e99dd9SToby Isaac       if (perm) {
534197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
534297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
534397e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
534497e99dd9SToby Isaac             cind++;
534597e99dd9SToby Isaac           }
534697e99dd9SToby Isaac         }
534797e99dd9SToby Isaac       } else {
534897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
534997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
535097e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
535197e99dd9SToby Isaac             cind++;
535297e99dd9SToby Isaac           }
5353a5e93ea8SMatthew G. Knepley         }
5354a5e93ea8SMatthew G. Knepley       }
5355a5e93ea8SMatthew G. Knepley     }
5356a5e93ea8SMatthew G. Knepley   }
5357a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5358a5e93ea8SMatthew G. Knepley }
5359a5e93ea8SMatthew G. Knepley 
536097e99dd9SToby 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[])
5361a6dfd86eSKarl Rupp {
5362552f7358SJed Brown   PetscScalar    *a;
53631a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
53641a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
536597e99dd9SToby Isaac   PetscInt        cind = 0, b;
5366552f7358SJed Brown   PetscErrorCode  ierr;
5367552f7358SJed Brown 
5368552f7358SJed Brown   PetscFunctionBegin;
5369552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5370552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
53711a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
53721a271a75SMatthew G. Knepley   a    = &array[foff];
5373552f7358SJed Brown   if (!fcdof || setBC) {
537497e99dd9SToby Isaac     if (clperm) {
537597e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
537697e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5377552f7358SJed Brown     } else {
537897e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
537997e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5380552f7358SJed Brown     }
5381552f7358SJed Brown   } else {
5382552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
538397e99dd9SToby Isaac     if (clperm) {
538497e99dd9SToby Isaac       if (perm) {
538597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
538697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
538797e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5388552f7358SJed Brown         }
5389552f7358SJed Brown       } else {
539097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
539197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
539297e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
539397e99dd9SToby Isaac         }
539497e99dd9SToby Isaac       }
539597e99dd9SToby Isaac     } else {
539697e99dd9SToby Isaac       if (perm) {
539797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
539897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
539997e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
540097e99dd9SToby Isaac         }
540197e99dd9SToby Isaac       } else {
540297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
540397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
540497e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5405552f7358SJed Brown         }
5406552f7358SJed Brown       }
5407552f7358SJed Brown     }
5408552f7358SJed Brown   }
54091a271a75SMatthew G. Knepley   *offset += fdof;
5410552f7358SJed Brown   PetscFunctionReturn(0);
5411552f7358SJed Brown }
5412552f7358SJed Brown 
5413ba322698SMatthew 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[])
5414a5e93ea8SMatthew G. Knepley {
5415a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
54161a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
54171a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
54185da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
5419ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
5420a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5421a5e93ea8SMatthew G. Knepley 
5422a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
54235da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
5424a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5425a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
54261a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
54271a271a75SMatthew G. Knepley   a    = &array[foff];
5428a5e93ea8SMatthew G. Knepley   if (fcdof) {
5429ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
5430a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
543197e99dd9SToby Isaac     if (clperm) {
543297e99dd9SToby Isaac       if (perm) {
5433ba322698SMatthew G. Knepley         if (comps) {
5434ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5435ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
54365da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5437ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5438ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5439ba322698SMatthew G. Knepley           }
5440ba322698SMatthew G. Knepley         } else {
544197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
544297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
544397e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5444a5e93ea8SMatthew G. Knepley               ++cind;
5445a5e93ea8SMatthew G. Knepley             }
5446a5e93ea8SMatthew G. Knepley           }
5447ba322698SMatthew G. Knepley         }
5448ba322698SMatthew G. Knepley       } else {
5449ba322698SMatthew G. Knepley         if (comps) {
5450ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5451ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
54525da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5453ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5454ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5455ba322698SMatthew G. Knepley           }
5456a5e93ea8SMatthew G. Knepley         } else {
545797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
545897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
545997e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
546097e99dd9SToby Isaac               ++cind;
546197e99dd9SToby Isaac             }
546297e99dd9SToby Isaac           }
546397e99dd9SToby Isaac         }
5464ba322698SMatthew G. Knepley       }
546597e99dd9SToby Isaac     } else {
546697e99dd9SToby Isaac       if (perm) {
5467ba322698SMatthew G. Knepley         if (comps) {
5468ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5469ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
54705da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5471ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5472ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5473ba322698SMatthew G. Knepley           }
5474ba322698SMatthew G. Knepley         } else {
547597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
547697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
547797e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
547897e99dd9SToby Isaac               ++cind;
547997e99dd9SToby Isaac             }
548097e99dd9SToby Isaac           }
5481ba322698SMatthew G. Knepley         }
5482ba322698SMatthew G. Knepley       } else {
5483ba322698SMatthew G. Knepley         if (comps) {
5484ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5485ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
54865da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5487ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5488ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5489ba322698SMatthew G. Knepley           }
549097e99dd9SToby Isaac         } else {
549197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
549297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
549397e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5494a5e93ea8SMatthew G. Knepley               ++cind;
5495a5e93ea8SMatthew G. Knepley             }
5496a5e93ea8SMatthew G. Knepley           }
5497a5e93ea8SMatthew G. Knepley         }
5498a5e93ea8SMatthew G. Knepley       }
5499a5e93ea8SMatthew G. Knepley     }
5500ba322698SMatthew G. Knepley   }
55011a271a75SMatthew G. Knepley   *offset += fdof;
5502a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5503a5e93ea8SMatthew G. Knepley }
5504a5e93ea8SMatthew G. Knepley 
55058f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5506a6dfd86eSKarl Rupp {
5507552f7358SJed Brown   PetscScalar    *array;
55081b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
55091b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5510552f7358SJed Brown   PetscErrorCode  ierr;
5511552f7358SJed Brown 
55121b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5513b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5514b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5515b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5516b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5517b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5518b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5519b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5520b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5521b6ebb6e6SMatthew G. Knepley 
5522b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5523b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5524b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5525b6ebb6e6SMatthew G. Knepley     {
5526b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5527b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5528b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5529b6ebb6e6SMatthew G. Knepley 
5530b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5531b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5532b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5533b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5534b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5535b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5536b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5537b6ebb6e6SMatthew G. Knepley           }
5538b6ebb6e6SMatthew G. Knepley         } else {
5539b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5540b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5541b6ebb6e6SMatthew G. Knepley           }
5542b6ebb6e6SMatthew G. Knepley         }
5543b6ebb6e6SMatthew G. Knepley       } else {
5544b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5545b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5546b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5547b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5548b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5549b6ebb6e6SMatthew G. Knepley           }
5550b6ebb6e6SMatthew G. Knepley         } else {
5551b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5552b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5553b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5554b6ebb6e6SMatthew G. Knepley           }
5555b6ebb6e6SMatthew G. Knepley         }
5556b6ebb6e6SMatthew G. Knepley       }
5557b6ebb6e6SMatthew G. Knepley     }
5558b6ebb6e6SMatthew G. Knepley   }
5559b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5560b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5561b6ebb6e6SMatthew G. Knepley }
55621b406b76SMatthew G. Knepley 
55631b406b76SMatthew G. Knepley /*@C
55641b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
55651b406b76SMatthew G. Knepley 
55661b406b76SMatthew G. Knepley   Not collective
55671b406b76SMatthew G. Knepley 
55681b406b76SMatthew G. Knepley   Input Parameters:
55691b406b76SMatthew G. Knepley + dm - The DM
55701b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
55711b406b76SMatthew G. Knepley . v - The local vector
5572eaf898f9SPatrick Sanan . point - The point in the DM
55731b406b76SMatthew G. Knepley . values - The array of values
557422c1ee49SMatthew 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,
557522c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
55761b406b76SMatthew G. Knepley 
55771b406b76SMatthew G. Knepley   Fortran Notes:
55781b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
55791b406b76SMatthew G. Knepley 
55801b406b76SMatthew G. Knepley   Level: intermediate
55811b406b76SMatthew G. Knepley 
55821b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
55831b406b76SMatthew G. Knepley @*/
55841b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
55851b406b76SMatthew G. Knepley {
55861b406b76SMatthew G. Knepley   PetscSection    clSection;
55871b406b76SMatthew G. Knepley   IS              clPoints;
55881b406b76SMatthew G. Knepley   PetscScalar    *array;
55891b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
559027f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
5591c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
55921b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
55931b406b76SMatthew G. Knepley 
55941a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
55951b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
559692fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
55971a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
55981a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
55991b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
56001b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
56011b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
56028f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
56031b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
56041b406b76SMatthew G. Knepley   }
56051a271a75SMatthew G. Knepley   /* Get points */
5606923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5607c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
5608c459fbc1SJed Brown     PetscInt dof;
5609c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5610c459fbc1SJed Brown     clsize += dof;
5611c459fbc1SJed Brown   }
5612c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
56131a271a75SMatthew G. Knepley   /* Get array */
5614552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
56151a271a75SMatthew G. Knepley   /* Get values */
5616ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
561797e99dd9SToby Isaac     PetscInt offset = 0, f;
5618552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
561997e99dd9SToby Isaac       const PetscInt    **perms = NULL;
562097e99dd9SToby Isaac       const PetscScalar **flips = NULL;
562197e99dd9SToby Isaac 
562297e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5623552f7358SJed Brown       switch (mode) {
5624552f7358SJed Brown       case INSERT_VALUES:
562597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
562697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
562797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
562897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
562997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5630552f7358SJed Brown         } break;
5631552f7358SJed Brown       case INSERT_ALL_VALUES:
563297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
563397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
563497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
563597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
563697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5637552f7358SJed Brown         } break;
5638a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
563997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
564097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
564197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
564297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5643ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5644a5e93ea8SMatthew G. Knepley         } break;
5645552f7358SJed Brown       case ADD_VALUES:
564697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
564797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
564897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
564997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
565097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5651552f7358SJed Brown         } break;
5652552f7358SJed Brown       case ADD_ALL_VALUES:
565397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
565497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
565597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
565697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
565797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5658552f7358SJed Brown         } break;
5659304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
566097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
566197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
566297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
566397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5664ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5665304ab55fSMatthew G. Knepley         } break;
5666552f7358SJed Brown       default:
5667f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5668552f7358SJed Brown       }
566997e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
56701a271a75SMatthew G. Knepley     }
5671552f7358SJed Brown   } else {
56721a271a75SMatthew G. Knepley     PetscInt dof, off;
567397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
567497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
56751a271a75SMatthew G. Knepley 
567697e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5677552f7358SJed Brown     switch (mode) {
5678552f7358SJed Brown     case INSERT_VALUES:
567997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
568097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
568197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
568297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
568397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
568497e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5685552f7358SJed Brown       } break;
5686552f7358SJed Brown     case INSERT_ALL_VALUES:
568797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
568897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
568997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
569097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
569197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
569297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5693552f7358SJed Brown       } break;
5694a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
569597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
569697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
569797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
569897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
569997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
570097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5701a5e93ea8SMatthew G. Knepley       } break;
5702552f7358SJed Brown     case ADD_VALUES:
570397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
570497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
570597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
570697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
570797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
570897e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5709552f7358SJed Brown       } break;
5710552f7358SJed Brown     case ADD_ALL_VALUES:
571197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
571697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5717552f7358SJed Brown       } break;
5718304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
571997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
572097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
572197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
572297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
572397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
572497e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5725304ab55fSMatthew G. Knepley       } break;
5726552f7358SJed Brown     default:
5727f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5728552f7358SJed Brown     }
572997e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5730552f7358SJed Brown   }
57311a271a75SMatthew G. Knepley   /* Cleanup points */
5732923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
57331a271a75SMatthew G. Knepley   /* Cleanup array */
5734552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5735552f7358SJed Brown   PetscFunctionReturn(0);
5736552f7358SJed Brown }
5737552f7358SJed Brown 
57385f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
57395f790a90SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
57405f790a90SMatthew G. Knepley {
57415f790a90SMatthew G. Knepley   PetscFunctionBegin;
57425f790a90SMatthew G. Knepley   if (label) {
57435f790a90SMatthew G. Knepley     PetscInt       val, fdof;
57445f790a90SMatthew G. Knepley     PetscErrorCode ierr;
57455f790a90SMatthew G. Knepley 
57465f790a90SMatthew G. Knepley     /* There is a problem with this:
57475f790a90SMatthew 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
57485f790a90SMatthew 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.
57495f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
57505f790a90SMatthew G. Knepley     */
57515f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
57525f790a90SMatthew G. Knepley     if (val < 0) {
57535f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
57545f790a90SMatthew G. Knepley       *offset += fdof;
57555f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
57565f790a90SMatthew G. Knepley     }
57575f790a90SMatthew G. Knepley   }
57585f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
57595f790a90SMatthew G. Knepley }
57605f790a90SMatthew G. Knepley 
576197529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
57625f790a90SMatthew 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)
5763e07394fbSMatthew G. Knepley {
5764e07394fbSMatthew G. Knepley   PetscSection      clSection;
5765e07394fbSMatthew G. Knepley   IS                clPoints;
5766e07394fbSMatthew G. Knepley   PetscScalar       *array;
5767e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
576897529cf3SJed Brown   const PetscInt    *clp;
5769e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
577097e99dd9SToby Isaac   PetscInt          offset = 0, f;
5771e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5772e07394fbSMatthew G. Knepley 
5773e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5774e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
577592fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5776e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5777e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5778e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5779e07394fbSMatthew G. Knepley   /* Get points */
5780923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5781e07394fbSMatthew G. Knepley   /* Get array */
5782e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5783e07394fbSMatthew G. Knepley   /* Get values */
5784e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
578597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
578697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
578797e99dd9SToby Isaac 
5788e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5789e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5790e07394fbSMatthew G. Knepley         PetscInt fdof;
5791e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5792e07394fbSMatthew G. Knepley         offset += fdof;
5793e07394fbSMatthew G. Knepley       }
5794e07394fbSMatthew G. Knepley       continue;
5795e07394fbSMatthew G. Knepley     }
579697e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5797e07394fbSMatthew G. Knepley     switch (mode) {
5798e07394fbSMatthew G. Knepley     case INSERT_VALUES:
579997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
580097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
580197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
580297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58035f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
580497529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5805e07394fbSMatthew G. Knepley       } break;
5806e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
580797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
580897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
580997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
581097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58115f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
581297529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5813e07394fbSMatthew G. Knepley       } break;
5814e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
581597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
581697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
581797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
581897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58195f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
582097529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5821e07394fbSMatthew G. Knepley       } break;
5822e07394fbSMatthew G. Knepley     case ADD_VALUES:
582397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
582497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
582597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
582697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58275f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
582897529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5829e07394fbSMatthew G. Knepley       } break;
5830e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
583197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
583297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
583397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
583497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
58355f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
583697529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5837e07394fbSMatthew G. Knepley       } break;
5838e07394fbSMatthew G. Knepley     default:
5839f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5840e07394fbSMatthew G. Knepley     }
584197e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5842e07394fbSMatthew G. Knepley   }
5843e07394fbSMatthew G. Knepley   /* Cleanup points */
5844923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5845e07394fbSMatthew G. Knepley   /* Cleanup array */
5846e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5847e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5848e07394fbSMatthew G. Knepley }
5849e07394fbSMatthew G. Knepley 
58507cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5851552f7358SJed Brown {
5852552f7358SJed Brown   PetscMPIInt    rank;
5853552f7358SJed Brown   PetscInt       i, j;
5854552f7358SJed Brown   PetscErrorCode ierr;
5855552f7358SJed Brown 
5856552f7358SJed Brown   PetscFunctionBegin;
5857ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr);
5858eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5859e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5860e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5861b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5862557cf195SMatthew G. Knepley   if (!values) PetscFunctionReturn(0);
5863b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5864e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5865b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5866519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
58677eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5868552f7358SJed Brown #else
5869b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5870552f7358SJed Brown #endif
5871552f7358SJed Brown     }
587277a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5873552f7358SJed Brown   }
5874552f7358SJed Brown   PetscFunctionReturn(0);
5875552f7358SJed Brown }
5876552f7358SJed Brown 
587705586334SMatthew G. Knepley /*
587805586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
587905586334SMatthew G. Knepley 
588005586334SMatthew G. Knepley   Input Parameters:
588105586334SMatthew G. Knepley + section - The section for this data layout
588236fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
588305586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
588405586334SMatthew G. Knepley . off     - The global offset of this point
588505586334SMatthew G. Knepley . loff    - The local offset of each field
588605586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
588705586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
588805586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
588905586334SMatthew G. Knepley 
589005586334SMatthew G. Knepley   Output Parameter:
589105586334SMatthew G. Knepley . indices - Indices for dofs on this point
589205586334SMatthew G. Knepley 
589305586334SMatthew G. Knepley   Level: developer
589405586334SMatthew G. Knepley 
589505586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
589605586334SMatthew G. Knepley */
589736fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5898a6dfd86eSKarl Rupp {
5899e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5900552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5901552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5902552f7358SJed Brown   PetscInt        cind = 0, k;
5903552f7358SJed Brown   PetscErrorCode  ierr;
5904552f7358SJed Brown 
5905552f7358SJed Brown   PetscFunctionBegin;
590636fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5907552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5908552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5909552f7358SJed Brown   if (!cdof || setBC) {
591005586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
591105586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
591205586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
591305586334SMatthew G. Knepley 
591405586334SMatthew G. Knepley       indices[ind] = off + k;
5915552f7358SJed Brown     }
5916552f7358SJed Brown   } else {
5917552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
59184acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
591905586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
592005586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
592105586334SMatthew G. Knepley 
59224acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
59234acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
592405586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
59254acb8e1eSToby Isaac         ++cind;
59264acb8e1eSToby Isaac       } else {
592736fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
5928552f7358SJed Brown       }
5929552f7358SJed Brown     }
5930552f7358SJed Brown   }
5931e6ccafaeSMatthew G Knepley   *loff += dof;
5932552f7358SJed Brown   PetscFunctionReturn(0);
5933552f7358SJed Brown }
5934552f7358SJed Brown 
59357e29afd2SMatthew G. Knepley /*
593636fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
59377e29afd2SMatthew G. Knepley 
593836fa2b79SJed Brown  Input Parameters:
593936fa2b79SJed Brown + section - a section (global or local)
594036fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
594136fa2b79SJed Brown . point - point within section
594236fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
594336fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
594436fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
594536fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
594636fa2b79SJed Brown . permsoff - offset
594736fa2b79SJed Brown - indperm - index permutation
594836fa2b79SJed Brown 
594936fa2b79SJed Brown  Output Parameter:
595036fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
595136fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
595236fa2b79SJed Brown 
595336fa2b79SJed Brown  Notes:
595436fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
595536fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
595636fa2b79SJed Brown  in the local vector.
595736fa2b79SJed Brown 
595836fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
595936fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
596036fa2b79SJed Brown 
596136fa2b79SJed Brown  Developer Note:
596236fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
596336fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
596436fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
596536fa2b79SJed Brown 
596636fa2b79SJed Brown  Example:
596736fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
596836fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
596936fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
597036fa2b79SJed 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.
597136fa2b79SJed Brown 
597236fa2b79SJed Brown  Level: developer
59737e29afd2SMatthew G. Knepley */
597436fa2b79SJed 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[])
5975a6dfd86eSKarl Rupp {
5976552f7358SJed Brown   PetscInt       numFields, foff, f;
5977552f7358SJed Brown   PetscErrorCode ierr;
5978552f7358SJed Brown 
5979552f7358SJed Brown   PetscFunctionBegin;
598036fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5981552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5982552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
59834acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5984552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
59854acb8e1eSToby Isaac     PetscInt        cind = 0, b;
59864acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5987552f7358SJed Brown 
5988552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5989552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5990552f7358SJed Brown     if (!cfdof || setBC) {
599105586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
599205586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
599305586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
599405586334SMatthew G. Knepley 
599505586334SMatthew G. Knepley         indices[ind] = off+foff+b;
599605586334SMatthew G. Knepley       }
5997552f7358SJed Brown     } else {
5998552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
599905586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
600005586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
600105586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
600205586334SMatthew G. Knepley 
60034acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
600405586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
6005552f7358SJed Brown           ++cind;
6006552f7358SJed Brown         } else {
600736fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
6008552f7358SJed Brown         }
6009552f7358SJed Brown       }
6010552f7358SJed Brown     }
601136fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
6012552f7358SJed Brown     foffs[f] += fdof;
6013552f7358SJed Brown   }
6014552f7358SJed Brown   PetscFunctionReturn(0);
6015552f7358SJed Brown }
6016552f7358SJed Brown 
60177e29afd2SMatthew G. Knepley /*
60187e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
60197e29afd2SMatthew G. Knepley 
60207e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
6021645102dcSJed Brown 
6022645102dcSJed Brown  Notes:
6023645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6024645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
60257e29afd2SMatthew G. Knepley */
6026645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
60277e29afd2SMatthew G. Knepley {
60287e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
60297e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
60307e29afd2SMatthew G. Knepley 
60317e29afd2SMatthew G. Knepley   PetscFunctionBegin;
60327e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
60337e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
60347e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
60357e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
60367e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
60377e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
60387e29afd2SMatthew G. Knepley 
60397e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
60407e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
60417e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
6042645102dcSJed Brown     if (!cfdof) {
604305586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
604405586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
604505586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
604605586334SMatthew G. Knepley 
604705586334SMatthew G. Knepley         indices[ind] = foff+b;
604805586334SMatthew G. Knepley       }
60497e29afd2SMatthew G. Knepley     } else {
60507e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
605105586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
605205586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
605305586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
605405586334SMatthew G. Knepley 
60557e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
605605586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
60577e29afd2SMatthew G. Knepley           ++cind;
60587e29afd2SMatthew G. Knepley         } else {
605905586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
60607e29afd2SMatthew G. Knepley         }
60617e29afd2SMatthew G. Knepley       }
60627e29afd2SMatthew G. Knepley     }
60637e29afd2SMatthew G. Knepley     foffs[f] += fdof;
60647e29afd2SMatthew G. Knepley   }
60657e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
60667e29afd2SMatthew G. Knepley }
60677e29afd2SMatthew G. Knepley 
60684acb8e1eSToby 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)
6069d3d1a6afSToby Isaac {
6070d3d1a6afSToby Isaac   Mat             cMat;
6071d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
6072d3d1a6afSToby Isaac   IS              aIS;
6073d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
6074d3d1a6afSToby Isaac   const PetscInt  *anchors;
6075e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
6076d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
6077d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
6078d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
6079d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
6080d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
6081d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
6082d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
60836ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
6084d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
6085d3d1a6afSToby Isaac   PetscErrorCode  ierr;
6086d3d1a6afSToby Isaac 
6087d3d1a6afSToby Isaac   PetscFunctionBegin;
6088d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6089d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6090d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6091d3d1a6afSToby Isaac 
6092a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
6093d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
6094d3d1a6afSToby Isaac   if (aSec) {
6095580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
6096d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
6097d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
6098d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
6099d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
6100d3d1a6afSToby Isaac      * into the global matrix anyway) */
6101d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
6102d3d1a6afSToby Isaac       PetscInt b    = points[p];
61034b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6104d3d1a6afSToby Isaac 
61054b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
61064b2f2278SToby Isaac       if (!bSecDof) {
61074b2f2278SToby Isaac         continue;
61084b2f2278SToby Isaac       }
6109d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6110d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
6111d3d1a6afSToby Isaac       }
6112d3d1a6afSToby Isaac       if (bDof) {
6113d3d1a6afSToby Isaac         /* this point is constrained */
6114d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
6115d3d1a6afSToby Isaac         PetscInt bOff, q;
6116d3d1a6afSToby Isaac 
6117d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
6118d3d1a6afSToby Isaac         newNumPoints  += bDof;
6119d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
6120d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6121d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
6122d3d1a6afSToby Isaac           PetscInt aDof;
6123d3d1a6afSToby Isaac 
6124d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
6125d3d1a6afSToby Isaac           newNumIndices += aDof;
6126d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
6127d3d1a6afSToby Isaac             PetscInt fDof;
6128d3d1a6afSToby Isaac 
6129d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
6130d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
6131d3d1a6afSToby Isaac           }
6132d3d1a6afSToby Isaac         }
6133d3d1a6afSToby Isaac       }
6134d3d1a6afSToby Isaac       else {
6135d3d1a6afSToby Isaac         /* this point is not constrained */
6136d3d1a6afSToby Isaac         newNumPoints++;
61374b2f2278SToby Isaac         newNumIndices += bSecDof;
6138d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
6139d3d1a6afSToby Isaac           PetscInt fDof;
6140d3d1a6afSToby Isaac 
6141d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6142d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
6143d3d1a6afSToby Isaac         }
6144d3d1a6afSToby Isaac       }
6145d3d1a6afSToby Isaac     }
6146d3d1a6afSToby Isaac   }
6147d3d1a6afSToby Isaac   if (!anyConstrained) {
614872b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
614972b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
615072b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
615172b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
615272b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
6153d3d1a6afSToby Isaac     PetscFunctionReturn(0);
6154d3d1a6afSToby Isaac   }
6155d3d1a6afSToby Isaac 
61566ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
61576ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
61586ecaa68aSToby Isaac 
6159f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
6160d3d1a6afSToby Isaac 
61616ecaa68aSToby Isaac   if (!outPoints && !outValues) {
61626ecaa68aSToby Isaac     if (offsets) {
61636ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
61646ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
61656ecaa68aSToby Isaac       }
61666ecaa68aSToby Isaac     }
61676ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
61686ecaa68aSToby Isaac     PetscFunctionReturn(0);
61696ecaa68aSToby Isaac   }
61706ecaa68aSToby Isaac 
6171f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
6172d3d1a6afSToby Isaac 
6173f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
6174d3d1a6afSToby Isaac 
6175d3d1a6afSToby Isaac   /* workspaces */
6176d3d1a6afSToby Isaac   if (numFields) {
6177d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
617869291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
617969291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6180d3d1a6afSToby Isaac     }
6181d3d1a6afSToby Isaac   }
6182d3d1a6afSToby Isaac   else {
618369291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
618469291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6185d3d1a6afSToby Isaac   }
6186d3d1a6afSToby Isaac 
6187d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
6188d3d1a6afSToby Isaac   if (numFields) {
61894b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
61904b2f2278SToby Isaac 
6191d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6192d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
61934b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6194d3d1a6afSToby Isaac 
61954b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
61964b2f2278SToby Isaac       if (!bSecDof) {
61974b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
61984b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
61994b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
62004b2f2278SToby Isaac         }
62014b2f2278SToby Isaac         continue;
62024b2f2278SToby Isaac       }
6203d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6204d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6205d3d1a6afSToby Isaac       }
6206d3d1a6afSToby Isaac       if (bDof) {
6207d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6208d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
6209d3d1a6afSToby Isaac 
6210d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6211d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6212d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
6213d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
6214d3d1a6afSToby Isaac             PetscInt aFDof;
6215d3d1a6afSToby Isaac 
6216d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
6217d3d1a6afSToby Isaac             allFDof += aFDof;
6218d3d1a6afSToby Isaac           }
6219d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
6220d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
6221d3d1a6afSToby Isaac         }
6222d3d1a6afSToby Isaac       }
6223d3d1a6afSToby Isaac       else {
6224d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6225d3d1a6afSToby Isaac           PetscInt fDof;
6226d3d1a6afSToby Isaac 
6227d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6228d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
6229d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
6230d3d1a6afSToby Isaac         }
6231d3d1a6afSToby Isaac       }
6232d3d1a6afSToby Isaac     }
62334b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
62344b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
62354b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
6236d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6237d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
6238d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
6239d3d1a6afSToby Isaac       }
624019f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
624119f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
624269291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
6243d3d1a6afSToby Isaac     }
6244d3d1a6afSToby Isaac   }
6245d3d1a6afSToby Isaac   else {
6246d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6247d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
62484b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6249d3d1a6afSToby Isaac 
62504b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
62514b2f2278SToby Isaac       if (!bSecDof) {
62524b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
62534b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
62544b2f2278SToby Isaac         continue;
62554b2f2278SToby Isaac       }
6256d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6257d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6258d3d1a6afSToby Isaac       }
6259d3d1a6afSToby Isaac       if (bDof) {
62604b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
6261d3d1a6afSToby Isaac 
6262d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6263d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6264d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
6265d3d1a6afSToby Isaac 
6266d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
6267d3d1a6afSToby Isaac           allDof += aDof;
6268d3d1a6afSToby Isaac         }
6269d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
62704b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
6271d3d1a6afSToby Isaac       }
6272d3d1a6afSToby Isaac       else {
62734b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
6274d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
6275d3d1a6afSToby Isaac       }
6276d3d1a6afSToby Isaac     }
6277d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
6278d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
6279d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6280d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6281d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6282d3d1a6afSToby Isaac     }
628369291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
6284d3d1a6afSToby Isaac   }
6285d3d1a6afSToby Isaac 
62866ecaa68aSToby Isaac   /* output arrays */
628769291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
62886ecaa68aSToby Isaac 
6289d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
6290d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
6291d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
629269291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
629369291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
6294d3d1a6afSToby Isaac   if (numFields) {
6295d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6296d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6297d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
62984b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6299d3d1a6afSToby Isaac 
63004b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
63014b2f2278SToby Isaac       if (!bSecDof) {
63024b2f2278SToby Isaac         continue;
63034b2f2278SToby Isaac       }
6304d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6305d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6306d3d1a6afSToby Isaac       }
6307d3d1a6afSToby Isaac       if (bDof) {
6308d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6309d3d1a6afSToby Isaac 
6310d3d1a6afSToby Isaac         fStart[0] = 0;
6311d3d1a6afSToby Isaac         fEnd[0]   = 0;
6312d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6313d3d1a6afSToby Isaac           PetscInt fDof;
6314d3d1a6afSToby Isaac 
6315d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6316d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6317d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6318d3d1a6afSToby Isaac         }
6319d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
632036fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6321d3d1a6afSToby Isaac 
6322d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6323d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6324d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6325d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6326d3d1a6afSToby Isaac 
6327d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6328d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6329d3d1a6afSToby Isaac         }
6330d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6331d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6332d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6333d3d1a6afSToby Isaac 
6334d3d1a6afSToby 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 */
6335d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6336d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6337302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
633836fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
6339d3d1a6afSToby Isaac         }
6340d3d1a6afSToby Isaac         newP += bDof;
6341d3d1a6afSToby Isaac 
63426ecaa68aSToby Isaac         if (outValues) {
6343d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
6344d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
6345d3d1a6afSToby 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);
6346d3d1a6afSToby Isaac           }
6347d3d1a6afSToby Isaac         }
63486ecaa68aSToby Isaac       }
6349d3d1a6afSToby Isaac       else {
6350d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6351d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6352d3d1a6afSToby Isaac         newP++;
6353d3d1a6afSToby Isaac       }
6354d3d1a6afSToby Isaac     }
6355d3d1a6afSToby Isaac   } else {
6356d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6357d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6358d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
63594b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6360d3d1a6afSToby Isaac 
63614b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
63624b2f2278SToby Isaac       if (!bSecDof) {
63634b2f2278SToby Isaac         continue;
63644b2f2278SToby Isaac       }
6365d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6366d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6367d3d1a6afSToby Isaac       }
6368d3d1a6afSToby Isaac       if (bDof) {
6369d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
6370d3d1a6afSToby Isaac 
6371d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
637236fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
6373d3d1a6afSToby Isaac 
6374d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
6375d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6376d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6377d3d1a6afSToby Isaac 
6378d3d1a6afSToby 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 */
6379d3d1a6afSToby Isaac 
6380d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6381d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6382302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
638336fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
6384d3d1a6afSToby Isaac         }
6385d3d1a6afSToby Isaac         newP += bDof;
6386d3d1a6afSToby Isaac 
6387d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
63886ecaa68aSToby Isaac         if (outValues) {
6389d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
6390d3d1a6afSToby Isaac         }
63916ecaa68aSToby Isaac       }
6392d3d1a6afSToby Isaac       else {
6393d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
6394d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
6395d3d1a6afSToby Isaac         newP++;
6396d3d1a6afSToby Isaac       }
6397d3d1a6afSToby Isaac     }
6398d3d1a6afSToby Isaac   }
6399d3d1a6afSToby Isaac 
64006ecaa68aSToby Isaac   if (outValues) {
640169291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
6402580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
6403d3d1a6afSToby Isaac     /* multiply constraints on the right */
6404d3d1a6afSToby Isaac     if (numFields) {
6405d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
6406d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
6407d3d1a6afSToby Isaac 
6408d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6409d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
6410d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6411d3d1a6afSToby Isaac           PetscInt c, r, k;
6412d3d1a6afSToby Isaac           PetscInt dof;
6413d3d1a6afSToby Isaac 
6414d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
64154b2f2278SToby Isaac           if (!dof) {
64164b2f2278SToby Isaac             continue;
64174b2f2278SToby Isaac           }
6418d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6419d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6420d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
6421d3d1a6afSToby Isaac 
6422d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6423d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
6424d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
64254acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6426d3d1a6afSToby Isaac                 }
6427d3d1a6afSToby Isaac               }
6428d3d1a6afSToby Isaac             }
6429d3d1a6afSToby Isaac           }
6430d3d1a6afSToby Isaac           else {
6431d3d1a6afSToby Isaac             /* copy this column as is */
6432d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
6433d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
6434d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6435d3d1a6afSToby Isaac               }
6436d3d1a6afSToby Isaac             }
6437d3d1a6afSToby Isaac           }
6438d3d1a6afSToby Isaac           oldOff += dof;
6439d3d1a6afSToby Isaac         }
6440d3d1a6afSToby Isaac       }
6441d3d1a6afSToby Isaac     }
6442d3d1a6afSToby Isaac     else {
6443d3d1a6afSToby Isaac       PetscInt oldOff = 0;
6444d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6445d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
6446d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
6447d3d1a6afSToby Isaac         PetscInt c, r, k;
6448d3d1a6afSToby Isaac         PetscInt dof;
6449d3d1a6afSToby Isaac 
6450d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
64514b2f2278SToby Isaac         if (!dof) {
64524b2f2278SToby Isaac           continue;
64534b2f2278SToby Isaac         }
6454d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6455d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6456d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
6457d3d1a6afSToby Isaac 
6458d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6459d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
6460d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
6461d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6462d3d1a6afSToby Isaac               }
6463d3d1a6afSToby Isaac             }
6464d3d1a6afSToby Isaac           }
6465d3d1a6afSToby Isaac         }
6466d3d1a6afSToby Isaac         else {
6467d3d1a6afSToby Isaac           /* copy this column as is */
6468d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
6469d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
6470d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6471d3d1a6afSToby Isaac             }
6472d3d1a6afSToby Isaac           }
6473d3d1a6afSToby Isaac         }
6474d3d1a6afSToby Isaac         oldOff += dof;
6475d3d1a6afSToby Isaac       }
6476d3d1a6afSToby Isaac     }
6477d3d1a6afSToby Isaac 
64786ecaa68aSToby Isaac     if (multiplyLeft) {
647969291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
6480580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
6481d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
6482d3d1a6afSToby Isaac       if (numFields) {
6483d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6484d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
6485d3d1a6afSToby Isaac 
6486d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
6487d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
6488d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
6489d3d1a6afSToby Isaac             PetscInt c, r, k;
6490d3d1a6afSToby Isaac             PetscInt dof;
6491d3d1a6afSToby Isaac 
6492d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
6493d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6494d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
6495d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
6496d3d1a6afSToby Isaac 
6497d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
6498d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6499d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
6500d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6501d3d1a6afSToby Isaac                   }
6502d3d1a6afSToby Isaac                 }
6503d3d1a6afSToby Isaac               }
6504d3d1a6afSToby Isaac             }
6505d3d1a6afSToby Isaac             else {
6506d3d1a6afSToby Isaac               /* copy this row as is */
6507d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6508d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6509d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6510d3d1a6afSToby Isaac                 }
6511d3d1a6afSToby Isaac               }
6512d3d1a6afSToby Isaac             }
6513d3d1a6afSToby Isaac             oldOff += dof;
6514d3d1a6afSToby Isaac           }
6515d3d1a6afSToby Isaac         }
6516d3d1a6afSToby Isaac       }
6517d3d1a6afSToby Isaac       else {
6518d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6519d3d1a6afSToby Isaac 
6520d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6521d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6522d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6523d3d1a6afSToby Isaac           PetscInt c, r, k;
6524d3d1a6afSToby Isaac           PetscInt dof;
6525d3d1a6afSToby Isaac 
6526d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6527d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6528d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6529d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6530d3d1a6afSToby Isaac 
6531d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6532d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6533d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6534d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6535d3d1a6afSToby Isaac                 }
6536d3d1a6afSToby Isaac               }
6537d3d1a6afSToby Isaac             }
6538d3d1a6afSToby Isaac           }
6539d3d1a6afSToby Isaac           else {
6540d3d1a6afSToby Isaac             /* copy this row as is */
65419fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6542d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6543d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6544d3d1a6afSToby Isaac               }
6545d3d1a6afSToby Isaac             }
6546d3d1a6afSToby Isaac           }
6547d3d1a6afSToby Isaac           oldOff += dof;
6548d3d1a6afSToby Isaac         }
6549d3d1a6afSToby Isaac       }
6550d3d1a6afSToby Isaac 
655169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
65526ecaa68aSToby Isaac     }
65536ecaa68aSToby Isaac     else {
65546ecaa68aSToby Isaac       newValues = tmpValues;
65556ecaa68aSToby Isaac     }
65566ecaa68aSToby Isaac   }
65576ecaa68aSToby Isaac 
6558d3d1a6afSToby Isaac   /* clean up */
655969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
656069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
65616ecaa68aSToby Isaac 
6562d3d1a6afSToby Isaac   if (numFields) {
6563d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
656469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
656569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
656669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6567d3d1a6afSToby Isaac     }
6568d3d1a6afSToby Isaac   }
6569d3d1a6afSToby Isaac   else {
657069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
657169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
657269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6573d3d1a6afSToby Isaac   }
6574d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6575d3d1a6afSToby Isaac 
6576d3d1a6afSToby Isaac   /* output */
65776ecaa68aSToby Isaac   if (outPoints) {
6578d3d1a6afSToby Isaac     *outPoints = newPoints;
65796ecaa68aSToby Isaac   }
65806ecaa68aSToby Isaac   else {
658169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
65826ecaa68aSToby Isaac   }
658331620726SToby Isaac   if (outValues) {
6584d3d1a6afSToby Isaac     *outValues = newValues;
65856ecaa68aSToby Isaac   }
65866ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6587d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6588d3d1a6afSToby Isaac   }
6589d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6590d3d1a6afSToby Isaac }
6591d3d1a6afSToby Isaac 
65924a1e0b3eSMatthew G. Knepley /*@C
659371f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
65947cd05799SMatthew G. Knepley 
65957cd05799SMatthew G. Knepley   Not collective
65967cd05799SMatthew G. Knepley 
65977cd05799SMatthew G. Knepley   Input Parameters:
65987cd05799SMatthew G. Knepley + dm         - The DM
659971f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
660071f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
660171f0bbf9SMatthew G. Knepley . point      - The point defining the closure
660271f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
66037cd05799SMatthew G. Knepley 
660471f0bbf9SMatthew G. Knepley   Output Parameters:
660571f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
660671f0bbf9SMatthew G. Knepley . indices    - The dof indices
660771f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
660871f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
66097cd05799SMatthew G. Knepley 
661036fa2b79SJed Brown   Notes:
661136fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
661236fa2b79SJed Brown 
661336fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
661436fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
661536fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
661636fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
661736fa2b79SJed Brown   indices (with the above semantics) are implied.
66187cd05799SMatthew G. Knepley 
66197cd05799SMatthew G. Knepley   Level: advanced
66207cd05799SMatthew G. Knepley 
662136fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
66224a1e0b3eSMatthew G. Knepley @*/
662371f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
662471f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
66257773e69fSMatthew G. Knepley {
662671f0bbf9SMatthew G. Knepley   /* Closure ordering */
66277773e69fSMatthew G. Knepley   PetscSection        clSection;
66287773e69fSMatthew G. Knepley   IS                  clPoints;
662971f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
663071f0bbf9SMatthew G. Knepley   PetscInt           *points;
663171f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
663271f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
66334acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
663471f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
663571f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
663671f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
663771f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
663871f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
663971f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
664071f0bbf9SMatthew G. Knepley 
664171f0bbf9SMatthew G. Knepley   PetscInt           *idx;
664271f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
664371f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
66447773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
66457773e69fSMatthew G. Knepley 
664671f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
66477773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66487773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
664936fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
665071f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
665171f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
665271f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
665371f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
66547773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
66557773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6656580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
665771f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
665871f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
6659c459fbc1SJed Brown   if (useClPerm) {
6660c459fbc1SJed Brown     PetscInt depth, clsize;
6661c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
6662c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
6663c459fbc1SJed Brown       PetscInt dof;
6664c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6665c459fbc1SJed Brown       clsize += dof;
6666c459fbc1SJed Brown     }
6667c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
6668c459fbc1SJed Brown   }
666971f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
667071f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
66717773e69fSMatthew G. Knepley     PetscInt dof, fdof;
66727773e69fSMatthew G. Knepley 
66737773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
66747773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
66757773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
66767773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
66777773e69fSMatthew G. Knepley     }
667871f0bbf9SMatthew G. Knepley     Ni += dof;
66797773e69fSMatthew G. Knepley   }
66807773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
668171f0bbf9SMatthew 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);
668271f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
668371f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
668471f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
668571f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
668671f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
668771f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
668871f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
66896ecaa68aSToby Isaac 
669071f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
669171f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
669271f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
669371f0bbf9SMatthew G. Knepley 
669471f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
669571f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
669671f0bbf9SMatthew G. Knepley         if (flip) {
669771f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
669871f0bbf9SMatthew G. Knepley 
669971f0bbf9SMatthew G. Knepley           if (!valCopy) {
670071f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
670171f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
670271f0bbf9SMatthew G. Knepley             *values = valCopy;
670371f0bbf9SMatthew G. Knepley           }
670471f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
670571f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
670671f0bbf9SMatthew G. Knepley 
670771f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
670871f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
670971f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
67106ecaa68aSToby Isaac             }
67116ecaa68aSToby Isaac           }
671271f0bbf9SMatthew G. Knepley         }
671371f0bbf9SMatthew G. Knepley         foffset += fdof;
671471f0bbf9SMatthew G. Knepley       }
671571f0bbf9SMatthew G. Knepley     }
671671f0bbf9SMatthew G. Knepley   }
671771f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
671871f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
671971f0bbf9SMatthew G. Knepley   if (NclC) {
672071f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
672171f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
672271f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
672371f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
672471f0bbf9SMatthew G. Knepley     }
672571f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
672671f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
672771f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
672871f0bbf9SMatthew G. Knepley     }
672971f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
673071f0bbf9SMatthew G. Knepley     Ncl     = NclC;
673171f0bbf9SMatthew G. Knepley     Ni      = NiC;
673271f0bbf9SMatthew G. Knepley     points  = pointsC;
673371f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
673471f0bbf9SMatthew G. Knepley   }
673571f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
673671f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
673771f0bbf9SMatthew G. Knepley   if (Nf) {
673871f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
673971f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
674071f0bbf9SMatthew G. Knepley 
674171f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
674271f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
674371f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
674471f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
674571f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
674671f0bbf9SMatthew G. Knepley 
674771f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
67487773e69fSMatthew G. Knepley       }
67497773e69fSMatthew G. Knepley     } else {
675071f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
675171f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
675271f0bbf9SMatthew G. Knepley 
675371f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
675471f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
675571f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
675671f0bbf9SMatthew G. Knepley          * global section. */
675771f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
675871f0bbf9SMatthew G. Knepley       }
675971f0bbf9SMatthew G. Knepley     }
676071f0bbf9SMatthew G. Knepley   } else {
676171f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
676271f0bbf9SMatthew G. Knepley 
676371f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
676471f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
67654acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
67664acb8e1eSToby Isaac 
676771f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
676871f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
676971f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
677071f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
67717773e69fSMatthew G. Knepley     }
67727773e69fSMatthew G. Knepley   }
677371f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
677471f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
677571f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
677671f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
67774acb8e1eSToby Isaac   }
677871f0bbf9SMatthew G. Knepley   if (NclC) {
677971f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
67807773e69fSMatthew G. Knepley   } else {
678171f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
67827773e69fSMatthew G. Knepley   }
678371f0bbf9SMatthew G. Knepley 
678471f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
678571f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
67867773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
67877773e69fSMatthew G. Knepley }
67887773e69fSMatthew G. Knepley 
67897cd05799SMatthew G. Knepley /*@C
679071f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
67917cd05799SMatthew G. Knepley 
67927cd05799SMatthew G. Knepley   Not collective
67937cd05799SMatthew G. Knepley 
67947cd05799SMatthew G. Knepley   Input Parameters:
67957cd05799SMatthew G. Knepley + dm         - The DM
679671f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
679771f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
679871f0bbf9SMatthew G. Knepley . point      - The point defining the closure
679971f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
680071f0bbf9SMatthew G. Knepley 
680171f0bbf9SMatthew G. Knepley   Output Parameters:
680271f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
680371f0bbf9SMatthew G. Knepley . indices    - The dof indices
680471f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
680571f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
680671f0bbf9SMatthew G. Knepley 
680771f0bbf9SMatthew G. Knepley   Notes:
680871f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
680971f0bbf9SMatthew G. Knepley 
681071f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
681171f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
681271f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
681371f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
681471f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
68157cd05799SMatthew G. Knepley 
68167cd05799SMatthew G. Knepley   Level: advanced
68177cd05799SMatthew G. Knepley 
681871f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
68197cd05799SMatthew G. Knepley @*/
682071f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
682171f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
68227773e69fSMatthew G. Knepley {
68237773e69fSMatthew G. Knepley   PetscErrorCode ierr;
68247773e69fSMatthew G. Knepley 
68257773e69fSMatthew G. Knepley   PetscFunctionBegin;
68267773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6827064a246eSJacob Faibussowitsch   PetscValidPointer(indices, 7);
682869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
68297773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
68307773e69fSMatthew G. Knepley }
68317773e69fSMatthew G. Knepley 
68327f5d1fdeSMatthew G. Knepley /*@C
68337f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
68347f5d1fdeSMatthew G. Knepley 
68357f5d1fdeSMatthew G. Knepley   Not collective
68367f5d1fdeSMatthew G. Knepley 
68377f5d1fdeSMatthew G. Knepley   Input Parameters:
68387f5d1fdeSMatthew G. Knepley + dm - The DM
6839ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6840ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
68417f5d1fdeSMatthew G. Knepley . A - The matrix
6842eaf898f9SPatrick Sanan . point - The point in the DM
68437f5d1fdeSMatthew G. Knepley . values - The array of values
68447f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
68457f5d1fdeSMatthew G. Knepley 
68467f5d1fdeSMatthew G. Knepley   Fortran Notes:
68477f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
68487f5d1fdeSMatthew G. Knepley 
68497f5d1fdeSMatthew G. Knepley   Level: intermediate
68507f5d1fdeSMatthew G. Knepley 
68514a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
68527f5d1fdeSMatthew G. Knepley @*/
68537c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6854552f7358SJed Brown {
6855552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
6856552f7358SJed Brown   PetscInt          *indices;
685771f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
685871f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
6859552f7358SJed Brown   PetscErrorCode     ierr;
6860552f7358SJed Brown 
6861552f7358SJed Brown   PetscFunctionBegin;
6862552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
686392fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
68643dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6865e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
68663dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
68673dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6868552f7358SJed Brown 
686971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
68700d644c17SKarl Rupp 
6871b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
68724a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6873552f7358SJed Brown   if (ierr) {
6874552f7358SJed Brown     PetscMPIInt    rank;
6875552f7358SJed Brown     PetscErrorCode ierr2;
6876552f7358SJed Brown 
687755b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6878e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6879b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
688071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
688171f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6882552f7358SJed Brown     CHKERRQ(ierr);
6883552f7358SJed Brown   }
68844a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
68854a1e0b3eSMatthew G. Knepley     PetscInt i;
68864a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
68874a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
68884a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68894a1e0b3eSMatthew G. Knepley   }
689071f0bbf9SMatthew G. Knepley 
689171f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
689271f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
689371f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
68944acb8e1eSToby Isaac }
689571f0bbf9SMatthew G. Knepley 
68964a1e0b3eSMatthew G. Knepley /*@C
68974a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
68984a1e0b3eSMatthew G. Knepley 
68994a1e0b3eSMatthew G. Knepley   Not collective
69004a1e0b3eSMatthew G. Knepley 
69014a1e0b3eSMatthew G. Knepley   Input Parameters:
69024a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
69034a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
69044a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
69054a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
69064a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
69074a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
69084a1e0b3eSMatthew G. Knepley . A - The matrix
69094a1e0b3eSMatthew G. Knepley . point - The point in the DMs
69104a1e0b3eSMatthew G. Knepley . values - The array of values
69114a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
69124a1e0b3eSMatthew G. Knepley 
69134a1e0b3eSMatthew G. Knepley   Level: intermediate
69144a1e0b3eSMatthew G. Knepley 
69154a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
69164a1e0b3eSMatthew G. Knepley @*/
691771f0bbf9SMatthew 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)
691871f0bbf9SMatthew G. Knepley {
691971f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
692071f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
692171f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
692271f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
692371f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
692471f0bbf9SMatthew G. Knepley 
692571f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
692671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
692771f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
692871f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
692971f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
693071f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
693171f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
693271f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
693371f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
693471f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
693571f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
693671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
693771f0bbf9SMatthew G. Knepley 
693871f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
693971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
694071f0bbf9SMatthew G. Knepley 
694171f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
69424a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
694371f0bbf9SMatthew G. Knepley   if (ierr) {
694471f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
694571f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
694671f0bbf9SMatthew G. Knepley 
694755b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
694871f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
694971f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
695071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
695171f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
695271f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
695371f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
6954d3d1a6afSToby Isaac   }
695571f0bbf9SMatthew G. Knepley 
695671f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
695771f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
695871f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
6959552f7358SJed Brown   PetscFunctionReturn(0);
6960552f7358SJed Brown }
6961552f7358SJed Brown 
6962de41b84cSMatthew 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)
6963de41b84cSMatthew G. Knepley {
6964de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6965de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6966de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6967de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
696817c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6969de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6970412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
69714ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6972de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6973de41b84cSMatthew G. Knepley 
6974de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6975de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6976de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
697792fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6978de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
697992fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6980de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6981e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6982de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6983e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6984de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6985de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6986de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6987de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6988580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6989580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
6990de41b84cSMatthew G. Knepley   /* Column indices */
6991de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
69924ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6993de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6994de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6995de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6996de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6997de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6998de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6999de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
7000de41b84cSMatthew G. Knepley       ++q;
7001de41b84cSMatthew G. Knepley     }
7002de41b84cSMatthew G. Knepley   }
7003de41b84cSMatthew G. Knepley   numCPoints = q;
7004de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
7005de41b84cSMatthew G. Knepley     PetscInt fdof;
7006de41b84cSMatthew G. Knepley 
7007de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
70084ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7009de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7010de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
7011de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
7012de41b84cSMatthew G. Knepley     }
7013de41b84cSMatthew G. Knepley     numCIndices += dof;
7014de41b84cSMatthew G. Knepley   }
7015de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
7016de41b84cSMatthew G. Knepley   /* Row indices */
7017412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7018412e9a14SMatthew G. Knepley   {
7019412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7020412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7021412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7022412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7023412e9a14SMatthew G. Knepley   }
702469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7025de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
7026de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
7027de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7028de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
7029de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
7030de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
7031de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
70324ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
70334ca5e9f5SMatthew G. Knepley         if (!dof) continue;
70344ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
70354ca5e9f5SMatthew G. Knepley         if (s < q) continue;
7036de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
7037de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
7038de41b84cSMatthew G. Knepley         ++q;
7039de41b84cSMatthew G. Knepley       }
7040de41b84cSMatthew G. Knepley     }
7041de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7042de41b84cSMatthew G. Knepley   }
7043de41b84cSMatthew G. Knepley   numFPoints = q;
7044de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
7045de41b84cSMatthew G. Knepley     PetscInt fdof;
7046de41b84cSMatthew G. Knepley 
7047de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
70484ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7049de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7050de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
7051de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
7052de41b84cSMatthew G. Knepley     }
7053de41b84cSMatthew G. Knepley     numFIndices += dof;
7054de41b84cSMatthew G. Knepley   }
7055de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
7056de41b84cSMatthew G. Knepley 
70578ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
70588ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
705969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
706069291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7061de41b84cSMatthew G. Knepley   if (numFields) {
70624acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
70634acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
70644acb8e1eSToby Isaac 
70654acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
70664acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
70674acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7068de41b84cSMatthew G. Knepley     }
70694acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
70704acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
707136fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
70724acb8e1eSToby Isaac     }
70734acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
70744acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
707536fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
70764acb8e1eSToby Isaac     }
70774acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
70784acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
70794acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7080de41b84cSMatthew G. Knepley     }
7081de41b84cSMatthew G. Knepley   } else {
70824acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
70834acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
70844acb8e1eSToby Isaac 
70854acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
70864acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
70874acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
70884acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
70894acb8e1eSToby Isaac 
70904acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
709136fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
7092de41b84cSMatthew G. Knepley     }
70934acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
70944acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
70954acb8e1eSToby Isaac 
70964acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
709736fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
7098de41b84cSMatthew G. Knepley     }
70994acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
71004acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
7101de41b84cSMatthew G. Knepley   }
7102de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
71034acb8e1eSToby Isaac   /* TODO: flips */
7104de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7105de41b84cSMatthew G. Knepley   if (ierr) {
7106de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
7107de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
7108de41b84cSMatthew G. Knepley 
710955b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
7110e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
7111de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
711269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
711369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
7114de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
7115de41b84cSMatthew G. Knepley   }
711669291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7117de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
711869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
711969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7120de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
7121de41b84cSMatthew G. Knepley }
7122de41b84cSMatthew G. Knepley 
71237c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
71247c927364SMatthew G. Knepley {
71257c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
71267c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
71277c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
712817c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7129412e9a14SMatthew G. Knepley   DMPolytopeType ct;
71307c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
71317c927364SMatthew G. Knepley   PetscErrorCode ierr;
71327c927364SMatthew G. Knepley 
71337c927364SMatthew G. Knepley   PetscFunctionBegin;
71347c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
71357c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
713692fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
71377c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
713892fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
71397c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7140e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
71417c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7142e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
71437c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
71447c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
71457c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7146580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7147580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
71487c927364SMatthew G. Knepley   /* Column indices */
71497c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
71507c927364SMatthew G. Knepley   maxFPoints = numCPoints;
71517c927364SMatthew G. Knepley   /* Compress out points not in the section */
71527c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
71537c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
71547c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
71557c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
71567c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
71577c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
71587c927364SMatthew G. Knepley       ++q;
71597c927364SMatthew G. Knepley     }
71607c927364SMatthew G. Knepley   }
71617c927364SMatthew G. Knepley   numCPoints = q;
71627c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
71637c927364SMatthew G. Knepley     PetscInt fdof;
71647c927364SMatthew G. Knepley 
71657c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
71667c927364SMatthew G. Knepley     if (!dof) continue;
71677c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
71687c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
71697c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
71707c927364SMatthew G. Knepley     }
71717c927364SMatthew G. Knepley     numCIndices += dof;
71727c927364SMatthew G. Knepley   }
71737c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
71747c927364SMatthew G. Knepley   /* Row indices */
7175412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7176412e9a14SMatthew G. Knepley   {
7177412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
7178412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
7179412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
7180412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
7181412e9a14SMatthew G. Knepley   }
718269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
71837c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
71847c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
71857c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
71867c927364SMatthew G. Knepley     /* Compress out points not in the section */
71877c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
71887c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
71897c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
71907c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
71917c927364SMatthew G. Knepley         if (!dof) continue;
71927c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
71937c927364SMatthew G. Knepley         if (s < q) continue;
71947c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
71957c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
71967c927364SMatthew G. Knepley         ++q;
71977c927364SMatthew G. Knepley       }
71987c927364SMatthew G. Knepley     }
71997c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
72007c927364SMatthew G. Knepley   }
72017c927364SMatthew G. Knepley   numFPoints = q;
72027c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
72037c927364SMatthew G. Knepley     PetscInt fdof;
72047c927364SMatthew G. Knepley 
72057c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
72067c927364SMatthew G. Knepley     if (!dof) continue;
72077c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
72087c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
72097c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
72107c927364SMatthew G. Knepley     }
72117c927364SMatthew G. Knepley     numFIndices += dof;
72127c927364SMatthew G. Knepley   }
72137c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
72147c927364SMatthew G. Knepley 
72158ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
72168ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
72177c927364SMatthew G. Knepley   if (numFields) {
72184acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
72194acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
72204acb8e1eSToby Isaac 
72214acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
72224acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
72234acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
72247c927364SMatthew G. Knepley     }
72254acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
72264acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
722736fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
72284acb8e1eSToby Isaac     }
72294acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
72304acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
723136fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
72324acb8e1eSToby Isaac     }
72334acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
72344acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
72354acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
72367c927364SMatthew G. Knepley     }
72377c927364SMatthew G. Knepley   } else {
72384acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
72394acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
72404acb8e1eSToby Isaac 
72414acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
72424acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
72434acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
72444acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
72454acb8e1eSToby Isaac 
72464acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
724736fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
72487c927364SMatthew G. Knepley     }
72494acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
72504acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
72514acb8e1eSToby Isaac 
72524acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
725336fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
72547c927364SMatthew G. Knepley     }
72554acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
72564acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
72577c927364SMatthew G. Knepley   }
725869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
72597c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
72607c927364SMatthew G. Knepley   PetscFunctionReturn(0);
72617c927364SMatthew G. Knepley }
72627c927364SMatthew G. Knepley 
72637cd05799SMatthew G. Knepley /*@C
72647cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
72657cd05799SMatthew G. Knepley 
72667cd05799SMatthew G. Knepley   Input Parameter:
72677cd05799SMatthew G. Knepley . dm   - The DMPlex object
72687cd05799SMatthew G. Knepley 
72697cd05799SMatthew G. Knepley   Output Parameter:
72707cd05799SMatthew G. Knepley . cellHeight - The height of a cell
72717cd05799SMatthew G. Knepley 
72727cd05799SMatthew G. Knepley   Level: developer
72737cd05799SMatthew G. Knepley 
72747cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
72757cd05799SMatthew G. Knepley @*/
7276552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7277552f7358SJed Brown {
7278552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7279552f7358SJed Brown 
7280552f7358SJed Brown   PetscFunctionBegin;
7281552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7282552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
7283552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
7284552f7358SJed Brown   PetscFunctionReturn(0);
7285552f7358SJed Brown }
7286552f7358SJed Brown 
72877cd05799SMatthew G. Knepley /*@C
72887cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
72897cd05799SMatthew G. Knepley 
72907cd05799SMatthew G. Knepley   Input Parameters:
72917cd05799SMatthew G. Knepley + dm   - The DMPlex object
72927cd05799SMatthew G. Knepley - cellHeight - The height of a cell
72937cd05799SMatthew G. Knepley 
72947cd05799SMatthew G. Knepley   Level: developer
72957cd05799SMatthew G. Knepley 
72967cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
72977cd05799SMatthew G. Knepley @*/
7298552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7299552f7358SJed Brown {
7300552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7301552f7358SJed Brown 
7302552f7358SJed Brown   PetscFunctionBegin;
7303552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7304552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7305552f7358SJed Brown   PetscFunctionReturn(0);
7306552f7358SJed Brown }
7307552f7358SJed Brown 
7308e6139122SMatthew G. Knepley /*@
7309e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7310e6139122SMatthew G. Knepley 
7311e6139122SMatthew G. Knepley   Input Parameter:
7312e6139122SMatthew G. Knepley . dm - The DMPlex object
7313e6139122SMatthew G. Knepley 
7314e6139122SMatthew G. Knepley   Output Parameters:
73152a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
73162a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7317e6139122SMatthew G. Knepley 
73182a9f31c0SMatthew G. Knepley   Level: advanced
7319e6139122SMatthew G. Knepley 
73208065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7321e6139122SMatthew G. Knepley @*/
7322e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7323e6139122SMatthew G. Knepley {
7324412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7325e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7326e6139122SMatthew G. Knepley 
7327e6139122SMatthew G. Knepley   PetscFunctionBegin;
7328e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7329412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
7330412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
7331e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
7332e6139122SMatthew G. Knepley }
7333e6139122SMatthew G. Knepley 
7334552f7358SJed Brown /* We can easily have a form that takes an IS instead */
73359886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7336552f7358SJed Brown {
7337552f7358SJed Brown   PetscSection   section, globalSection;
7338552f7358SJed Brown   PetscInt      *numbers, p;
7339552f7358SJed Brown   PetscErrorCode ierr;
7340552f7358SJed Brown 
7341552f7358SJed Brown   PetscFunctionBegin;
734282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
7343552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
7344552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7345552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
7346552f7358SJed Brown   }
7347552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
734815b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
7349854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
7350552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
7351552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
7352ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7353ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
7354552f7358SJed Brown   }
735582f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
7356ef48cebcSMatthew G. Knepley   if (globalSize) {
7357ef48cebcSMatthew G. Knepley     PetscLayout layout;
7358ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
7359ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
7360ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
7361ef48cebcSMatthew G. Knepley   }
7362552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
7363552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
7364552f7358SJed Brown   PetscFunctionReturn(0);
7365552f7358SJed Brown }
7366552f7358SJed Brown 
736781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7368552f7358SJed Brown {
7369412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
7370552f7358SJed Brown   PetscErrorCode ierr;
7371552f7358SJed Brown 
7372552f7358SJed Brown   PetscFunctionBegin;
7373552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7374412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
7375412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
73769886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
737781ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
7378552f7358SJed Brown }
737981ed3555SMatthew G. Knepley 
73808dab3259SMatthew G. Knepley /*@
73817cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
73827cd05799SMatthew G. Knepley 
73837cd05799SMatthew G. Knepley   Input Parameter:
73847cd05799SMatthew G. Knepley . dm   - The DMPlex object
73857cd05799SMatthew G. Knepley 
73867cd05799SMatthew G. Knepley   Output Parameter:
73877cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
73887cd05799SMatthew G. Knepley 
73897cd05799SMatthew G. Knepley   Level: developer
73907cd05799SMatthew G. Knepley 
73917cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
73927cd05799SMatthew G. Knepley @*/
739381ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
739481ed3555SMatthew G. Knepley {
739581ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
739681ed3555SMatthew G. Knepley   PetscErrorCode ierr;
739781ed3555SMatthew G. Knepley 
739881ed3555SMatthew G. Knepley   PetscFunctionBegin;
739981ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
740081ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
7401552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
7402552f7358SJed Brown   PetscFunctionReturn(0);
7403552f7358SJed Brown }
7404552f7358SJed Brown 
740581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
740681ed3555SMatthew G. Knepley {
7407412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
740881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
740981ed3555SMatthew G. Knepley 
741081ed3555SMatthew G. Knepley   PetscFunctionBegin;
741181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
741281ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
74139886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
741481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
741581ed3555SMatthew G. Knepley }
741681ed3555SMatthew G. Knepley 
74178dab3259SMatthew G. Knepley /*@
74186aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
74197cd05799SMatthew G. Knepley 
74207cd05799SMatthew G. Knepley   Input Parameter:
74217cd05799SMatthew G. Knepley . dm   - The DMPlex object
74227cd05799SMatthew G. Knepley 
74237cd05799SMatthew G. Knepley   Output Parameter:
74247cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
74257cd05799SMatthew G. Knepley 
74267cd05799SMatthew G. Knepley   Level: developer
74277cd05799SMatthew G. Knepley 
74287cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
74297cd05799SMatthew G. Knepley @*/
7430552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7431552f7358SJed Brown {
7432552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
7433552f7358SJed Brown   PetscErrorCode ierr;
7434552f7358SJed Brown 
7435552f7358SJed Brown   PetscFunctionBegin;
7436552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
743781ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
7438552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
7439552f7358SJed Brown   PetscFunctionReturn(0);
7440552f7358SJed Brown }
7441552f7358SJed Brown 
74428dab3259SMatthew G. Knepley /*@
74437cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
74447cd05799SMatthew G. Knepley 
74457cd05799SMatthew G. Knepley   Input Parameter:
74467cd05799SMatthew G. Knepley . dm   - The DMPlex object
74477cd05799SMatthew G. Knepley 
74487cd05799SMatthew G. Knepley   Output Parameter:
74497cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
74507cd05799SMatthew G. Knepley 
74517cd05799SMatthew G. Knepley   Level: developer
74527cd05799SMatthew G. Knepley 
74537cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
74547cd05799SMatthew G. Knepley @*/
7455ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7456ef48cebcSMatthew G. Knepley {
7457ef48cebcSMatthew G. Knepley   IS             nums[4];
7458862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
7459ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
7460ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
7461ef48cebcSMatthew G. Knepley 
7462ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
7463ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7464ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
74658abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
74668abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
7467862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7468862913ffSStefano Zampini     PetscInt end;
7469862913ffSStefano Zampini 
7470862913ffSStefano Zampini     depths[d] = depth-d;
7471862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
7472862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7473862913ffSStefano Zampini   }
7474862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
7475820f2d46SBarry Smith   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
7476862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
7477862913ffSStefano 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]);
7478862913ffSStefano Zampini   }
7479ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
7480ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
7481ef48cebcSMatthew G. Knepley 
7482862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
74839886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
7484ef48cebcSMatthew G. Knepley     shift += gsize;
7485ef48cebcSMatthew G. Knepley   }
7486302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7487ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7488ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7489ef48cebcSMatthew G. Knepley }
7490ef48cebcSMatthew G. Knepley 
749108a22f4bSMatthew G. Knepley /*@
749208a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
749308a22f4bSMatthew G. Knepley 
749408a22f4bSMatthew G. Knepley   Input Parameter:
749508a22f4bSMatthew G. Knepley . dm - The DMPlex object
749608a22f4bSMatthew G. Knepley 
749708a22f4bSMatthew G. Knepley   Output Parameter:
749808a22f4bSMatthew G. Knepley . ranks - The rank field
749908a22f4bSMatthew G. Knepley 
750008a22f4bSMatthew G. Knepley   Options Database Keys:
750108a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
750208a22f4bSMatthew G. Knepley 
750308a22f4bSMatthew G. Knepley   Level: intermediate
750408a22f4bSMatthew G. Knepley 
750508a22f4bSMatthew G. Knepley .seealso: DMView()
750608a22f4bSMatthew G. Knepley @*/
750708a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
750808a22f4bSMatthew G. Knepley {
750908a22f4bSMatthew G. Knepley   DM             rdm;
751008a22f4bSMatthew G. Knepley   PetscFE        fe;
751108a22f4bSMatthew G. Knepley   PetscScalar   *r;
751208a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
7513a55f9a55SMatthew G. Knepley   DMPolytopeType ct;
751408a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
7515a55f9a55SMatthew G. Knepley   PetscBool      simplex;
751608a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
751708a22f4bSMatthew G. Knepley 
751808a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7519f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7520f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
7521ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
752208a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
752308a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7524a55f9a55SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7525a55f9a55SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
7526a55f9a55SMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
7527a55f9a55SMatthew G. Knepley   ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
752808a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7529e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
753008a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7531e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
753208a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
753308a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
753408a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
753508a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
753608a22f4bSMatthew G. Knepley     PetscScalar *lr;
753708a22f4bSMatthew G. Knepley 
753808a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
753971f09efeSPierre Jolivet     if (lr) *lr = rank;
754008a22f4bSMatthew G. Knepley   }
754108a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
754208a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
754308a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
754408a22f4bSMatthew G. Knepley }
754508a22f4bSMatthew G. Knepley 
7546ca8062c8SMatthew G. Knepley /*@
754718e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
754818e14f0cSMatthew G. Knepley 
754918e14f0cSMatthew G. Knepley   Input Parameters:
755018e14f0cSMatthew G. Knepley + dm    - The DMPlex
755118e14f0cSMatthew G. Knepley - label - The DMLabel
755218e14f0cSMatthew G. Knepley 
755318e14f0cSMatthew G. Knepley   Output Parameter:
755418e14f0cSMatthew G. Knepley . val - The label value field
755518e14f0cSMatthew G. Knepley 
755618e14f0cSMatthew G. Knepley   Options Database Keys:
755718e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
755818e14f0cSMatthew G. Knepley 
755918e14f0cSMatthew G. Knepley   Level: intermediate
756018e14f0cSMatthew G. Knepley 
756118e14f0cSMatthew G. Knepley .seealso: DMView()
756218e14f0cSMatthew G. Knepley @*/
756318e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
756418e14f0cSMatthew G. Knepley {
756518e14f0cSMatthew G. Knepley   DM             rdm;
756618e14f0cSMatthew G. Knepley   PetscFE        fe;
756718e14f0cSMatthew G. Knepley   PetscScalar   *v;
756818e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
756918e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
757018e14f0cSMatthew G. Knepley 
757118e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
757218e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
757318e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
757418e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
757518e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
757618e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
757718e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
757818e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7579e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
758018e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7581e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
758218e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
758318e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7584effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
758518e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
758618e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
758718e14f0cSMatthew G. Knepley     PetscScalar *lv;
758818e14f0cSMatthew G. Knepley     PetscInt     cval;
758918e14f0cSMatthew G. Knepley 
759018e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
759118e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
759218e14f0cSMatthew G. Knepley     *lv = cval;
759318e14f0cSMatthew G. Knepley   }
759418e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
759518e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
759618e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
759718e14f0cSMatthew G. Knepley }
759818e14f0cSMatthew G. Knepley 
759918e14f0cSMatthew G. Knepley /*@
7600ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7601ca8062c8SMatthew G. Knepley 
760269916449SMatthew G. Knepley   Input Parameter:
760369916449SMatthew G. Knepley . dm - The DMPlex object
7604ca8062c8SMatthew G. Knepley 
760595eb5ee5SVaclav Hapla   Notes:
760695eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
760795eb5ee5SVaclav Hapla 
760895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7609ca8062c8SMatthew G. Knepley 
7610ca8062c8SMatthew G. Knepley   Level: developer
7611ca8062c8SMatthew G. Knepley 
761295eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7613ca8062c8SMatthew G. Knepley @*/
7614ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7615ca8062c8SMatthew G. Knepley {
7616ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7617ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7618ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
761957beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
762057beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7621ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7622ca8062c8SMatthew G. Knepley 
7623ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7624ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7625412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7626ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7627ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7628ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7629ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7630ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7631ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7632ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7633ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
763442e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
763542e66dfaSMatthew G. Knepley       PetscInt  d;
763642e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
763742e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
763842e66dfaSMatthew G. Knepley       }
7639ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7640ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7641ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7642ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7643ca8062c8SMatthew G. Knepley       }
764442e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
76458ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7646ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
76478ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7648ca8062c8SMatthew G. Knepley         }
7649302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
76508ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7651ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
76528ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7653ca8062c8SMatthew G. Knepley         }
7654302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
76558ccfff9cSToby 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]);
76568ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7657ca8062c8SMatthew G. Knepley       }
765842e66dfaSMatthew G. Knepley     }
765957beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
766057beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7661ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7662ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7663ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7664ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7665ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7666ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
766757beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
766857beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7669ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7670ca8062c8SMatthew G. Knepley       }
7671ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
76728ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7673ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
76748ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7675ca8062c8SMatthew G. Knepley         }
7676302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
76778ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7678ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
76798ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7680ca8062c8SMatthew G. Knepley         }
7681302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
76828ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7683ca8062c8SMatthew G. Knepley       }
7684ca8062c8SMatthew G. Knepley     }
7685ca8062c8SMatthew G. Knepley   }
768657beb4faSStefano Zampini   if (storagecheck) {
7687ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7688ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
76898ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
769057beb4faSStefano Zampini   }
7691ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7692ca8062c8SMatthew G. Knepley }
7693ca8062c8SMatthew G. Knepley 
7694412e9a14SMatthew G. Knepley /*
7695412e9a14SMatthew 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.
7696412e9a14SMatthew G. Knepley */
7697412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7698412e9a14SMatthew G. Knepley {
7699412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7700412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7701412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7702412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7703412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7704412e9a14SMatthew G. Knepley 
7705412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7706412e9a14SMatthew G. Knepley   *unsplit = 0;
7707412e9a14SMatthew G. Knepley   switch (ct) {
7708412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7709412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7710412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7711412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7712412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7713412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7714412e9a14SMatthew G. Knepley       }
7715412e9a14SMatthew G. Knepley       break;
7716412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7717412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7718412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7719412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7720412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7721412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7722412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7723412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7724412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7725412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7726412e9a14SMatthew G. Knepley             PetscInt p;
7727412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7728412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7729412e9a14SMatthew G. Knepley           }
7730412e9a14SMatthew G. Knepley         }
7731412e9a14SMatthew G. Knepley       }
7732412e9a14SMatthew G. Knepley       break;
7733412e9a14SMatthew G. Knepley     default: break;
7734412e9a14SMatthew G. Knepley   }
7735412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7736412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7737412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7738412e9a14SMatthew G. Knepley   }
7739412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7740412e9a14SMatthew G. Knepley }
7741412e9a14SMatthew G. Knepley 
7742ca8062c8SMatthew G. Knepley /*@
7743ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7744ca8062c8SMatthew G. Knepley 
7745ca8062c8SMatthew G. Knepley   Input Parameters:
7746ca8062c8SMatthew G. Knepley + dm - The DMPlex object
774758723a97SMatthew G. Knepley - cellHeight - Normally 0
7748ca8062c8SMatthew G. Knepley 
774995eb5ee5SVaclav Hapla   Notes:
775095eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
775125c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7752ca8062c8SMatthew G. Knepley 
775395eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
775495eb5ee5SVaclav Hapla 
7755ca8062c8SMatthew G. Knepley   Level: developer
7756ca8062c8SMatthew G. Knepley 
775795eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7758ca8062c8SMatthew G. Knepley @*/
775925c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7760ca8062c8SMatthew G. Knepley {
7761412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7762412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7763412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7764ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7765ca8062c8SMatthew G. Knepley 
7766ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7767ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7768412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
776925c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
777058723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7771412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7772412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7773412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
777458723a97SMatthew G. Knepley 
7775412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7776412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7777412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7778412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7779412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7780d4961f80SStefano 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));
7781412e9a14SMatthew G. Knepley     }
778258723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
778358723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
778458723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7785412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
778658723a97SMatthew G. Knepley     }
778758723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7788412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7789412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7790412e9a14SMatthew G. Knepley       PetscInt unsplit;
779142363296SMatthew G. Knepley 
7792412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7793412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
779442363296SMatthew G. Knepley     }
7795d4961f80SStefano 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));
779642363296SMatthew G. Knepley   }
7797ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7798ca8062c8SMatthew G. Knepley }
77999bf0dad6SMatthew G. Knepley 
78009bf0dad6SMatthew G. Knepley /*@
78019bf0dad6SMatthew 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
78029bf0dad6SMatthew G. Knepley 
7803899ea2b8SJacob Faibussowitsch   Not Collective
7804899ea2b8SJacob Faibussowitsch 
78059bf0dad6SMatthew G. Knepley   Input Parameters:
78069bf0dad6SMatthew G. Knepley + dm - The DMPlex object
78079bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
78089bf0dad6SMatthew G. Knepley 
780945da879fSVaclav Hapla   Notes:
781045da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
781145da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
781245da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
781345da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
78149bf0dad6SMatthew G. Knepley 
781595eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
781695eb5ee5SVaclav Hapla 
78179bf0dad6SMatthew G. Knepley   Level: developer
78189bf0dad6SMatthew G. Knepley 
781995eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
78209bf0dad6SMatthew G. Knepley @*/
782125c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
78229bf0dad6SMatthew G. Knepley {
7823ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
78249bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7825899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
78269bf0dad6SMatthew G. Knepley 
78279bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
78289bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7829899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
783045da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
783145da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7832899ea2b8SJacob Faibussowitsch     PetscMPIInt rank;
7833899ea2b8SJacob Faibussowitsch     MPI_Comm    comm;
7834899ea2b8SJacob Faibussowitsch 
7835899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7836ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
783745da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7838899ea2b8SJacob Faibussowitsch   }
7839899ea2b8SJacob Faibussowitsch 
7840c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7841ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
78429bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7843ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
78443554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
78453554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7846412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7847412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7848ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7849412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7850412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
78519bf0dad6SMatthew G. Knepley 
7852ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7853412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7854412e9a14SMatthew G. Knepley       if (unsplit) continue;
78559bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
78569bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
78579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
78589bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
78599bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
78609bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
78619bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
78629bf0dad6SMatthew G. Knepley       }
7863412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
7864d4961f80SStefano 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);
78659bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
7866d4961f80SStefano Zampini         DMPolytopeType fct;
78679bf0dad6SMatthew G. Knepley         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
78689bf0dad6SMatthew G. Knepley 
7869d4961f80SStefano Zampini         ierr = DMPlexGetCellType(dm, cone[f], &fct);CHKERRQ(ierr);
78709bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
78719bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
78729bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
78739bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
78749bf0dad6SMatthew G. Knepley         }
7875d4961f80SStefano 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]);
78769bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7877d4961f80SStefano 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]);
78789bf0dad6SMatthew G. Knepley         }
78799bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7880412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
78819bf0dad6SMatthew G. Knepley       }
7882412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
78839bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
78849bf0dad6SMatthew G. Knepley     }
78853554e41dSMatthew G. Knepley   }
7886552f7358SJed Brown   PetscFunctionReturn(0);
7887552f7358SJed Brown }
78883913d7c8SMatthew G. Knepley 
7889bb6a34a8SMatthew G. Knepley /*@
7890bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7891bb6a34a8SMatthew G. Knepley 
7892bb6a34a8SMatthew G. Knepley   Input Parameter:
7893bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7894bb6a34a8SMatthew G. Knepley 
789595eb5ee5SVaclav Hapla   Notes:
789695eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
789795eb5ee5SVaclav Hapla 
789895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7899bb6a34a8SMatthew G. Knepley 
7900bb6a34a8SMatthew G. Knepley   Level: developer
7901bb6a34a8SMatthew G. Knepley 
790295eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7903bb6a34a8SMatthew G. Knepley @*/
7904bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7905bb6a34a8SMatthew G. Knepley {
7906a2a9e04cSMatthew G. Knepley   Vec            coordinates;
7907bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7908bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7909412e9a14SMatthew G. Knepley   PetscBool      periodic;
791051a74b61SMatthew G. Knepley   PetscInt       dim, depth, dE, d, cStart, cEnd, c;
7911bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7912bb6a34a8SMatthew G. Knepley 
7913bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7914bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
791551a74b61SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
791651a74b61SMatthew G. Knepley   if (dim != dE) PetscFunctionReturn(0);
7917bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7918412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7919bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7920bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7921a2a9e04cSMatthew G. Knepley   /* Make sure local coordinates are created, because that step is collective */
7922a2a9e04cSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
7923412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7924412e9a14SMatthew G. Knepley     DMPolytopeType ct;
7925412e9a14SMatthew G. Knepley     PetscInt       unsplit;
7926412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
7927412e9a14SMatthew G. Knepley 
7928412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7929412e9a14SMatthew G. Knepley     switch (ct) {
7930412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7931412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7932412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7933412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
7934412e9a14SMatthew G. Knepley       default: break;
7935412e9a14SMatthew G. Knepley     }
7936412e9a14SMatthew G. Knepley     switch (ct) {
7937412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
7938412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7939412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7940a2a9e04cSMatthew G. Knepley       case DM_POLYTOPE_PYRAMID:
7941412e9a14SMatthew G. Knepley         continue;
7942412e9a14SMatthew G. Knepley       default: break;
7943412e9a14SMatthew G. Knepley     }
7944412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7945412e9a14SMatthew G. Knepley     if (unsplit) continue;
7946bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7947d4961f80SStefano 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);
7948bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7949412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
7950bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7951d4961f80SStefano 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);
7952bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7953bb6a34a8SMatthew G. Knepley     }
7954bb6a34a8SMatthew G. Knepley   }
7955bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7956bb6a34a8SMatthew G. Knepley }
7957bb6a34a8SMatthew G. Knepley 
795803da9461SVaclav Hapla /*@
7959e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
796003da9461SVaclav Hapla 
796103da9461SVaclav Hapla   Input Parameters:
796203da9461SVaclav Hapla . dm - The DMPlex object
796303da9461SVaclav Hapla 
7964e83a0d2dSVaclav Hapla   Notes:
7965e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
79668918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
796703da9461SVaclav Hapla 
796895eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
796995eb5ee5SVaclav Hapla 
797003da9461SVaclav Hapla   Level: developer
797103da9461SVaclav Hapla 
797295eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
797303da9461SVaclav Hapla @*/
797403da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
797503da9461SVaclav Hapla {
7976f0cfc026SVaclav Hapla   PetscSF         pointSF;
7977f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7978f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
7979f0cfc026SVaclav Hapla   PetscBool       distributed;
798003da9461SVaclav Hapla   PetscErrorCode  ierr;
798103da9461SVaclav Hapla 
798203da9461SVaclav Hapla   PetscFunctionBegin;
798303da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7984f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7985f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7986f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7987f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7988f0cfc026SVaclav Hapla   if (overlap) {
79898918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
79908918e3e2SVaclav Hapla     PetscFunctionReturn(0);
79918918e3e2SVaclav Hapla   }
7992f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7993f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7994f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7995f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
7996f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
799703da9461SVaclav Hapla 
7998ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7999f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8000f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8001f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8002f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
8003f5869d18SMatthew G. Knepley 
8004f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
800503da9461SVaclav Hapla   }
8006ece87651SVaclav Hapla 
8007f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8008f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8009f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
8010f5869d18SMatthew G. Knepley     const PetscInt *cone;
8011f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
8012f5869d18SMatthew G. Knepley 
8013f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
8014f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
8015f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
8016f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
8017f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
8018f5869d18SMatthew 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]);
8019f5869d18SMatthew G. Knepley       }
8020f5869d18SMatthew G. Knepley     }
8021ece87651SVaclav Hapla   }
802203da9461SVaclav Hapla   PetscFunctionReturn(0);
802303da9461SVaclav Hapla }
802403da9461SVaclav Hapla 
8025068a5610SStefano Zampini typedef struct cell_stats
8026068a5610SStefano Zampini {
8027068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
8028068a5610SStefano Zampini   PetscInt  count;
8029068a5610SStefano Zampini } cell_stats_t;
8030068a5610SStefano Zampini 
803125befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
8032068a5610SStefano Zampini {
8033068a5610SStefano Zampini   PetscInt i, N = *len;
8034068a5610SStefano Zampini 
8035068a5610SStefano Zampini   for (i = 0; i < N; i++) {
8036068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
8037068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
8038068a5610SStefano Zampini 
8039068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
8040068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
8041068a5610SStefano Zampini     B->sum += A->sum;
8042068a5610SStefano Zampini     B->squaresum += A->squaresum;
8043068a5610SStefano Zampini     B->count += A->count;
8044068a5610SStefano Zampini   }
8045068a5610SStefano Zampini }
8046068a5610SStefano Zampini 
8047068a5610SStefano Zampini /*@
804843fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
8049068a5610SStefano Zampini 
80508261a58bSMatthew G. Knepley   Collective on dm
80518261a58bSMatthew G. Knepley 
8052068a5610SStefano Zampini   Input Parameters:
8053068a5610SStefano Zampini + dm        - The DMPlex object
805443fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
805543fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
8056068a5610SStefano Zampini 
805795eb5ee5SVaclav Hapla   Notes:
805895eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
805995eb5ee5SVaclav Hapla 
806095eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8061068a5610SStefano Zampini 
8062068a5610SStefano Zampini   Level: developer
8063068a5610SStefano Zampini 
8064f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
8065068a5610SStefano Zampini @*/
806643fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8067068a5610SStefano Zampini {
8068068a5610SStefano Zampini   DM             dmCoarse;
806943fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
807043fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
807143fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
807243fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8073412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
807443fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
8075068a5610SStefano Zampini   PetscErrorCode ierr;
8076068a5610SStefano Zampini 
8077068a5610SStefano Zampini   PetscFunctionBegin;
8078068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8079068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
8080068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
8081068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
8082068a5610SStefano Zampini   stats.count = 0;
8083068a5610SStefano Zampini 
8084ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
8085ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
808643fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
808743fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
8088412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
808943fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
8090412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
8091068a5610SStefano Zampini     PetscInt  i;
8092068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
8093068a5610SStefano Zampini 
8094068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
8095068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
809643fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
8097068a5610SStefano Zampini       frobJ    += J[i] * J[i];
8098068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
8099068a5610SStefano Zampini     }
8100068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
8101068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
8102068a5610SStefano Zampini 
8103068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
8104068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
8105068a5610SStefano Zampini     stats.sum       += cond;
8106068a5610SStefano Zampini     stats.squaresum += cond2;
8107068a5610SStefano Zampini     stats.count++;
81088261a58bSMatthew G. Knepley     if (output && cond > limit) {
810943fa8764SMatthew G. Knepley       PetscSection coordSection;
811043fa8764SMatthew G. Knepley       Vec          coordsLocal;
811143fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
811243fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
811343fa8764SMatthew G. Knepley 
811443fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
811543fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
811643fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
8117087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
811843fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
811943fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
812043fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
812143fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
812248afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
812343fa8764SMatthew G. Knepley         }
812443fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
812543fa8764SMatthew G. Knepley       }
812643fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
812743fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
812843fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
812943fa8764SMatthew G. Knepley 
813043fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
813143fa8764SMatthew G. Knepley           PetscReal len;
813243fa8764SMatthew G. Knepley 
813343fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
8134087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
813543fa8764SMatthew G. Knepley         }
813643fa8764SMatthew G. Knepley       }
813743fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
813843fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
813943fa8764SMatthew G. Knepley     }
8140068a5610SStefano Zampini   }
81418261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
8142068a5610SStefano Zampini 
8143068a5610SStefano Zampini   if (size > 1) {
8144068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
8145068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
8146068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
8147068a5610SStefano Zampini     MPI_Op        statReduce;
8148068a5610SStefano Zampini 
8149ffc4695bSBarry Smith     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRMPI(ierr);
8150ffc4695bSBarry Smith     ierr = MPI_Type_commit(&statType);CHKERRMPI(ierr);
8151ffc4695bSBarry Smith     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRMPI(ierr);
8152ffc4695bSBarry Smith     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRMPI(ierr);
8153ffc4695bSBarry Smith     ierr = MPI_Op_free(&statReduce);CHKERRMPI(ierr);
8154ffc4695bSBarry Smith     ierr = MPI_Type_free(&statType);CHKERRMPI(ierr);
8155068a5610SStefano Zampini   } else {
8156580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
8157068a5610SStefano Zampini   }
8158068a5610SStefano Zampini   if (!rank) {
8159068a5610SStefano Zampini     count = globalStats.count;
8160068a5610SStefano Zampini     min   = globalStats.min;
8161068a5610SStefano Zampini     max   = globalStats.max;
8162068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
8163068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
8164068a5610SStefano Zampini   }
8165068a5610SStefano Zampini 
8166068a5610SStefano Zampini   if (output) {
8167068a5610SStefano 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);
8168068a5610SStefano Zampini   }
8169068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
8170068a5610SStefano Zampini 
8171068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
8172068a5610SStefano Zampini   if (dmCoarse) {
8173068a5610SStefano Zampini     PetscBool isplex;
8174068a5610SStefano Zampini 
8175068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
8176068a5610SStefano Zampini     if (isplex) {
817743fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
8178068a5610SStefano Zampini     }
8179068a5610SStefano Zampini   }
8180068a5610SStefano Zampini   PetscFunctionReturn(0);
8181068a5610SStefano Zampini }
8182068a5610SStefano Zampini 
8183f108dbd7SJacob Faibussowitsch /*@
8184f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8185f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
8186f108dbd7SJacob Faibussowitsch 
81876ed19f2fSJacob Faibussowitsch   Collective on dm
8188f108dbd7SJacob Faibussowitsch 
8189f108dbd7SJacob Faibussowitsch   Input Parameters:
8190f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
8191f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
8192f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
8193f108dbd7SJacob Faibussowitsch 
8194f108dbd7SJacob Faibussowitsch   Output Parameters:
8195f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
8196f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
8197f108dbd7SJacob Faibussowitsch 
8198f108dbd7SJacob Faibussowitsch   Options Database Keys:
8199f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
8200f108dbd7SJacob Faibussowitsch supported.
8201f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
8202f108dbd7SJacob Faibussowitsch 
8203f108dbd7SJacob Faibussowitsch   Notes:
8204f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
8205f108dbd7SJacob Faibussowitsch 
8206f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
8207f108dbd7SJacob Faibussowitsch 
8208f108dbd7SJacob 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
8209f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8210f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8211f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
8212f108dbd7SJacob Faibussowitsch 
8213f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
8214f108dbd7SJacob Faibussowitsch 
8215f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
8216f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
8217f108dbd7SJacob Faibussowitsch 
8218f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
8219f108dbd7SJacob Faibussowitsch 
8220f108dbd7SJacob Faibussowitsch   Level: intermediate
8221f108dbd7SJacob Faibussowitsch 
8222f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
8223f108dbd7SJacob Faibussowitsch @*/
8224f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
8225f108dbd7SJacob Faibussowitsch {
82266ed19f2fSJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
82276ed19f2fSJacob Faibussowitsch   PetscInt                *idx;
82286ed19f2fSJacob Faibussowitsch   PetscScalar             *oqVals;
8229f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
82306ed19f2fSJacob Faibussowitsch   PetscReal               *ci, *fi, *Ai;
8231f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
8232f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
8233f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
8234f108dbd7SJacob Faibussowitsch   IS                      glob;
8235f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
8236f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
8237f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
8238f108dbd7SJacob Faibussowitsch 
8239f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
8240f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82416ed19f2fSJacob Faibussowitsch   if (fv) {PetscValidHeaderSpecific(fv, PETSCFV_CLASSID, 2);}
8242f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
82436ed19f2fSJacob 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);
8244f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
8245f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
82466ed19f2fSJacob Faibussowitsch   if (nc < 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
82476ed19f2fSJacob Faibussowitsch   {
82486ed19f2fSJacob Faibussowitsch     DMPlexInterpolatedFlag interpFlag;
82496ed19f2fSJacob Faibussowitsch 
8250f108dbd7SJacob Faibussowitsch     ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
8251f108dbd7SJacob Faibussowitsch     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
8252f108dbd7SJacob Faibussowitsch       PetscMPIInt rank;
8253f108dbd7SJacob Faibussowitsch 
8254ffc4695bSBarry Smith       ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
8255f108dbd7SJacob Faibussowitsch       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
8256f108dbd7SJacob Faibussowitsch     }
82576ed19f2fSJacob Faibussowitsch   }
8258f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8259f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
8260f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
8261f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
82626ed19f2fSJacob Faibussowitsch   } else {*OrthQualLabel = NULL;}
8263f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8264f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8265f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
8266f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
8267f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
8268f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
8269f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
8270f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
8271f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
8272f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
8273f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
8274f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
8275f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
8276f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8277f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8278f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
8279f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
82806ed19f2fSJacob Faibussowitsch   ierr = PetscMalloc5(cEnd-cStart, &idx, cEnd-cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
82816ed19f2fSJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cellIter++,cell++) {
82826ed19f2fSJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
8283f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
8284f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
8285898cd552SSatish Balay     PetscReal          minvalc = 1.0, minvalf = 1.0;
8286f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
8287f108dbd7SJacob Faibussowitsch 
82886ed19f2fSJacob Faibussowitsch     idx[cellIter] = cell-cStart;
8289f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
8290f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
8291f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
8292f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
8293f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8294f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
82956ed19f2fSJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++,cellneigh++) {
82966ed19f2fSJacob Faibussowitsch       PetscInt         i;
82976ed19f2fSJacob Faibussowitsch       const PetscInt   neigh = adj[cellneigh];
8298f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
8299f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
8300f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
8301f108dbd7SJacob Faibussowitsch 
8302f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
8303f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
8304f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
8305f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
83066ed19f2fSJacob Faibussowitsch       {
83076ed19f2fSJacob Faibussowitsch         PetscInt       numcovpts;
83086ed19f2fSJacob Faibussowitsch         const PetscInt *covpts;
83096ed19f2fSJacob Faibussowitsch 
8310f108dbd7SJacob Faibussowitsch         ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
8311f108dbd7SJacob Faibussowitsch         ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
8312f108dbd7SJacob Faibussowitsch         ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
83136ed19f2fSJacob Faibussowitsch       }
8314f108dbd7SJacob Faibussowitsch 
8315f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
8316f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8317f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8318f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
8319f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
8320addd1e01SJunchao Zhang         normci += PetscPowReal(ci[i], 2);
8321addd1e01SJunchao Zhang         normfi += PetscPowReal(fi[i], 2);
8322addd1e01SJunchao Zhang         normai += PetscPowReal(Ai[i], 2);
8323f108dbd7SJacob Faibussowitsch       }
8324addd1e01SJunchao Zhang       normci = PetscSqrtReal(normci);
8325addd1e01SJunchao Zhang       normfi = PetscSqrtReal(normfi);
8326addd1e01SJunchao Zhang       normai = PetscSqrtReal(normai);
8327f108dbd7SJacob Faibussowitsch 
8328f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
8329f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
8330f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
8331f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
8332f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
8333f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
8334f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8335f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8336f108dbd7SJacob Faibussowitsch       }
8337f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8338f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
8339f108dbd7SJacob Faibussowitsch       }
8340f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8341f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
8342f108dbd7SJacob Faibussowitsch       }
8343f108dbd7SJacob Faibussowitsch     }
8344f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
8345f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
8346f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
83476ed19f2fSJacob Faibussowitsch     oqVals[cellIter] = PetscMin(minvalf, minvalc);
8348f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
83496ed19f2fSJacob Faibussowitsch       if (PetscRealPart(oqVals[cellIter]) <= atol) {ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);}
8350f108dbd7SJacob Faibussowitsch     }
8351f108dbd7SJacob Faibussowitsch   }
83526ed19f2fSJacob Faibussowitsch   ierr = VecSetValuesLocal(*OrthQual, cEnd-cStart, idx, oqVals, INSERT_VALUES);CHKERRQ(ierr);
8353f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
8354f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
8355f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8356f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8357f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
8358f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
83596ed19f2fSJacob Faibussowitsch     if (vwr) {ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);}
8360f108dbd7SJacob Faibussowitsch   }
83616ed19f2fSJacob Faibussowitsch   ierr = PetscFree5(idx, oqVals, ci, fi, Ai);CHKERRQ(ierr);
8362f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
8363f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
8364f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
8365f108dbd7SJacob Faibussowitsch }
8366f108dbd7SJacob Faibussowitsch 
83671eb70e55SToby Isaac /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
83681eb70e55SToby Isaac  * interpolator construction */
83691eb70e55SToby Isaac static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
83701eb70e55SToby Isaac {
83711eb70e55SToby Isaac   PetscSection   section, newSection, gsection;
83721eb70e55SToby Isaac   PetscSF        sf;
83731eb70e55SToby Isaac   PetscBool      hasConstraints, ghasConstraints;
83741eb70e55SToby Isaac   PetscErrorCode ierr;
83751eb70e55SToby Isaac 
83761eb70e55SToby Isaac   PetscFunctionBegin;
83771eb70e55SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83781eb70e55SToby Isaac   PetscValidPointer(odm,2);
83791eb70e55SToby Isaac   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
83801eb70e55SToby Isaac   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
83811eb70e55SToby Isaac   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
83821eb70e55SToby Isaac   if (!ghasConstraints) {
83831eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
83841eb70e55SToby Isaac     *odm = dm;
83851eb70e55SToby Isaac     PetscFunctionReturn(0);
83861eb70e55SToby Isaac   }
83871eb70e55SToby Isaac   ierr = DMClone(dm, odm);CHKERRQ(ierr);
83881eb70e55SToby Isaac   ierr = DMCopyFields(dm, *odm);CHKERRQ(ierr);
83891eb70e55SToby Isaac   ierr = DMGetLocalSection(*odm, &newSection);CHKERRQ(ierr);
83901eb70e55SToby Isaac   ierr = DMGetPointSF(*odm, &sf);CHKERRQ(ierr);
83911eb70e55SToby Isaac   ierr = PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
83921eb70e55SToby Isaac   ierr = DMSetGlobalSection(*odm, gsection);CHKERRQ(ierr);
83931eb70e55SToby Isaac   ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
83941eb70e55SToby Isaac   PetscFunctionReturn(0);
83951eb70e55SToby Isaac }
83961eb70e55SToby Isaac 
83971eb70e55SToby Isaac static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
83981eb70e55SToby Isaac {
83991eb70e55SToby Isaac   DM             dmco, dmfo;
84001eb70e55SToby Isaac   Mat            interpo;
84011eb70e55SToby Isaac   Vec            rscale;
84021eb70e55SToby Isaac   Vec            cglobalo, clocal;
84031eb70e55SToby Isaac   Vec            fglobal, fglobalo, flocal;
84041eb70e55SToby Isaac   PetscBool      regular;
84051eb70e55SToby Isaac   PetscErrorCode ierr;
84061eb70e55SToby Isaac 
84071eb70e55SToby Isaac   PetscFunctionBegin;
84081eb70e55SToby Isaac   ierr = DMGetFullDM(dmc, &dmco);CHKERRQ(ierr);
84091eb70e55SToby Isaac   ierr = DMGetFullDM(dmf, &dmfo);CHKERRQ(ierr);
84101eb70e55SToby Isaac   ierr = DMSetCoarseDM(dmfo, dmco);CHKERRQ(ierr);
84111eb70e55SToby Isaac   ierr = DMPlexGetRegularRefinement(dmf, &regular);CHKERRQ(ierr);
84121eb70e55SToby Isaac   ierr = DMPlexSetRegularRefinement(dmfo, regular);CHKERRQ(ierr);
84131eb70e55SToby Isaac   ierr = DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);CHKERRQ(ierr);
84141eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmco, &cglobalo);CHKERRQ(ierr);
84151eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmc, &clocal);CHKERRQ(ierr);
84161eb70e55SToby Isaac   ierr = VecSet(cglobalo, 0.);CHKERRQ(ierr);
84171eb70e55SToby Isaac   ierr = VecSet(clocal, 0.);CHKERRQ(ierr);
84181eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmf, &fglobal);CHKERRQ(ierr);
84191eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmfo, &fglobalo);CHKERRQ(ierr);
84201eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmf, &flocal);CHKERRQ(ierr);
84211eb70e55SToby Isaac   ierr = VecSet(fglobal, 0.);CHKERRQ(ierr);
84221eb70e55SToby Isaac   ierr = VecSet(fglobalo, 0.);CHKERRQ(ierr);
84231eb70e55SToby Isaac   ierr = VecSet(flocal, 0.);CHKERRQ(ierr);
84241eb70e55SToby Isaac   ierr = DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);CHKERRQ(ierr);
84251eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
84261eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
84271eb70e55SToby Isaac   ierr = MatMult(interpo, cglobalo, fglobalo);CHKERRQ(ierr);
84281eb70e55SToby Isaac   ierr = DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
84291eb70e55SToby Isaac   ierr = DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
84301eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
84311eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
84321eb70e55SToby Isaac   *shift = fglobal;
84331eb70e55SToby Isaac   ierr = VecDestroy(&flocal);CHKERRQ(ierr);
84341eb70e55SToby Isaac   ierr = VecDestroy(&fglobalo);CHKERRQ(ierr);
84351eb70e55SToby Isaac   ierr = VecDestroy(&clocal);CHKERRQ(ierr);
84361eb70e55SToby Isaac   ierr = VecDestroy(&cglobalo);CHKERRQ(ierr);
84371eb70e55SToby Isaac   ierr = VecDestroy(&rscale);CHKERRQ(ierr);
84381eb70e55SToby Isaac   ierr = MatDestroy(&interpo);CHKERRQ(ierr);
84391eb70e55SToby Isaac   ierr = DMDestroy(&dmfo);CHKERRQ(ierr);
84401eb70e55SToby Isaac   ierr = DMDestroy(&dmco);CHKERRQ(ierr);
84411eb70e55SToby Isaac   PetscFunctionReturn(0);
84421eb70e55SToby Isaac }
84431eb70e55SToby Isaac 
84441eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
84451eb70e55SToby Isaac {
84461eb70e55SToby Isaac   PetscObject    shifto;
84471eb70e55SToby Isaac   Vec            shift;
84481eb70e55SToby Isaac 
84491eb70e55SToby Isaac   PetscErrorCode ierr;
84501eb70e55SToby Isaac 
84511eb70e55SToby Isaac   PetscFunctionBegin;
84521eb70e55SToby Isaac   if (!interp) {
84531eb70e55SToby Isaac     Vec rscale;
84541eb70e55SToby Isaac 
84551eb70e55SToby Isaac     ierr = DMCreateInterpolation(coarse, fine, &interp, &rscale);CHKERRQ(ierr);
84561eb70e55SToby Isaac     ierr = VecDestroy(&rscale);CHKERRQ(ierr);
84571eb70e55SToby Isaac   } else {
84581eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)interp);CHKERRQ(ierr);
84591eb70e55SToby Isaac   }
84601eb70e55SToby Isaac   ierr = PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);CHKERRQ(ierr);
84611eb70e55SToby Isaac   if (!shifto) {
84621eb70e55SToby Isaac     ierr = DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);CHKERRQ(ierr);
84631eb70e55SToby Isaac     ierr = PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);CHKERRQ(ierr);
84641eb70e55SToby Isaac     shifto = (PetscObject) shift;
84651eb70e55SToby Isaac     ierr = VecDestroy(&shift);CHKERRQ(ierr);
84661eb70e55SToby Isaac   }
84671eb70e55SToby Isaac   shift = (Vec) shifto;
84681eb70e55SToby Isaac   ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
84691eb70e55SToby Isaac   ierr = VecAXPY(fineSol, 1.0, shift);CHKERRQ(ierr);
84701eb70e55SToby Isaac   ierr = MatDestroy(&interp);CHKERRQ(ierr);
84711eb70e55SToby Isaac   PetscFunctionReturn(0);
84721eb70e55SToby Isaac }
84731eb70e55SToby Isaac 
8474bceba477SMatthew G. Knepley /* Pointwise interpolation
8475bceba477SMatthew G. Knepley      Just code FEM for now
8476bceba477SMatthew G. Knepley      u^f = I u^c
84774ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
84784ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
84794ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
8480bceba477SMatthew G. Knepley */
8481bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8482bceba477SMatthew G. Knepley {
8483bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
8484bceba477SMatthew G. Knepley   PetscInt       m, n;
8485a063dac3SMatthew G. Knepley   void          *ctx;
848668132eb9SMatthew G. Knepley   DM             cdm;
8487cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
8488bceba477SMatthew G. Knepley   PetscErrorCode ierr;
8489bceba477SMatthew G. Knepley 
8490bceba477SMatthew G. Knepley   PetscFunctionBegin;
8491e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8492bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8493e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8494bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
849568132eb9SMatthew G. Knepley 
8496fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
8497bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
8498bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8499fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
8500a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
850168132eb9SMatthew G. Knepley 
8502a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
850368132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8504cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
850568132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
850668132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
85074db47ee9SStefano Zampini   if (scaling) {
85085d1c2e58SMatthew G. Knepley     /* Use naive scaling */
85095d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
85104db47ee9SStefano Zampini   }
8511a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
8512a063dac3SMatthew G. Knepley }
8513bceba477SMatthew G. Knepley 
85146dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8515a063dac3SMatthew G. Knepley {
851690748bafSMatthew G. Knepley   PetscErrorCode ierr;
85176dbf9973SLawrence Mitchell   VecScatter     ctx;
851890748bafSMatthew G. Knepley 
8519a063dac3SMatthew G. Knepley   PetscFunctionBegin;
85206dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
85216dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
85226dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
8523bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
8524bceba477SMatthew G. Knepley }
8525bceba477SMatthew G. Knepley 
85263e9753d6SMatthew G. Knepley static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
85273e9753d6SMatthew G. Knepley                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
85283e9753d6SMatthew G. Knepley                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
85293e9753d6SMatthew G. Knepley                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
85303e9753d6SMatthew G. Knepley {
85313e9753d6SMatthew G. Knepley   g0[0] = 1.0;
85323e9753d6SMatthew G. Knepley }
85333e9753d6SMatthew G. Knepley 
8534bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8535bd041c0cSMatthew G. Knepley {
8536bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
8537bd041c0cSMatthew G. Knepley   PetscInt       m, n;
8538bd041c0cSMatthew G. Knepley   void          *ctx;
8539bd041c0cSMatthew G. Knepley   DM             cdm;
8540bd041c0cSMatthew G. Knepley   PetscBool      regular;
8541bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
8542bd041c0cSMatthew G. Knepley 
8543bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
85443e9753d6SMatthew G. Knepley   if (dmFine == dmCoarse) {
85453e9753d6SMatthew G. Knepley     DM       dmc;
85463e9753d6SMatthew G. Knepley     PetscDS  ds;
85473e9753d6SMatthew G. Knepley     Vec      u;
85483e9753d6SMatthew G. Knepley     IS       cellIS;
854906ad1575SMatthew G. Knepley     PetscFormKey key;
85503e9753d6SMatthew G. Knepley     PetscInt depth;
85513e9753d6SMatthew G. Knepley 
85523e9753d6SMatthew G. Knepley     ierr = DMClone(dmFine, &dmc);CHKERRQ(ierr);
85533e9753d6SMatthew G. Knepley     ierr = DMCopyDisc(dmFine, dmc);CHKERRQ(ierr);
85543e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
85553e9753d6SMatthew G. Knepley     ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
85563e9753d6SMatthew G. Knepley     ierr = DMCreateMatrix(dmc, mass);CHKERRQ(ierr);
85573e9753d6SMatthew G. Knepley     ierr = DMGetGlobalVector(dmc, &u);CHKERRQ(ierr);
85583e9753d6SMatthew G. Knepley     ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
85593e9753d6SMatthew G. Knepley     ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
85603e9753d6SMatthew G. Knepley     ierr = MatZeroEntries(*mass);CHKERRQ(ierr);
85616528b96dSMatthew G. Knepley     key.label = NULL;
85626528b96dSMatthew G. Knepley     key.value = 0;
85636528b96dSMatthew G. Knepley     key.field = 0;
856406ad1575SMatthew G. Knepley     key.part  = 0;
85656528b96dSMatthew G. Knepley     ierr = DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);CHKERRQ(ierr);
85663e9753d6SMatthew G. Knepley     ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
85673e9753d6SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmc, &u);CHKERRQ(ierr);
85683e9753d6SMatthew G. Knepley     ierr = DMDestroy(&dmc);CHKERRQ(ierr);
85693e9753d6SMatthew G. Knepley   } else {
8570e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
8571bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
8572e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
8573bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
8574bd041c0cSMatthew G. Knepley 
8575bd041c0cSMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
8576bd041c0cSMatthew G. Knepley     ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
8577bd041c0cSMatthew G. Knepley     ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
8578bd041c0cSMatthew G. Knepley     ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
8579bd041c0cSMatthew G. Knepley 
8580bd041c0cSMatthew G. Knepley     ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
8581bd041c0cSMatthew G. Knepley     ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
8582bd041c0cSMatthew G. Knepley     if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
8583bd041c0cSMatthew G. Knepley     else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
85843e9753d6SMatthew G. Knepley   }
8585bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
8586bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
8587bd041c0cSMatthew G. Knepley }
8588bd041c0cSMatthew G. Knepley 
85890aef6b92SMatthew G. Knepley /*@
85900aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
85910aef6b92SMatthew G. Knepley 
85920aef6b92SMatthew G. Knepley   Input Parameter:
85930aef6b92SMatthew G. Knepley . dm - The DMPlex object
85940aef6b92SMatthew G. Knepley 
85950aef6b92SMatthew G. Knepley   Output Parameter:
85960aef6b92SMatthew G. Knepley . regular - The flag
85970aef6b92SMatthew G. Knepley 
85980aef6b92SMatthew G. Knepley   Level: intermediate
85990aef6b92SMatthew G. Knepley 
86000aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
86010aef6b92SMatthew G. Knepley @*/
86020aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
86030aef6b92SMatthew G. Knepley {
86040aef6b92SMatthew G. Knepley   PetscFunctionBegin;
86050aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86060aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
86070aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
86080aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
86090aef6b92SMatthew G. Knepley }
86100aef6b92SMatthew G. Knepley 
86110aef6b92SMatthew G. Knepley /*@
86120aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
86130aef6b92SMatthew G. Knepley 
86140aef6b92SMatthew G. Knepley   Input Parameters:
86150aef6b92SMatthew G. Knepley + dm - The DMPlex object
86160aef6b92SMatthew G. Knepley - regular - The flag
86170aef6b92SMatthew G. Knepley 
86180aef6b92SMatthew G. Knepley   Level: intermediate
86190aef6b92SMatthew G. Knepley 
86200aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
86210aef6b92SMatthew G. Knepley @*/
86220aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
86230aef6b92SMatthew G. Knepley {
86240aef6b92SMatthew G. Knepley   PetscFunctionBegin;
86250aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86260aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
86270aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
86280aef6b92SMatthew G. Knepley }
86290aef6b92SMatthew G. Knepley 
8630412e9a14SMatthew G. Knepley /*@
8631412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
8632412e9a14SMatthew G. Knepley 
8633412e9a14SMatthew G. Knepley   Input Parameter:
8634412e9a14SMatthew G. Knepley . dm - The DMPlex object
8635412e9a14SMatthew G. Knepley 
8636412e9a14SMatthew G. Knepley   Output Parameter:
8637412e9a14SMatthew G. Knepley . cr - The strategy number
8638412e9a14SMatthew G. Knepley 
8639412e9a14SMatthew G. Knepley   Level: intermediate
8640412e9a14SMatthew G. Knepley 
8641412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8642412e9a14SMatthew G. Knepley @*/
8643412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8644412e9a14SMatthew G. Knepley {
8645412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8646412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8647412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
8648412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8649412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8650412e9a14SMatthew G. Knepley }
8651412e9a14SMatthew G. Knepley 
8652412e9a14SMatthew G. Knepley /*@
8653412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
8654412e9a14SMatthew G. Knepley 
8655412e9a14SMatthew G. Knepley   Input Parameters:
8656412e9a14SMatthew G. Knepley + dm - The DMPlex object
8657412e9a14SMatthew G. Knepley - cr - The strategy number
8658412e9a14SMatthew G. Knepley 
8659412e9a14SMatthew G. Knepley   Level: intermediate
8660412e9a14SMatthew G. Knepley 
8661412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8662412e9a14SMatthew G. Knepley @*/
8663412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8664412e9a14SMatthew G. Knepley {
8665412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8666412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8667412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
8668412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8669412e9a14SMatthew G. Knepley }
8670412e9a14SMatthew G. Knepley 
8671f7c74593SToby Isaac /* anchors */
8672a68b90caSToby Isaac /*@
8673f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
8674f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
8675a68b90caSToby Isaac 
8676e228b242SToby Isaac   not collective
8677a68b90caSToby Isaac 
8678a68b90caSToby Isaac   Input Parameters:
8679a68b90caSToby Isaac . dm - The DMPlex object
8680a68b90caSToby Isaac 
8681a68b90caSToby Isaac   Output Parameters:
8682a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
8683a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
8684a68b90caSToby Isaac 
8685a68b90caSToby Isaac   Level: intermediate
8686a68b90caSToby Isaac 
8687f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8688a68b90caSToby Isaac @*/
8689a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8690a68b90caSToby Isaac {
8691a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
869241e6d900SToby Isaac   PetscErrorCode ierr;
8693a68b90caSToby Isaac 
8694a68b90caSToby Isaac   PetscFunctionBegin;
8695a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
869641e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
8697a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
8698a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
8699a68b90caSToby Isaac   PetscFunctionReturn(0);
8700a68b90caSToby Isaac }
8701a68b90caSToby Isaac 
8702a68b90caSToby Isaac /*@
8703f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
8704f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
8705a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
8706a68b90caSToby Isaac 
8707a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
8708f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
8709a68b90caSToby Isaac 
8710e228b242SToby Isaac   collective on dm
8711a68b90caSToby Isaac 
8712a68b90caSToby Isaac   Input Parameters:
8713a68b90caSToby Isaac + dm - The DMPlex object
8714e228b242SToby 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).
8715e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
8716a68b90caSToby Isaac 
8717a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
8718a68b90caSToby Isaac 
8719a68b90caSToby Isaac   Level: intermediate
8720a68b90caSToby Isaac 
8721f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8722a68b90caSToby Isaac @*/
8723a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8724a68b90caSToby Isaac {
8725a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8726e228b242SToby Isaac   PetscMPIInt    result;
8727a68b90caSToby Isaac   PetscErrorCode ierr;
8728a68b90caSToby Isaac 
8729a68b90caSToby Isaac   PetscFunctionBegin;
8730a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8731e228b242SToby Isaac   if (anchorSection) {
8732e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
8733ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRMPI(ierr);
8734f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8735e228b242SToby Isaac   }
8736e228b242SToby Isaac   if (anchorIS) {
8737e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
8738ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRMPI(ierr);
8739f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8740e228b242SToby Isaac   }
8741a68b90caSToby Isaac 
8742a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
8743a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
8744a68b90caSToby Isaac   plex->anchorSection = anchorSection;
8745a68b90caSToby Isaac 
8746a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
8747a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
8748a68b90caSToby Isaac   plex->anchorIS = anchorIS;
8749a68b90caSToby Isaac 
8750cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8751a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
8752a68b90caSToby Isaac     const PetscInt *anchors;
8753a68b90caSToby Isaac 
8754a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
8755a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
8756a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
8757a68b90caSToby Isaac     for (a = 0; a < size; a++) {
8758a68b90caSToby Isaac       PetscInt p;
8759a68b90caSToby Isaac 
8760a68b90caSToby Isaac       p = anchors[a];
8761a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
8762a68b90caSToby Isaac         PetscInt dof;
8763a68b90caSToby Isaac 
8764a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8765a68b90caSToby Isaac         if (dof) {
8766a68b90caSToby Isaac           PetscErrorCode ierr2;
8767a68b90caSToby Isaac 
8768a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
87698ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8770a68b90caSToby Isaac         }
8771a68b90caSToby Isaac       }
8772a68b90caSToby Isaac     }
8773a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
8774a68b90caSToby Isaac   }
8775f7c74593SToby Isaac   /* reset the generic constraints */
8776f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
8777a68b90caSToby Isaac   PetscFunctionReturn(0);
8778a68b90caSToby Isaac }
8779a68b90caSToby Isaac 
8780f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8781a68b90caSToby Isaac {
8782f7c74593SToby Isaac   PetscSection anchorSection;
87836995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
8784a68b90caSToby Isaac   PetscErrorCode ierr;
8785a68b90caSToby Isaac 
8786a68b90caSToby Isaac   PetscFunctionBegin;
8787a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8788a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
8789e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
8790a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
87916995de1eSToby Isaac   if (numFields) {
8792719ab38cSToby Isaac     PetscInt f;
8793a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
8794719ab38cSToby Isaac 
8795719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
8796719ab38cSToby Isaac       PetscInt numComp;
8797719ab38cSToby Isaac 
8798719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
8799719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
8800719ab38cSToby Isaac     }
88016995de1eSToby Isaac   }
8802a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
88036995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
88046995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
88056995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
88066995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
8807a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
8808a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
8809a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
8810a68b90caSToby Isaac     if (dof) {
8811a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
8812a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
8813a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
8814a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
8815a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
8816a68b90caSToby Isaac       }
8817a68b90caSToby Isaac     }
8818a68b90caSToby Isaac   }
8819a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
8820ae65431dSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *cSec, "Constraint Section");CHKERRQ(ierr);
8821a68b90caSToby Isaac   PetscFunctionReturn(0);
8822a68b90caSToby Isaac }
8823a68b90caSToby Isaac 
8824f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8825a68b90caSToby Isaac {
8826f7c74593SToby Isaac   PetscSection   aSec;
8827ae65431dSMatthew G. Knepley   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
88280ac89760SToby Isaac   const PetscInt *anchors;
88290ac89760SToby Isaac   PetscInt       numFields, f;
883066ad2231SToby Isaac   IS             aIS;
88310ac89760SToby Isaac   PetscErrorCode ierr;
8832e19f7ee6SMark Adams   MatType        mtype;
8833e19f7ee6SMark Adams   PetscBool      iscuda,iskokkos;
88340ac89760SToby Isaac 
88350ac89760SToby Isaac   PetscFunctionBegin;
88360ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88370ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
88380ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
88390ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
88400ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
8841e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);CHKERRQ(ierr);
8842e19f7ee6SMark Adams   if (!iscuda) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);CHKERRQ(ierr); }
8843e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);CHKERRQ(ierr);
8844e19f7ee6SMark Adams   if (!iskokkos) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);CHKERRQ(ierr); }
8845e19f7ee6SMark Adams   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8846e19f7ee6SMark Adams   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8847e19f7ee6SMark Adams   else mtype = MATSEQAIJ;
8848e19f7ee6SMark Adams   ierr = MatSetType(*cMat,mtype);CHKERRQ(ierr);
8849a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
885066ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
88516995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
88526995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
8853ae65431dSMatthew G. Knepley   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
88540ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
88550ac89760SToby Isaac   i[0] = 0;
88560ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
88570ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
8858f19733c5SToby Isaac     PetscInt rDof, rOff, r;
8859f19733c5SToby Isaac 
8860f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
8861f19733c5SToby Isaac     if (!rDof) continue;
8862f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
88630ac89760SToby Isaac     if (numFields) {
88640ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
88650ac89760SToby Isaac         annz = 0;
8866f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
8867f19733c5SToby Isaac           a = anchors[rOff + r];
8868ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
88690ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
88700ac89760SToby Isaac           annz += aDof;
88710ac89760SToby Isaac         }
88720ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
88730ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
88740ac89760SToby Isaac         for (q = 0; q < dof; q++) {
88750ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
88760ac89760SToby Isaac         }
88770ac89760SToby Isaac       }
88780ac89760SToby Isaac     }
88790ac89760SToby Isaac     else {
88800ac89760SToby Isaac       annz = 0;
8881326b8f31SMatthew G. Knepley       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
88820ac89760SToby Isaac       for (q = 0; q < dof; q++) {
8883ae65431dSMatthew G. Knepley         a = anchors[rOff + q];
8884ae65431dSMatthew G. Knepley         if (a < sStart || a >= sEnd) continue;
88850ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
88860ac89760SToby Isaac         annz += aDof;
88870ac89760SToby Isaac       }
88880ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
88890ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
88900ac89760SToby Isaac       for (q = 0; q < dof; q++) {
88910ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
88920ac89760SToby Isaac       }
88930ac89760SToby Isaac     }
88940ac89760SToby Isaac   }
88950ac89760SToby Isaac   nnz = i[m];
88960ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
88970ac89760SToby Isaac   offset = 0;
88980ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
88990ac89760SToby Isaac     if (numFields) {
89000ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
89010ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
89020ac89760SToby Isaac         for (q = 0; q < dof; q++) {
89030ac89760SToby Isaac           PetscInt rDof, rOff, r;
89040ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
89050ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89060ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
89070ac89760SToby Isaac             PetscInt s;
89080ac89760SToby Isaac 
89090ac89760SToby Isaac             a = anchors[rOff + r];
8910ae65431dSMatthew G. Knepley             if (a < sStart || a >= sEnd) continue;
89110ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
89120ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
89130ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
89140ac89760SToby Isaac               j[offset++] = aOff + s;
89150ac89760SToby Isaac             }
89160ac89760SToby Isaac           }
89170ac89760SToby Isaac         }
89180ac89760SToby Isaac       }
89190ac89760SToby Isaac     }
89200ac89760SToby Isaac     else {
89210ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
89220ac89760SToby Isaac       for (q = 0; q < dof; q++) {
89230ac89760SToby Isaac         PetscInt rDof, rOff, r;
89240ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
89250ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
89260ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
89270ac89760SToby Isaac           PetscInt s;
89280ac89760SToby Isaac 
89290ac89760SToby Isaac           a = anchors[rOff + r];
8930ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
89310ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
89320ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
89330ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
89340ac89760SToby Isaac             j[offset++] = aOff + s;
89350ac89760SToby Isaac           }
89360ac89760SToby Isaac         }
89370ac89760SToby Isaac       }
89380ac89760SToby Isaac     }
89390ac89760SToby Isaac   }
89400ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
894125570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
894225570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
894366ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
89440ac89760SToby Isaac   PetscFunctionReturn(0);
89450ac89760SToby Isaac }
89460ac89760SToby Isaac 
894766ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
894866ad2231SToby Isaac {
8949f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8950f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
895166ad2231SToby Isaac   Mat            cMat;
895266ad2231SToby Isaac   PetscErrorCode ierr;
895366ad2231SToby Isaac 
895466ad2231SToby Isaac   PetscFunctionBegin;
895566ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8956a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
895766ad2231SToby Isaac   if (anchorSection) {
895844a7f3ddSMatthew G. Knepley     PetscInt Nf;
8959e228b242SToby Isaac 
896092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
8961f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
8962f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
896344a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
896444a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
896566ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
896666ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
896766ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
896866ad2231SToby Isaac   }
896966ad2231SToby Isaac   PetscFunctionReturn(0);
897066ad2231SToby Isaac }
8971a93c429eSMatthew G. Knepley 
8972a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8973a93c429eSMatthew G. Knepley {
8974a93c429eSMatthew G. Knepley   IS             subis;
8975a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
8976a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
8977a93c429eSMatthew G. Knepley 
8978a93c429eSMatthew G. Knepley   PetscFunctionBegin;
897992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
8980a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8981a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8982a93c429eSMatthew G. Knepley   /* Create subdomain */
8983a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
8984a93c429eSMatthew G. Knepley   /* Create submodel */
898597d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
8986a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
898792fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
8988a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
8989e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
8990a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
8991a93c429eSMatthew G. Knepley   if (is) {
8992a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
8993a93c429eSMatthew G. Knepley     IS              spIS;
8994a93c429eSMatthew G. Knepley     const PetscInt *spmap;
8995a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
8996a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8997a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
8998a93c429eSMatthew G. Knepley 
899997d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
9000a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
9001a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
90026f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90036f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
9004a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
9005a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9006a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
9007a93c429eSMatthew G. Knepley 
9008a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
9009a93c429eSMatthew G. Knepley       if (gdof > 0) {
9010a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9011a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
9012a93c429eSMatthew G. Knepley 
9013a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
9014a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
9015a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
9016a93c429eSMatthew G. Knepley         }
9017a93c429eSMatthew G. Knepley         subSize += pSubSize;
9018a93c429eSMatthew G. Knepley         if (pSubSize) {
9019a93c429eSMatthew G. Knepley           if (bs < 0) {
9020a93c429eSMatthew G. Knepley             bs = pSubSize;
9021a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
9022a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
9023a93c429eSMatthew G. Knepley             bs = 1;
9024a93c429eSMatthew G. Knepley           }
9025a93c429eSMatthew G. Knepley         }
9026a93c429eSMatthew G. Knepley       }
9027a93c429eSMatthew G. Knepley     }
9028a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
9029a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9030a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9031a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9032a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
9033a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
9034a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9035a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
9036a93c429eSMatthew G. Knepley 
9037a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
9038a93c429eSMatthew G. Knepley       if (gdof > 0) {
9039a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
9040a93c429eSMatthew G. Knepley 
9041a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
9042a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9043a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
9044a93c429eSMatthew G. Knepley 
9045a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
9046a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
9047a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
9048a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
9049a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
9050a93c429eSMatthew G. Knepley           }
9051a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
9052a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
9053a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
9054a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
9055a93c429eSMatthew G. Knepley           }
9056a93c429eSMatthew G. Knepley         }
9057a93c429eSMatthew G. Knepley       }
9058a93c429eSMatthew G. Knepley     }
9059a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
9060a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
9061a93c429eSMatthew G. Knepley     if (bs > 1) {
9062a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
9063a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
9064a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
9065a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
9066a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
9067a93c429eSMatthew G. Knepley         }
9068a93c429eSMatthew G. Knepley       }
9069a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
9070a93c429eSMatthew G. Knepley     }
9071a93c429eSMatthew G. Knepley     /* Attach nullspace */
9072a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
9073a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
9074a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
9075a93c429eSMatthew G. Knepley     }
9076a93c429eSMatthew G. Knepley     if (f < Nf) {
9077a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
90788cda7954SMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);CHKERRQ(ierr);
90796823f3c5SBlaise Bourdin 
9080a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
9081a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
9082a93c429eSMatthew G. Knepley     }
9083a93c429eSMatthew G. Knepley   }
9084a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
9085a93c429eSMatthew G. Knepley }
9086c0f0dcc3SMatthew G. Knepley 
9087c0f0dcc3SMatthew G. Knepley /*@
9088c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
9089c0f0dcc3SMatthew G. Knepley 
9090c0f0dcc3SMatthew G. Knepley   Input Parameter:
9091c0f0dcc3SMatthew G. Knepley - dm - The DM
9092c0f0dcc3SMatthew G. Knepley 
9093c0f0dcc3SMatthew G. Knepley   Level: developer
9094c0f0dcc3SMatthew G. Knepley 
9095c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9096c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
9097c0f0dcc3SMatthew G. Knepley 
9098c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
9099c0f0dcc3SMatthew G. Knepley @*/
9100c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
9101c0f0dcc3SMatthew G. Knepley {
9102e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
9103c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
9104c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
9105c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
9106c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
9107c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
9108c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
9109c0f0dcc3SMatthew G. Knepley   const char        *name;
9110c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
9111e5ed2c37SJose E. Roman #endif
9112c0f0dcc3SMatthew G. Knepley 
9113c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9114c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9115c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
9116c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
9117c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9118c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
9119c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
9120c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
9121c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
9122c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
9123c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
9124c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
9125c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
9126fae64647SBarry 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);
9127c0f0dcc3SMatthew G. Knepley #else
9128c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
9129c0f0dcc3SMatthew G. Knepley #endif
9130c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9131c0f0dcc3SMatthew G. Knepley }
9132