xref: /petsc/src/dm/impls/plex/plex.c (revision fae64647e6a4f93729fc9247b87ddfea0004ae7d)
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 */
1130b0ce1bSStefano Zampini 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;
12552f7358SJed Brown 
135a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
14552f7358SJed Brown 
15e5337592SStefano Zampini /*@
16e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
17e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
18e5337592SStefano Zampini 
19e5337592SStefano Zampini   Collective
20e5337592SStefano Zampini 
21e5337592SStefano Zampini   Input Parameters:
22e5337592SStefano Zampini . dm - The DMPlex object
23e5337592SStefano Zampini 
24e5337592SStefano Zampini   Output Parameters:
25e5337592SStefano Zampini . dmRefined - The refined DMPlex object
26e5337592SStefano Zampini 
27e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
28e5337592SStefano Zampini 
29e5337592SStefano Zampini   Level: intermediate
30e5337592SStefano Zampini 
31e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
32e5337592SStefano Zampini @*/
33e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
34e5337592SStefano Zampini {
35e5337592SStefano Zampini   CellRefiner      cellRefiner;
36ba2698f1SMatthew G. Knepley   DMPolytopeType   ct;
37ba2698f1SMatthew G. Knepley   PetscInt         dim, cMax, fMax, cStart, cEnd;
38e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
39e5337592SStefano Zampini   PetscErrorCode   ierr;
40e5337592SStefano Zampini 
41e5337592SStefano Zampini   PetscFunctionBegin;
42e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
434330a3fcSStefano Zampini   PetscValidPointer(dmRefined, 1);
44e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
45e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
46e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
47e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
48e5337592SStefano Zampini   else {
49ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
50ba2698f1SMatthew G. Knepley     switch (ct) {
51ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_POINT:
52ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_SEGMENT:
53ba2698f1SMatthew G. Knepley         cellRefiner = REFINER_NOOP;break;
54ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TRIANGLE:
554330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
564330a3fcSStefano Zampini         else           cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
57e5337592SStefano Zampini         break;
58ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_QUADRILATERAL:
594330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
604330a3fcSStefano Zampini         else           cellRefiner = REFINER_NOOP;
61e5337592SStefano Zampini         break;
62ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TETRAHEDRON:
634330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
644330a3fcSStefano Zampini         else           cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
654330a3fcSStefano Zampini         break;
66ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
67ba2698f1SMatthew G. Knepley         cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;break;
68ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_HEXAHEDRON:
69ba2698f1SMatthew G. Knepley         if (cMax >= 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Simplex2Tensor in 3D with Hybrid mesh not yet done");
704330a3fcSStefano Zampini         else           cellRefiner = REFINER_NOOP;
71e5337592SStefano Zampini         break;
72ba2698f1SMatthew G. Knepley       default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle cell polytope type %s", DMPolytopeTypes[ct]);
73e5337592SStefano Zampini     }
74e5337592SStefano Zampini   }
75e5337592SStefano Zampini   /* return if we don't need to refine */
76e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
77e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
78e5337592SStefano Zampini   if (allnoop) {
79e5337592SStefano Zampini     *dmRefined = NULL;
80e5337592SStefano Zampini     PetscFunctionReturn(0);
81e5337592SStefano Zampini   }
82e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
83e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
84e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
85e5337592SStefano Zampini   if (localized) {
86e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
87e5337592SStefano Zampini   }
88e5337592SStefano Zampini   PetscFunctionReturn(0);
89e5337592SStefano Zampini }
90e5337592SStefano Zampini 
917afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
927e42fee7SMatthew G. Knepley {
93485ad865SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cMax;
94a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
957e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
967e42fee7SMatthew G. Knepley 
977e42fee7SMatthew G. Knepley   PetscFunctionBegin;
98e630c359SToby Isaac   *ft  = PETSC_VTK_INVALID;
99c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1007e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1017e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
102485ad865SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
103485ad865SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1047e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1057e42fee7SMatthew G. Knepley   if (field >= 0) {
106a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
107a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1087e42fee7SMatthew G. Knepley   } else {
109a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
110a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1117e42fee7SMatthew G. Knepley   }
1125483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
113a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1147e42fee7SMatthew G. Knepley     *sStart = vStart;
1157e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
116a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1177e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
118a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1197e42fee7SMatthew G. Knepley     *sStart = cStart;
1207e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
121a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1227e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
123e630c359SToby Isaac   } else {
124e630c359SToby Isaac     if (field >= 0) {
125e630c359SToby Isaac       const char *fieldname;
126e630c359SToby Isaac 
127e630c359SToby Isaac       ierr = PetscSectionGetFieldName(section, field, &fieldname);CHKERRQ(ierr);
128e630c359SToby Isaac       ierr = PetscInfo2((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);CHKERRQ(ierr);
129e630c359SToby Isaac     } else {
130e630c359SToby Isaac       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output typp of section\"%s\"\n");CHKERRQ(ierr);
131e630c359SToby Isaac     }
132e630c359SToby Isaac   }
1337e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1347e42fee7SMatthew G. Knepley }
1357e42fee7SMatthew G. Knepley 
1367cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
137e412dcbdSMatthew G. Knepley {
138e412dcbdSMatthew G. Knepley   DM                 dm;
139d1df6f1dSMatthew G. Knepley   PetscSection       s;
140e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
141e412dcbdSMatthew G. Knepley   DM                 cdm;
142e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
143e412dcbdSMatthew G. Knepley   Vec                coordinates;
144e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
145e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
146339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
147339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
148d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
149e412dcbdSMatthew G. Knepley   const char        *name;
150339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
151e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
152e412dcbdSMatthew G. Knepley 
153e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
154d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
155d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
156d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
157d1df6f1dSMatthew G. Knepley 
158e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
159e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1608135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
16192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
162d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
163e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
164e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
16592fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
167e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley 
170e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
171339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
172e412dcbdSMatthew G. Knepley 
173e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
175e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1760c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1770c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
178e412dcbdSMatthew G. Knepley   }
179e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
180e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
181e412dcbdSMatthew G. Knepley 
182d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
183d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
184d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
185d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
186d1df6f1dSMatthew G. Knepley     IS   fis;
187d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
188d1df6f1dSMatthew G. Knepley     const char *fname;
189d1df6f1dSMatthew G. Knepley 
190d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
191d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
192d1df6f1dSMatthew G. Knepley 
193a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
194d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
195d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
196d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
197d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
198a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
199a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
200d1df6f1dSMatthew G. Knepley     }
201d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
202d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
203d1df6f1dSMatthew G. Knepley 
204d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
205d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
206d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
207d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
208d1df6f1dSMatthew G. Knepley 
209d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
210d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
211339e3443SMatthew G. Knepley       if (!flg) {
212d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
213d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
214d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
215339e3443SMatthew G. Knepley       }
216e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
217339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
218d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
219e412dcbdSMatthew G. Knepley 
220d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
221e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22299a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
223e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
224e412dcbdSMatthew G. Knepley 
225d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
226339e3443SMatthew G. Knepley         if (a) {
227d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
228339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
229339e3443SMatthew G. Knepley         } else {
230339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
231339e3443SMatthew G. Knepley           PetscInt     numVals, va;
232339e3443SMatthew G. Knepley 
233d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
234d1df6f1dSMatthew 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);
235d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
236d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
237d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
238d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
239339e3443SMatthew G. Knepley             break;
240d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
241d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
242d1df6f1dSMatthew 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]);
243d1df6f1dSMatthew G. Knepley             break;
244d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
245339e3443SMatthew G. Knepley           }
246d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
247339e3443SMatthew G. Knepley         }
248e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
249e412dcbdSMatthew G. Knepley         switch (numCoords) {
250e412dcbdSMatthew G. Knepley         case 6:
251339e3443SMatthew 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);
252e412dcbdSMatthew G. Knepley           break;
253e412dcbdSMatthew G. Knepley         case 8:
254339e3443SMatthew 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);
255339e3443SMatthew 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);
256e412dcbdSMatthew G. Knepley           break;
257e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
258e412dcbdSMatthew G. Knepley         }
259e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
260e412dcbdSMatthew G. Knepley       }
261d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
262e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
263e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
264e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
265d1df6f1dSMatthew G. Knepley     }
266d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
267d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
268d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
269d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
270d1df6f1dSMatthew G. Knepley     }
271d1df6f1dSMatthew G. Knepley   }
272e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
273e412dcbdSMatthew G. Knepley }
274e412dcbdSMatthew G. Knepley 
275684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
276684b87d9SLisandro Dalcin {
277684b87d9SLisandro Dalcin   DM                      dm;
278684b87d9SLisandro Dalcin   Vec                     locv;
279684b87d9SLisandro Dalcin   const char              *name;
280684b87d9SLisandro Dalcin   PetscSection            section;
281684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
282e630c359SToby Isaac   PetscInt                numFields;
283684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
284684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
285684b87d9SLisandro Dalcin 
286684b87d9SLisandro Dalcin   PetscFunctionBegin;
287684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
288684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
289684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
290684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
291684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
29292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
293e630c359SToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
294e630c359SToby Isaac   if (!numFields) {
295684b87d9SLisandro Dalcin     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
296e630c359SToby Isaac     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
297e630c359SToby Isaac   } else {
298e630c359SToby Isaac     PetscInt f;
299e630c359SToby Isaac 
300e630c359SToby Isaac     for (f = 0; f < numFields; f++) {
301e630c359SToby Isaac       ierr = DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);CHKERRQ(ierr);
302e630c359SToby Isaac       if (ft == PETSC_VTK_INVALID) continue;
303e630c359SToby Isaac       ierr = PetscObjectReference((PetscObject)locv);CHKERRQ(ierr);
304e630c359SToby Isaac       ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
305e630c359SToby Isaac     }
306e630c359SToby Isaac     ierr = VecDestroy(&locv);CHKERRQ(ierr);
307e630c359SToby Isaac   }
308684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
309684b87d9SLisandro Dalcin }
310684b87d9SLisandro Dalcin 
311552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
312552f7358SJed Brown {
313552f7358SJed Brown   DM             dm;
314684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
315552f7358SJed Brown   PetscErrorCode ierr;
316552f7358SJed Brown 
317552f7358SJed Brown   PetscFunctionBegin;
318552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
31982f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
320552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
321b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
322f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3238135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
325684b87d9SLisandro Dalcin     PetscInt    i,numFields;
326684b87d9SLisandro Dalcin     PetscObject fe;
327ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
328684b87d9SLisandro Dalcin     Vec         locv = v;
329684b87d9SLisandro Dalcin     const char  *name;
330684b87d9SLisandro Dalcin     PetscInt    step;
331684b87d9SLisandro Dalcin     PetscReal   time;
332ef31f671SMatthew G. Knepley 
333ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
334684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
33544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &fe);CHKERRQ(ierr);
336684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
337ef31f671SMatthew G. Knepley     }
338684b87d9SLisandro Dalcin     if (fem) {
339684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
340684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
341684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
342684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
343684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
344684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
345ef31f671SMatthew G. Knepley     }
346552f7358SJed Brown     if (isvtk) {
347684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
348b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
349b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
350684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
351b136c2c9SMatthew G. Knepley #else
352b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
353b136c2c9SMatthew G. Knepley #endif
354f13a32a3SMatthew G. Knepley     } else if (isdraw) {
355684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
356684b87d9SLisandro Dalcin     } else if (isglvis) {
357684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
358684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
359684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
360684b87d9SLisandro Dalcin     }
361684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
362552f7358SJed Brown   } else {
363684b87d9SLisandro Dalcin     PetscBool isseq;
364684b87d9SLisandro Dalcin 
365684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
36655f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
36755f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
368552f7358SJed Brown   }
369552f7358SJed Brown   PetscFunctionReturn(0);
370552f7358SJed Brown }
371552f7358SJed Brown 
372552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
373552f7358SJed Brown {
374552f7358SJed Brown   DM             dm;
375684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
376552f7358SJed Brown   PetscErrorCode ierr;
377552f7358SJed Brown 
378552f7358SJed Brown   PetscFunctionBegin;
379552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
38082f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
381552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
38233c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
383e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3848135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
385684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
386552f7358SJed Brown     Vec         locv;
387552f7358SJed Brown     const char *name;
388552f7358SJed Brown 
389c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
390552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
391552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
392552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
393552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
394552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
395c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
396b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
397b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
39839d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
399b136c2c9SMatthew G. Knepley #else
400b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
401b136c2c9SMatthew G. Knepley #endif
402552f7358SJed Brown   } else {
403684b87d9SLisandro Dalcin     PetscBool isseq;
404684b87d9SLisandro Dalcin 
405684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
40655f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
40755f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
408552f7358SJed Brown   }
409552f7358SJed Brown   PetscFunctionReturn(0);
410552f7358SJed Brown }
411552f7358SJed Brown 
412d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
413d930f514SMatthew G. Knepley {
414d930f514SMatthew G. Knepley   DM                dm;
415d930f514SMatthew G. Knepley   MPI_Comm          comm;
416d930f514SMatthew G. Knepley   PetscViewerFormat format;
417d930f514SMatthew G. Knepley   Vec               v;
418d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
419d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
420d930f514SMatthew G. Knepley 
421d930f514SMatthew G. Knepley   PetscFunctionBegin;
422d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
423d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4242c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
425d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
429a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
430a8ad634aSStefano Zampini     /* this need a better fix */
431a8ad634aSStefano Zampini     if (dm->useNatural) {
432a8ad634aSStefano Zampini       if (dm->sfNatural) {
433d930f514SMatthew G. Knepley         const char *vecname;
434d930f514SMatthew G. Knepley         PetscInt    n, nroots;
435d930f514SMatthew G. Knepley 
436ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
437d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley         if (n == nroots) {
439d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
440d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
441d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
442d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
443d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
444d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
445d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
446a8ad634aSStefano Zampini     } else v = originalv;
447a8ad634aSStefano Zampini   } else v = originalv;
448a8ad634aSStefano Zampini 
449d930f514SMatthew G. Knepley   if (ishdf5) {
450d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
45139d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
452d930f514SMatthew G. Knepley #else
453d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
454d930f514SMatthew G. Knepley #endif
455d930f514SMatthew G. Knepley   } else if (isvtk) {
456d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
457d930f514SMatthew G. Knepley   } else {
458d930f514SMatthew G. Knepley     PetscBool isseq;
459d930f514SMatthew G. Knepley 
460d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
461d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
462d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
463d930f514SMatthew G. Knepley   }
464a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
465d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
466d930f514SMatthew G. Knepley }
467d930f514SMatthew G. Knepley 
4682c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4692c40f234SMatthew G. Knepley {
4702c40f234SMatthew G. Knepley   DM             dm;
4712c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4722c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4732c40f234SMatthew G. Knepley 
4742c40f234SMatthew G. Knepley   PetscFunctionBegin;
4752c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4772c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4782c40f234SMatthew G. Knepley   if (ishdf5) {
4792c40f234SMatthew G. Knepley     DM          dmBC;
4802c40f234SMatthew G. Knepley     Vec         gv;
4812c40f234SMatthew G. Knepley     const char *name;
4822c40f234SMatthew G. Knepley 
4832c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4842c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4852c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4862c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4872c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4882c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4892c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4902c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4912c40f234SMatthew G. Knepley   } else {
4922c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   }
4942c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4952c40f234SMatthew G. Knepley }
4962c40f234SMatthew G. Knepley 
4972c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4982c40f234SMatthew G. Knepley {
4992c40f234SMatthew G. Knepley   DM             dm;
5002c40f234SMatthew G. Knepley   PetscBool      ishdf5;
5012c40f234SMatthew G. Knepley   PetscErrorCode ierr;
5022c40f234SMatthew G. Knepley 
5032c40f234SMatthew G. Knepley   PetscFunctionBegin;
5042c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
5052c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
5062c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5072c40f234SMatthew G. Knepley   if (ishdf5) {
508878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
50939d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
510b136c2c9SMatthew G. Knepley #else
511b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
512878b459fSMatthew G. Knepley #endif
5132c40f234SMatthew G. Knepley   } else {
5142c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
515552f7358SJed Brown   }
516552f7358SJed Brown   PetscFunctionReturn(0);
517552f7358SJed Brown }
518552f7358SJed Brown 
519d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
520d930f514SMatthew G. Knepley {
521d930f514SMatthew G. Knepley   DM                dm;
522d930f514SMatthew G. Knepley   PetscViewerFormat format;
523d930f514SMatthew G. Knepley   PetscBool         ishdf5;
524d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
525d930f514SMatthew G. Knepley 
526d930f514SMatthew G. Knepley   PetscFunctionBegin;
527d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
529d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
530d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
532a8ad634aSStefano Zampini     if (dm->useNatural) {
533d930f514SMatthew G. Knepley       if (dm->sfNatural) {
534d930f514SMatthew G. Knepley         if (ishdf5) {
535d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
536d930f514SMatthew G. Knepley           Vec         v;
537d930f514SMatthew G. Knepley           const char *vecname;
538d930f514SMatthew G. Knepley 
539d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
540d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
541d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
54239d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
543d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
544d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
545d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
546d930f514SMatthew G. Knepley #else
547d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
548d930f514SMatthew G. Knepley #endif
549d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
550d930f514SMatthew G. Knepley       }
551a8ad634aSStefano Zampini     } else {
552a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
553a8ad634aSStefano Zampini     }
554d930f514SMatthew G. Knepley   }
555d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
556d930f514SMatthew G. Knepley }
557d930f514SMatthew G. Knepley 
5587cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
559731e8ddeSMatthew G. Knepley {
560731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
561731e8ddeSMatthew G. Knepley   Vec                coordinates;
562ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
563731e8ddeSMatthew G. Knepley   const char        *name[4];
564731e8ddeSMatthew G. Knepley   const PetscScalar *a;
565731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
566731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
567731e8ddeSMatthew G. Knepley 
568731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
569731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
570731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
571731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
572731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
573ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
575731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
577731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
578731e8ddeSMatthew G. Knepley   name[1]     = "edge";
579731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
580731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
581731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
582731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
583ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
584731e8ddeSMatthew G. Knepley 
585ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
586ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
587731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
588731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
590731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
591731e8ddeSMatthew G. Knepley 
592731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
593731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
594731e8ddeSMatthew G. Knepley       if (!dof) continue;
595731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
596731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
597f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
598731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
599731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
600731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
601731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
602087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
603731e8ddeSMatthew G. Knepley         }
604731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
605731e8ddeSMatthew G. Knepley       }
606731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
607731e8ddeSMatthew G. Knepley     }
608731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
609731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
610731e8ddeSMatthew G. Knepley   }
611731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
612731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
613731e8ddeSMatthew G. Knepley }
614731e8ddeSMatthew G. Knepley 
6157cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
616552f7358SJed Brown {
617552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
618552f7358SJed Brown   DM                cdm;
619552f7358SJed Brown   DMLabel           markers;
620552f7358SJed Brown   PetscSection      coordSection;
621552f7358SJed Brown   Vec               coordinates;
622552f7358SJed Brown   PetscViewerFormat format;
623552f7358SJed Brown   PetscErrorCode    ierr;
624552f7358SJed Brown 
625552f7358SJed Brown   PetscFunctionBegin;
626552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62792fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
628552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
629552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
630552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
631552f7358SJed Brown     const char *name;
632f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
633552f7358SJed Brown     PetscInt    pStart, pEnd, p;
634552f7358SJed Brown     PetscMPIInt rank, size;
635552f7358SJed Brown 
63682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
63782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
638552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
639552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
640552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
641f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
642f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
643f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
644f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
645f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
646e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6474d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
648e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
649552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
650552f7358SJed Brown       PetscInt dof, off, s;
651552f7358SJed Brown 
652552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
653552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
654552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
655e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
656552f7358SJed Brown       }
657552f7358SJed Brown     }
658552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
659e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
660e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
661552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
662552f7358SJed Brown       PetscInt dof, off, c;
663552f7358SJed Brown 
664552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
665552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
666552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
667e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
668552f7358SJed Brown       }
669552f7358SJed Brown     }
670552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6714d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6723d2e540fSStefano Zampini     if (coordSection && coordinates) {
67380180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6743d2e540fSStefano Zampini     }
675c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6767422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
677552f7358SJed Brown     if (size > 1) {
678552f7358SJed Brown       PetscSF sf;
679552f7358SJed Brown 
680552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
681552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
682552f7358SJed Brown     }
683552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
684552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6850588280cSMatthew G. Knepley     const char  *name, *color;
6860588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6870588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
688fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
689552f7358SJed Brown     PetscReal    scale         = 2.0;
69078081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6910588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6920588280cSMatthew G. Knepley     double       tcoords[3];
693552f7358SJed Brown     PetscScalar *coords;
6940588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
695552f7358SJed Brown     PetscMPIInt  rank, size;
6960588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
697fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
698fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
699fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
700552f7358SJed Brown 
7010588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
702552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
703c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
7040588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
7050588280cSMatthew G. Knepley     numColors  = 10;
7060588280cSMatthew G. Knepley     numLColors = 10;
7070588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
708c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
70978081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
710c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
711c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
7120588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
713c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
7140588280cSMatthew G. Knepley     if (!useColors) {
7150588280cSMatthew G. Knepley       numColors = 3;
7160588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
7170588280cSMatthew G. Knepley     }
718c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
7190588280cSMatthew G. Knepley     if (!useColors) {
7200588280cSMatthew G. Knepley       numLColors = 4;
7210588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7220588280cSMatthew G. Knepley     }
723fe1cc32dSStefano Zampini     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, PETSC_MAX_PATH_LEN, &lflg);CHKERRQ(ierr);
724202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
725202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
726202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
727202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
728fe1cc32dSStefano Zampini 
729fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
730fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
731fe1cc32dSStefano Zampini     if (lflg) {
732fe1cc32dSStefano Zampini       DMLabel lbl;
733fe1cc32dSStefano Zampini 
734fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
735fe1cc32dSStefano Zampini       if (lbl) {
736fe1cc32dSStefano Zampini         PetscInt val, defval;
737fe1cc32dSStefano Zampini 
738fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
739fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
740fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
741fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
742fe1cc32dSStefano Zampini           PetscInt  closureSize;
743fe1cc32dSStefano Zampini 
744fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
745fe1cc32dSStefano Zampini           if (val == defval) continue;
746fe1cc32dSStefano Zampini 
747fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
748fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
749fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
750fe1cc32dSStefano Zampini           }
751fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
752fe1cc32dSStefano Zampini         }
753fe1cc32dSStefano Zampini       }
754fe1cc32dSStefano Zampini     }
755fe1cc32dSStefano Zampini 
75682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
75782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
758552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
759770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7600588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
761552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
762552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
763552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7640588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7650588280cSMatthew G. Knepley     if (size > 1) {
766f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
767770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
768770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
769770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
770770b213bSMatthew G Knepley         } else if (p > 0) {
771770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
772770b213bSMatthew G Knepley         }
773770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
774770b213bSMatthew G Knepley       }
7750588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7760588280cSMatthew G. Knepley     }
777087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
778fe1cc32dSStefano Zampini 
779552f7358SJed Brown     /* Plot vertices */
780552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
781552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7824d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
783552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
784552f7358SJed Brown       PetscInt  off, dof, d;
7850588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
786552f7358SJed Brown 
787fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
788552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
789552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7900588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
791f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7920588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7930588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
794c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7950588280cSMatthew G. Knepley       }
7960588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7970588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
798552f7358SJed Brown       for (d = 0; d < dof; ++d) {
799552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
800087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
801552f7358SJed Brown       }
8020588280cSMatthew G. Knepley       color = colors[rank%numColors];
8030588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
8040588280cSMatthew G. Knepley         PetscInt val;
805c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8060588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
8070588280cSMatthew G. Knepley       }
8080588280cSMatthew G. Knepley       if (useNumbers) {
809e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
8100588280cSMatthew G. Knepley       } else {
811e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
8120588280cSMatthew G. Knepley       }
813552f7358SJed Brown     }
814552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
815552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
816846a3e8bSMatthew G. Knepley     /* Plot cells */
817846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
81878081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
819846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
820846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
821846a3e8bSMatthew G. Knepley         const PetscInt *cone;
822846a3e8bSMatthew G. Knepley 
823fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
824846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
825846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
826846a3e8bSMatthew G. Knepley           PetscInt val;
827846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
828846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
829846a3e8bSMatthew G. Knepley         }
830846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
831846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
832846a3e8bSMatthew G. Knepley       }
833846a3e8bSMatthew G. Knepley     } else {
834846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
835846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
836846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
837846a3e8bSMatthew G. Knepley 
838fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
839846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
840846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
841846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
842846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
843846a3e8bSMatthew G. Knepley 
844846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
845846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
846846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
847846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
848846a3e8bSMatthew G. Knepley         }
849846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
850846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
851846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
852846a3e8bSMatthew G. Knepley       }
853846a3e8bSMatthew G. Knepley     }
854846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
855846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
856846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
857846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
858846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
859846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
860846a3e8bSMatthew G. Knepley 
861fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
862846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
863846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
864846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
865846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
866846a3e8bSMatthew G. Knepley         PetscInt       off;
867846a3e8bSMatthew G. Knepley 
868846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
869846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
870846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
871846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
872846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
873846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
874846a3e8bSMatthew G. Knepley         }
875846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
876846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
877846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
878846a3e8bSMatthew G. Knepley         ++n;
879846a3e8bSMatthew G. Knepley       }
880846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
881846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
882846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
883846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
884087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
885846a3e8bSMatthew G. Knepley       }
886846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
887846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
888846a3e8bSMatthew G. Knepley         PetscInt val;
889846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
890846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
891846a3e8bSMatthew G. Knepley       }
892846a3e8bSMatthew G. Knepley       if (useNumbers) {
893846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
894846a3e8bSMatthew G. Knepley       } else {
895846a3e8bSMatthew 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);
896846a3e8bSMatthew G. Knepley       }
897846a3e8bSMatthew G. Knepley     }
898846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
899552f7358SJed Brown     /* Plot edges */
900202fd40aSStefano Zampini     if (plotEdges) {
901552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
902552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
903552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
904552f7358SJed Brown         const PetscInt *cone;
905552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
906552f7358SJed Brown 
907fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
908552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
909f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
910552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
911552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
912552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
913552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
914552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
915552f7358SJed Brown         for (d = 0; d < dof; ++d) {
916202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
917c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
918552f7358SJed Brown         }
9190588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
9200588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
9210588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9220588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
923f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9240588280cSMatthew G. Knepley         }
9250588280cSMatthew G. Knepley         color = colors[rank%numColors];
9260588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9270588280cSMatthew G. Knepley           PetscInt val;
928c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9290750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9300588280cSMatthew G. Knepley         }
931e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
932552f7358SJed Brown       }
933552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
934552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
935552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9360588280cSMatthew G. Knepley     }
937552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9384d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9390588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
940770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9410588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9420588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9430588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9440588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
945fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9460f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9470f7d6e4aSStefano Zampini     Vec                    cown,acown;
9480f7d6e4aSStefano Zampini     VecScatter             sct;
9490f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9500f7d6e4aSStefano Zampini     IS                     gid,acis;
9510f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9520f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9530f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9540f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9550f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9560f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9570f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9580f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
9590f7d6e4aSStefano Zampini 
9600f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
9610f7d6e4aSStefano Zampini     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
962b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
9630f7d6e4aSStefano Zampini     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRQ(ierr);
9640f7d6e4aSStefano Zampini #endif
9650f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
9660f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(comm,&ggroup);CHKERRQ(ierr);
9670f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRQ(ierr);
9680f7d6e4aSStefano Zampini       d1   = 0;
9690f7d6e4aSStefano Zampini       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRQ(ierr);
9700f7d6e4aSStefano Zampini       nid  = d2;
9710f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ggroup);CHKERRQ(ierr);
9720f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ngroup);CHKERRQ(ierr);
9730f7d6e4aSStefano Zampini       ierr = MPI_Comm_free(&ncomm);CHKERRQ(ierr);
9740f7d6e4aSStefano Zampini     } else nid = 0.0;
9750f7d6e4aSStefano Zampini 
9760f7d6e4aSStefano Zampini     /* Get connectivity */
9770f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
9780f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
9790f7d6e4aSStefano Zampini 
9800f7d6e4aSStefano Zampini     /* filter overlapped local cells */
9810f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
9820f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
9830f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
9840f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
9850f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
9860f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
9870f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
9880f7d6e4aSStefano Zampini     }
9890f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
9900f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
9910f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9920f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
9930f7d6e4aSStefano Zampini 
9940f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
9950f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
9960f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
9970f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
9980f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
9990f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
10000f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
10010f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
10020f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10030f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10040f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
10050f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
10060f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
10070f7d6e4aSStefano Zampini 
10080f7d6e4aSStefano Zampini     /* compute edgeCut */
10090f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
10100f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
10110f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
10120f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
10130f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
10140f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
10150f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
10160f7d6e4aSStefano Zampini       PetscInt totl;
10170f7d6e4aSStefano Zampini 
10180f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
10190f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
10200f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
10210f7d6e4aSStefano Zampini         if (work[i] < 0) {
10220f7d6e4aSStefano Zampini           ect  += 1;
10230f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10240f7d6e4aSStefano Zampini         }
10250f7d6e4aSStefano Zampini       }
10260f7d6e4aSStefano Zampini     }
10270f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10280f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10290f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10300f7d6e4aSStefano Zampini     lm[1] = -numVertices;
10310f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRQ(ierr);
10320f7d6e4aSStefano 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);
10330f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10340f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10350f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
10360f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRQ(ierr);
10370f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1038b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
10390f7d6e4aSStefano 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);
10400f7d6e4aSStefano Zampini #else
10410f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10420f7d6e4aSStefano Zampini #endif
10430f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10440f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10450f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10460f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1047552f7358SJed Brown   } else {
104882f516ccSBarry Smith     MPI_Comm    comm;
1049d80ece95SMatthew G. Knepley     PetscInt   *sizes, *hybsizes, *ghostsizes;
1050f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
1051d80ece95SMatthew G. Knepley     PetscInt    pStart, pEnd, p, gcStart, gcEnd, gcNum;
1052a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
10531143a3c0SMatthew G. Knepley     const char *name;
1054552f7358SJed Brown     PetscMPIInt size;
1055552f7358SJed Brown 
105682f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1057552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1058c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1059f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10605f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1061f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1062f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1063f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1064552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1065b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
1066a04427b4SStefano Zampini     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);CHKERRQ(ierr);
1067d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1068d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1069d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1070552f7358SJed Brown     if (depth == 1) {
1071552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1072552f7358SJed Brown       pEnd = pEnd - pStart;
1073a04427b4SStefano Zampini       pMax[0] -= pStart;
1074552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1075a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1076d80ece95SMatthew G. Knepley       ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1077f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
1078a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1079a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1080a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1081a04427b4SStefano Zampini       }
1082552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1083552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1084552f7358SJed Brown       pEnd = pEnd - pStart;
1085a04427b4SStefano Zampini       pMax[depth] -= pStart;
1086552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1087a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1088552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
1089a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1090d80ece95SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);
1091d80ece95SMatthew G. Knepley         if (hybsizes[p] >= 0)   {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1092d80ece95SMatthew G. Knepley         if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1093a04427b4SStefano Zampini       }
1094552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1095552f7358SJed Brown     } else {
1096cbb7f117SMark Adams       PetscMPIInt rank;
1097cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1098552f7358SJed Brown       for (d = 0; d <= dim; d++) {
1099552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1100834065abSMatthew G. Knepley         pEnd    -= pStart;
1101834065abSMatthew G. Knepley         pMax[d] -= pStart;
1102552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1103834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1104d80ece95SMatthew G. Knepley         if (d == dim) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);}
1105552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
1106834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
1107cbb7f117SMark Adams           if (!rank) {
1108d80ece95SMatthew G. Knepley             ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);
1109d80ece95SMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1110d80ece95SMatthew G. Knepley             if (d == dim && ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1111834065abSMatthew G. Knepley           }
1112cbb7f117SMark Adams         }
1113552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1114552f7358SJed Brown       }
1115552f7358SJed Brown     }
1116d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
1117c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1118a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1119a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1120a57dd577SMatthew G Knepley       DMLabel         label;
1121a57dd577SMatthew G Knepley       const char     *name;
1122a57dd577SMatthew G Knepley       IS              valueIS;
1123a57dd577SMatthew G Knepley       const PetscInt *values;
1124a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1125a57dd577SMatthew G Knepley 
1126c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1127c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1128a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1129d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1130a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1131a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1132120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1133a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1134a57dd577SMatthew G Knepley         PetscInt size;
1135a57dd577SMatthew G Knepley 
1136a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1137a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1138d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1139a57dd577SMatthew G Knepley       }
1140a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1141120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1142a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11434d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1144a57dd577SMatthew G Knepley     }
114534aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
114634aa8a36SMatthew G. Knepley     if (dm->Nf) {
114734aa8a36SMatthew G. Knepley       PetscInt f;
114834aa8a36SMatthew G. Knepley 
114934aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
115034aa8a36SMatthew G. Knepley         const char *name;
115134aa8a36SMatthew G. Knepley 
115234aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
115334aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
115434aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
115534aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
115634aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
115734aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1158ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
115934aa8a36SMatthew G. Knepley         } else {
116034aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
116134aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
116234aa8a36SMatthew G. Knepley         }
116334aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
116434aa8a36SMatthew G. Knepley       }
116534aa8a36SMatthew G. Knepley     }
1166a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11678e7ff633SMatthew G. Knepley     if (cdm) {
11688e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11698e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11708e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11718e7ff633SMatthew G. Knepley     }
1172552f7358SJed Brown   }
1173552f7358SJed Brown   PetscFunctionReturn(0);
1174552f7358SJed Brown }
1175552f7358SJed Brown 
11767cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1177e412dcbdSMatthew G. Knepley {
1178e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1179e412dcbdSMatthew G. Knepley   DM                 cdm;
1180e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1181e412dcbdSMatthew G. Knepley   Vec                coordinates;
1182e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
118329494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1184e412dcbdSMatthew G. Knepley   PetscBool          isnull;
1185e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1186cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
1187e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1188e412dcbdSMatthew G. Knepley 
1189e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1190e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1191e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1192e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
119392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1194e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1195e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1196e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1197e412dcbdSMatthew G. Knepley 
1198e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1199e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1200e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1201e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1202e412dcbdSMatthew G. Knepley 
1203e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1204e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1205e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
12060c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
12070c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1208e412dcbdSMatthew G. Knepley   }
1209e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
121029494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
121129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
121229494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1213e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1214e412dcbdSMatthew G. Knepley 
1215cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1216cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1217cf3064d3SMatthew G. Knepley     PetscScalar   *coords = NULL;
1218ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
1219ba2698f1SMatthew G. Knepley     PetscInt       numCoords;
1220cf3064d3SMatthew G. Knepley 
1221ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
1222cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1223ba2698f1SMatthew G. Knepley     switch (ct) {
1224ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
1225cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1226cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1227cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1228cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1229cf3064d3SMatthew G. Knepley       break;
1230ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1231a46c2b5aSMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1232cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1233cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1234a46c2b5aSMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1235a46c2b5aSMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1236a46c2b5aSMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1237cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1238cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1239cf3064d3SMatthew G. Knepley       break;
1240ba2698f1SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1241cf3064d3SMatthew G. Knepley     }
1242cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1243cf3064d3SMatthew G. Knepley   }
1244e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1245e412dcbdSMatthew G. Knepley     PetscScalar   *coords = NULL;
1246ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
1247ba2698f1SMatthew G. Knepley     PetscInt       numCoords;
1248e412dcbdSMatthew G. Knepley 
1249ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
1250e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1251ba2698f1SMatthew G. Knepley     switch (ct) {
1252ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
12530c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12540c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12550c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1256e412dcbdSMatthew G. Knepley       break;
1257ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
12580c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12590c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12600c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12610c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1262e412dcbdSMatthew G. Knepley       break;
1263ba2698f1SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1264e412dcbdSMatthew G. Knepley     }
1265e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1266e412dcbdSMatthew G. Knepley   }
1267e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1268e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1269e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1270e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1271e412dcbdSMatthew G. Knepley }
1272e412dcbdSMatthew G. Knepley 
1273552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1274552f7358SJed Brown {
1275e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1276002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1277552f7358SJed Brown   PetscErrorCode ierr;
1278552f7358SJed Brown 
1279552f7358SJed Brown   PetscFunctionBegin;
1280552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1281552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1282552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1283fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1284c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1285e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
12868135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1287552f7358SJed Brown   if (iascii) {
12888135c375SStefano Zampini     PetscViewerFormat format;
12898135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12908135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
12918135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12928135c375SStefano Zampini     } else {
1293552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
12948135c375SStefano Zampini     }
1295c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1296c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
129739d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1298c6ccd67eSMatthew G. Knepley #else
1299c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1300552f7358SJed Brown #endif
1301e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1302fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1303e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1304e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
13058135c375SStefano Zampini   } else if (isglvis) {
13068135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
130762201deeSVaclav Hapla   } else {
1308a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1309fcf6c8fdSToby Isaac   }
1310cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1311cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1312cb3ba0daSMatthew G. Knepley   if (flg) {
1313cb3ba0daSMatthew G. Knepley     Vec ranks;
1314cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1315cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1316cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1317cb3ba0daSMatthew G. Knepley   }
1318002a2709SMatthew G. Knepley   /* Optionally view a label */
1319002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1320002a2709SMatthew G. Knepley   if (flg) {
1321002a2709SMatthew G. Knepley     DMLabel label;
1322002a2709SMatthew G. Knepley     Vec     val;
1323002a2709SMatthew G. Knepley 
1324002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1325002a2709SMatthew 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);
1326002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1327002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1328002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1329002a2709SMatthew G. Knepley   }
1330552f7358SJed Brown   PetscFunctionReturn(0);
1331552f7358SJed Brown }
1332552f7358SJed Brown 
13332c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13342c40f234SMatthew G. Knepley {
1335d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13362c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13372c40f234SMatthew G. Knepley 
13382c40f234SMatthew G. Knepley   PetscFunctionBegin;
13392c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13402c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13412c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1342d4f5a9a0SVaclav Hapla   if (ishdf5) {
13432c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13449c48423bSVaclav Hapla     PetscViewerFormat format;
13459c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13469c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13479c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1348509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
134939d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1350509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
13512c40f234SMatthew G. Knepley #else
13522c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1353552f7358SJed Brown #endif
1354d4f5a9a0SVaclav Hapla   } else {
1355d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1356552f7358SJed Brown   }
1357552f7358SJed Brown   PetscFunctionReturn(0);
1358552f7358SJed Brown }
1359552f7358SJed Brown 
1360552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1361552f7358SJed Brown {
1362552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1363552f7358SJed Brown   PetscErrorCode ierr;
1364552f7358SJed Brown 
1365552f7358SJed Brown   PetscFunctionBegin;
13668135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13678135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
136828d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
13690d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1370552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1371552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1372552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1373552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1374be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1375552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1376552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1377d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1378d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
137977623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1380a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1381552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1382552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1383a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1384a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1385d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1386d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1387d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1388d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1389d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1390d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1391fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1392552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1393552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1394552f7358SJed Brown   PetscFunctionReturn(0);
1395552f7358SJed Brown }
1396552f7358SJed Brown 
1397b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1398552f7358SJed Brown {
13998d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1400acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1401552f7358SJed Brown   PetscInt               localSize;
1402837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1403552f7358SJed Brown   PetscErrorCode         ierr;
1404b412c318SBarry Smith   MatType                mtype;
14051428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1406552f7358SJed Brown 
1407552f7358SJed Brown   PetscFunctionBegin;
1408607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1409b412c318SBarry Smith   mtype = dm->mattype;
1410e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1411552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1412552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
141382f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1414552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1415552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1416552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1417acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1418acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1419552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1420552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1421552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1422552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1423552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1424552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1425552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1426837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1427552f7358SJed Brown   if (!isShell) {
1428be36d101SStefano Zampini     PetscSection subSection;
1429837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
14300be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1431fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1432552f7358SJed Brown 
143300140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1434be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1435be36d101SStefano Zampini       PetscSection section;
1436be36d101SStefano Zampini       PetscInt     size;
1437be36d101SStefano Zampini 
143892fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1439be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1440be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1441be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1442be36d101SStefano Zampini     } else {
144300140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1444be36d101SStefano Zampini     }
1445552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1446be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1447a9d99c84SMatthew G. Knepley       PetscInt bdof;
1448a9d99c84SMatthew G. Knepley 
1449552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1450fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
14511d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
14521d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
14531d17a0a3SMatthew G. Knepley       if (dof) {
14541d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1455be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1456be36d101SStefano Zampini       }
1457be36d101SStefano Zampini       if (isMatIS) {
1458be36d101SStefano Zampini         PetscInt loff,c,off;
1459be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1460be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1461be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1462552f7358SJed Brown       }
14632a28c762SMatthew G Knepley     }
14642a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14650be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14660be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14670be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14680be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14696fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
14706fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
14714fa26246SMatthew G. Knepley       PetscInt l;
14726fd5c86aSStefano Zampini 
14736fd5c86aSStefano Zampini       lsize = lsize/bs;
14746fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
14754fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1476be36d101SStefano Zampini     }
1477be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1478be36d101SStefano Zampini     if (isMatIS) {
1479be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1480be36d101SStefano Zampini     }
14811795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
14828d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1483552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1484552f7358SJed Brown   }
1485b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1486552f7358SJed Brown   PetscFunctionReturn(0);
1487552f7358SJed Brown }
1488552f7358SJed Brown 
14897cd05799SMatthew G. Knepley /*@
1490a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1491be36d101SStefano Zampini 
1492be36d101SStefano Zampini   Not collective
1493be36d101SStefano Zampini 
1494be36d101SStefano Zampini   Input Parameter:
1495be36d101SStefano Zampini . mesh - The DMPlex
1496be36d101SStefano Zampini 
1497be36d101SStefano Zampini   Output Parameters:
1498be36d101SStefano Zampini . subsection - The subdomain section
1499be36d101SStefano Zampini 
1500be36d101SStefano Zampini   Level: developer
1501be36d101SStefano Zampini 
1502be36d101SStefano Zampini .seealso:
15037cd05799SMatthew G. Knepley @*/
1504be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1505be36d101SStefano Zampini {
1506be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1507be36d101SStefano Zampini   PetscErrorCode ierr;
1508be36d101SStefano Zampini 
1509be36d101SStefano Zampini   PetscFunctionBegin;
1510be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1511be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1512be36d101SStefano Zampini     PetscSection section;
1513be36d101SStefano Zampini     PetscSF      sf;
1514be36d101SStefano Zampini 
1515be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
151692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1517be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1518be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1519be36d101SStefano Zampini   }
1520be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1521be36d101SStefano Zampini   PetscFunctionReturn(0);
1522be36d101SStefano Zampini }
1523be36d101SStefano Zampini 
1524552f7358SJed Brown /*@
1525552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1526552f7358SJed Brown 
1527552f7358SJed Brown   Not collective
1528552f7358SJed Brown 
1529552f7358SJed Brown   Input Parameter:
1530552f7358SJed Brown . mesh - The DMPlex
1531552f7358SJed Brown 
1532552f7358SJed Brown   Output Parameters:
1533552f7358SJed Brown + pStart - The first mesh point
1534552f7358SJed Brown - pEnd   - The upper bound for mesh points
1535552f7358SJed Brown 
1536552f7358SJed Brown   Level: beginner
1537552f7358SJed Brown 
1538552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1539552f7358SJed Brown @*/
1540552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1541552f7358SJed Brown {
1542552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1543552f7358SJed Brown   PetscErrorCode ierr;
1544552f7358SJed Brown 
1545552f7358SJed Brown   PetscFunctionBegin;
1546552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1547552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1548552f7358SJed Brown   PetscFunctionReturn(0);
1549552f7358SJed Brown }
1550552f7358SJed Brown 
1551552f7358SJed Brown /*@
1552552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1553552f7358SJed Brown 
1554552f7358SJed Brown   Not collective
1555552f7358SJed Brown 
1556552f7358SJed Brown   Input Parameters:
1557552f7358SJed Brown + mesh - The DMPlex
1558552f7358SJed Brown . pStart - The first mesh point
1559552f7358SJed Brown - pEnd   - The upper bound for mesh points
1560552f7358SJed Brown 
1561552f7358SJed Brown   Output Parameters:
1562552f7358SJed Brown 
1563552f7358SJed Brown   Level: beginner
1564552f7358SJed Brown 
1565552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1566552f7358SJed Brown @*/
1567552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1568552f7358SJed Brown {
1569552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1570552f7358SJed Brown   PetscErrorCode ierr;
1571552f7358SJed Brown 
1572552f7358SJed Brown   PetscFunctionBegin;
1573552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1574552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1575552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1576552f7358SJed Brown   PetscFunctionReturn(0);
1577552f7358SJed Brown }
1578552f7358SJed Brown 
1579552f7358SJed Brown /*@
1580eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1581552f7358SJed Brown 
1582552f7358SJed Brown   Not collective
1583552f7358SJed Brown 
1584552f7358SJed Brown   Input Parameters:
1585552f7358SJed Brown + mesh - The DMPlex
1586eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1587552f7358SJed Brown 
1588552f7358SJed Brown   Output Parameter:
1589552f7358SJed Brown . size - The cone size for point p
1590552f7358SJed Brown 
1591552f7358SJed Brown   Level: beginner
1592552f7358SJed Brown 
1593552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1594552f7358SJed Brown @*/
1595552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1596552f7358SJed Brown {
1597552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1598552f7358SJed Brown   PetscErrorCode ierr;
1599552f7358SJed Brown 
1600552f7358SJed Brown   PetscFunctionBegin;
1601552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1602552f7358SJed Brown   PetscValidPointer(size, 3);
1603552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1604552f7358SJed Brown   PetscFunctionReturn(0);
1605552f7358SJed Brown }
1606552f7358SJed Brown 
1607552f7358SJed Brown /*@
1608eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1609552f7358SJed Brown 
1610552f7358SJed Brown   Not collective
1611552f7358SJed Brown 
1612552f7358SJed Brown   Input Parameters:
1613552f7358SJed Brown + mesh - The DMPlex
1614eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1615552f7358SJed Brown - size - The cone size for point p
1616552f7358SJed Brown 
1617552f7358SJed Brown   Output Parameter:
1618552f7358SJed Brown 
1619552f7358SJed Brown   Note:
1620552f7358SJed Brown   This should be called after DMPlexSetChart().
1621552f7358SJed Brown 
1622552f7358SJed Brown   Level: beginner
1623552f7358SJed Brown 
1624552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1625552f7358SJed Brown @*/
1626552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1627552f7358SJed Brown {
1628552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1629552f7358SJed Brown   PetscErrorCode ierr;
1630552f7358SJed Brown 
1631552f7358SJed Brown   PetscFunctionBegin;
1632552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1633552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
16340d644c17SKarl Rupp 
1635552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1636552f7358SJed Brown   PetscFunctionReturn(0);
1637552f7358SJed Brown }
1638552f7358SJed Brown 
1639f5a469b9SMatthew G. Knepley /*@
1640eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1641f5a469b9SMatthew G. Knepley 
1642f5a469b9SMatthew G. Knepley   Not collective
1643f5a469b9SMatthew G. Knepley 
1644f5a469b9SMatthew G. Knepley   Input Parameters:
1645f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1646eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1647f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1648f5a469b9SMatthew G. Knepley 
1649f5a469b9SMatthew G. Knepley   Output Parameter:
1650f5a469b9SMatthew G. Knepley 
1651f5a469b9SMatthew G. Knepley   Note:
1652f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1653f5a469b9SMatthew G. Knepley 
1654f5a469b9SMatthew G. Knepley   Level: beginner
1655f5a469b9SMatthew G. Knepley 
1656f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1657f5a469b9SMatthew G. Knepley @*/
1658f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1659f5a469b9SMatthew G. Knepley {
1660f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1661f5a469b9SMatthew G. Knepley   PetscInt       csize;
1662f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1663f5a469b9SMatthew G. Knepley 
1664f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1665f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1666f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1667f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1668f5a469b9SMatthew G. Knepley 
1669f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1670f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1671f5a469b9SMatthew G. Knepley }
1672f5a469b9SMatthew G. Knepley 
1673552f7358SJed Brown /*@C
1674eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1675552f7358SJed Brown 
1676552f7358SJed Brown   Not collective
1677552f7358SJed Brown 
1678552f7358SJed Brown   Input Parameters:
1679833c876bSVaclav Hapla + dm - The DMPlex
1680eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1681552f7358SJed Brown 
1682552f7358SJed Brown   Output Parameter:
1683552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1684552f7358SJed Brown 
1685552f7358SJed Brown   Level: beginner
1686552f7358SJed Brown 
16873813dfbdSMatthew G Knepley   Fortran Notes:
16883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1690922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1691922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
16923813dfbdSMatthew G Knepley 
16930ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1694552f7358SJed Brown @*/
1695552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1696552f7358SJed Brown {
1697552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1698552f7358SJed Brown   PetscInt       off;
1699552f7358SJed Brown   PetscErrorCode ierr;
1700552f7358SJed Brown 
1701552f7358SJed Brown   PetscFunctionBegin;
1702552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1703552f7358SJed Brown   PetscValidPointer(cone, 3);
1704552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1705552f7358SJed Brown   *cone = &mesh->cones[off];
1706552f7358SJed Brown   PetscFunctionReturn(0);
1707552f7358SJed Brown }
1708552f7358SJed Brown 
17090ce7577fSVaclav Hapla /*@C
17100ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
17110ce7577fSVaclav Hapla 
17120ce7577fSVaclav Hapla   Not collective
17130ce7577fSVaclav Hapla 
17140ce7577fSVaclav Hapla   Input Parameters:
17150ce7577fSVaclav Hapla + dm - The DMPlex
17160ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
17170ce7577fSVaclav Hapla 
17180ce7577fSVaclav Hapla   Output Parameter:
17190ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
17200ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
17210ce7577fSVaclav Hapla 
17220ce7577fSVaclav Hapla   Level: intermediate
17230ce7577fSVaclav Hapla 
1724d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
17250ce7577fSVaclav Hapla @*/
17260ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
17270ce7577fSVaclav Hapla {
17280ce7577fSVaclav Hapla   PetscSection        cs, newcs;
17290ce7577fSVaclav Hapla   PetscInt            *cones;
17300ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
17310ce7577fSVaclav Hapla   PetscInt            n;
17320ce7577fSVaclav Hapla   PetscErrorCode      ierr;
17330ce7577fSVaclav Hapla 
17340ce7577fSVaclav Hapla   PetscFunctionBegin;
17350ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
17360ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
17370ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
17380ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
17390ce7577fSVaclav Hapla   if (pCones) {
17400ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
17410ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
17420ce7577fSVaclav Hapla   }
17430ce7577fSVaclav Hapla   PetscFunctionReturn(0);
17440ce7577fSVaclav Hapla }
17450ce7577fSVaclav Hapla 
1746af9eab45SVaclav Hapla /*@
1747af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1748d4636a37SVaclav Hapla 
1749d4636a37SVaclav Hapla   Not collective
1750d4636a37SVaclav Hapla 
1751d4636a37SVaclav Hapla   Input Parameters:
1752d4636a37SVaclav Hapla + dm - The DMPlex
1753af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1754d4636a37SVaclav Hapla 
1755d4636a37SVaclav Hapla   Output Parameter:
1756af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1757d4636a37SVaclav Hapla 
1758d4636a37SVaclav Hapla   Level: advanced
1759d4636a37SVaclav Hapla 
1760af9eab45SVaclav Hapla   Notes:
1761af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1762af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1763af9eab45SVaclav Hapla 
1764af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1765d4636a37SVaclav Hapla @*/
1766af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1767d4636a37SVaclav Hapla {
1768af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1769af9eab45SVaclav Hapla   PetscInt            depth;
1770d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1771d4636a37SVaclav Hapla 
1772d4636a37SVaclav Hapla   PetscFunctionBegin;
1773af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1774af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1775af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1776af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1777af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1778af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1779af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1780af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1781af9eab45SVaclav Hapla }
1782af9eab45SVaclav Hapla 
1783af9eab45SVaclav Hapla /*@
1784af9eab45SVaclav 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).
1785af9eab45SVaclav Hapla 
1786af9eab45SVaclav Hapla   Not collective
1787af9eab45SVaclav Hapla 
1788af9eab45SVaclav Hapla   Input Parameters:
1789af9eab45SVaclav Hapla + dm - The DMPlex
1790af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1791af9eab45SVaclav Hapla 
1792af9eab45SVaclav Hapla   Output Parameter:
1793af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1794af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1795af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1796af9eab45SVaclav Hapla 
1797af9eab45SVaclav Hapla   Level: advanced
1798af9eab45SVaclav Hapla 
1799af9eab45SVaclav Hapla   Notes:
1800af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1801af9eab45SVaclav Hapla 
1802af9eab45SVaclav 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.
1803af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1804af9eab45SVaclav Hapla 
1805af9eab45SVaclav 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:
1806af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1807af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1808af9eab45SVaclav Hapla 
1809af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1810af9eab45SVaclav Hapla @*/
1811af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1812af9eab45SVaclav Hapla {
1813af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1814af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1815af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1816af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1817af9eab45SVaclav Hapla   PetscSection        *sections_;
1818af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1819af9eab45SVaclav Hapla 
1820af9eab45SVaclav Hapla   PetscFunctionBegin;
1821af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1822af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1823af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1824af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1825af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1826af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1827af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1828af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1829af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1830af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1831af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1832af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1833af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1834af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1835af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1836af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
1837af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
1838af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
1839af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
1840af9eab45SVaclav Hapla       } else {
1841af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
1842af9eab45SVaclav Hapla       }
1843af9eab45SVaclav Hapla     }
1844af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
1845af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
1846af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
1847af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1848af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
1849af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
1850af9eab45SVaclav Hapla       if (cn > 1) {
1851af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
1852af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
1853af9eab45SVaclav Hapla       } else {
1854af9eab45SVaclav Hapla         newarr[co] = arr[i];
1855af9eab45SVaclav Hapla       }
1856af9eab45SVaclav Hapla     }
1857af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
1858af9eab45SVaclav Hapla     arr = newarr;
1859af9eab45SVaclav Hapla     n = newn;
1860af9eab45SVaclav Hapla   }
1861ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
1862af9eab45SVaclav Hapla   *depth = depth_;
1863af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
1864af9eab45SVaclav Hapla   else {
1865af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
1866af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
1867af9eab45SVaclav Hapla   }
1868af9eab45SVaclav Hapla   if (sections) *sections = sections_;
1869af9eab45SVaclav Hapla   else {
1870af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
1871af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
1872af9eab45SVaclav Hapla   }
1873af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1874af9eab45SVaclav Hapla }
1875af9eab45SVaclav Hapla 
1876af9eab45SVaclav Hapla /*@
1877af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
1878af9eab45SVaclav Hapla 
1879af9eab45SVaclav Hapla   Not collective
1880af9eab45SVaclav Hapla 
1881af9eab45SVaclav Hapla   Input Parameters:
1882af9eab45SVaclav Hapla + dm - The DMPlex
1883af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1884af9eab45SVaclav Hapla 
1885af9eab45SVaclav Hapla   Output Parameter:
1886af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1887af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
1888af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1889af9eab45SVaclav Hapla 
1890af9eab45SVaclav Hapla   Level: advanced
1891af9eab45SVaclav Hapla 
1892af9eab45SVaclav Hapla   Notes:
1893af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
1894af9eab45SVaclav Hapla 
1895af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1896af9eab45SVaclav Hapla @*/
1897af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1898af9eab45SVaclav Hapla {
1899af9eab45SVaclav Hapla   PetscInt            d, depth_;
1900af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1901af9eab45SVaclav Hapla 
1902af9eab45SVaclav Hapla   PetscFunctionBegin;
1903af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1904af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1905af9eab45SVaclav Hapla   if (depth) *depth = 0;
1906af9eab45SVaclav Hapla   if (expandedPoints) {
1907af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
1908af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
1909af9eab45SVaclav Hapla   }
1910af9eab45SVaclav Hapla   if (sections)  {
1911af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
1912af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
1913af9eab45SVaclav Hapla   }
1914d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1915d4636a37SVaclav Hapla }
1916d4636a37SVaclav Hapla 
1917552f7358SJed Brown /*@
191892371b87SBarry 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
1919552f7358SJed Brown 
1920552f7358SJed Brown   Not collective
1921552f7358SJed Brown 
1922552f7358SJed Brown   Input Parameters:
1923552f7358SJed Brown + mesh - The DMPlex
1924eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1925552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1926552f7358SJed Brown 
1927552f7358SJed Brown   Output Parameter:
1928552f7358SJed Brown 
1929552f7358SJed Brown   Note:
1930552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1931552f7358SJed Brown 
193292371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
193392371b87SBarry Smith 
1934552f7358SJed Brown   Level: beginner
1935552f7358SJed Brown 
193692371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1937552f7358SJed Brown @*/
1938552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1939552f7358SJed Brown {
1940552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1941552f7358SJed Brown   PetscInt       pStart, pEnd;
1942552f7358SJed Brown   PetscInt       dof, off, c;
1943552f7358SJed Brown   PetscErrorCode ierr;
1944552f7358SJed Brown 
1945552f7358SJed Brown   PetscFunctionBegin;
1946552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1947552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1948552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1949552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1950552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
195182f516ccSBarry 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);
1952552f7358SJed Brown   for (c = 0; c < dof; ++c) {
195382f516ccSBarry 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);
1954552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1955552f7358SJed Brown   }
1956552f7358SJed Brown   PetscFunctionReturn(0);
1957552f7358SJed Brown }
1958552f7358SJed Brown 
1959552f7358SJed Brown /*@C
1960eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1961552f7358SJed Brown 
1962552f7358SJed Brown   Not collective
1963552f7358SJed Brown 
1964552f7358SJed Brown   Input Parameters:
1965552f7358SJed Brown + mesh - The DMPlex
1966eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1967552f7358SJed Brown 
1968552f7358SJed Brown   Output Parameter:
1969552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1970552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1971552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1972552f7358SJed Brown                     the index of the cone point on which to start.
1973552f7358SJed Brown 
1974552f7358SJed Brown   Level: beginner
1975552f7358SJed Brown 
19763813dfbdSMatthew G Knepley   Fortran Notes:
19773813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19783813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19793b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1980922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
19813813dfbdSMatthew G Knepley 
1982552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1983552f7358SJed Brown @*/
1984552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1985552f7358SJed Brown {
1986552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1987552f7358SJed Brown   PetscInt       off;
1988552f7358SJed Brown   PetscErrorCode ierr;
1989552f7358SJed Brown 
1990552f7358SJed Brown   PetscFunctionBegin;
1991552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1992552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1993552f7358SJed Brown   {
1994552f7358SJed Brown     PetscInt dof;
1995552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1996552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1997552f7358SJed Brown   }
1998552f7358SJed Brown #endif
1999552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
20000d644c17SKarl Rupp 
2001552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2002552f7358SJed Brown   PetscFunctionReturn(0);
2003552f7358SJed Brown }
2004552f7358SJed Brown 
2005552f7358SJed Brown /*@
2006eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2007552f7358SJed Brown 
2008552f7358SJed Brown   Not collective
2009552f7358SJed Brown 
2010552f7358SJed Brown   Input Parameters:
2011552f7358SJed Brown + mesh - The DMPlex
2012eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2013552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2014552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
2015552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
2016552f7358SJed Brown                     the index of the cone point on which to start.
2017552f7358SJed Brown 
2018552f7358SJed Brown   Output Parameter:
2019552f7358SJed Brown 
2020552f7358SJed Brown   Note:
2021552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2022552f7358SJed Brown 
2023552f7358SJed Brown   Level: beginner
2024552f7358SJed Brown 
2025552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2026552f7358SJed Brown @*/
2027552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2028552f7358SJed Brown {
2029552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2030552f7358SJed Brown   PetscInt       pStart, pEnd;
2031552f7358SJed Brown   PetscInt       dof, off, c;
2032552f7358SJed Brown   PetscErrorCode ierr;
2033552f7358SJed Brown 
2034552f7358SJed Brown   PetscFunctionBegin;
2035552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2036552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2037552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2038552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2039552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
204082f516ccSBarry 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);
2041552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2042552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2043552f7358SJed Brown 
2044552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
204582f516ccSBarry 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);
2046552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2047552f7358SJed Brown   }
2048552f7358SJed Brown   PetscFunctionReturn(0);
2049552f7358SJed Brown }
2050552f7358SJed Brown 
20517cd05799SMatthew G. Knepley /*@
2052eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
20537cd05799SMatthew G. Knepley 
20547cd05799SMatthew G. Knepley   Not collective
20557cd05799SMatthew G. Knepley 
20567cd05799SMatthew G. Knepley   Input Parameters:
20577cd05799SMatthew G. Knepley + mesh - The DMPlex
2058eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20597cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20607cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
20617cd05799SMatthew G. Knepley 
20627cd05799SMatthew G. Knepley   Level: beginner
20637cd05799SMatthew G. Knepley 
20647cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20657cd05799SMatthew G. Knepley @*/
2066552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2067552f7358SJed Brown {
2068552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2069552f7358SJed Brown   PetscInt       pStart, pEnd;
2070552f7358SJed Brown   PetscInt       dof, off;
2071552f7358SJed Brown   PetscErrorCode ierr;
2072552f7358SJed Brown 
2073552f7358SJed Brown   PetscFunctionBegin;
2074552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2075552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
207682f516ccSBarry 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);
207782f516ccSBarry 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);
207877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
207977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
208077c88f5bSMatthew 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);
2081552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2082552f7358SJed Brown   PetscFunctionReturn(0);
2083552f7358SJed Brown }
2084552f7358SJed Brown 
20857cd05799SMatthew G. Knepley /*@
2086eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
20877cd05799SMatthew G. Knepley 
20887cd05799SMatthew G. Knepley   Not collective
20897cd05799SMatthew G. Knepley 
20907cd05799SMatthew G. Knepley   Input Parameters:
20917cd05799SMatthew G. Knepley + mesh - The DMPlex
2092eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20937cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20947cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
20957cd05799SMatthew G. Knepley 
20967cd05799SMatthew G. Knepley   Level: beginner
20977cd05799SMatthew G. Knepley 
20987cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20997cd05799SMatthew G. Knepley @*/
210077c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
210177c88f5bSMatthew G Knepley {
210277c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
210377c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
210477c88f5bSMatthew G Knepley   PetscInt       dof, off;
210577c88f5bSMatthew G Knepley   PetscErrorCode ierr;
210677c88f5bSMatthew G Knepley 
210777c88f5bSMatthew G Knepley   PetscFunctionBegin;
210877c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
210977c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
211077c88f5bSMatthew 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);
211177c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
211277c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
211377c88f5bSMatthew 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);
211477c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
211577c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
211677c88f5bSMatthew G Knepley }
211777c88f5bSMatthew G Knepley 
2118552f7358SJed Brown /*@
2119eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2120552f7358SJed Brown 
2121552f7358SJed Brown   Not collective
2122552f7358SJed Brown 
2123552f7358SJed Brown   Input Parameters:
2124552f7358SJed Brown + mesh - The DMPlex
2125eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2126552f7358SJed Brown 
2127552f7358SJed Brown   Output Parameter:
2128552f7358SJed Brown . size - The support size for point p
2129552f7358SJed Brown 
2130552f7358SJed Brown   Level: beginner
2131552f7358SJed Brown 
2132552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2133552f7358SJed Brown @*/
2134552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2135552f7358SJed Brown {
2136552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2137552f7358SJed Brown   PetscErrorCode ierr;
2138552f7358SJed Brown 
2139552f7358SJed Brown   PetscFunctionBegin;
2140552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2141552f7358SJed Brown   PetscValidPointer(size, 3);
2142552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2143552f7358SJed Brown   PetscFunctionReturn(0);
2144552f7358SJed Brown }
2145552f7358SJed Brown 
2146552f7358SJed Brown /*@
2147eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2148552f7358SJed Brown 
2149552f7358SJed Brown   Not collective
2150552f7358SJed Brown 
2151552f7358SJed Brown   Input Parameters:
2152552f7358SJed Brown + mesh - The DMPlex
2153eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2154552f7358SJed Brown - size - The support size for point p
2155552f7358SJed Brown 
2156552f7358SJed Brown   Output Parameter:
2157552f7358SJed Brown 
2158552f7358SJed Brown   Note:
2159552f7358SJed Brown   This should be called after DMPlexSetChart().
2160552f7358SJed Brown 
2161552f7358SJed Brown   Level: beginner
2162552f7358SJed Brown 
2163552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2164552f7358SJed Brown @*/
2165552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2166552f7358SJed Brown {
2167552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2168552f7358SJed Brown   PetscErrorCode ierr;
2169552f7358SJed Brown 
2170552f7358SJed Brown   PetscFunctionBegin;
2171552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2172552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
21730d644c17SKarl Rupp 
2174552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2175552f7358SJed Brown   PetscFunctionReturn(0);
2176552f7358SJed Brown }
2177552f7358SJed Brown 
2178552f7358SJed Brown /*@C
2179eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2180552f7358SJed Brown 
2181552f7358SJed Brown   Not collective
2182552f7358SJed Brown 
2183552f7358SJed Brown   Input Parameters:
2184552f7358SJed Brown + mesh - The DMPlex
2185eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2186552f7358SJed Brown 
2187552f7358SJed Brown   Output Parameter:
2188552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2189552f7358SJed Brown 
2190552f7358SJed Brown   Level: beginner
2191552f7358SJed Brown 
21923813dfbdSMatthew G Knepley   Fortran Notes:
21933813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21943813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21953b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2196922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
21973813dfbdSMatthew G Knepley 
2198552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2199552f7358SJed Brown @*/
2200552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2201552f7358SJed Brown {
2202552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2203552f7358SJed Brown   PetscInt       off;
2204552f7358SJed Brown   PetscErrorCode ierr;
2205552f7358SJed Brown 
2206552f7358SJed Brown   PetscFunctionBegin;
2207552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2208552f7358SJed Brown   PetscValidPointer(support, 3);
2209552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2210552f7358SJed Brown   *support = &mesh->supports[off];
2211552f7358SJed Brown   PetscFunctionReturn(0);
2212552f7358SJed Brown }
2213552f7358SJed Brown 
2214552f7358SJed Brown /*@
221592371b87SBarry 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
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()
222292371b87SBarry Smith - support - An array of points which are on the out-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 DMPlexSetSupportSize() and DMSetUp().
2228552f7358SJed Brown 
2229552f7358SJed Brown   Level: beginner
2230552f7358SJed Brown 
223192371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2232552f7358SJed Brown @*/
2233552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2234552f7358SJed Brown {
2235552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2236552f7358SJed Brown   PetscInt       pStart, pEnd;
2237552f7358SJed Brown   PetscInt       dof, off, c;
2238552f7358SJed Brown   PetscErrorCode ierr;
2239552f7358SJed Brown 
2240552f7358SJed Brown   PetscFunctionBegin;
2241552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2242552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2243552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2244552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2245552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
224682f516ccSBarry 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);
2247552f7358SJed Brown   for (c = 0; c < dof; ++c) {
224882f516ccSBarry 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);
2249552f7358SJed Brown     mesh->supports[off+c] = support[c];
2250552f7358SJed Brown   }
2251552f7358SJed Brown   PetscFunctionReturn(0);
2252552f7358SJed Brown }
2253552f7358SJed Brown 
22547cd05799SMatthew G. Knepley /*@
2255eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
22567cd05799SMatthew G. Knepley 
22577cd05799SMatthew G. Knepley   Not collective
22587cd05799SMatthew G. Knepley 
22597cd05799SMatthew G. Knepley   Input Parameters:
22607cd05799SMatthew G. Knepley + mesh - The DMPlex
2261eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22627cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
22637cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
22647cd05799SMatthew G. Knepley 
22657cd05799SMatthew G. Knepley   Level: beginner
22667cd05799SMatthew G. Knepley 
22677cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22687cd05799SMatthew G. Knepley @*/
2269552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2270552f7358SJed Brown {
2271552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2272552f7358SJed Brown   PetscInt       pStart, pEnd;
2273552f7358SJed Brown   PetscInt       dof, off;
2274552f7358SJed Brown   PetscErrorCode ierr;
2275552f7358SJed Brown 
2276552f7358SJed Brown   PetscFunctionBegin;
2277552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2278552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2279552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2280552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
228182f516ccSBarry 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);
228282f516ccSBarry 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);
228382f516ccSBarry 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);
2284552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2285552f7358SJed Brown   PetscFunctionReturn(0);
2286552f7358SJed Brown }
2287552f7358SJed Brown 
2288552f7358SJed Brown /*@C
2289eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2290552f7358SJed Brown 
2291552f7358SJed Brown   Not collective
2292552f7358SJed Brown 
2293552f7358SJed Brown   Input Parameters:
2294552f7358SJed Brown + mesh - The DMPlex
2295eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2296552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
22970298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2298552f7358SJed Brown 
2299552f7358SJed Brown   Output Parameters:
2300552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2301552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2302552f7358SJed Brown 
2303552f7358SJed Brown   Note:
23040298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2305552f7358SJed Brown 
23063813dfbdSMatthew G Knepley   Fortran Notes:
23073813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23083813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23093813dfbdSMatthew G Knepley 
23103813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23113813dfbdSMatthew G Knepley 
2312552f7358SJed Brown   Level: beginner
2313552f7358SJed Brown 
2314552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2315552f7358SJed Brown @*/
2316552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2317552f7358SJed Brown {
2318552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2319552f7358SJed Brown   PetscInt       *closure, *fifo;
23200298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2321552f7358SJed Brown   PetscInt        tmpSize, t;
2322552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2323552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2324552f7358SJed Brown   PetscErrorCode  ierr;
2325552f7358SJed Brown 
2326552f7358SJed Brown   PetscFunctionBegin;
2327552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2328552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2329552f7358SJed Brown   /* This is only 1-level */
2330552f7358SJed Brown   if (useCone) {
2331552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2332552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2333552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2334552f7358SJed Brown   } else {
2335552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2336552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2337552f7358SJed Brown   }
2338bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2339bfbcdd7aSMatthew G. Knepley     if (*points) {
2340bfbcdd7aSMatthew G. Knepley       closure = *points;
2341bfbcdd7aSMatthew G. Knepley     } else {
2342bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
234369291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2344bfbcdd7aSMatthew G. Knepley     }
2345bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2346bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2347bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2348bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2349bfbcdd7aSMatthew G. Knepley     }
2350bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2351bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2352bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2353bfbcdd7aSMatthew G. Knepley   }
235424c766afSToby Isaac   {
235524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
235624c766afSToby Isaac 
235724c766afSToby Isaac     c = mesh->maxConeSize;
235824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
235924c766afSToby Isaac     s = mesh->maxSupportSize;
236024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
236124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
236224c766afSToby Isaac   }
236369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2364bfbcdd7aSMatthew G. Knepley   if (*points) {
2365bfbcdd7aSMatthew G. Knepley     closure = *points;
2366bfbcdd7aSMatthew G. Knepley   } else {
236769291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2368bfbcdd7aSMatthew G. Knepley   }
2369bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2370552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2371552f7358SJed Brown     const PetscInt cp = tmp[t];
2372552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2373552f7358SJed Brown 
2374552f7358SJed Brown     closure[closureSize]   = cp;
2375552f7358SJed Brown     closure[closureSize+1] = co;
2376552f7358SJed Brown     fifo[fifoSize]         = cp;
2377552f7358SJed Brown     fifo[fifoSize+1]       = co;
2378552f7358SJed Brown   }
2379bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2380552f7358SJed Brown   while (fifoSize - fifoStart) {
2381552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2382552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2383552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2384552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2385552f7358SJed Brown 
2386552f7358SJed Brown     if (useCone) {
2387552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2388552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2389552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2390552f7358SJed Brown     } else {
2391552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2392552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
23930298fd71SBarry Smith       tmpO = NULL;
2394552f7358SJed Brown     }
2395552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2396552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2397552f7358SJed Brown       const PetscInt cp = tmp[i];
23982e1b13c2SMatthew 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. */
23992e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
24002e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
24012e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2402552f7358SJed Brown       PetscInt       c;
2403552f7358SJed Brown 
24042e1b13c2SMatthew G. Knepley       if (rev) {
24052e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
24062e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
24072e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
24082e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
24092e1b13c2SMatthew G. Knepley       }
2410552f7358SJed Brown       /* Check for duplicate */
2411552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2412552f7358SJed Brown         if (closure[c] == cp) break;
2413552f7358SJed Brown       }
2414552f7358SJed Brown       if (c == closureSize) {
2415552f7358SJed Brown         closure[closureSize]   = cp;
2416552f7358SJed Brown         closure[closureSize+1] = co;
2417552f7358SJed Brown         fifo[fifoSize]         = cp;
2418552f7358SJed Brown         fifo[fifoSize+1]       = co;
2419552f7358SJed Brown         closureSize           += 2;
2420552f7358SJed Brown         fifoSize              += 2;
2421552f7358SJed Brown       }
2422552f7358SJed Brown     }
2423552f7358SJed Brown     fifoStart += 2;
2424552f7358SJed Brown   }
2425552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2426552f7358SJed Brown   if (points)    *points    = closure;
242769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2428552f7358SJed Brown   PetscFunctionReturn(0);
2429552f7358SJed Brown }
2430552f7358SJed Brown 
24319bf0dad6SMatthew G. Knepley /*@C
2432eaf898f9SPatrick 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
24339bf0dad6SMatthew G. Knepley 
24349bf0dad6SMatthew G. Knepley   Not collective
24359bf0dad6SMatthew G. Knepley 
24369bf0dad6SMatthew G. Knepley   Input Parameters:
24379bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2438eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24399bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
24409bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24419bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
24429bf0dad6SMatthew G. Knepley 
24439bf0dad6SMatthew G. Knepley   Output Parameters:
24449bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
24459bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
24469bf0dad6SMatthew G. Knepley 
24479bf0dad6SMatthew G. Knepley   Note:
24489bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
24499bf0dad6SMatthew G. Knepley 
24509bf0dad6SMatthew G. Knepley   Fortran Notes:
24519bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24529bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
24539bf0dad6SMatthew G. Knepley 
24549bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24559bf0dad6SMatthew G. Knepley 
24569bf0dad6SMatthew G. Knepley   Level: beginner
24579bf0dad6SMatthew G. Knepley 
24589bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
24599bf0dad6SMatthew G. Knepley @*/
24609bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
24619bf0dad6SMatthew G. Knepley {
24629bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
24639bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
24649bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
24659bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
24669bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
24679bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
24689bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
24699bf0dad6SMatthew G. Knepley 
24709bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
24719bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24729bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24739bf0dad6SMatthew G. Knepley   /* This is only 1-level */
24749bf0dad6SMatthew G. Knepley   if (useCone) {
24759bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
24769bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
24779bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
24789bf0dad6SMatthew G. Knepley   } else {
24799bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
24809bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
24819bf0dad6SMatthew G. Knepley   }
24829bf0dad6SMatthew G. Knepley   if (depth == 1) {
24839bf0dad6SMatthew G. Knepley     if (*points) {
24849bf0dad6SMatthew G. Knepley       closure = *points;
24859bf0dad6SMatthew G. Knepley     } else {
24869bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
248769291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24889bf0dad6SMatthew G. Knepley     }
24899bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
24909bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
24919bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
24929bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
24939bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
24949bf0dad6SMatthew G. Knepley     }
24959bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
24969bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
24979bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
24989bf0dad6SMatthew G. Knepley   }
249924c766afSToby Isaac   {
250024c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
250124c766afSToby Isaac 
250224c766afSToby Isaac     c = mesh->maxConeSize;
250324c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
250424c766afSToby Isaac     s = mesh->maxSupportSize;
250524c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
250624c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
250724c766afSToby Isaac   }
250869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25099bf0dad6SMatthew G. Knepley   if (*points) {
25109bf0dad6SMatthew G. Knepley     closure = *points;
25119bf0dad6SMatthew G. Knepley   } else {
251269291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
25139bf0dad6SMatthew G. Knepley   }
25149bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
25159bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
25169bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
25179bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
25189bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
25199bf0dad6SMatthew G. Knepley 
25209bf0dad6SMatthew G. Knepley     if (ornt < 0) {
25219bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
25229bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
252386b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
25249bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25259bf0dad6SMatthew G. Knepley     }
25269bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
25279bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
25289bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
25299bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
25309bf0dad6SMatthew G. Knepley   }
25319bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
25329bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
25339bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
25349bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
25359bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
25369bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
25379bf0dad6SMatthew G. Knepley 
25389bf0dad6SMatthew G. Knepley     if (useCone) {
25399bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
25409bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
25419bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
25429bf0dad6SMatthew G. Knepley     } else {
25439bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
25449bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25459bf0dad6SMatthew G. Knepley       tmpO = NULL;
25469bf0dad6SMatthew G. Knepley     }
25479bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
25489bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
25499bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
25509bf0dad6SMatthew 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. */
25519bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25529bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25539bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
25549bf0dad6SMatthew G. Knepley       PetscInt       c;
25559bf0dad6SMatthew G. Knepley 
25569bf0dad6SMatthew G. Knepley       if (rev) {
25579bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
25589bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25599bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25609bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25619bf0dad6SMatthew G. Knepley       }
25629bf0dad6SMatthew G. Knepley       /* Check for duplicate */
25639bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
25649bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
25659bf0dad6SMatthew G. Knepley       }
25669bf0dad6SMatthew G. Knepley       if (c == closureSize) {
25679bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
25689bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
25699bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
25709bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
25719bf0dad6SMatthew G. Knepley         closureSize           += 2;
25729bf0dad6SMatthew G. Knepley         fifoSize              += 2;
25739bf0dad6SMatthew G. Knepley       }
25749bf0dad6SMatthew G. Knepley     }
25759bf0dad6SMatthew G. Knepley     fifoStart += 2;
25769bf0dad6SMatthew G. Knepley   }
25779bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
25789bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
257969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25809bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
25819bf0dad6SMatthew G. Knepley }
25829bf0dad6SMatthew G. Knepley 
2583552f7358SJed Brown /*@C
2584eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of 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
2592e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2593e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2594552f7358SJed Brown 
2595552f7358SJed Brown   Note:
25960298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2597552f7358SJed Brown 
25983813dfbdSMatthew G Knepley   Fortran Notes:
25993813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26003813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26013813dfbdSMatthew G Knepley 
26023813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26033813dfbdSMatthew G Knepley 
2604552f7358SJed Brown   Level: beginner
2605552f7358SJed Brown 
2606552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2607552f7358SJed Brown @*/
2608552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2609552f7358SJed Brown {
2610552f7358SJed Brown   PetscErrorCode ierr;
2611552f7358SJed Brown 
2612552f7358SJed Brown   PetscFunctionBegin;
2613552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2614e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2615e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
261669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
26174ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2618552f7358SJed Brown   PetscFunctionReturn(0);
2619552f7358SJed Brown }
2620552f7358SJed Brown 
2621552f7358SJed Brown /*@
2622eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2623552f7358SJed Brown 
2624552f7358SJed Brown   Not collective
2625552f7358SJed Brown 
2626552f7358SJed Brown   Input Parameter:
2627552f7358SJed Brown . mesh - The DMPlex
2628552f7358SJed Brown 
2629552f7358SJed Brown   Output Parameters:
2630552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2631552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2632552f7358SJed Brown 
2633552f7358SJed Brown   Level: beginner
2634552f7358SJed Brown 
2635552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2636552f7358SJed Brown @*/
2637552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2638552f7358SJed Brown {
2639552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2640552f7358SJed Brown 
2641552f7358SJed Brown   PetscFunctionBegin;
2642552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2643552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2644552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2645552f7358SJed Brown   PetscFunctionReturn(0);
2646552f7358SJed Brown }
2647552f7358SJed Brown 
2648552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2649552f7358SJed Brown {
2650552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2651552f7358SJed Brown   PetscInt       size;
2652552f7358SJed Brown   PetscErrorCode ierr;
2653552f7358SJed Brown 
2654552f7358SJed Brown   PetscFunctionBegin;
2655552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2656552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2657552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
26581795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
26591795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2660552f7358SJed Brown   if (mesh->maxSupportSize) {
2661552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2662552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
26631795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2664552f7358SJed Brown   }
2665552f7358SJed Brown   PetscFunctionReturn(0);
2666552f7358SJed Brown }
2667552f7358SJed Brown 
2668276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2669552f7358SJed Brown {
2670552f7358SJed Brown   PetscErrorCode ierr;
2671552f7358SJed Brown 
2672552f7358SJed Brown   PetscFunctionBegin;
26734d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2674792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2675c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2676736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2677f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2678f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2679f94b4a02SBlaise Bourdin 
26803dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
26813dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2682c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
2683f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2684f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2685f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2686f94b4a02SBlaise Bourdin 
2687f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2688c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2689f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2690f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2691f94b4a02SBlaise Bourdin   }
2692552f7358SJed Brown   PetscFunctionReturn(0);
2693552f7358SJed Brown }
2694552f7358SJed Brown 
26952adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
26962adcc780SMatthew G. Knepley {
26972adcc780SMatthew G. Knepley   PetscErrorCode ierr;
26983dcd263cSBlaise Bourdin   PetscInt       i = 0;
26992adcc780SMatthew G. Knepley 
27002adcc780SMatthew G. Knepley   PetscFunctionBegin;
2701435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2702792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2703c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
27043dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
27053dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
27063dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
27073dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
27083dcd263cSBlaise Bourdin 
27093dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
27103dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2711c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
271292fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
27133dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
27143dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
27153dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
27163dcd263cSBlaise Bourdin 
27173dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2718c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
27193dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
27203dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
27213dcd263cSBlaise Bourdin       break;
27223dcd263cSBlaise Bourdin     }
27233dcd263cSBlaise Bourdin   }
27242adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
27252adcc780SMatthew G. Knepley }
27262adcc780SMatthew G. Knepley 
2727552f7358SJed Brown /*@
2728eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2729552f7358SJed Brown 
2730552f7358SJed Brown   Not collective
2731552f7358SJed Brown 
2732552f7358SJed Brown   Input Parameter:
2733552f7358SJed Brown . mesh - The DMPlex
2734552f7358SJed Brown 
2735552f7358SJed Brown   Output Parameter:
2736552f7358SJed Brown 
2737552f7358SJed Brown   Note:
2738552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2739552f7358SJed Brown 
2740552f7358SJed Brown   Level: beginner
2741552f7358SJed Brown 
2742552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2743552f7358SJed Brown @*/
2744552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2745552f7358SJed Brown {
2746552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2747552f7358SJed Brown   PetscInt      *offsets;
2748552f7358SJed Brown   PetscInt       supportSize;
2749552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2750552f7358SJed Brown   PetscErrorCode ierr;
2751552f7358SJed Brown 
2752552f7358SJed Brown   PetscFunctionBegin;
2753552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
275482f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
275530b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2756552f7358SJed Brown   /* Calculate support sizes */
2757552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2758552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2759552f7358SJed Brown     PetscInt dof, off, c;
2760552f7358SJed Brown 
2761552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2762552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2763552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2764552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2765552f7358SJed Brown     }
2766552f7358SJed Brown   }
2767552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2768552f7358SJed Brown     PetscInt dof;
2769552f7358SJed Brown 
2770552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
27710d644c17SKarl Rupp 
2772552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2773552f7358SJed Brown   }
2774552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2775552f7358SJed Brown   /* Calculate supports */
2776552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
27771795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
27781795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2779552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2780552f7358SJed Brown     PetscInt dof, off, c;
2781552f7358SJed Brown 
2782552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2783552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2784552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2785552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2786552f7358SJed Brown       PetscInt       offS;
2787552f7358SJed Brown 
2788552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
27890d644c17SKarl Rupp 
2790552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2791552f7358SJed Brown       ++offsets[q];
2792552f7358SJed Brown     }
2793552f7358SJed Brown   }
2794552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
279530b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2796552f7358SJed Brown   PetscFunctionReturn(0);
2797552f7358SJed Brown }
2798552f7358SJed Brown 
2799277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2800277ea44aSLisandro Dalcin {
2801277ea44aSLisandro Dalcin   IS             stratumIS;
2802277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2803277ea44aSLisandro Dalcin 
2804277ea44aSLisandro Dalcin   PetscFunctionBegin;
2805277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
2806277ea44aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
2807277ea44aSLisandro Dalcin   {
2808277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2809277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2810277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2811277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2812277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2813277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2814277ea44aSLisandro Dalcin     }
2815277ea44aSLisandro 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);
2816277ea44aSLisandro Dalcin   }
2817277ea44aSLisandro Dalcin #endif
2818277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2819277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2820277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2821277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2822277ea44aSLisandro Dalcin }
2823277ea44aSLisandro Dalcin 
28247d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
28257d5acc75SStefano Zampini 
2826552f7358SJed Brown /*@
2827a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
28286dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2829552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2830552f7358SJed Brown   the DAG.
2831552f7358SJed Brown 
2832bf4602e4SToby Isaac   Collective on dm
2833552f7358SJed Brown 
2834552f7358SJed Brown   Input Parameter:
2835552f7358SJed Brown . mesh - The DMPlex
2836552f7358SJed Brown 
2837552f7358SJed Brown   Output Parameter:
2838552f7358SJed Brown 
2839552f7358SJed Brown   Notes:
2840b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
2841b1bb481bSMatthew 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
2842b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2843c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2844150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2845552f7358SJed Brown 
2846b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
2847b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
2848b1bb481bSMatthew 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
2849b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
2850b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
2851b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
2852b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
2853b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
2854b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
2855b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
2856b1bb481bSMatthew Knepley 
2857150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2858552f7358SJed Brown 
2859552f7358SJed Brown   Level: beginner
2860552f7358SJed Brown 
2861ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
2862552f7358SJed Brown @*/
2863552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2864552f7358SJed Brown {
2865df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2866aa50250dSMatthew G. Knepley   DMLabel        label;
2867552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2868552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
28697d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2870552f7358SJed Brown   PetscErrorCode ierr;
2871552f7358SJed Brown 
2872552f7358SJed Brown   PetscFunctionBegin;
2873552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2874552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2875277ea44aSLisandro Dalcin 
2876277ea44aSLisandro Dalcin   /* Create depth label */
2877aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2878c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2879aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2880277ea44aSLisandro Dalcin 
2881277ea44aSLisandro Dalcin   {
2882552f7358SJed Brown     /* Initialize roots and count leaves */
2883277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2884277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2885552f7358SJed Brown     PetscInt coneSize, supportSize;
2886552f7358SJed Brown 
2887277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2888552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2889552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2890552f7358SJed Brown       if (!coneSize && supportSize) {
2891277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2892277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2893552f7358SJed Brown         ++numRoots;
2894552f7358SJed Brown       } else if (!supportSize && coneSize) {
2895552f7358SJed Brown         ++numLeaves;
2896552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2897552f7358SJed Brown         /* Isolated points */
2898277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2899277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2900552f7358SJed Brown       }
2901552f7358SJed Brown     }
2902277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2903277ea44aSLisandro Dalcin   }
2904277ea44aSLisandro Dalcin 
2905552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2906277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2907277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2908552f7358SJed Brown     PetscInt coneSize, supportSize;
2909552f7358SJed Brown 
2910277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2911552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2912552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2913552f7358SJed Brown       if (!supportSize && coneSize) {
2914277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2915277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2916552f7358SJed Brown       }
2917552f7358SJed Brown     }
2918277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2919552f7358SJed Brown   } else {
2920277ea44aSLisandro Dalcin     PetscInt level = 0;
2921277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2922552f7358SJed Brown 
2923277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2924277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2925277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2926277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
292774ef644bSMatthew G. Knepley 
2928277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
292974ef644bSMatthew G. Knepley         const PetscInt *support;
293074ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
293174ef644bSMatthew G. Knepley 
2932277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2933277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
293474ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2935277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2936277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2937552f7358SJed Brown         }
2938552f7358SJed Brown       }
2939277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2940277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2941277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
294274ef644bSMatthew G. Knepley     }
294374ef644bSMatthew G. Knepley   }
2944bf4602e4SToby Isaac   { /* just in case there is an empty process */
2945bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2946bf4602e4SToby Isaac 
2947bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
29486d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2949bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2950367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2951bf4602e4SToby Isaac     }
2952bf4602e4SToby Isaac   }
2953d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
29547d5acc75SStefano Zampini 
29557d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
29567d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
29577d5acc75SStefano Zampini     PetscInt dim;
2958277ea44aSLisandro Dalcin     DMLabel  dimLabel;
29597d5acc75SStefano Zampini 
29607d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
29617d5acc75SStefano Zampini     ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
29627d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
29637d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
29647d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
29657d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
29667d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
29677d5acc75SStefano Zampini   }
2968552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2969552f7358SJed Brown   PetscFunctionReturn(0);
2970552f7358SJed Brown }
2971552f7358SJed Brown 
2972ba2698f1SMatthew G. Knepley /*@
2973ba2698f1SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
2974ba2698f1SMatthew G. Knepley 
2975ba2698f1SMatthew G. Knepley   Collective on dm
2976ba2698f1SMatthew G. Knepley 
2977ba2698f1SMatthew G. Knepley   Input Parameter:
2978ba2698f1SMatthew G. Knepley . mesh - The DMPlex
2979ba2698f1SMatthew G. Knepley 
2980ba2698f1SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
2981ba2698f1SMatthew G. Knepley 
2982ba2698f1SMatthew G. Knepley   Level: beginner
2983ba2698f1SMatthew G. Knepley 
2984ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify()
2985ba2698f1SMatthew G. Knepley @*/
2986ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
2987ba2698f1SMatthew G. Knepley {
2988ba2698f1SMatthew G. Knepley   DM_Plex       *mesh;
2989ba2698f1SMatthew G. Knepley   DMLabel        label;
2990ba2698f1SMatthew G. Knepley   PetscInt       dim, depth, gcStart, gcEnd, pStart, pEnd, p;
2991ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
2992ba2698f1SMatthew G. Knepley 
2993ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
2994ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2995ba2698f1SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
2996ba2698f1SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
2997ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
2998ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2999ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3000ba2698f1SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3001ba2698f1SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
3002ba2698f1SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3003ba2698f1SMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3004ba2698f1SMatthew G. Knepley     PetscInt       pdepth, pheight, coneSize;
3005ba2698f1SMatthew G. Knepley 
3006ba2698f1SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3007ba2698f1SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3008ba2698f1SMatthew G. Knepley     pheight = depth - pdepth;
3009ba2698f1SMatthew G. Knepley     if (depth <= 1) {
3010ba2698f1SMatthew G. Knepley       switch (pdepth) {
3011ba2698f1SMatthew G. Knepley         case 0: ct = DM_POLYTOPE_POINT;break;
3012ba2698f1SMatthew G. Knepley         case 1:
3013ba2698f1SMatthew G. Knepley           switch (coneSize) {
3014ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3015ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3016ba2698f1SMatthew G. Knepley             case 4:
3017ba2698f1SMatthew G. Knepley             switch (dim) {
3018ba2698f1SMatthew G. Knepley               case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3019ba2698f1SMatthew G. Knepley               case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3020ba2698f1SMatthew G. Knepley               default: break;
3021ba2698f1SMatthew G. Knepley             }
3022ba2698f1SMatthew G. Knepley             break;
3023ba2698f1SMatthew G. Knepley           case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3024ba2698f1SMatthew G. Knepley           case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3025ba2698f1SMatthew G. Knepley           default: break;
3026ba2698f1SMatthew G. Knepley         }
3027ba2698f1SMatthew G. Knepley       }
3028ba2698f1SMatthew G. Knepley     } else {
3029ba2698f1SMatthew G. Knepley       if (pdepth == 0) {
3030ba2698f1SMatthew G. Knepley         ct = DM_POLYTOPE_POINT;
3031ba2698f1SMatthew G. Knepley       } else if (pheight == 0) {
3032ba2698f1SMatthew G. Knepley         if ((p >= gcStart) && (p < gcEnd)) {
3033ba2698f1SMatthew G. Knepley           if (coneSize == 1) ct = DM_POLYTOPE_FV_GHOST;
3034ba2698f1SMatthew G. Knepley           else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %D should have a cone size of 1, not %D", p, coneSize);
3035ba2698f1SMatthew G. Knepley         } else {
3036ba2698f1SMatthew G. Knepley           switch (dim) {
3037ba2698f1SMatthew G. Knepley             case 1:
3038ba2698f1SMatthew G. Knepley               switch (coneSize) {
3039ba2698f1SMatthew G. Knepley                 case 2: ct = DM_POLYTOPE_SEGMENT;break;
3040ba2698f1SMatthew G. Knepley                 default: break;
3041ba2698f1SMatthew G. Knepley               }
3042ba2698f1SMatthew G. Knepley               break;
3043ba2698f1SMatthew G. Knepley             case 2:
3044ba2698f1SMatthew G. Knepley               switch (coneSize) {
3045ba2698f1SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3046ba2698f1SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3047ba2698f1SMatthew G. Knepley                 default: break;
3048ba2698f1SMatthew G. Knepley               }
3049ba2698f1SMatthew G. Knepley               break;
3050ba2698f1SMatthew G. Knepley             case 3:
3051ba2698f1SMatthew G. Knepley               switch (coneSize) {
3052ba2698f1SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3053ba2698f1SMatthew G. Knepley                 case 5: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3054ba2698f1SMatthew G. Knepley                 case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3055ba2698f1SMatthew G. Knepley                 default: break;
3056ba2698f1SMatthew G. Knepley               }
3057ba2698f1SMatthew G. Knepley               break;
3058ba2698f1SMatthew G. Knepley             default: break;
3059ba2698f1SMatthew G. Knepley           }
3060ba2698f1SMatthew G. Knepley         }
3061ba2698f1SMatthew G. Knepley       } else if (pheight > 0) {
3062ba2698f1SMatthew G. Knepley         switch (coneSize) {
3063ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3064ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3065ba2698f1SMatthew G. Knepley           case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3066ba2698f1SMatthew G. Knepley           default: break;
3067ba2698f1SMatthew G. Knepley         }
3068ba2698f1SMatthew G. Knepley       }
3069ba2698f1SMatthew G. Knepley     }
3070ba2698f1SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3071ba2698f1SMatthew G. Knepley     ierr = DMLabelSetValue(label, p, ct);CHKERRQ(ierr);
3072ba2698f1SMatthew G. Knepley   }
3073ba2698f1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->celltypeState);CHKERRQ(ierr);
3074ba2698f1SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) label, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3075ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3076ba2698f1SMatthew G. Knepley }
3077ba2698f1SMatthew G. Knepley 
3078552f7358SJed Brown /*@C
3079552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3080552f7358SJed Brown 
3081552f7358SJed Brown   Not Collective
3082552f7358SJed Brown 
3083552f7358SJed Brown   Input Parameters:
3084552f7358SJed Brown + dm - The DMPlex object
3085552f7358SJed Brown . numPoints - The number of input points for the join
3086552f7358SJed Brown - points - The input points
3087552f7358SJed Brown 
3088552f7358SJed Brown   Output Parameters:
3089552f7358SJed Brown + numCoveredPoints - The number of points in the join
3090552f7358SJed Brown - coveredPoints - The points in the join
3091552f7358SJed Brown 
3092552f7358SJed Brown   Level: intermediate
3093552f7358SJed Brown 
3094552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3095552f7358SJed Brown 
30963813dfbdSMatthew G Knepley   Fortran Notes:
30973813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30983813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30993813dfbdSMatthew G Knepley 
31003813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31013813dfbdSMatthew G Knepley 
3102552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3103552f7358SJed Brown @*/
3104552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3105552f7358SJed Brown {
3106552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3107552f7358SJed Brown   PetscInt      *join[2];
3108552f7358SJed Brown   PetscInt       joinSize, i = 0;
3109552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3110552f7358SJed Brown   PetscErrorCode ierr;
3111552f7358SJed Brown 
3112552f7358SJed Brown   PetscFunctionBegin;
3113552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
311448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
311548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
311648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
311769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
311869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3119552f7358SJed Brown   /* Copy in support of first point */
3120552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3121552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3122552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3123552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3124552f7358SJed Brown   }
3125552f7358SJed Brown   /* Check each successive support */
3126552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3127552f7358SJed Brown     PetscInt newJoinSize = 0;
3128552f7358SJed Brown 
3129552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3130552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3131552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3132552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3133552f7358SJed Brown 
3134552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3135552f7358SJed Brown         if (point == join[i][m]) {
3136552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3137552f7358SJed Brown           break;
3138552f7358SJed Brown         }
3139552f7358SJed Brown       }
3140552f7358SJed Brown     }
3141552f7358SJed Brown     joinSize = newJoinSize;
3142552f7358SJed Brown     i        = 1-i;
3143552f7358SJed Brown   }
3144552f7358SJed Brown   *numCoveredPoints = joinSize;
3145552f7358SJed Brown   *coveredPoints    = join[i];
314669291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3147552f7358SJed Brown   PetscFunctionReturn(0);
3148552f7358SJed Brown }
3149552f7358SJed Brown 
3150552f7358SJed Brown /*@C
3151552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3152552f7358SJed Brown 
3153552f7358SJed Brown   Not Collective
3154552f7358SJed Brown 
3155552f7358SJed Brown   Input Parameters:
3156552f7358SJed Brown + dm - The DMPlex object
3157552f7358SJed Brown . numPoints - The number of input points for the join
3158552f7358SJed Brown - points - The input points
3159552f7358SJed Brown 
3160552f7358SJed Brown   Output Parameters:
3161552f7358SJed Brown + numCoveredPoints - The number of points in the join
3162552f7358SJed Brown - coveredPoints - The points in the join
3163552f7358SJed Brown 
31643813dfbdSMatthew G Knepley   Fortran Notes:
31653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31673813dfbdSMatthew G Knepley 
31683813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31693813dfbdSMatthew G Knepley 
3170552f7358SJed Brown   Level: intermediate
3171552f7358SJed Brown 
3172552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3173552f7358SJed Brown @*/
3174552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3175552f7358SJed Brown {
3176552f7358SJed Brown   PetscErrorCode ierr;
3177552f7358SJed Brown 
3178552f7358SJed Brown   PetscFunctionBegin;
3179552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3180d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3181d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3182d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
318369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3184d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3185552f7358SJed Brown   PetscFunctionReturn(0);
3186552f7358SJed Brown }
3187552f7358SJed Brown 
3188552f7358SJed Brown /*@C
3189552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3190552f7358SJed Brown 
3191552f7358SJed Brown   Not Collective
3192552f7358SJed Brown 
3193552f7358SJed Brown   Input Parameters:
3194552f7358SJed Brown + dm - The DMPlex object
3195552f7358SJed Brown . numPoints - The number of input points for the join
3196552f7358SJed Brown - points - The input points
3197552f7358SJed Brown 
3198552f7358SJed Brown   Output Parameters:
3199552f7358SJed Brown + numCoveredPoints - The number of points in the join
3200552f7358SJed Brown - coveredPoints - The points in the join
3201552f7358SJed Brown 
32023813dfbdSMatthew G Knepley   Fortran Notes:
32033813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32043813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32053813dfbdSMatthew G Knepley 
32063813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32073813dfbdSMatthew G Knepley 
3208552f7358SJed Brown   Level: intermediate
3209552f7358SJed Brown 
3210552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3211552f7358SJed Brown @*/
3212552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3213552f7358SJed Brown {
3214552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3215552f7358SJed Brown   PetscInt      *offsets, **closures;
3216552f7358SJed Brown   PetscInt      *join[2];
3217552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
321824c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3219552f7358SJed Brown   PetscErrorCode ierr;
3220552f7358SJed Brown 
3221552f7358SJed Brown   PetscFunctionBegin;
3222552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
322348bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
322448bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
322548bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3226552f7358SJed Brown 
3227552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
32281795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
322969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
323024c766afSToby Isaac   ms      = mesh->maxSupportSize;
323124c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
323269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
323369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3234552f7358SJed Brown 
3235552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3236552f7358SJed Brown     PetscInt closureSize;
3237552f7358SJed Brown 
3238552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
32390d644c17SKarl Rupp 
3240552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3241552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3242552f7358SJed Brown       PetscInt pStart, pEnd, i;
3243552f7358SJed Brown 
3244552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3245552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3246552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3247552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3248552f7358SJed Brown           break;
3249552f7358SJed Brown         }
3250552f7358SJed Brown       }
3251552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3252552f7358SJed Brown     }
325382f516ccSBarry 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);
3254552f7358SJed Brown   }
3255552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3256552f7358SJed Brown     PetscInt dof;
3257552f7358SJed Brown 
3258552f7358SJed Brown     /* Copy in support of first point */
3259552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3260552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3261552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3262552f7358SJed Brown     }
3263552f7358SJed Brown     /* Check each successive cone */
3264552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3265552f7358SJed Brown       PetscInt newJoinSize = 0;
3266552f7358SJed Brown 
3267552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3268552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3269552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3270552f7358SJed Brown 
3271552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3272552f7358SJed Brown           if (point == join[i][m]) {
3273552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3274552f7358SJed Brown             break;
3275552f7358SJed Brown           }
3276552f7358SJed Brown         }
3277552f7358SJed Brown       }
3278552f7358SJed Brown       joinSize = newJoinSize;
3279552f7358SJed Brown       i        = 1-i;
3280552f7358SJed Brown     }
3281552f7358SJed Brown     if (joinSize) break;
3282552f7358SJed Brown   }
3283552f7358SJed Brown   *numCoveredPoints = joinSize;
3284552f7358SJed Brown   *coveredPoints    = join[i];
3285552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
32860298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3287552f7358SJed Brown   }
3288552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
328969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
329069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3291552f7358SJed Brown   PetscFunctionReturn(0);
3292552f7358SJed Brown }
3293552f7358SJed Brown 
3294552f7358SJed Brown /*@C
3295552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3296552f7358SJed Brown 
3297552f7358SJed Brown   Not Collective
3298552f7358SJed Brown 
3299552f7358SJed Brown   Input Parameters:
3300552f7358SJed Brown + dm - The DMPlex object
3301552f7358SJed Brown . numPoints - The number of input points for the meet
3302552f7358SJed Brown - points - The input points
3303552f7358SJed Brown 
3304552f7358SJed Brown   Output Parameters:
3305552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3306552f7358SJed Brown - coveredPoints - The points in the meet
3307552f7358SJed Brown 
3308552f7358SJed Brown   Level: intermediate
3309552f7358SJed Brown 
3310552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3311552f7358SJed Brown 
33123813dfbdSMatthew G Knepley   Fortran Notes:
33133813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33143813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33153813dfbdSMatthew G Knepley 
33163813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33173813dfbdSMatthew G Knepley 
3318552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3319552f7358SJed Brown @*/
3320552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3321552f7358SJed Brown {
3322552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3323552f7358SJed Brown   PetscInt      *meet[2];
3324552f7358SJed Brown   PetscInt       meetSize, i = 0;
3325552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3326552f7358SJed Brown   PetscErrorCode ierr;
3327552f7358SJed Brown 
3328552f7358SJed Brown   PetscFunctionBegin;
3329552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3330552f7358SJed Brown   PetscValidPointer(points, 2);
3331552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3332552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
333369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
333469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3335552f7358SJed Brown   /* Copy in cone of first point */
3336552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3337552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3338552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3339552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3340552f7358SJed Brown   }
3341552f7358SJed Brown   /* Check each successive cone */
3342552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3343552f7358SJed Brown     PetscInt newMeetSize = 0;
3344552f7358SJed Brown 
3345552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3346552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3347552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3348552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3349552f7358SJed Brown 
3350552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3351552f7358SJed Brown         if (point == meet[i][m]) {
3352552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3353552f7358SJed Brown           break;
3354552f7358SJed Brown         }
3355552f7358SJed Brown       }
3356552f7358SJed Brown     }
3357552f7358SJed Brown     meetSize = newMeetSize;
3358552f7358SJed Brown     i        = 1-i;
3359552f7358SJed Brown   }
3360552f7358SJed Brown   *numCoveringPoints = meetSize;
3361552f7358SJed Brown   *coveringPoints    = meet[i];
336269291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3363552f7358SJed Brown   PetscFunctionReturn(0);
3364552f7358SJed Brown }
3365552f7358SJed Brown 
3366552f7358SJed Brown /*@C
3367552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3368552f7358SJed Brown 
3369552f7358SJed Brown   Not Collective
3370552f7358SJed Brown 
3371552f7358SJed Brown   Input Parameters:
3372552f7358SJed Brown + dm - The DMPlex object
3373552f7358SJed Brown . numPoints - The number of input points for the meet
3374552f7358SJed Brown - points - The input points
3375552f7358SJed Brown 
3376552f7358SJed Brown   Output Parameters:
3377552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3378552f7358SJed Brown - coveredPoints - The points in the meet
3379552f7358SJed Brown 
3380552f7358SJed Brown   Level: intermediate
3381552f7358SJed Brown 
33823813dfbdSMatthew G Knepley   Fortran Notes:
33833813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33843813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33853813dfbdSMatthew G Knepley 
33863813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33873813dfbdSMatthew G Knepley 
3388552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3389552f7358SJed Brown @*/
3390552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3391552f7358SJed Brown {
3392552f7358SJed Brown   PetscErrorCode ierr;
3393552f7358SJed Brown 
3394552f7358SJed Brown   PetscFunctionBegin;
3395552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3396d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3397d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3398d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
339969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3400d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3401552f7358SJed Brown   PetscFunctionReturn(0);
3402552f7358SJed Brown }
3403552f7358SJed Brown 
3404552f7358SJed Brown /*@C
3405552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3406552f7358SJed Brown 
3407552f7358SJed Brown   Not Collective
3408552f7358SJed Brown 
3409552f7358SJed Brown   Input Parameters:
3410552f7358SJed Brown + dm - The DMPlex object
3411552f7358SJed Brown . numPoints - The number of input points for the meet
3412552f7358SJed Brown - points - The input points
3413552f7358SJed Brown 
3414552f7358SJed Brown   Output Parameters:
3415552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3416552f7358SJed Brown - coveredPoints - The points in the meet
3417552f7358SJed Brown 
3418552f7358SJed Brown   Level: intermediate
3419552f7358SJed Brown 
34203813dfbdSMatthew G Knepley   Fortran Notes:
34213813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
34223813dfbdSMatthew G Knepley   include petsc.h90 in your code.
34233813dfbdSMatthew G Knepley 
34243813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
34253813dfbdSMatthew G Knepley 
3426552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3427552f7358SJed Brown @*/
3428552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3429552f7358SJed Brown {
3430552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3431552f7358SJed Brown   PetscInt      *offsets, **closures;
3432552f7358SJed Brown   PetscInt      *meet[2];
3433552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
343424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3435552f7358SJed Brown   PetscErrorCode ierr;
3436552f7358SJed Brown 
3437552f7358SJed Brown   PetscFunctionBegin;
3438552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3439552f7358SJed Brown   PetscValidPointer(points, 2);
3440552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3441552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3442552f7358SJed Brown 
3443552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3444785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
344569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
344624c766afSToby Isaac   mc      = mesh->maxConeSize;
344724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
344869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
344969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3450552f7358SJed Brown 
3451552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3452552f7358SJed Brown     PetscInt closureSize;
3453552f7358SJed Brown 
3454552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
34550d644c17SKarl Rupp 
3456552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3457552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3458552f7358SJed Brown       PetscInt pStart, pEnd, i;
3459552f7358SJed Brown 
3460552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3461552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3462552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3463552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3464552f7358SJed Brown           break;
3465552f7358SJed Brown         }
3466552f7358SJed Brown       }
3467552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3468552f7358SJed Brown     }
346982f516ccSBarry 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);
3470552f7358SJed Brown   }
3471552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3472552f7358SJed Brown     PetscInt dof;
3473552f7358SJed Brown 
3474552f7358SJed Brown     /* Copy in cone of first point */
3475552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3476552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3477552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3478552f7358SJed Brown     }
3479552f7358SJed Brown     /* Check each successive cone */
3480552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3481552f7358SJed Brown       PetscInt newMeetSize = 0;
3482552f7358SJed Brown 
3483552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3484552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3485552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3486552f7358SJed Brown 
3487552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3488552f7358SJed Brown           if (point == meet[i][m]) {
3489552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3490552f7358SJed Brown             break;
3491552f7358SJed Brown           }
3492552f7358SJed Brown         }
3493552f7358SJed Brown       }
3494552f7358SJed Brown       meetSize = newMeetSize;
3495552f7358SJed Brown       i        = 1-i;
3496552f7358SJed Brown     }
3497552f7358SJed Brown     if (meetSize) break;
3498552f7358SJed Brown   }
3499552f7358SJed Brown   *numCoveredPoints = meetSize;
3500552f7358SJed Brown   *coveredPoints    = meet[i];
3501552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
35020298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3503552f7358SJed Brown   }
3504552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
350569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
350669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3507552f7358SJed Brown   PetscFunctionReturn(0);
3508552f7358SJed Brown }
3509552f7358SJed Brown 
35104e3744c5SMatthew G. Knepley /*@C
35114e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
35124e3744c5SMatthew G. Knepley 
35134e3744c5SMatthew G. Knepley   Not Collective
35144e3744c5SMatthew G. Knepley 
35154e3744c5SMatthew G. Knepley   Input Parameters:
35164e3744c5SMatthew G. Knepley + dmA - A DMPlex object
35174e3744c5SMatthew G. Knepley - dmB - A DMPlex object
35184e3744c5SMatthew G. Knepley 
35194e3744c5SMatthew G. Knepley   Output Parameters:
35204e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
35214e3744c5SMatthew G. Knepley 
35224e3744c5SMatthew G. Knepley   Level: intermediate
35234e3744c5SMatthew G. Knepley 
35244e3744c5SMatthew G. Knepley   Notes:
35254e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
35264e3744c5SMatthew G. Knepley 
35274e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
35284e3744c5SMatthew G. Knepley @*/
35294e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
35304e3744c5SMatthew G. Knepley {
35314e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
35324e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
35334e3744c5SMatthew G. Knepley 
35344e3744c5SMatthew G. Knepley   PetscFunctionBegin;
35354e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
35364e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
35374e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
35384e3744c5SMatthew G. Knepley 
35394e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
35404e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
35414e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
35424e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
35434e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
35444e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
35454e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
35464e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35474e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
35484e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
35494e3744c5SMatthew G. Knepley 
35504e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
35514e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
35524e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
35534e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
35544e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
35554e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
35564e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
35574e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
35584e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
35594e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
35604e3744c5SMatthew G. Knepley     }
35614e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
35624e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
35634e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
35644e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
35654e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
35664e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
35674e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
35684e3744c5SMatthew G. Knepley     }
35694e3744c5SMatthew G. Knepley   }
35704e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
35714e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
35724e3744c5SMatthew G. Knepley }
35734e3744c5SMatthew G. Knepley 
35747cd05799SMatthew G. Knepley /*@C
35757cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
35767cd05799SMatthew G. Knepley 
35777cd05799SMatthew G. Knepley   Not Collective
35787cd05799SMatthew G. Knepley 
35797cd05799SMatthew G. Knepley   Input Parameters:
35807cd05799SMatthew G. Knepley + dm         - The DMPlex
35817cd05799SMatthew G. Knepley . cellDim    - The cell dimension
35827cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
35837cd05799SMatthew G. Knepley 
35847cd05799SMatthew G. Knepley   Output Parameters:
35857cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
35867cd05799SMatthew G. Knepley 
35877cd05799SMatthew G. Knepley   Level: developer
35887cd05799SMatthew G. Knepley 
35897cd05799SMatthew G. Knepley   Notes:
35907cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
35917cd05799SMatthew G. Knepley 
35927cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
35937cd05799SMatthew G. Knepley @*/
359418ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3595a6dfd86eSKarl Rupp {
359682f516ccSBarry Smith   MPI_Comm       comm;
3597552f7358SJed Brown   PetscErrorCode ierr;
3598552f7358SJed Brown 
3599552f7358SJed Brown   PetscFunctionBegin;
360082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3601552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3602552f7358SJed Brown   switch (cellDim) {
3603552f7358SJed Brown   case 0:
3604552f7358SJed Brown     *numFaceVertices = 0;
3605552f7358SJed Brown     break;
3606552f7358SJed Brown   case 1:
3607552f7358SJed Brown     *numFaceVertices = 1;
3608552f7358SJed Brown     break;
3609552f7358SJed Brown   case 2:
3610552f7358SJed Brown     switch (numCorners) {
361119436ca2SJed Brown     case 3: /* triangle */
361219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3613552f7358SJed Brown       break;
361419436ca2SJed Brown     case 4: /* quadrilateral */
361519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3616552f7358SJed Brown       break;
361719436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
361819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3619552f7358SJed Brown       break;
362019436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
362119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3622552f7358SJed Brown       break;
3623552f7358SJed Brown     default:
3624f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3625552f7358SJed Brown     }
3626552f7358SJed Brown     break;
3627552f7358SJed Brown   case 3:
3628552f7358SJed Brown     switch (numCorners) {
362919436ca2SJed Brown     case 4: /* tetradehdron */
363019436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3631552f7358SJed Brown       break;
363219436ca2SJed Brown     case 6: /* tet cohesive cells */
363319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3634552f7358SJed Brown       break;
363519436ca2SJed Brown     case 8: /* hexahedron */
363619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3637552f7358SJed Brown       break;
363819436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
363919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3640552f7358SJed Brown       break;
364119436ca2SJed Brown     case 10: /* quadratic tetrahedron */
364219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3643552f7358SJed Brown       break;
364419436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
364519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3646552f7358SJed Brown       break;
364719436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
364819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3649552f7358SJed Brown       break;
365019436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
365119436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3652552f7358SJed Brown       break;
3653552f7358SJed Brown     default:
3654f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3655552f7358SJed Brown     }
3656552f7358SJed Brown     break;
3657552f7358SJed Brown   default:
3658f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3659552f7358SJed Brown   }
3660552f7358SJed Brown   PetscFunctionReturn(0);
3661552f7358SJed Brown }
3662552f7358SJed Brown 
3663552f7358SJed Brown /*@
3664aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3665552f7358SJed Brown 
3666552f7358SJed Brown   Not Collective
3667552f7358SJed Brown 
3668aa50250dSMatthew G. Knepley   Input Parameter:
3669552f7358SJed Brown . dm    - The DMPlex object
3670552f7358SJed Brown 
3671aa50250dSMatthew G. Knepley   Output Parameter:
3672aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3673552f7358SJed Brown 
3674552f7358SJed Brown   Level: developer
3675552f7358SJed Brown 
3676aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3677aa50250dSMatthew G. Knepley @*/
3678aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3679aa50250dSMatthew G. Knepley {
3680aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3681aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3682aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3683c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3684aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3685aa50250dSMatthew G. Knepley }
3686aa50250dSMatthew G. Knepley 
3687aa50250dSMatthew G. Knepley /*@
3688aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3689aa50250dSMatthew G. Knepley 
3690aa50250dSMatthew G. Knepley   Not Collective
3691aa50250dSMatthew G. Knepley 
3692aa50250dSMatthew G. Knepley   Input Parameter:
3693aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3694aa50250dSMatthew G. Knepley 
3695aa50250dSMatthew G. Knepley   Output Parameter:
3696aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3697aa50250dSMatthew G. Knepley 
3698aa50250dSMatthew G. Knepley   Level: developer
3699552f7358SJed Brown 
3700b1bb481bSMatthew Knepley   Notes:
3701b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3702b1bb481bSMatthew Knepley   The point depth is described more in detail in DMPlexSymmetrize().
3703b1bb481bSMatthew Knepley 
3704b1bb481bSMatthew Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexSymmetrize()
3705552f7358SJed Brown @*/
3706552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3707552f7358SJed Brown {
3708aa50250dSMatthew G. Knepley   DMLabel        label;
3709aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3710552f7358SJed Brown   PetscErrorCode ierr;
3711552f7358SJed Brown 
3712552f7358SJed Brown   PetscFunctionBegin;
3713552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3714552f7358SJed Brown   PetscValidPointer(depth, 2);
3715aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3716aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3717552f7358SJed Brown   *depth = d-1;
3718552f7358SJed Brown   PetscFunctionReturn(0);
3719552f7358SJed Brown }
3720552f7358SJed Brown 
3721552f7358SJed Brown /*@
3722552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3723552f7358SJed Brown 
3724552f7358SJed Brown   Not Collective
3725552f7358SJed Brown 
3726552f7358SJed Brown   Input Parameters:
3727552f7358SJed Brown + dm           - The DMPlex object
3728552f7358SJed Brown - stratumValue - The requested depth
3729552f7358SJed Brown 
3730552f7358SJed Brown   Output Parameters:
3731552f7358SJed Brown + start - The first point at this depth
3732552f7358SJed Brown - end   - One beyond the last point at this depth
3733552f7358SJed Brown 
3734647867b2SJed Brown   Notes:
3735647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3736647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3737647867b2SJed Brown   higher dimension, e.g., "edges".
3738647867b2SJed Brown 
3739552f7358SJed Brown   Level: developer
3740552f7358SJed Brown 
3741552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3742552f7358SJed Brown @*/
37430adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37440adebc6cSBarry Smith {
3745aa50250dSMatthew G. Knepley   DMLabel        label;
374663d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3747552f7358SJed Brown   PetscErrorCode ierr;
3748552f7358SJed Brown 
3749552f7358SJed Brown   PetscFunctionBegin;
3750552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
375163d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
375263d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3753552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
37540d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
375563d1a920SMatthew G. Knepley   if (stratumValue < 0) {
375663d1a920SMatthew G. Knepley     if (start) *start = pStart;
375763d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
375863d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3759552f7358SJed Brown   }
3760aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
376163d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
376263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3763552f7358SJed Brown   PetscFunctionReturn(0);
3764552f7358SJed Brown }
3765552f7358SJed Brown 
3766552f7358SJed Brown /*@
3767552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3768552f7358SJed Brown 
3769552f7358SJed Brown   Not Collective
3770552f7358SJed Brown 
3771552f7358SJed Brown   Input Parameters:
3772552f7358SJed Brown + dm           - The DMPlex object
3773552f7358SJed Brown - stratumValue - The requested height
3774552f7358SJed Brown 
3775552f7358SJed Brown   Output Parameters:
3776552f7358SJed Brown + start - The first point at this height
3777552f7358SJed Brown - end   - One beyond the last point at this height
3778552f7358SJed Brown 
3779647867b2SJed Brown   Notes:
3780647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3781647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3782647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3783647867b2SJed Brown 
3784552f7358SJed Brown   Level: developer
3785552f7358SJed Brown 
3786552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3787552f7358SJed Brown @*/
37880adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37890adebc6cSBarry Smith {
3790aa50250dSMatthew G. Knepley   DMLabel        label;
379163d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3792552f7358SJed Brown   PetscErrorCode ierr;
3793552f7358SJed Brown 
3794552f7358SJed Brown   PetscFunctionBegin;
3795552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379663d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
379763d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3798552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
37990d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
380063d1a920SMatthew G. Knepley   if (stratumValue < 0) {
380163d1a920SMatthew G. Knepley     if (start) *start = pStart;
380263d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
380363d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3804552f7358SJed Brown   }
3805aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
380613903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
380763d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
380863d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3809552f7358SJed Brown   PetscFunctionReturn(0);
3810552f7358SJed Brown }
3811552f7358SJed Brown 
3812ba2698f1SMatthew G. Knepley /*@
3813ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
3814ba2698f1SMatthew G. Knepley 
3815ba2698f1SMatthew G. Knepley   Not Collective
3816ba2698f1SMatthew G. Knepley 
3817ba2698f1SMatthew G. Knepley   Input Parameter:
3818ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
3819ba2698f1SMatthew G. Knepley - point - The point
3820ba2698f1SMatthew G. Knepley 
3821ba2698f1SMatthew G. Knepley   Output Parameter:
3822ba2698f1SMatthew G. Knepley . depth - The depth of the point
3823ba2698f1SMatthew G. Knepley 
3824ba2698f1SMatthew G. Knepley   Level: intermediate
3825ba2698f1SMatthew G. Knepley 
3826ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3827ba2698f1SMatthew G. Knepley @*/
3828ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
3829ba2698f1SMatthew G. Knepley {
3830ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3831ba2698f1SMatthew G. Knepley 
3832ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3833ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383440a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
3835ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
3836ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3837ba2698f1SMatthew G. Knepley }
3838ba2698f1SMatthew G. Knepley 
3839ba2698f1SMatthew G. Knepley /*@
3840ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
3841ba2698f1SMatthew G. Knepley 
3842ba2698f1SMatthew G. Knepley   Not Collective
3843ba2698f1SMatthew G. Knepley 
3844ba2698f1SMatthew G. Knepley   Input Parameter:
3845ba2698f1SMatthew G. Knepley . dm - The DMPlex object
3846ba2698f1SMatthew G. Knepley 
3847ba2698f1SMatthew G. Knepley   Output Parameter:
3848ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
3849ba2698f1SMatthew G. Knepley 
3850ba2698f1SMatthew G. Knepley   Level: developer
3851ba2698f1SMatthew G. Knepley 
3852ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3853ba2698f1SMatthew G. Knepley @*/
3854ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
3855ba2698f1SMatthew G. Knepley {
3856ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3857ba2698f1SMatthew G. Knepley 
3858ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3859ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3860ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
3861ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
3862ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
3863ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3864ba2698f1SMatthew G. Knepley }
3865ba2698f1SMatthew G. Knepley 
3866ba2698f1SMatthew G. Knepley /*@
3867ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
3868ba2698f1SMatthew G. Knepley 
3869ba2698f1SMatthew G. Knepley   Not Collective
3870ba2698f1SMatthew G. Knepley 
3871ba2698f1SMatthew G. Knepley   Input Parameter:
3872ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
3873ba2698f1SMatthew G. Knepley - cell - The cell
3874ba2698f1SMatthew G. Knepley 
3875ba2698f1SMatthew G. Knepley   Output Parameter:
3876ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
3877ba2698f1SMatthew G. Knepley 
3878ba2698f1SMatthew G. Knepley   Level: intermediate
3879ba2698f1SMatthew G. Knepley 
3880ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3881ba2698f1SMatthew G. Knepley @*/
3882ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
3883ba2698f1SMatthew G. Knepley {
3884ba2698f1SMatthew G. Knepley   DMLabel        label;
3885ba2698f1SMatthew G. Knepley   PetscInt       ct;
3886ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3887ba2698f1SMatthew G. Knepley 
3888ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3889ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3890ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
3891ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3892ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
3893ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
3894ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3895ba2698f1SMatthew G. Knepley }
3896ba2698f1SMatthew G. Knepley 
38970adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
38980adebc6cSBarry Smith {
3899efe440bfSMatthew G. Knepley   PetscSection   section, s;
3900efe440bfSMatthew G. Knepley   Mat            m;
39013e922f36SToby Isaac   PetscInt       maxHeight;
3902552f7358SJed Brown   PetscErrorCode ierr;
3903552f7358SJed Brown 
3904552f7358SJed Brown   PetscFunctionBegin;
390538221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
39063e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
39073e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
390882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
390992fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
39101d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3911efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3912efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3913efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3914efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3915efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
39168f4c458bSMatthew G. Knepley 
39178f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
39188f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3919552f7358SJed Brown   PetscFunctionReturn(0);
3920552f7358SJed Brown }
3921552f7358SJed Brown 
3922f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3923f19dbd58SToby Isaac {
3924f19dbd58SToby Isaac   Vec            coordsLocal;
3925f19dbd58SToby Isaac   DM             coordsDM;
3926f19dbd58SToby Isaac   PetscErrorCode ierr;
3927f19dbd58SToby Isaac 
3928f19dbd58SToby Isaac   PetscFunctionBegin;
3929f19dbd58SToby Isaac   *field = NULL;
3930f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3931f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3932f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3933f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3934f19dbd58SToby Isaac   }
3935f19dbd58SToby Isaac   PetscFunctionReturn(0);
3936f19dbd58SToby Isaac }
3937f19dbd58SToby Isaac 
39387cd05799SMatthew G. Knepley /*@C
39397cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
39407cd05799SMatthew G. Knepley 
39417cd05799SMatthew G. Knepley   Not Collective
39427cd05799SMatthew G. Knepley 
39437cd05799SMatthew G. Knepley   Input Parameters:
39447cd05799SMatthew G. Knepley . dm        - The DMPlex object
39457cd05799SMatthew G. Knepley 
39467cd05799SMatthew G. Knepley   Output Parameter:
39477cd05799SMatthew G. Knepley . section - The PetscSection object
39487cd05799SMatthew G. Knepley 
39497cd05799SMatthew G. Knepley   Level: developer
39507cd05799SMatthew G. Knepley 
39517cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
39527cd05799SMatthew G. Knepley @*/
39530adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
39540adebc6cSBarry Smith {
3955552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3956552f7358SJed Brown 
3957552f7358SJed Brown   PetscFunctionBegin;
3958552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3959552f7358SJed Brown   if (section) *section = mesh->coneSection;
3960552f7358SJed Brown   PetscFunctionReturn(0);
3961552f7358SJed Brown }
3962552f7358SJed Brown 
39637cd05799SMatthew G. Knepley /*@C
39647cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
39657cd05799SMatthew G. Knepley 
39667cd05799SMatthew G. Knepley   Not Collective
39677cd05799SMatthew G. Knepley 
39687cd05799SMatthew G. Knepley   Input Parameters:
39697cd05799SMatthew G. Knepley . dm        - The DMPlex object
39707cd05799SMatthew G. Knepley 
39717cd05799SMatthew G. Knepley   Output Parameter:
39727cd05799SMatthew G. Knepley . section - The PetscSection object
39737cd05799SMatthew G. Knepley 
39747cd05799SMatthew G. Knepley   Level: developer
39757cd05799SMatthew G. Knepley 
39767cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
39777cd05799SMatthew G. Knepley @*/
39788cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
39798cb4d582SMatthew G. Knepley {
39808cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
39818cb4d582SMatthew G. Knepley 
39828cb4d582SMatthew G. Knepley   PetscFunctionBegin;
39838cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39848cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
39858cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
39868cb4d582SMatthew G. Knepley }
39878cb4d582SMatthew G. Knepley 
39887cd05799SMatthew G. Knepley /*@C
39897cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
39907cd05799SMatthew G. Knepley 
39917cd05799SMatthew G. Knepley   Not Collective
39927cd05799SMatthew G. Knepley 
39937cd05799SMatthew G. Knepley   Input Parameters:
39947cd05799SMatthew G. Knepley . dm        - The DMPlex object
39957cd05799SMatthew G. Knepley 
39967cd05799SMatthew G. Knepley   Output Parameter:
39977cd05799SMatthew G. Knepley . cones - The cone for each point
39987cd05799SMatthew G. Knepley 
39997cd05799SMatthew G. Knepley   Level: developer
40007cd05799SMatthew G. Knepley 
40017cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40027cd05799SMatthew G. Knepley @*/
4003a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4004a6dfd86eSKarl Rupp {
4005552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4006552f7358SJed Brown 
4007552f7358SJed Brown   PetscFunctionBegin;
4008552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4009552f7358SJed Brown   if (cones) *cones = mesh->cones;
4010552f7358SJed Brown   PetscFunctionReturn(0);
4011552f7358SJed Brown }
4012552f7358SJed Brown 
40137cd05799SMatthew G. Knepley /*@C
40147cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
40157cd05799SMatthew G. Knepley 
40167cd05799SMatthew G. Knepley   Not Collective
40177cd05799SMatthew G. Knepley 
40187cd05799SMatthew G. Knepley   Input Parameters:
40197cd05799SMatthew G. Knepley . dm        - The DMPlex object
40207cd05799SMatthew G. Knepley 
40217cd05799SMatthew G. Knepley   Output Parameter:
40227cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
40237cd05799SMatthew G. Knepley 
40247cd05799SMatthew G. Knepley   Level: developer
40257cd05799SMatthew G. Knepley 
40267cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40277cd05799SMatthew G. Knepley @*/
4028a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4029a6dfd86eSKarl Rupp {
4030552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4031552f7358SJed Brown 
4032552f7358SJed Brown   PetscFunctionBegin;
4033552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4034552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4035552f7358SJed Brown   PetscFunctionReturn(0);
4036552f7358SJed Brown }
4037552f7358SJed Brown 
4038552f7358SJed Brown /******************************** FEM Support **********************************/
4039552f7358SJed Brown 
40409e8305c2SJed Brown /*
40419e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
40429e8305c2SJed Brown  representing a line in the section.
40439e8305c2SJed Brown */
40449e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
40459e8305c2SJed Brown {
40469e8305c2SJed Brown   PetscErrorCode ierr;
40479e8305c2SJed Brown 
40489e8305c2SJed Brown   PetscFunctionBeginHot;
40499e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4050a433471fSStefano Zampini   if (line < 0) {
4051a433471fSStefano Zampini     *k = 0;
4052a433471fSStefano Zampini     *Nc = 0;
4053a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
40549e8305c2SJed Brown     *k = 1;
40559e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
40569e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
40579e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
40589e8305c2SJed Brown     *k = *k / *Nc + 1;
40599e8305c2SJed Brown   }
40609e8305c2SJed Brown   PetscFunctionReturn(0);
40619e8305c2SJed Brown }
40629e8305c2SJed Brown 
4063a4355906SMatthew Knepley /*@
4064bc1eb3faSJed Brown 
4065bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4066bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
40671bb6d2a8SBarry Smith   section provided (or the section of the DM).
4068a4355906SMatthew Knepley 
4069a4355906SMatthew Knepley   Input Parameters:
4070a4355906SMatthew Knepley + dm      - The DM
4071a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4072a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4073a4355906SMatthew Knepley 
4074a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4075a4355906SMatthew Knepley   degree of the basis.
4076a4355906SMatthew Knepley 
4077bc1eb3faSJed Brown   Example:
4078bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4079bc1eb3faSJed Brown .vb
4080bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4081bc1eb3faSJed Brown 
4082bc1eb3faSJed Brown   v4 -- e6 -- v3
4083bc1eb3faSJed Brown   |           |
4084bc1eb3faSJed Brown   e7    c0    e8
4085bc1eb3faSJed Brown   |           |
4086bc1eb3faSJed Brown   v1 -- e5 -- v2
4087bc1eb3faSJed Brown .ve
4088bc1eb3faSJed Brown 
4089bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4090bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4091bc1eb3faSJed Brown .vb
4092bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4093bc1eb3faSJed Brown     v1 -> [4]
4094bc1eb3faSJed Brown     ...
4095bc1eb3faSJed Brown     e5 -> [8, 9]
4096bc1eb3faSJed Brown .ve
4097bc1eb3faSJed Brown 
4098bc1eb3faSJed Brown   which corresponds to the dofs
4099bc1eb3faSJed Brown .vb
4100bc1eb3faSJed Brown     6   10  11  7
4101bc1eb3faSJed Brown     13  2   3   15
4102bc1eb3faSJed Brown     12  0   1   14
4103bc1eb3faSJed Brown     4   8   9   5
4104bc1eb3faSJed Brown .ve
4105bc1eb3faSJed Brown 
4106bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4107bc1eb3faSJed Brown .vb
4108bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4109bc1eb3faSJed Brown .ve
4110bc1eb3faSJed Brown 
4111bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4112bc1eb3faSJed Brown .vb
4113bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4114bc1eb3faSJed Brown .ve
4115bc1eb3faSJed Brown 
4116a4355906SMatthew Knepley   Level: developer
4117a4355906SMatthew Knepley 
41189df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4119a4355906SMatthew Knepley @*/
4120bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
41213194fc30SMatthew G. Knepley {
41227391a63aSMatthew G. Knepley   DMLabel        label;
41233194fc30SMatthew G. Knepley   PetscInt      *perm;
4124a433471fSStefano Zampini   PetscInt       dim, depth = -1, eStart = -1, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
41259e8305c2SJed Brown   PetscBool      vertexchart;
41263194fc30SMatthew G. Knepley   PetscErrorCode ierr;
41273194fc30SMatthew G. Knepley 
41283194fc30SMatthew G. Knepley   PetscFunctionBegin;
41293194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4130a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4131a433471fSStefano Zampini   if (point < 0) {
4132a433471fSStefano Zampini     PetscInt sStart,sEnd;
4133a433471fSStefano Zampini 
4134a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4135a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4136a433471fSStefano Zampini   }
41377391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4138a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4139a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
41407391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
41417391a63aSMatthew G. Knepley   else if  (depth == dim) {
41427391a63aSMatthew G. Knepley     const PetscInt *cone;
41437391a63aSMatthew G. Knepley 
41447391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4145d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4146d4e6627bSStefano Zampini     else if (dim == 3) {
4147d4e6627bSStefano Zampini       const PetscInt *cone2;
4148d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4149d4e6627bSStefano Zampini       eStart = cone2[0];
4150d4e6627bSStefano 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);
4151a433471fSStefano 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);
41529e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
41539e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
41549e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
41559e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
41569e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
41579e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
41589e8305c2SJed Brown   }
41593194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
41603194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
41619e8305c2SJed Brown     ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
416289eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
41633194fc30SMatthew G. Knepley   }
41643194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
41653194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
416689eabcffSMatthew G. Knepley     switch (dim) {
4167babf31e0SJed Brown     case 1:
41689e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4169babf31e0SJed Brown       /*
4170babf31e0SJed Brown         Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4171babf31e0SJed Brown         We want              [ vtx0; edge of length k-1; vtx1 ]
4172babf31e0SJed Brown       */
4173babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4174babf31e0SJed Brown       for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4175babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4176babf31e0SJed Brown       foffset = offset;
4177babf31e0SJed Brown       break;
417889eabcffSMatthew G. Knepley     case 2:
41793194fc30SMatthew 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} */
41809e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
41813194fc30SMatthew G. Knepley       /* The SEM order is
41823194fc30SMatthew G. Knepley 
41833194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
418489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
41853194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
41863194fc30SMatthew G. Knepley       */
41873194fc30SMatthew G. Knepley       {
41883194fc30SMatthew G. Knepley         const PetscInt of   = 0;
41893194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
41903194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
41913194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
41923194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
41933194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
41943194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
41953194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
41963194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
41973194fc30SMatthew G. Knepley         PetscInt       o;
41983194fc30SMatthew G. Knepley 
41993194fc30SMatthew G. Knepley         /* bottom */
42003194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
42013194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
42023194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
42033194fc30SMatthew G. Knepley         /* middle */
42043194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
42053194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
42063194fc30SMatthew 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;
42073194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
42083194fc30SMatthew G. Knepley         }
42093194fc30SMatthew G. Knepley         /* top */
42103194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
42113194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
42123194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
42133194fc30SMatthew G. Knepley         foffset = offset;
42143194fc30SMatthew G. Knepley       }
421589eabcffSMatthew G. Knepley       break;
421689eabcffSMatthew G. Knepley     case 3:
421789eabcffSMatthew G. Knepley       /* The original hex closure is
421889eabcffSMatthew G. Knepley 
421989eabcffSMatthew G. Knepley          {c,
422089eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
422189eabcffSMatthew 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,
422289eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
422389eabcffSMatthew G. Knepley       */
42249e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
422589eabcffSMatthew G. Knepley       /* The SEM order is
422689eabcffSMatthew G. Knepley          Bottom Slice
422789eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
422889eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
422989eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
423089eabcffSMatthew G. Knepley 
423189eabcffSMatthew G. Knepley          Middle Slice (j)
423289eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
423389eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
423489eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
423589eabcffSMatthew G. Knepley 
423689eabcffSMatthew G. Knepley          Top Slice
423789eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
423889eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
423989eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
424089eabcffSMatthew G. Knepley       */
424189eabcffSMatthew G. Knepley       {
424289eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
424389eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
424489eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
424589eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
424689eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
424789eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
424889eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
424989eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
425089eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
425189eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
425289eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
425389eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
425489eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
425589eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
425689eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
425789eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
425889eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
425989eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
426089eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
426189eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
426289eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
426389eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
426489eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
426589eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
426689eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
426789eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
426889eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
426989eabcffSMatthew G. Knepley         PetscInt       o, n;
427089eabcffSMatthew G. Knepley 
427189eabcffSMatthew G. Knepley         /* Bottom Slice */
427289eabcffSMatthew G. Knepley         /*   bottom */
427389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
427489eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
427589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
427689eabcffSMatthew G. Knepley         /*   middle */
427789eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
427889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4279316b7f87SMax 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;}
428089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
42813194fc30SMatthew G. Knepley         }
428289eabcffSMatthew G. Knepley         /*   top */
428389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
428489eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
428589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
428689eabcffSMatthew G. Knepley 
428789eabcffSMatthew G. Knepley         /* Middle Slice */
428889eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
428989eabcffSMatthew G. Knepley           /*   bottom */
429089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
429189eabcffSMatthew 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;
429289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
429389eabcffSMatthew G. Knepley           /*   middle */
429489eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
429589eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
429689eabcffSMatthew 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;
429789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
429889eabcffSMatthew G. Knepley           }
429989eabcffSMatthew G. Knepley           /*   top */
430089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
430189eabcffSMatthew 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;
430289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
430389eabcffSMatthew G. Knepley         }
430489eabcffSMatthew G. Knepley 
430589eabcffSMatthew G. Knepley         /* Top Slice */
430689eabcffSMatthew G. Knepley         /*   bottom */
430789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
430889eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
430989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
431089eabcffSMatthew G. Knepley         /*   middle */
431189eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
431289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
431389eabcffSMatthew 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;
431489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
431589eabcffSMatthew G. Knepley         }
431689eabcffSMatthew G. Knepley         /*   top */
431789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
431889eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
431989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
432089eabcffSMatthew G. Knepley 
432189eabcffSMatthew G. Knepley         foffset = offset;
432289eabcffSMatthew G. Knepley       }
432389eabcffSMatthew G. Knepley       break;
432489eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
432589eabcffSMatthew G. Knepley     }
432689eabcffSMatthew G. Knepley   }
432789eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
43283194fc30SMatthew G. Knepley   /* Check permutation */
43293194fc30SMatthew G. Knepley   {
43303194fc30SMatthew G. Knepley     PetscInt *check;
43313194fc30SMatthew G. Knepley 
43323194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
43333194fc30SMatthew 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]);}
43343194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
43353194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
43363194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
43373194fc30SMatthew G. Knepley   }
43381fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
43393194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
43403194fc30SMatthew G. Knepley }
43413194fc30SMatthew G. Knepley 
4342e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4343e071409bSToby Isaac {
4344e071409bSToby Isaac   PetscDS        prob;
4345e071409bSToby Isaac   PetscInt       depth, Nf, h;
4346e071409bSToby Isaac   DMLabel        label;
4347e071409bSToby Isaac   PetscErrorCode ierr;
4348e071409bSToby Isaac 
4349e071409bSToby Isaac   PetscFunctionBeginHot;
4350e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4351e071409bSToby Isaac   Nf      = prob->Nf;
4352e071409bSToby Isaac   label   = dm->depthLabel;
4353e071409bSToby Isaac   *dspace = NULL;
4354e071409bSToby Isaac   if (field < Nf) {
4355e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4356e071409bSToby Isaac 
4357e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4358e071409bSToby Isaac       PetscDualSpace dsp;
4359e071409bSToby Isaac 
4360e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4361e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4362e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4363e071409bSToby Isaac       h    = depth - 1 - h;
4364e071409bSToby Isaac       if (h) {
4365e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4366e071409bSToby Isaac       } else {
4367e071409bSToby Isaac         *dspace = dsp;
4368e071409bSToby Isaac       }
4369e071409bSToby Isaac     }
4370e071409bSToby Isaac   }
4371e071409bSToby Isaac   PetscFunctionReturn(0);
4372e071409bSToby Isaac }
4373e071409bSToby Isaac 
4374e071409bSToby Isaac 
43751a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4376a6dfd86eSKarl Rupp {
4377552f7358SJed Brown   PetscScalar    *array, *vArray;
4378d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
43791a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4380552f7358SJed Brown   PetscErrorCode  ierr;
4381552f7358SJed Brown 
43821b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
43832a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
43845a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
43855a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
43865a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
43873f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
43889df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
43899df71ca4SMatthew G. Knepley       PetscInt dof;
4390d9917b9dSMatthew G. Knepley 
43919df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
43929df71ca4SMatthew G. Knepley       size += dof;
43939df71ca4SMatthew G. Knepley     }
43949df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
43959df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
43962a3aaacfSMatthew G. Knepley       PetscInt       dof;
43975a1bb5cfSMatthew G. Knepley 
43985a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
43992a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
44005a1bb5cfSMatthew G. Knepley       size += dof;
44015a1bb5cfSMatthew G. Knepley     }
44023f7cbbe7SMatthew G. Knepley     if (!values) {
44033f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
44043f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
44053f7cbbe7SMatthew G. Knepley     }
440669291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4407982e9ed1SMatthew G. Knepley   } else {
4408982e9ed1SMatthew G. Knepley     array = *values;
4409982e9ed1SMatthew G. Knepley   }
44109df71ca4SMatthew G. Knepley   size = 0;
44115a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
44129df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
44139df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
44149df71ca4SMatthew G. Knepley     PetscScalar *varr;
4415d9917b9dSMatthew G. Knepley 
44169df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
44179df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
44189df71ca4SMatthew G. Knepley     varr = &vArray[off];
44191a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
44201a271a75SMatthew G. Knepley       array[offset] = varr[d];
44219df71ca4SMatthew G. Knepley     }
44229df71ca4SMatthew G. Knepley     size += dof;
44239df71ca4SMatthew G. Knepley   }
44249df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
44259df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
44269df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
44275a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
44285a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
44295a1bb5cfSMatthew G. Knepley 
443052ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
44315a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
44325a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
44335a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
44345a1bb5cfSMatthew G. Knepley     if (o >= 0) {
44351a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
44361a271a75SMatthew G. Knepley         array[offset] = varr[d];
44375a1bb5cfSMatthew G. Knepley       }
44385a1bb5cfSMatthew G. Knepley     } else {
44391a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
44401a271a75SMatthew G. Knepley         array[offset] = varr[d];
44415a1bb5cfSMatthew G. Knepley       }
44425a1bb5cfSMatthew G. Knepley     }
44439df71ca4SMatthew G. Knepley     size += dof;
44445a1bb5cfSMatthew G. Knepley   }
44455a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
44469df71ca4SMatthew G. Knepley   if (!*values) {
44475a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
44485a1bb5cfSMatthew G. Knepley     *values = array;
44499df71ca4SMatthew G. Knepley   } else {
44508ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
44518c312ff3SMatthew G. Knepley     *csize = size;
44529df71ca4SMatthew G. Knepley   }
44535a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
44545a1bb5cfSMatthew G. Knepley }
4455d9917b9dSMatthew G. Knepley 
44561dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4457923c78e0SToby Isaac {
4458923c78e0SToby Isaac   const PetscInt *cla;
4459923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4460923c78e0SToby Isaac   PetscErrorCode ierr;
4461923c78e0SToby Isaac 
4462923c78e0SToby Isaac   PetscFunctionBeginHot;
4463923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
4464923c78e0SToby Isaac   if (!*clPoints) {
4465923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
4466923c78e0SToby Isaac 
4467923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4468923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
4469923c78e0SToby Isaac     /* Compress out points not in the section */
4470923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
4471923c78e0SToby Isaac       PetscInt r = pts[2*p];
4472923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
4473923c78e0SToby Isaac         pts[q*2]   = r;
4474923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
4475923c78e0SToby Isaac         ++q;
4476923c78e0SToby Isaac       }
4477923c78e0SToby Isaac     }
4478923c78e0SToby Isaac     np = q;
4479923c78e0SToby Isaac     cla = NULL;
4480923c78e0SToby Isaac   } else {
4481923c78e0SToby Isaac     PetscInt dof, off;
4482923c78e0SToby Isaac 
4483923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4484923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4485923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4486923c78e0SToby Isaac     np   = dof/2;
4487923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4488923c78e0SToby Isaac   }
4489923c78e0SToby Isaac   *numPoints = np;
4490923c78e0SToby Isaac   *points    = pts;
4491923c78e0SToby Isaac   *clp       = cla;
4492923c78e0SToby Isaac 
4493923c78e0SToby Isaac   PetscFunctionReturn(0);
4494923c78e0SToby Isaac }
4495923c78e0SToby Isaac 
44961dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4497923c78e0SToby Isaac {
4498923c78e0SToby Isaac   PetscErrorCode ierr;
4499923c78e0SToby Isaac 
4500923c78e0SToby Isaac   PetscFunctionBeginHot;
4501923c78e0SToby Isaac   if (!*clPoints) {
4502923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4503923c78e0SToby Isaac   } else {
4504923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4505923c78e0SToby Isaac   }
4506923c78e0SToby Isaac   *numPoints = 0;
4507923c78e0SToby Isaac   *points    = NULL;
4508923c78e0SToby Isaac   *clSec     = NULL;
4509923c78e0SToby Isaac   *clPoints  = NULL;
4510923c78e0SToby Isaac   *clp       = NULL;
4511923c78e0SToby Isaac   PetscFunctionReturn(0);
4512923c78e0SToby Isaac }
4513923c78e0SToby Isaac 
451497e99dd9SToby 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[])
45151a271a75SMatthew G. Knepley {
45161a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
451797e99dd9SToby Isaac   const PetscInt    **perms = NULL;
451897e99dd9SToby Isaac   const PetscScalar **flips = NULL;
45191a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
45201a271a75SMatthew G. Knepley 
45211a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4522fe02ba77SJed Brown   *size = 0;
452397e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
452497e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
452597e99dd9SToby Isaac     const PetscInt    point = points[2*p];
452697e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
452797e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
45281a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
45291a271a75SMatthew G. Knepley     const PetscScalar *varr;
45301a271a75SMatthew G. Knepley 
45311a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
45321a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
45331a271a75SMatthew G. Knepley     varr = &vArray[off];
453497e99dd9SToby Isaac     if (clperm) {
453597e99dd9SToby Isaac       if (perm) {
453697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
45371a271a75SMatthew G. Knepley       } else {
453897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
453997e99dd9SToby Isaac       }
454097e99dd9SToby Isaac       if (flip) {
454197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
454297e99dd9SToby Isaac       }
454397e99dd9SToby Isaac     } else {
454497e99dd9SToby Isaac       if (perm) {
454597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
454697e99dd9SToby Isaac       } else {
454797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
454897e99dd9SToby Isaac       }
454997e99dd9SToby Isaac       if (flip) {
455097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
45511a271a75SMatthew G. Knepley       }
45521a271a75SMatthew G. Knepley     }
455397e99dd9SToby Isaac     offset += dof;
455497e99dd9SToby Isaac   }
455597e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45561a271a75SMatthew G. Knepley   *size = offset;
45571a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
45581a271a75SMatthew G. Knepley }
45591a271a75SMatthew G. Knepley 
456097e99dd9SToby 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[])
45611a271a75SMatthew G. Knepley {
45621a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
45631a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
45641a271a75SMatthew G. Knepley 
45651a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4566fe02ba77SJed Brown   *size = 0;
45671a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
456897e99dd9SToby Isaac     PetscInt          p;
456997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
457097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
45711a271a75SMatthew G. Knepley 
457297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
457397e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
457497e99dd9SToby Isaac       const PetscInt    point = points[2*p];
457597e99dd9SToby Isaac       PetscInt          fdof, foff, b;
45761a271a75SMatthew G. Knepley       const PetscScalar *varr;
457797e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
457897e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
45791a271a75SMatthew G. Knepley 
45801a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
45811a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
45821a271a75SMatthew G. Knepley       varr = &vArray[foff];
458397e99dd9SToby Isaac       if (clperm) {
458497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
458597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
458697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
45871a271a75SMatthew G. Knepley       } else {
458897e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
458997e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
459097e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
45911a271a75SMatthew G. Knepley       }
459297e99dd9SToby Isaac       offset += fdof;
45931a271a75SMatthew G. Knepley     }
459497e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45951a271a75SMatthew G. Knepley   }
45961a271a75SMatthew G. Knepley   *size = offset;
45971a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
45981a271a75SMatthew G. Knepley }
45991a271a75SMatthew G. Knepley 
4600552f7358SJed Brown /*@C
4601552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4602552f7358SJed Brown 
4603552f7358SJed Brown   Not collective
4604552f7358SJed Brown 
4605552f7358SJed Brown   Input Parameters:
4606552f7358SJed Brown + dm - The DM
4607552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4608552f7358SJed Brown . v - The local vector
460922c1ee49SMatthew G. Knepley . point - The point in the DM
461022c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
461122c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4612552f7358SJed Brown 
4613552f7358SJed Brown   Output Parameters:
461422c1ee49SMatthew G. Knepley + csize - The number of values in the closure
461522c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
461622c1ee49SMatthew G. Knepley 
461722c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
461822c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
461922c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
462022c1ee49SMatthew G. Knepley $
462122c1ee49SMatthew G. Knepley $ A typical use could be
462222c1ee49SMatthew G. Knepley $
462322c1ee49SMatthew G. Knepley $  values = NULL;
462422c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
462522c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
462622c1ee49SMatthew G. Knepley $    <Compute on closure>
462722c1ee49SMatthew G. Knepley $  }
462822c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
462922c1ee49SMatthew G. Knepley $
463022c1ee49SMatthew G. Knepley $ or
463122c1ee49SMatthew G. Knepley $
463222c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
463322c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
463422c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
463522c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
463622c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
463722c1ee49SMatthew G. Knepley $      <Compute on closure>
463822c1ee49SMatthew G. Knepley $    }
463922c1ee49SMatthew G. Knepley $  }
464022c1ee49SMatthew G. Knepley $  PetscFree(values);
4641552f7358SJed Brown 
4642552f7358SJed Brown   Fortran Notes:
4643552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4644552f7358SJed Brown   include petsc.h90 in your code.
4645552f7358SJed Brown 
4646552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4647552f7358SJed Brown 
4648552f7358SJed Brown   Level: intermediate
4649552f7358SJed Brown 
4650552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4651552f7358SJed Brown @*/
4652552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4653552f7358SJed Brown {
4654552f7358SJed Brown   PetscSection       clSection;
4655d9917b9dSMatthew G. Knepley   IS                 clPoints;
46568a84db2dSMatthew G. Knepley   PetscScalar       *array;
46578a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4658552f7358SJed Brown   PetscInt          *points = NULL;
46598f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
46601a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4661552f7358SJed Brown   PetscErrorCode     ierr;
4662552f7358SJed Brown 
4663d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4664552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
466592fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
46661a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46671a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4668552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4669552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4670552f7358SJed Brown   if (depth == 1 && numFields < 2) {
46711a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4672552f7358SJed Brown     PetscFunctionReturn(0);
4673552f7358SJed Brown   }
46741a271a75SMatthew G. Knepley   /* Get points */
4675923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46761fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
46771a271a75SMatthew G. Knepley   /* Get array */
4678bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
46791a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4680552f7358SJed Brown 
46811a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4682552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
46831a271a75SMatthew G. Knepley       asize += dof;
4684552f7358SJed Brown     }
4685bd6c0d32SMatthew G. Knepley     if (!values) {
4686923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46871a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4688bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4689bd6c0d32SMatthew G. Knepley     }
469069291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4691d0f6b257SMatthew G. Knepley   } else {
4692d0f6b257SMatthew G. Knepley     array = *values;
4693d0f6b257SMatthew G. Knepley   }
46948a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
46951a271a75SMatthew G. Knepley   /* Get values */
469697e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
469797e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
46981a271a75SMatthew G. Knepley   /* Cleanup points */
4699923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47001a271a75SMatthew G. Knepley   /* Cleanup array */
47018a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4702d0f6b257SMatthew G. Knepley   if (!*values) {
4703552f7358SJed Brown     if (csize) *csize = size;
4704552f7358SJed Brown     *values = array;
4705d0f6b257SMatthew G. Knepley   } else {
4706f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4707d0f6b257SMatthew G. Knepley     *csize = size;
4708d0f6b257SMatthew G. Knepley   }
4709552f7358SJed Brown   PetscFunctionReturn(0);
4710552f7358SJed Brown }
4711552f7358SJed Brown 
4712552f7358SJed Brown /*@C
4713552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4714552f7358SJed Brown 
4715552f7358SJed Brown   Not collective
4716552f7358SJed Brown 
4717552f7358SJed Brown   Input Parameters:
4718552f7358SJed Brown + dm - The DM
47190298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4720552f7358SJed Brown . v - The local vector
4721eaf898f9SPatrick Sanan . point - The point in the DM
47220298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4723552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4724552f7358SJed Brown 
472522c1ee49SMatthew 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()
472622c1ee49SMatthew G. Knepley 
47273813dfbdSMatthew G Knepley   Fortran Notes:
47283813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
47293813dfbdSMatthew G Knepley   include petsc.h90 in your code.
47303813dfbdSMatthew G Knepley 
47313813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
47323813dfbdSMatthew G Knepley 
4733552f7358SJed Brown   Level: intermediate
4734552f7358SJed Brown 
4735552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4736552f7358SJed Brown @*/
47377c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4738a6dfd86eSKarl Rupp {
4739552f7358SJed Brown   PetscInt       size = 0;
4740552f7358SJed Brown   PetscErrorCode ierr;
4741552f7358SJed Brown 
4742552f7358SJed Brown   PetscFunctionBegin;
4743552f7358SJed Brown   /* Should work without recalculating size */
474469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4745c9fdaa05SMatthew G. Knepley   *values = NULL;
4746552f7358SJed Brown   PetscFunctionReturn(0);
4747552f7358SJed Brown }
4748552f7358SJed Brown 
4749552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4750552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4751552f7358SJed Brown 
475297e99dd9SToby 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[])
4753552f7358SJed Brown {
4754552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4755552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4756552f7358SJed Brown   PetscScalar    *a;
4757552f7358SJed Brown   PetscInt        off, cind = 0, k;
4758552f7358SJed Brown   PetscErrorCode  ierr;
4759552f7358SJed Brown 
4760552f7358SJed Brown   PetscFunctionBegin;
4761552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4762552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4763552f7358SJed Brown   a    = &array[off];
4764552f7358SJed Brown   if (!cdof || setBC) {
476597e99dd9SToby Isaac     if (clperm) {
476697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
476797e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4768552f7358SJed Brown     } else {
476997e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
477097e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4771552f7358SJed Brown     }
4772552f7358SJed Brown   } else {
4773552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
477497e99dd9SToby Isaac     if (clperm) {
477597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4776552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
477797e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4778552f7358SJed Brown         }
4779552f7358SJed Brown       } else {
4780552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4781552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
478297e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
478397e99dd9SToby Isaac         }
478497e99dd9SToby Isaac       }
478597e99dd9SToby Isaac     } else {
478697e99dd9SToby Isaac       if (perm) {
478797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
478897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
478997e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
479097e99dd9SToby Isaac         }
479197e99dd9SToby Isaac       } else {
479297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
479397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
479497e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
479597e99dd9SToby Isaac         }
4796552f7358SJed Brown       }
4797552f7358SJed Brown     }
4798552f7358SJed Brown   }
4799552f7358SJed Brown   PetscFunctionReturn(0);
4800552f7358SJed Brown }
4801552f7358SJed Brown 
480297e99dd9SToby 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[])
4803a5e93ea8SMatthew G. Knepley {
4804a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4805a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4806a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4807a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4808a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4809a5e93ea8SMatthew G. Knepley 
4810a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4811a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4812a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4813a5e93ea8SMatthew G. Knepley   a    = &array[off];
4814a5e93ea8SMatthew G. Knepley   if (cdof) {
4815a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
481697e99dd9SToby Isaac     if (clperm) {
481797e99dd9SToby Isaac       if (perm) {
4818a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4819a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
482097e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
482197e99dd9SToby Isaac             cind++;
4822a5e93ea8SMatthew G. Knepley           }
4823a5e93ea8SMatthew G. Knepley         }
4824a5e93ea8SMatthew G. Knepley       } else {
4825a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4826a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
482797e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
482897e99dd9SToby Isaac             cind++;
482997e99dd9SToby Isaac           }
483097e99dd9SToby Isaac         }
483197e99dd9SToby Isaac       }
483297e99dd9SToby Isaac     } else {
483397e99dd9SToby Isaac       if (perm) {
483497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
483597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
483697e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
483797e99dd9SToby Isaac             cind++;
483897e99dd9SToby Isaac           }
483997e99dd9SToby Isaac         }
484097e99dd9SToby Isaac       } else {
484197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
484297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
484397e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
484497e99dd9SToby Isaac             cind++;
484597e99dd9SToby Isaac           }
4846a5e93ea8SMatthew G. Knepley         }
4847a5e93ea8SMatthew G. Knepley       }
4848a5e93ea8SMatthew G. Knepley     }
4849a5e93ea8SMatthew G. Knepley   }
4850a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4851a5e93ea8SMatthew G. Knepley }
4852a5e93ea8SMatthew G. Knepley 
485397e99dd9SToby 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[])
4854a6dfd86eSKarl Rupp {
4855552f7358SJed Brown   PetscScalar    *a;
48561a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
48571a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
485897e99dd9SToby Isaac   PetscInt        cind = 0, b;
4859552f7358SJed Brown   PetscErrorCode  ierr;
4860552f7358SJed Brown 
4861552f7358SJed Brown   PetscFunctionBegin;
4862552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4863552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
48641a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
48651a271a75SMatthew G. Knepley   a    = &array[foff];
4866552f7358SJed Brown   if (!fcdof || setBC) {
486797e99dd9SToby Isaac     if (clperm) {
486897e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
486997e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4870552f7358SJed Brown     } else {
487197e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
487297e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4873552f7358SJed Brown     }
4874552f7358SJed Brown   } else {
4875552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
487697e99dd9SToby Isaac     if (clperm) {
487797e99dd9SToby Isaac       if (perm) {
487897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
487997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
488097e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4881552f7358SJed Brown         }
4882552f7358SJed Brown       } else {
488397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
488497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
488597e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
488697e99dd9SToby Isaac         }
488797e99dd9SToby Isaac       }
488897e99dd9SToby Isaac     } else {
488997e99dd9SToby Isaac       if (perm) {
489097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
489197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
489297e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
489397e99dd9SToby Isaac         }
489497e99dd9SToby Isaac       } else {
489597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
489697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
489797e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4898552f7358SJed Brown         }
4899552f7358SJed Brown       }
4900552f7358SJed Brown     }
4901552f7358SJed Brown   }
49021a271a75SMatthew G. Knepley   *offset += fdof;
4903552f7358SJed Brown   PetscFunctionReturn(0);
4904552f7358SJed Brown }
4905552f7358SJed Brown 
4906ba322698SMatthew 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[])
4907a5e93ea8SMatthew G. Knepley {
4908a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
49091a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
49101a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4911ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4912ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4913a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4914a5e93ea8SMatthew G. Knepley 
4915a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4916a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4917a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
49181a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
49191a271a75SMatthew G. Knepley   a    = &array[foff];
4920a5e93ea8SMatthew G. Knepley   if (fcdof) {
4921ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4922a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
492397e99dd9SToby Isaac     if (clperm) {
492497e99dd9SToby Isaac       if (perm) {
4925ba322698SMatthew G. Knepley         if (comps) {
4926ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4927ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4928ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4929ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4930ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4931ba322698SMatthew G. Knepley           }
4932ba322698SMatthew G. Knepley         } else {
493397e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
493497e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
493597e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4936a5e93ea8SMatthew G. Knepley               ++cind;
4937a5e93ea8SMatthew G. Knepley             }
4938a5e93ea8SMatthew G. Knepley           }
4939ba322698SMatthew G. Knepley         }
4940ba322698SMatthew G. Knepley       } else {
4941ba322698SMatthew G. Knepley         if (comps) {
4942ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4943ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4944ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4945ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4946ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4947ba322698SMatthew G. Knepley           }
4948a5e93ea8SMatthew G. Knepley         } else {
494997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
495097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
495197e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
495297e99dd9SToby Isaac               ++cind;
495397e99dd9SToby Isaac             }
495497e99dd9SToby Isaac           }
495597e99dd9SToby Isaac         }
4956ba322698SMatthew G. Knepley       }
495797e99dd9SToby Isaac     } else {
495897e99dd9SToby Isaac       if (perm) {
4959ba322698SMatthew G. Knepley         if (comps) {
4960ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4961ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4962ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4963ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4964ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4965ba322698SMatthew G. Knepley           }
4966ba322698SMatthew G. Knepley         } else {
496797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
496897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
496997e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
497097e99dd9SToby Isaac               ++cind;
497197e99dd9SToby Isaac             }
497297e99dd9SToby Isaac           }
4973ba322698SMatthew G. Knepley         }
4974ba322698SMatthew G. Knepley       } else {
4975ba322698SMatthew G. Knepley         if (comps) {
4976ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4977ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4978ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4979ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4980ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4981ba322698SMatthew G. Knepley           }
498297e99dd9SToby Isaac         } else {
498397e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
498497e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
498597e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4986a5e93ea8SMatthew G. Knepley               ++cind;
4987a5e93ea8SMatthew G. Knepley             }
4988a5e93ea8SMatthew G. Knepley           }
4989a5e93ea8SMatthew G. Knepley         }
4990a5e93ea8SMatthew G. Knepley       }
4991a5e93ea8SMatthew G. Knepley     }
4992ba322698SMatthew G. Knepley   }
49931a271a75SMatthew G. Knepley   *offset += fdof;
4994a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4995a5e93ea8SMatthew G. Knepley }
4996a5e93ea8SMatthew G. Knepley 
49978f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4998a6dfd86eSKarl Rupp {
4999552f7358SJed Brown   PetscScalar    *array;
50001b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
50011b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5002552f7358SJed Brown   PetscErrorCode  ierr;
5003552f7358SJed Brown 
50041b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5005b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5006b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5007b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5008b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5009b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5010b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5011b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5012b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5013b6ebb6e6SMatthew G. Knepley 
5014b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5015b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5016b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5017b6ebb6e6SMatthew G. Knepley     {
5018b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5019b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5020b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5021b6ebb6e6SMatthew G. Knepley 
5022b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5023b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5024b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5025b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5026b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5027b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5028b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5029b6ebb6e6SMatthew G. Knepley           }
5030b6ebb6e6SMatthew G. Knepley         } else {
5031b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5032b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5033b6ebb6e6SMatthew G. Knepley           }
5034b6ebb6e6SMatthew G. Knepley         }
5035b6ebb6e6SMatthew G. Knepley       } else {
5036b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5037b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5038b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5039b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5040b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5041b6ebb6e6SMatthew G. Knepley           }
5042b6ebb6e6SMatthew G. Knepley         } else {
5043b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5044b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5045b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5046b6ebb6e6SMatthew G. Knepley           }
5047b6ebb6e6SMatthew G. Knepley         }
5048b6ebb6e6SMatthew G. Knepley       }
5049b6ebb6e6SMatthew G. Knepley     }
5050b6ebb6e6SMatthew G. Knepley   }
5051b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5052b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5053b6ebb6e6SMatthew G. Knepley }
50541b406b76SMatthew G. Knepley 
50551b406b76SMatthew G. Knepley /*@C
50561b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
50571b406b76SMatthew G. Knepley 
50581b406b76SMatthew G. Knepley   Not collective
50591b406b76SMatthew G. Knepley 
50601b406b76SMatthew G. Knepley   Input Parameters:
50611b406b76SMatthew G. Knepley + dm - The DM
50621b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
50631b406b76SMatthew G. Knepley . v - The local vector
5064eaf898f9SPatrick Sanan . point - The point in the DM
50651b406b76SMatthew G. Knepley . values - The array of values
506622c1ee49SMatthew 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,
506722c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
50681b406b76SMatthew G. Knepley 
50691b406b76SMatthew G. Knepley   Fortran Notes:
50701b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
50711b406b76SMatthew G. Knepley 
50721b406b76SMatthew G. Knepley   Level: intermediate
50731b406b76SMatthew G. Knepley 
50741b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
50751b406b76SMatthew G. Knepley @*/
50761b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
50771b406b76SMatthew G. Knepley {
50781b406b76SMatthew G. Knepley   PetscSection    clSection;
50791b406b76SMatthew G. Knepley   IS              clPoints;
50801b406b76SMatthew G. Knepley   PetscScalar    *array;
50811b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
508297e99dd9SToby Isaac   const PetscInt *clp, *clperm;
50831a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
50841b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
50851b406b76SMatthew G. Knepley 
50861a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
50871b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
508892fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
50891a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
50901a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
50911b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
50921b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
50931b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
50948f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
50951b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
50961b406b76SMatthew G. Knepley   }
50971a271a75SMatthew G. Knepley   /* Get points */
509897e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5099923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51001a271a75SMatthew G. Knepley   /* Get array */
5101552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
51021a271a75SMatthew G. Knepley   /* Get values */
5103ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
510497e99dd9SToby Isaac     PetscInt offset = 0, f;
5105552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
510697e99dd9SToby Isaac       const PetscInt    **perms = NULL;
510797e99dd9SToby Isaac       const PetscScalar **flips = NULL;
510897e99dd9SToby Isaac 
510997e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5110552f7358SJed Brown       switch (mode) {
5111552f7358SJed Brown       case INSERT_VALUES:
511297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
511397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
511497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
511597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
511697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5117552f7358SJed Brown         } break;
5118552f7358SJed Brown       case INSERT_ALL_VALUES:
511997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
512097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
512197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
512297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
512397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5124552f7358SJed Brown         } break;
5125a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
512697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
512797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
512897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
512997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5130ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5131a5e93ea8SMatthew G. Knepley         } break;
5132552f7358SJed Brown       case ADD_VALUES:
513397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
513497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
513597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
513697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
513797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5138552f7358SJed Brown         } break;
5139552f7358SJed Brown       case ADD_ALL_VALUES:
514097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
514197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
514297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
514397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
514497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5145552f7358SJed Brown         } break;
5146304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
514797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
514897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
514997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
515097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5151ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5152304ab55fSMatthew G. Knepley         } break;
5153552f7358SJed Brown       default:
5154f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5155552f7358SJed Brown       }
515697e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
51571a271a75SMatthew G. Knepley     }
5158552f7358SJed Brown   } else {
51591a271a75SMatthew G. Knepley     PetscInt dof, off;
516097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
516197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
51621a271a75SMatthew G. Knepley 
516397e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5164552f7358SJed Brown     switch (mode) {
5165552f7358SJed Brown     case INSERT_VALUES:
516697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
516797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
516897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
516997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
517097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
517197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5172552f7358SJed Brown       } break;
5173552f7358SJed Brown     case INSERT_ALL_VALUES:
517497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
517597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
517697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
517797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
517897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
517997e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5180552f7358SJed Brown       } break;
5181a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
518297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
518397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
518497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
518597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
518697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
518797e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5188a5e93ea8SMatthew G. Knepley       } break;
5189552f7358SJed Brown     case ADD_VALUES:
519097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
519197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
519297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
519397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
519497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
519597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5196552f7358SJed Brown       } break;
5197552f7358SJed Brown     case ADD_ALL_VALUES:
519897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
519997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
520097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
520197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
520297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
520397e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5204552f7358SJed Brown       } break;
5205304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
520697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
520797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
520897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
520997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
521097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
521197e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5212304ab55fSMatthew G. Knepley       } break;
5213552f7358SJed Brown     default:
5214f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5215552f7358SJed Brown     }
521697e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5217552f7358SJed Brown   }
52181a271a75SMatthew G. Knepley   /* Cleanup points */
5219923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52201a271a75SMatthew G. Knepley   /* Cleanup array */
5221552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5222552f7358SJed Brown   PetscFunctionReturn(0);
5223552f7358SJed Brown }
5224552f7358SJed Brown 
5225ba322698SMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], const PetscScalar values[], InsertMode mode)
5226e07394fbSMatthew G. Knepley {
5227e07394fbSMatthew G. Knepley   PetscSection      clSection;
5228e07394fbSMatthew G. Knepley   IS                clPoints;
5229e07394fbSMatthew G. Knepley   PetscScalar       *array;
5230e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
5231b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
5232e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
523397e99dd9SToby Isaac   PetscInt          offset = 0, f;
5234e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5235e07394fbSMatthew G. Knepley 
5236e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5237e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
523892fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5239e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5240e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5241e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5242e07394fbSMatthew G. Knepley   /* Get points */
5243b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5244923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5245e07394fbSMatthew G. Knepley   /* Get array */
5246e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5247e07394fbSMatthew G. Knepley   /* Get values */
5248e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
524997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
525097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
525197e99dd9SToby Isaac 
5252e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5253e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5254e07394fbSMatthew G. Knepley         PetscInt fdof;
5255e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5256e07394fbSMatthew G. Knepley         offset += fdof;
5257e07394fbSMatthew G. Knepley       }
5258e07394fbSMatthew G. Knepley       continue;
5259e07394fbSMatthew G. Knepley     }
526097e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5261e07394fbSMatthew G. Knepley     switch (mode) {
5262e07394fbSMatthew G. Knepley     case INSERT_VALUES:
526397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
526497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
526597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
526697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5267b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5268e07394fbSMatthew G. Knepley       } break;
5269e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
527097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
527197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
527297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
527397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5274b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5275e07394fbSMatthew G. Knepley         } break;
5276e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
527797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
527897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
527997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
528097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5281ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
5282e07394fbSMatthew G. Knepley       } break;
5283e07394fbSMatthew G. Knepley     case ADD_VALUES:
528497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
528597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
528697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
528797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5288b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5289e07394fbSMatthew G. Knepley       } break;
5290e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
529197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
529297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
529397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
529497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5295b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5296e07394fbSMatthew G. Knepley       } break;
5297e07394fbSMatthew G. Knepley     default:
5298f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5299e07394fbSMatthew G. Knepley     }
530097e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5301e07394fbSMatthew G. Knepley   }
5302e07394fbSMatthew G. Knepley   /* Cleanup points */
5303923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5304e07394fbSMatthew G. Knepley   /* Cleanup array */
5305e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5306e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5307e07394fbSMatthew G. Knepley }
5308e07394fbSMatthew G. Knepley 
53097cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5310552f7358SJed Brown {
5311552f7358SJed Brown   PetscMPIInt    rank;
5312552f7358SJed Brown   PetscInt       i, j;
5313552f7358SJed Brown   PetscErrorCode ierr;
5314552f7358SJed Brown 
5315552f7358SJed Brown   PetscFunctionBegin;
531682f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
5317eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5318e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5319e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5320b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5321b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5322e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5323b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5324519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
53257eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5326552f7358SJed Brown #else
5327b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5328552f7358SJed Brown #endif
5329552f7358SJed Brown     }
533077a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5331552f7358SJed Brown   }
5332552f7358SJed Brown   PetscFunctionReturn(0);
5333552f7358SJed Brown }
5334552f7358SJed Brown 
533505586334SMatthew G. Knepley /*
533605586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
533705586334SMatthew G. Knepley 
533805586334SMatthew G. Knepley   Input Parameters:
533905586334SMatthew G. Knepley + section - The section for this data layout
534005586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
534105586334SMatthew G. Knepley . off     - The global offset of this point
534205586334SMatthew G. Knepley . loff    - The local offset of each field
534305586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
534405586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
534505586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
534605586334SMatthew G. Knepley 
534705586334SMatthew G. Knepley   Output Parameter:
534805586334SMatthew G. Knepley . indices - Indices for dofs on this point
534905586334SMatthew G. Knepley 
535005586334SMatthew G. Knepley   Level: developer
535105586334SMatthew G. Knepley 
535205586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
535305586334SMatthew G. Knepley */
535405586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5355a6dfd86eSKarl Rupp {
5356e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5357552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5358552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5359552f7358SJed Brown   PetscInt        cind = 0, k;
5360552f7358SJed Brown   PetscErrorCode  ierr;
5361552f7358SJed Brown 
5362552f7358SJed Brown   PetscFunctionBegin;
5363552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5364552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5365552f7358SJed Brown   if (!cdof || setBC) {
536605586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
536705586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
536805586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
536905586334SMatthew G. Knepley 
537005586334SMatthew G. Knepley       indices[ind] = off + k;
5371552f7358SJed Brown     }
5372552f7358SJed Brown   } else {
5373552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
53744acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
537505586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
537605586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
537705586334SMatthew G. Knepley 
53784acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
53794acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
538005586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
53814acb8e1eSToby Isaac         ++cind;
53824acb8e1eSToby Isaac       } else {
538305586334SMatthew G. Knepley         indices[ind] = off+k-cind;
5384552f7358SJed Brown       }
5385552f7358SJed Brown     }
5386552f7358SJed Brown   }
5387e6ccafaeSMatthew G Knepley   *loff += dof;
5388552f7358SJed Brown   PetscFunctionReturn(0);
5389552f7358SJed Brown }
5390552f7358SJed Brown 
53917e29afd2SMatthew G. Knepley /*
53927e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
53937e29afd2SMatthew G. Knepley 
53947e29afd2SMatthew G. Knepley  . off - The global offset of this point
53957e29afd2SMatthew G. Knepley */
539605586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5397a6dfd86eSKarl Rupp {
5398552f7358SJed Brown   PetscInt       numFields, foff, f;
5399552f7358SJed Brown   PetscErrorCode ierr;
5400552f7358SJed Brown 
5401552f7358SJed Brown   PetscFunctionBegin;
5402552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5403552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
54044acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5405552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
54064acb8e1eSToby Isaac     PetscInt        cind = 0, b;
54074acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5408552f7358SJed Brown 
5409552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5410552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5411552f7358SJed Brown     if (!cfdof || setBC) {
541205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
541305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
541405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
541505586334SMatthew G. Knepley 
541605586334SMatthew G. Knepley         indices[ind] = off+foff+b;
541705586334SMatthew G. Knepley       }
5418552f7358SJed Brown     } else {
5419552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
542005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
542105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
542205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
542305586334SMatthew G. Knepley 
54244acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
542505586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5426552f7358SJed Brown           ++cind;
5427552f7358SJed Brown         } else {
542805586334SMatthew G. Knepley           indices[ind] = off+foff+b-cind;
5429552f7358SJed Brown         }
5430552f7358SJed Brown       }
5431552f7358SJed Brown     }
5432a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
5433552f7358SJed Brown     foffs[f] += fdof;
5434552f7358SJed Brown   }
5435552f7358SJed Brown   PetscFunctionReturn(0);
5436552f7358SJed Brown }
5437552f7358SJed Brown 
54387e29afd2SMatthew G. Knepley /*
54397e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
54407e29afd2SMatthew G. Knepley 
54417e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
54427e29afd2SMatthew G. Knepley */
544305586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
54447e29afd2SMatthew G. Knepley {
54457e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
54467e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
54477e29afd2SMatthew G. Knepley 
54487e29afd2SMatthew G. Knepley   PetscFunctionBegin;
54497e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
54507e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
54517e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
54527e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
54537e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
54547e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
54557e29afd2SMatthew G. Knepley 
54567e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
54577e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
54587e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
54597e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
546005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
546105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
546205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
546305586334SMatthew G. Knepley 
546405586334SMatthew G. Knepley         indices[ind] = foff+b;
546505586334SMatthew G. Knepley       }
54667e29afd2SMatthew G. Knepley     } else {
54677e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
546805586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
546905586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
547005586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
547105586334SMatthew G. Knepley 
54727e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
547305586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
54747e29afd2SMatthew G. Knepley           ++cind;
54757e29afd2SMatthew G. Knepley         } else {
547605586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
54777e29afd2SMatthew G. Knepley         }
54787e29afd2SMatthew G. Knepley       }
54797e29afd2SMatthew G. Knepley     }
54807e29afd2SMatthew G. Knepley     foffs[f] += fdof;
54817e29afd2SMatthew G. Knepley   }
54827e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
54837e29afd2SMatthew G. Knepley }
54847e29afd2SMatthew G. Knepley 
54854acb8e1eSToby 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)
5486d3d1a6afSToby Isaac {
5487d3d1a6afSToby Isaac   Mat             cMat;
5488d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5489d3d1a6afSToby Isaac   IS              aIS;
5490d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5491d3d1a6afSToby Isaac   const PetscInt  *anchors;
5492e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5493d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5494d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5495d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5496d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5497d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5498d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5499d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
55006ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5501d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5502d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5503d3d1a6afSToby Isaac 
5504d3d1a6afSToby Isaac   PetscFunctionBegin;
5505d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5506d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5507d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5508d3d1a6afSToby Isaac 
5509a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5510d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5511d3d1a6afSToby Isaac   if (aSec) {
5512580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5513d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5514d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5515d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5516d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5517d3d1a6afSToby Isaac      * into the global matrix anyway) */
5518d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5519d3d1a6afSToby Isaac       PetscInt b    = points[p];
55204b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5521d3d1a6afSToby Isaac 
55224b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
55234b2f2278SToby Isaac       if (!bSecDof) {
55244b2f2278SToby Isaac         continue;
55254b2f2278SToby Isaac       }
5526d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5527d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5528d3d1a6afSToby Isaac       }
5529d3d1a6afSToby Isaac       if (bDof) {
5530d3d1a6afSToby Isaac         /* this point is constrained */
5531d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5532d3d1a6afSToby Isaac         PetscInt bOff, q;
5533d3d1a6afSToby Isaac 
5534d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5535d3d1a6afSToby Isaac         newNumPoints  += bDof;
5536d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5537d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5538d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5539d3d1a6afSToby Isaac           PetscInt aDof;
5540d3d1a6afSToby Isaac 
5541d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5542d3d1a6afSToby Isaac           newNumIndices += aDof;
5543d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5544d3d1a6afSToby Isaac             PetscInt fDof;
5545d3d1a6afSToby Isaac 
5546d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5547d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5548d3d1a6afSToby Isaac           }
5549d3d1a6afSToby Isaac         }
5550d3d1a6afSToby Isaac       }
5551d3d1a6afSToby Isaac       else {
5552d3d1a6afSToby Isaac         /* this point is not constrained */
5553d3d1a6afSToby Isaac         newNumPoints++;
55544b2f2278SToby Isaac         newNumIndices += bSecDof;
5555d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5556d3d1a6afSToby Isaac           PetscInt fDof;
5557d3d1a6afSToby Isaac 
5558d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5559d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5560d3d1a6afSToby Isaac         }
5561d3d1a6afSToby Isaac       }
5562d3d1a6afSToby Isaac     }
5563d3d1a6afSToby Isaac   }
5564d3d1a6afSToby Isaac   if (!anyConstrained) {
556572b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
556672b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
556772b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
556872b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
556972b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5570d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5571d3d1a6afSToby Isaac   }
5572d3d1a6afSToby Isaac 
55736ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
55746ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
55756ecaa68aSToby Isaac 
5576f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5577d3d1a6afSToby Isaac 
55786ecaa68aSToby Isaac   if (!outPoints && !outValues) {
55796ecaa68aSToby Isaac     if (offsets) {
55806ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
55816ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
55826ecaa68aSToby Isaac       }
55836ecaa68aSToby Isaac     }
55846ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
55856ecaa68aSToby Isaac     PetscFunctionReturn(0);
55866ecaa68aSToby Isaac   }
55876ecaa68aSToby Isaac 
5588f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5589d3d1a6afSToby Isaac 
5590f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5591d3d1a6afSToby Isaac 
5592d3d1a6afSToby Isaac   /* workspaces */
5593d3d1a6afSToby Isaac   if (numFields) {
5594d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
559569291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
559669291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5597d3d1a6afSToby Isaac     }
5598d3d1a6afSToby Isaac   }
5599d3d1a6afSToby Isaac   else {
560069291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
560169291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5602d3d1a6afSToby Isaac   }
5603d3d1a6afSToby Isaac 
5604d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5605d3d1a6afSToby Isaac   if (numFields) {
56064b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
56074b2f2278SToby Isaac 
5608d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5609d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
56104b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5611d3d1a6afSToby Isaac 
56124b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
56134b2f2278SToby Isaac       if (!bSecDof) {
56144b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
56154b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
56164b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
56174b2f2278SToby Isaac         }
56184b2f2278SToby Isaac         continue;
56194b2f2278SToby Isaac       }
5620d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5621d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5622d3d1a6afSToby Isaac       }
5623d3d1a6afSToby Isaac       if (bDof) {
5624d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5625d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5626d3d1a6afSToby Isaac 
5627d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5628d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5629d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5630d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5631d3d1a6afSToby Isaac             PetscInt aFDof;
5632d3d1a6afSToby Isaac 
5633d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5634d3d1a6afSToby Isaac             allFDof += aFDof;
5635d3d1a6afSToby Isaac           }
5636d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5637d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5638d3d1a6afSToby Isaac         }
5639d3d1a6afSToby Isaac       }
5640d3d1a6afSToby Isaac       else {
5641d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5642d3d1a6afSToby Isaac           PetscInt fDof;
5643d3d1a6afSToby Isaac 
5644d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5645d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5646d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5647d3d1a6afSToby Isaac         }
5648d3d1a6afSToby Isaac       }
5649d3d1a6afSToby Isaac     }
56504b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
56514b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
56524b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5653d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5654d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5655d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5656d3d1a6afSToby Isaac       }
565719f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
565819f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
565969291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5660d3d1a6afSToby Isaac     }
5661d3d1a6afSToby Isaac   }
5662d3d1a6afSToby Isaac   else {
5663d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5664d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
56654b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5666d3d1a6afSToby Isaac 
56674b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
56684b2f2278SToby Isaac       if (!bSecDof) {
56694b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
56704b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
56714b2f2278SToby Isaac         continue;
56724b2f2278SToby Isaac       }
5673d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5674d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5675d3d1a6afSToby Isaac       }
5676d3d1a6afSToby Isaac       if (bDof) {
56774b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5678d3d1a6afSToby Isaac 
5679d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5680d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5681d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5682d3d1a6afSToby Isaac 
5683d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5684d3d1a6afSToby Isaac           allDof += aDof;
5685d3d1a6afSToby Isaac         }
5686d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
56874b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5688d3d1a6afSToby Isaac       }
5689d3d1a6afSToby Isaac       else {
56904b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5691d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5692d3d1a6afSToby Isaac       }
5693d3d1a6afSToby Isaac     }
5694d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5695d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5696d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5697d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5698d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5699d3d1a6afSToby Isaac     }
570069291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5701d3d1a6afSToby Isaac   }
5702d3d1a6afSToby Isaac 
57036ecaa68aSToby Isaac   /* output arrays */
570469291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
57056ecaa68aSToby Isaac 
5706d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5707d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5708d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
570969291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
571069291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5711d3d1a6afSToby Isaac   if (numFields) {
5712d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5713d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5714d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
57154b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5716d3d1a6afSToby Isaac 
57174b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
57184b2f2278SToby Isaac       if (!bSecDof) {
57194b2f2278SToby Isaac         continue;
57204b2f2278SToby Isaac       }
5721d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5722d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5723d3d1a6afSToby Isaac       }
5724d3d1a6afSToby Isaac       if (bDof) {
5725d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5726d3d1a6afSToby Isaac 
5727d3d1a6afSToby Isaac         fStart[0] = 0;
5728d3d1a6afSToby Isaac         fEnd[0]   = 0;
5729d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5730d3d1a6afSToby Isaac           PetscInt fDof;
5731d3d1a6afSToby Isaac 
5732d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5733d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5734d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5735d3d1a6afSToby Isaac         }
5736d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
573705586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
5738d3d1a6afSToby Isaac 
5739d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5740d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5741d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5742d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5743d3d1a6afSToby Isaac 
5744d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5745d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5746d3d1a6afSToby Isaac         }
5747d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5748d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5749d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5750d3d1a6afSToby Isaac 
5751d3d1a6afSToby 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 */
5752d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5753d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5754302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
575505586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
5756d3d1a6afSToby Isaac         }
5757d3d1a6afSToby Isaac         newP += bDof;
5758d3d1a6afSToby Isaac 
57596ecaa68aSToby Isaac         if (outValues) {
5760d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5761d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5762d3d1a6afSToby 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);
5763d3d1a6afSToby Isaac           }
5764d3d1a6afSToby Isaac         }
57656ecaa68aSToby Isaac       }
5766d3d1a6afSToby Isaac       else {
5767d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5768d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5769d3d1a6afSToby Isaac         newP++;
5770d3d1a6afSToby Isaac       }
5771d3d1a6afSToby Isaac     }
5772d3d1a6afSToby Isaac   } else {
5773d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5774d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5775d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
57764b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5777d3d1a6afSToby Isaac 
57784b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
57794b2f2278SToby Isaac       if (!bSecDof) {
57804b2f2278SToby Isaac         continue;
57814b2f2278SToby Isaac       }
5782d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5783d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5784d3d1a6afSToby Isaac       }
5785d3d1a6afSToby Isaac       if (bDof) {
5786d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5787d3d1a6afSToby Isaac 
5788d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
578905586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
5790d3d1a6afSToby Isaac 
5791d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5792d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5793d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5794d3d1a6afSToby Isaac 
5795d3d1a6afSToby 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 */
5796d3d1a6afSToby Isaac 
5797d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5798d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5799302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
580005586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
5801d3d1a6afSToby Isaac         }
5802d3d1a6afSToby Isaac         newP += bDof;
5803d3d1a6afSToby Isaac 
5804d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
58056ecaa68aSToby Isaac         if (outValues) {
5806d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5807d3d1a6afSToby Isaac         }
58086ecaa68aSToby Isaac       }
5809d3d1a6afSToby Isaac       else {
5810d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5811d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5812d3d1a6afSToby Isaac         newP++;
5813d3d1a6afSToby Isaac       }
5814d3d1a6afSToby Isaac     }
5815d3d1a6afSToby Isaac   }
5816d3d1a6afSToby Isaac 
58176ecaa68aSToby Isaac   if (outValues) {
581869291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5819580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
5820d3d1a6afSToby Isaac     /* multiply constraints on the right */
5821d3d1a6afSToby Isaac     if (numFields) {
5822d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5823d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5824d3d1a6afSToby Isaac 
5825d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5826d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5827d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5828d3d1a6afSToby Isaac           PetscInt c, r, k;
5829d3d1a6afSToby Isaac           PetscInt dof;
5830d3d1a6afSToby Isaac 
5831d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
58324b2f2278SToby Isaac           if (!dof) {
58334b2f2278SToby Isaac             continue;
58344b2f2278SToby Isaac           }
5835d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5836d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5837d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5838d3d1a6afSToby Isaac 
5839d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5840d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5841d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
58424acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5843d3d1a6afSToby Isaac                 }
5844d3d1a6afSToby Isaac               }
5845d3d1a6afSToby Isaac             }
5846d3d1a6afSToby Isaac           }
5847d3d1a6afSToby Isaac           else {
5848d3d1a6afSToby Isaac             /* copy this column as is */
5849d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5850d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5851d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5852d3d1a6afSToby Isaac               }
5853d3d1a6afSToby Isaac             }
5854d3d1a6afSToby Isaac           }
5855d3d1a6afSToby Isaac           oldOff += dof;
5856d3d1a6afSToby Isaac         }
5857d3d1a6afSToby Isaac       }
5858d3d1a6afSToby Isaac     }
5859d3d1a6afSToby Isaac     else {
5860d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5861d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5862d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5863d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5864d3d1a6afSToby Isaac         PetscInt c, r, k;
5865d3d1a6afSToby Isaac         PetscInt dof;
5866d3d1a6afSToby Isaac 
5867d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
58684b2f2278SToby Isaac         if (!dof) {
58694b2f2278SToby Isaac           continue;
58704b2f2278SToby Isaac         }
5871d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5872d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5873d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5874d3d1a6afSToby Isaac 
5875d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5876d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5877d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5878d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5879d3d1a6afSToby Isaac               }
5880d3d1a6afSToby Isaac             }
5881d3d1a6afSToby Isaac           }
5882d3d1a6afSToby Isaac         }
5883d3d1a6afSToby Isaac         else {
5884d3d1a6afSToby Isaac           /* copy this column as is */
5885d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5886d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5887d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5888d3d1a6afSToby Isaac             }
5889d3d1a6afSToby Isaac           }
5890d3d1a6afSToby Isaac         }
5891d3d1a6afSToby Isaac         oldOff += dof;
5892d3d1a6afSToby Isaac       }
5893d3d1a6afSToby Isaac     }
5894d3d1a6afSToby Isaac 
58956ecaa68aSToby Isaac     if (multiplyLeft) {
589669291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5897580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
5898d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5899d3d1a6afSToby Isaac       if (numFields) {
5900d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5901d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5902d3d1a6afSToby Isaac 
5903d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5904d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5905d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5906d3d1a6afSToby Isaac             PetscInt c, r, k;
5907d3d1a6afSToby Isaac             PetscInt dof;
5908d3d1a6afSToby Isaac 
5909d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5910d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5911d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5912d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5913d3d1a6afSToby Isaac 
5914d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5915d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5916d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5917d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5918d3d1a6afSToby Isaac                   }
5919d3d1a6afSToby Isaac                 }
5920d3d1a6afSToby Isaac               }
5921d3d1a6afSToby Isaac             }
5922d3d1a6afSToby Isaac             else {
5923d3d1a6afSToby Isaac               /* copy this row as is */
5924d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5925d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5926d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5927d3d1a6afSToby Isaac                 }
5928d3d1a6afSToby Isaac               }
5929d3d1a6afSToby Isaac             }
5930d3d1a6afSToby Isaac             oldOff += dof;
5931d3d1a6afSToby Isaac           }
5932d3d1a6afSToby Isaac         }
5933d3d1a6afSToby Isaac       }
5934d3d1a6afSToby Isaac       else {
5935d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5936d3d1a6afSToby Isaac 
5937d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5938d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5939d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5940d3d1a6afSToby Isaac           PetscInt c, r, k;
5941d3d1a6afSToby Isaac           PetscInt dof;
5942d3d1a6afSToby Isaac 
5943d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5944d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5945d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5946d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5947d3d1a6afSToby Isaac 
5948d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5949d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5950d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5951d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5952d3d1a6afSToby Isaac                 }
5953d3d1a6afSToby Isaac               }
5954d3d1a6afSToby Isaac             }
5955d3d1a6afSToby Isaac           }
5956d3d1a6afSToby Isaac           else {
5957d3d1a6afSToby Isaac             /* copy this row as is */
59589fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5959d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5960d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5961d3d1a6afSToby Isaac               }
5962d3d1a6afSToby Isaac             }
5963d3d1a6afSToby Isaac           }
5964d3d1a6afSToby Isaac           oldOff += dof;
5965d3d1a6afSToby Isaac         }
5966d3d1a6afSToby Isaac       }
5967d3d1a6afSToby Isaac 
596869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
59696ecaa68aSToby Isaac     }
59706ecaa68aSToby Isaac     else {
59716ecaa68aSToby Isaac       newValues = tmpValues;
59726ecaa68aSToby Isaac     }
59736ecaa68aSToby Isaac   }
59746ecaa68aSToby Isaac 
5975d3d1a6afSToby Isaac   /* clean up */
597669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
597769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
59786ecaa68aSToby Isaac 
5979d3d1a6afSToby Isaac   if (numFields) {
5980d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
598169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
598269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
598369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5984d3d1a6afSToby Isaac     }
5985d3d1a6afSToby Isaac   }
5986d3d1a6afSToby Isaac   else {
598769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
598869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
598969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5990d3d1a6afSToby Isaac   }
5991d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5992d3d1a6afSToby Isaac 
5993d3d1a6afSToby Isaac   /* output */
59946ecaa68aSToby Isaac   if (outPoints) {
5995d3d1a6afSToby Isaac     *outPoints = newPoints;
59966ecaa68aSToby Isaac   }
59976ecaa68aSToby Isaac   else {
599869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
59996ecaa68aSToby Isaac   }
600031620726SToby Isaac   if (outValues) {
6001d3d1a6afSToby Isaac     *outValues = newValues;
60026ecaa68aSToby Isaac   }
60036ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6004d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6005d3d1a6afSToby Isaac   }
6006d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6007d3d1a6afSToby Isaac }
6008d3d1a6afSToby Isaac 
60097cd05799SMatthew G. Knepley /*@C
60102cdc749cSStefano Zampini   DMPlexGetClosureIndices - Get the global indices for all local points in the closure of the given point
60117cd05799SMatthew G. Knepley 
60127cd05799SMatthew G. Knepley   Not collective
60137cd05799SMatthew G. Knepley 
60147cd05799SMatthew G. Knepley   Input Parameters:
60157cd05799SMatthew G. Knepley + dm - The DM
60162cdc749cSStefano Zampini . section - The section describing the local layout
60172cdc749cSStefano Zampini . globalSection - The section describing the parallel layout
60187cd05799SMatthew G. Knepley - point - The mesh point
60197cd05799SMatthew G. Knepley 
60207cd05799SMatthew G. Knepley   Output parameters:
60217cd05799SMatthew G. Knepley + numIndices - The number of indices
60227cd05799SMatthew G. Knepley . indices - The indices
60237cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
60247cd05799SMatthew G. Knepley 
60257cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
60267cd05799SMatthew G. Knepley 
60277cd05799SMatthew G. Knepley   Level: advanced
60287cd05799SMatthew G. Knepley 
60297cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
60307cd05799SMatthew G. Knepley @*/
60316ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
60327773e69fSMatthew G. Knepley {
60337773e69fSMatthew G. Knepley   PetscSection    clSection;
60347773e69fSMatthew G. Knepley   IS              clPoints;
603505586334SMatthew G. Knepley   const PetscInt *clp, *clperm;
60364acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
60377773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
60387773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
60397773e69fSMatthew G. Knepley   PetscInt        offsets[32];
60407773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
60417773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
60427773e69fSMatthew G. Knepley 
60437773e69fSMatthew G. Knepley   PetscFunctionBegin;
60447773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60457773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
60467773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
60477773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
60487773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
60497773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
60507773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6051580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
60527773e69fSMatthew G. Knepley   /* Get points in closure */
6053923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
605405586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
60557773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
60567773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
60577773e69fSMatthew G. Knepley     PetscInt dof, fdof;
60587773e69fSMatthew G. Knepley 
60597773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
60607773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
60617773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
60627773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
60637773e69fSMatthew G. Knepley     }
60647773e69fSMatthew G. Knepley     Nind += dof;
60657773e69fSMatthew G. Knepley   }
60667773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
60678ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
60684acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
60694acb8e1eSToby Isaac   /* Get dual space symmetries */
60704acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
60714acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
60724acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
60734acb8e1eSToby Isaac   }
60747773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
60757773e69fSMatthew G. Knepley   {
60764acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
60777773e69fSMatthew G. Knepley     if (numPointsNew) {
60784acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
60794acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
60804acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
60814acb8e1eSToby Isaac       }
60824acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
60834acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
60844acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
60854acb8e1eSToby Isaac       }
6086923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
60877773e69fSMatthew G. Knepley       numPoints = numPointsNew;
60887773e69fSMatthew G. Knepley       Nind      = NindNew;
60897773e69fSMatthew G. Knepley       points    = pointsNew;
60907773e69fSMatthew G. Knepley     }
60917773e69fSMatthew G. Knepley   }
60927773e69fSMatthew G. Knepley   /* Calculate indices */
609369291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
60947773e69fSMatthew G. Knepley   if (Nf) {
60956ecaa68aSToby Isaac     if (outOffsets) {
60966ecaa68aSToby Isaac       PetscInt f;
60976ecaa68aSToby Isaac 
609846bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
60996ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
61006ecaa68aSToby Isaac       }
61016ecaa68aSToby Isaac     }
61024acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
61034acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
61042cdc749cSStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, *indices);CHKERRQ(ierr);
61057773e69fSMatthew G. Knepley     }
61067773e69fSMatthew G. Knepley   } else {
61074acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
61084acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
61094acb8e1eSToby Isaac 
61104acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
61112cdc749cSStefano Zampini       ierr = DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, *indices);CHKERRQ(ierr);
61127773e69fSMatthew G. Knepley     }
61137773e69fSMatthew G. Knepley   }
61147773e69fSMatthew G. Knepley   /* Cleanup points */
61154acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
61164acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
61174acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
61184acb8e1eSToby Isaac   }
61197773e69fSMatthew G. Knepley   if (numPointsNew) {
612069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
61217773e69fSMatthew G. Knepley   } else {
6122923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
61237773e69fSMatthew G. Knepley   }
61247773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
61257773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
61267773e69fSMatthew G. Knepley }
61277773e69fSMatthew G. Knepley 
61287cd05799SMatthew G. Knepley /*@C
61297cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
61307cd05799SMatthew G. Knepley 
61317cd05799SMatthew G. Knepley   Not collective
61327cd05799SMatthew G. Knepley 
61337cd05799SMatthew G. Knepley   Input Parameters:
61347cd05799SMatthew G. Knepley + dm - The DM
61357cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
61367cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
61377cd05799SMatthew G. Knepley . point - The mesh point
61387cd05799SMatthew G. Knepley . numIndices - The number of indices
61397cd05799SMatthew G. Knepley . indices - The indices
61407cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
61417cd05799SMatthew G. Knepley 
61427cd05799SMatthew G. Knepley   Level: advanced
61437cd05799SMatthew G. Knepley 
61447cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
61457cd05799SMatthew G. Knepley @*/
614646bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
61477773e69fSMatthew G. Knepley {
61487773e69fSMatthew G. Knepley   PetscErrorCode ierr;
61497773e69fSMatthew G. Knepley 
61507773e69fSMatthew G. Knepley   PetscFunctionBegin;
61517773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61527773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
615369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
61547773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
61557773e69fSMatthew G. Knepley }
61567773e69fSMatthew G. Knepley 
61577f5d1fdeSMatthew G. Knepley /*@C
61587f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
61597f5d1fdeSMatthew G. Knepley 
61607f5d1fdeSMatthew G. Knepley   Not collective
61617f5d1fdeSMatthew G. Knepley 
61627f5d1fdeSMatthew G. Knepley   Input Parameters:
61637f5d1fdeSMatthew G. Knepley + dm - The DM
6164ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6165ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
61667f5d1fdeSMatthew G. Knepley . A - The matrix
6167eaf898f9SPatrick Sanan . point - The point in the DM
61687f5d1fdeSMatthew G. Knepley . values - The array of values
61697f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
61707f5d1fdeSMatthew G. Knepley 
61717f5d1fdeSMatthew G. Knepley   Fortran Notes:
61727f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
61737f5d1fdeSMatthew G. Knepley 
61747f5d1fdeSMatthew G. Knepley   Level: intermediate
61757f5d1fdeSMatthew G. Knepley 
61767f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
61777f5d1fdeSMatthew G. Knepley @*/
61787c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6179552f7358SJed Brown {
6180552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
61811b406b76SMatthew G. Knepley   PetscSection        clSection;
61821b406b76SMatthew G. Knepley   IS                  clPoints;
6183d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
618405586334SMatthew G. Knepley   const PetscInt     *clp, *clperm;
6185552f7358SJed Brown   PetscInt           *indices;
6186552f7358SJed Brown   PetscInt            offsets[32];
61874acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
61884acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
6189923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
61904acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
6191d3d1a6afSToby Isaac   PetscScalar        *newValues;
6192552f7358SJed Brown   PetscErrorCode      ierr;
6193552f7358SJed Brown 
6194552f7358SJed Brown   PetscFunctionBegin;
6195552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
619692fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
61973dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6198e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
61993dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
62003dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6201552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
620282f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6203580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
620405586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
6205923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6206552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
6207552f7358SJed Brown     PetscInt fdof;
6208552f7358SJed Brown 
6209552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
6210552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
6211552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
6212552f7358SJed Brown       offsets[f+1] += fdof;
6213552f7358SJed Brown     }
6214552f7358SJed Brown     numIndices += dof;
6215552f7358SJed Brown   }
62160d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
62170d644c17SKarl Rupp 
62188ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
62194acb8e1eSToby Isaac   /* Get symmetries */
62204acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
62214acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
62224acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
62234acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
62244acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
62254acb8e1eSToby Isaac 
62264acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
62274acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
62284acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
62294acb8e1eSToby Isaac 
62304acb8e1eSToby Isaac         if (!numFields) {
62314acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
62324acb8e1eSToby Isaac         } else {
62334acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
62344acb8e1eSToby Isaac         }
62354acb8e1eSToby Isaac         if (flip) {
62364acb8e1eSToby Isaac           PetscInt i, j, k;
62374acb8e1eSToby Isaac 
62384acb8e1eSToby Isaac           if (!valCopy) {
623969291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
62404acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
62414acb8e1eSToby Isaac             values = valCopy;
62424acb8e1eSToby Isaac           }
62434acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
6244775f7be2SToby Isaac             PetscScalar fval = flip[i];
62454acb8e1eSToby Isaac 
62464acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
62474acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
62484acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
62494acb8e1eSToby Isaac             }
62504acb8e1eSToby Isaac           }
62514acb8e1eSToby Isaac         }
62524acb8e1eSToby Isaac         foffset += fdof;
62534acb8e1eSToby Isaac       }
62544acb8e1eSToby Isaac     }
62554acb8e1eSToby Isaac   }
62564acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
6257d3d1a6afSToby Isaac   if (newNumPoints) {
62584acb8e1eSToby Isaac     if (valCopy) {
625969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
62604acb8e1eSToby Isaac     }
62614acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
62624acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
62634acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
62644acb8e1eSToby Isaac     }
62654acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
62664acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
62674acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
62684acb8e1eSToby Isaac     }
6269923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6270d3d1a6afSToby Isaac     numPoints  = newNumPoints;
6271d3d1a6afSToby Isaac     numIndices = newNumIndices;
6272d3d1a6afSToby Isaac     points     = newPoints;
6273d3d1a6afSToby Isaac     values     = newValues;
6274d3d1a6afSToby Isaac   }
627569291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6276552f7358SJed Brown   if (numFields) {
62777e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
62787e29afd2SMatthew G. Knepley 
62797e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
62807e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
62817e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
628205586334SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, clperm, indices);
62837e29afd2SMatthew G. Knepley       }
62847e29afd2SMatthew G. Knepley     } else {
62854acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
62864acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
628705586334SMatthew G. Knepley         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, indices);
6288552f7358SJed Brown       }
62897e29afd2SMatthew G. Knepley     }
6290552f7358SJed Brown   } else {
62914acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
62924acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
62934acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
629405586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, indices);
6295552f7358SJed Brown     }
6296552f7358SJed Brown   }
6297b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
6298552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6299ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
6300ab1d0545SMatthew G. Knepley     PetscInt i;
6301ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
63028ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
6303ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
6304ab1d0545SMatthew G. Knepley   }
6305552f7358SJed Brown   if (ierr) {
6306552f7358SJed Brown     PetscMPIInt    rank;
6307552f7358SJed Brown     PetscErrorCode ierr2;
6308552f7358SJed Brown 
630982f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6310e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6311b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
631269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
6313552f7358SJed Brown     CHKERRQ(ierr);
6314552f7358SJed Brown   }
63154acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
63164acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
63174acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
63184acb8e1eSToby Isaac   }
6319d3d1a6afSToby Isaac   if (newNumPoints) {
632069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
632169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
6322d3d1a6afSToby Isaac   }
6323d3d1a6afSToby Isaac   else {
63244acb8e1eSToby Isaac     if (valCopy) {
632569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
63264acb8e1eSToby Isaac     }
6327923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6328d3d1a6afSToby Isaac   }
632969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6330552f7358SJed Brown   PetscFunctionReturn(0);
6331552f7358SJed Brown }
6332552f7358SJed Brown 
6333de41b84cSMatthew 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)
6334de41b84cSMatthew G. Knepley {
6335de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6336de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6337de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6338de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
633917c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6340de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6341de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
63424ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6343de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6344de41b84cSMatthew G. Knepley 
6345de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6346de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6347de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
634892fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6349de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
635092fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6351de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6352e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6353de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6354e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6355de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6356de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6357de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6358de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6359580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6360580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
6361de41b84cSMatthew G. Knepley   /* Column indices */
6362de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
63634ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6364de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6365de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6366de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6367de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6368de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6369de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6370de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6371de41b84cSMatthew G. Knepley       ++q;
6372de41b84cSMatthew G. Knepley     }
6373de41b84cSMatthew G. Knepley   }
6374de41b84cSMatthew G. Knepley   numCPoints = q;
6375de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6376de41b84cSMatthew G. Knepley     PetscInt fdof;
6377de41b84cSMatthew G. Knepley 
6378de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
63794ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6380de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6381de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6382de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6383de41b84cSMatthew G. Knepley     }
6384de41b84cSMatthew G. Knepley     numCIndices += dof;
6385de41b84cSMatthew G. Knepley   }
6386de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6387de41b84cSMatthew G. Knepley   /* Row indices */
6388de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
6389de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
639069291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6391de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6392de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6393de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6394de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6395de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6396de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6397de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
63984ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
63994ca5e9f5SMatthew G. Knepley         if (!dof) continue;
64004ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
64014ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6402de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6403de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6404de41b84cSMatthew G. Knepley         ++q;
6405de41b84cSMatthew G. Knepley       }
6406de41b84cSMatthew G. Knepley     }
6407de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6408de41b84cSMatthew G. Knepley   }
6409de41b84cSMatthew G. Knepley   numFPoints = q;
6410de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6411de41b84cSMatthew G. Knepley     PetscInt fdof;
6412de41b84cSMatthew G. Knepley 
6413de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
64144ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6415de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6416de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6417de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6418de41b84cSMatthew G. Knepley     }
6419de41b84cSMatthew G. Knepley     numFIndices += dof;
6420de41b84cSMatthew G. Knepley   }
6421de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6422de41b84cSMatthew G. Knepley 
64238ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
64248ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
642569291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
642669291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6427de41b84cSMatthew G. Knepley   if (numFields) {
64284acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
64294acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
64304acb8e1eSToby Isaac 
64314acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
64324acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
64334acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6434de41b84cSMatthew G. Knepley     }
64354acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
64364acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
643705586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
64384acb8e1eSToby Isaac     }
64394acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
64404acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
644105586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
64424acb8e1eSToby Isaac     }
64434acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
64444acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
64454acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6446de41b84cSMatthew G. Knepley     }
6447de41b84cSMatthew G. Knepley   } else {
64484acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
64494acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
64504acb8e1eSToby Isaac 
64514acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
64524acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
64534acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
64544acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
64554acb8e1eSToby Isaac 
64564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
645705586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6458de41b84cSMatthew G. Knepley     }
64594acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
64604acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
64614acb8e1eSToby Isaac 
64624acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
646305586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6464de41b84cSMatthew G. Knepley     }
64654acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
64664acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6467de41b84cSMatthew G. Knepley   }
6468de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
64694acb8e1eSToby Isaac   /* TODO: flips */
6470de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6471de41b84cSMatthew G. Knepley   if (ierr) {
6472de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6473de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6474de41b84cSMatthew G. Knepley 
6475de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6476e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6477de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
647869291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
647969291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6480de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6481de41b84cSMatthew G. Knepley   }
648269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6483de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
648469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
648569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6486de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6487de41b84cSMatthew G. Knepley }
6488de41b84cSMatthew G. Knepley 
64897c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
64907c927364SMatthew G. Knepley {
64917c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
64927c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
64937c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
649417c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
64957c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
64967c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
64977c927364SMatthew G. Knepley   PetscErrorCode ierr;
64987c927364SMatthew G. Knepley 
64997c927364SMatthew G. Knepley   PetscFunctionBegin;
65007c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
65017c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
650292fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
65037c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
650492fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
65057c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6506e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
65077c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6508e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
65097c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
65107c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
65117c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6512580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6513580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
65147c927364SMatthew G. Knepley   /* Column indices */
65157c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
65167c927364SMatthew G. Knepley   maxFPoints = numCPoints;
65177c927364SMatthew G. Knepley   /* Compress out points not in the section */
65187c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
65197c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
65207c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
65217c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
65227c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
65237c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
65247c927364SMatthew G. Knepley       ++q;
65257c927364SMatthew G. Knepley     }
65267c927364SMatthew G. Knepley   }
65277c927364SMatthew G. Knepley   numCPoints = q;
65287c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
65297c927364SMatthew G. Knepley     PetscInt fdof;
65307c927364SMatthew G. Knepley 
65317c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
65327c927364SMatthew G. Knepley     if (!dof) continue;
65337c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
65347c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
65357c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
65367c927364SMatthew G. Knepley     }
65377c927364SMatthew G. Knepley     numCIndices += dof;
65387c927364SMatthew G. Knepley   }
65397c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
65407c927364SMatthew G. Knepley   /* Row indices */
65417c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
65427c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
654369291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
65447c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
65457c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
65467c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
65477c927364SMatthew G. Knepley     /* Compress out points not in the section */
65487c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
65497c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
65507c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
65517c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
65527c927364SMatthew G. Knepley         if (!dof) continue;
65537c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
65547c927364SMatthew G. Knepley         if (s < q) continue;
65557c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
65567c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
65577c927364SMatthew G. Knepley         ++q;
65587c927364SMatthew G. Knepley       }
65597c927364SMatthew G. Knepley     }
65607c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
65617c927364SMatthew G. Knepley   }
65627c927364SMatthew G. Knepley   numFPoints = q;
65637c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
65647c927364SMatthew G. Knepley     PetscInt fdof;
65657c927364SMatthew G. Knepley 
65667c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
65677c927364SMatthew G. Knepley     if (!dof) continue;
65687c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
65697c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
65707c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
65717c927364SMatthew G. Knepley     }
65727c927364SMatthew G. Knepley     numFIndices += dof;
65737c927364SMatthew G. Knepley   }
65747c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
65757c927364SMatthew G. Knepley 
65768ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
65778ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
65787c927364SMatthew G. Knepley   if (numFields) {
65794acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
65804acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
65814acb8e1eSToby Isaac 
65824acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
65834acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
65844acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
65857c927364SMatthew G. Knepley     }
65864acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
65874acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
658805586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
65894acb8e1eSToby Isaac     }
65904acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
65914acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
659205586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
65934acb8e1eSToby Isaac     }
65944acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
65954acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
65964acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
65977c927364SMatthew G. Knepley     }
65987c927364SMatthew G. Knepley   } else {
65994acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
66004acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
66014acb8e1eSToby Isaac 
66024acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
66034acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
66044acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
66054acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
66064acb8e1eSToby Isaac 
66074acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
660805586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
66097c927364SMatthew G. Knepley     }
66104acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
66114acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
66124acb8e1eSToby Isaac 
66134acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
661405586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
66157c927364SMatthew G. Knepley     }
66164acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
66174acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
66187c927364SMatthew G. Knepley   }
661969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
66207c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
66217c927364SMatthew G. Knepley   PetscFunctionReturn(0);
66227c927364SMatthew G. Knepley }
66237c927364SMatthew G. Knepley 
6624f96281f8SMatthew G. Knepley /*@
6625f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6626f96281f8SMatthew G. Knepley 
6627f96281f8SMatthew G. Knepley   Input Parameter:
6628f96281f8SMatthew G. Knepley . dm - The DMPlex object
6629f96281f8SMatthew G. Knepley 
6630f96281f8SMatthew G. Knepley   Output Parameters:
6631f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6632dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6633dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6634dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6635f96281f8SMatthew G. Knepley 
6636f96281f8SMatthew G. Knepley   Level: developer
6637f96281f8SMatthew G. Knepley 
6638dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6639f96281f8SMatthew G. Knepley @*/
6640770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6641552f7358SJed Brown {
6642552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6643770b213bSMatthew G Knepley   PetscInt       dim;
6644770b213bSMatthew G Knepley   PetscErrorCode ierr;
6645552f7358SJed Brown 
6646552f7358SJed Brown   PetscFunctionBegin;
6647552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6648c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
66497d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6650770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
66517d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6652770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6653770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6654552f7358SJed Brown   PetscFunctionReturn(0);
6655552f7358SJed Brown }
6656552f7358SJed Brown 
6657aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
66584a3e9fdbSToby Isaac {
66594a3e9fdbSToby Isaac   IS             is, his;
66607d5acc75SStefano Zampini   PetscInt       first = 0, stride;
66614a3e9fdbSToby Isaac   PetscBool      isStride;
66624a3e9fdbSToby Isaac   PetscErrorCode ierr;
66634a3e9fdbSToby Isaac 
66644a3e9fdbSToby Isaac   PetscFunctionBegin;
66654a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
66664a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
6667ba2698f1SMatthew G. Knepley   if (isStride) {ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);}
66687d5acc75SStefano Zampini   if (is && (!isStride || stride != 1)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM is not stratified: depth %D IS is not contiguous", d);
66694a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6670aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
66714a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
66724a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
66734a3e9fdbSToby Isaac   PetscFunctionReturn(0);
66744a3e9fdbSToby Isaac }
66754a3e9fdbSToby Isaac 
6676dc5a3409SMatthew G. Knepley /*@
6677dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6678dc5a3409SMatthew G. Knepley 
6679dc5a3409SMatthew G. Knepley   Input Parameters:
6680a2b725a8SWilliam Gropp + dm   - The DMPlex object
6681dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6682dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6683dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6684dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6685dc5a3409SMatthew G. Knepley 
6686dc5a3409SMatthew G. Knepley   Level: developer
6687dc5a3409SMatthew G. Knepley 
6688dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6689dc5a3409SMatthew G. Knepley @*/
6690770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6691552f7358SJed Brown {
6692552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6693770b213bSMatthew G Knepley   PetscInt       dim;
6694770b213bSMatthew G Knepley   PetscErrorCode ierr;
6695552f7358SJed Brown 
6696552f7358SJed Brown   PetscFunctionBegin;
6697552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6698c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
66997d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6700770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
67017d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6702770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6703770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6704552f7358SJed Brown   PetscFunctionReturn(0);
6705552f7358SJed Brown }
6706552f7358SJed Brown 
67077cd05799SMatthew G. Knepley /*@C
67087cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
67097cd05799SMatthew G. Knepley 
67107cd05799SMatthew G. Knepley   Input Parameter:
67117cd05799SMatthew G. Knepley . dm   - The DMPlex object
67127cd05799SMatthew G. Knepley 
67137cd05799SMatthew G. Knepley   Output Parameter:
67147cd05799SMatthew G. Knepley . cellHeight - The height of a cell
67157cd05799SMatthew G. Knepley 
67167cd05799SMatthew G. Knepley   Level: developer
67177cd05799SMatthew G. Knepley 
67187cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
67197cd05799SMatthew G. Knepley @*/
6720552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6721552f7358SJed Brown {
6722552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6723552f7358SJed Brown 
6724552f7358SJed Brown   PetscFunctionBegin;
6725552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6726552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6727552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6728552f7358SJed Brown   PetscFunctionReturn(0);
6729552f7358SJed Brown }
6730552f7358SJed Brown 
67317cd05799SMatthew G. Knepley /*@C
67327cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
67337cd05799SMatthew G. Knepley 
67347cd05799SMatthew G. Knepley   Input Parameters:
67357cd05799SMatthew G. Knepley + dm   - The DMPlex object
67367cd05799SMatthew G. Knepley - cellHeight - The height of a cell
67377cd05799SMatthew G. Knepley 
67387cd05799SMatthew G. Knepley   Level: developer
67397cd05799SMatthew G. Knepley 
67407cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
67417cd05799SMatthew G. Knepley @*/
6742552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6743552f7358SJed Brown {
6744552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6745552f7358SJed Brown 
6746552f7358SJed Brown   PetscFunctionBegin;
6747552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6748552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6749552f7358SJed Brown   PetscFunctionReturn(0);
6750552f7358SJed Brown }
6751552f7358SJed Brown 
6752e6139122SMatthew G. Knepley /*@
6753e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
6754e6139122SMatthew G. Knepley 
6755e6139122SMatthew G. Knepley   Input Parameter:
6756e6139122SMatthew G. Knepley . dm - The DMPlex object
6757e6139122SMatthew G. Knepley 
6758e6139122SMatthew G. Knepley   Output Parameters:
67592a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
67602a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
6761e6139122SMatthew G. Knepley 
67622a9f31c0SMatthew G. Knepley   Level: advanced
6763e6139122SMatthew G. Knepley 
6764e6139122SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum(), DMPlexGetHybridBounds()
6765e6139122SMatthew G. Knepley @*/
6766e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
6767e6139122SMatthew G. Knepley {
6768e6139122SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
6769e6139122SMatthew G. Knepley   PetscInt       dim;
6770e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6771e6139122SMatthew G. Knepley 
6772e6139122SMatthew G. Knepley   PetscFunctionBegin;
6773e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6774e6139122SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6775e6139122SMatthew G. Knepley   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6776e6139122SMatthew G. Knepley   if (gcStart) {PetscValidIntPointer(gcStart, 2); *gcStart = mesh->ghostCellStart;}
6777e6139122SMatthew G. Knepley   if (gcEnd)   {
6778e6139122SMatthew G. Knepley     PetscValidIntPointer(gcEnd, 3);
6779e6139122SMatthew G. Knepley     if (mesh->ghostCellStart >= 0) {ierr = DMPlexGetHeightStratum(dm, 0, NULL, gcEnd);CHKERRQ(ierr);}
6780e6139122SMatthew G. Knepley     else                           {*gcEnd = -1;}
6781e6139122SMatthew G. Knepley   }
6782e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6783e6139122SMatthew G. Knepley }
6784e6139122SMatthew G. Knepley 
6785e6139122SMatthew G. Knepley /*@
6786e6139122SMatthew G. Knepley   DMPlexSetGhostCellStratum - Set the range of cells which are used to enforce FV boundary conditions
6787e6139122SMatthew G. Knepley 
6788e6139122SMatthew G. Knepley   Input Parameters:
6789e6139122SMatthew G. Knepley + dm      - The DMPlex object
67902a9f31c0SMatthew G. Knepley . gcStart - The first ghost cell, or PETSC_DETERMINE
67912a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or PETSC_DETERMINE
6792e6139122SMatthew G. Knepley 
67932a9f31c0SMatthew G. Knepley   Level: advanced
67942a9f31c0SMatthew G. Knepley 
67952a9f31c0SMatthew G. Knepley   Note: This is not usually called directly by a user.
6796e6139122SMatthew G. Knepley 
6797e6139122SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum(), DMPlexSetHybridBounds()
6798e6139122SMatthew G. Knepley @*/
6799e6139122SMatthew G. Knepley PetscErrorCode DMPlexSetGhostCellStratum(DM dm, PetscInt gcStart, PetscInt gcEnd)
6800e6139122SMatthew G. Knepley {
6801e6139122SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
6802e6139122SMatthew G. Knepley   PetscInt       dim;
6803e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6804e6139122SMatthew G. Knepley 
6805e6139122SMatthew G. Knepley   PetscFunctionBegin;
6806e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6807e6139122SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6808e6139122SMatthew G. Knepley   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6809e6139122SMatthew G. Knepley   mesh->ghostCellStart = gcStart;
6810e6139122SMatthew G. Knepley   if (gcEnd >= 0) {
6811e6139122SMatthew G. Knepley     PetscInt cEnd;
6812e6139122SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
6813e6139122SMatthew G. Knepley     if (gcEnd != cEnd) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Ghost cells must appear at the end of the cell range, but gcEnd %D is not equal to cEnd %D", gcEnd, cEnd);
6814e6139122SMatthew G. Knepley   }
6815e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6816e6139122SMatthew G. Knepley }
6817e6139122SMatthew G. Knepley 
6818e6139122SMatthew G. Knepley /*@
6819e6139122SMatthew G. Knepley   DMPlexGetInteriorCellStratum - Get the range of cells which are neither hybrid nor ghost FV cells
6820e6139122SMatthew G. Knepley 
6821e6139122SMatthew G. Knepley   Input Parameter:
6822e6139122SMatthew G. Knepley . dm - The DMPlex object
6823e6139122SMatthew G. Knepley 
6824e6139122SMatthew G. Knepley   Output Parameters:
6825e6139122SMatthew G. Knepley + cStartInterior - The first ghost cell
6826e6139122SMatthew G. Knepley - cEndInterior   - The upper bound on ghost cells
6827e6139122SMatthew G. Knepley 
6828e6139122SMatthew G. Knepley   Level: developer
6829e6139122SMatthew G. Knepley 
6830e6139122SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum(), DMPlexGetHybridBounds()
6831e6139122SMatthew G. Knepley @*/
6832e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetInteriorCellStratum(DM dm, PetscInt *cStartInterior, PetscInt *cEndInterior)
6833e6139122SMatthew G. Knepley {
6834e6139122SMatthew G. Knepley   PetscInt       gcEnd, cMax;
6835e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6836e6139122SMatthew G. Knepley 
6837e6139122SMatthew G. Knepley   PetscFunctionBegin;
6838e6139122SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, cStartInterior, cEndInterior);CHKERRQ(ierr);
6839e6139122SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &gcEnd, NULL);CHKERRQ(ierr);
6840e6139122SMatthew G. Knepley   *cEndInterior = gcEnd < 0 ? *cEndInterior : gcEnd;
6841e6139122SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6842e6139122SMatthew G. Knepley   *cEndInterior = cMax  < 0 ? *cEndInterior : cMax;
6843e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6844e6139122SMatthew G. Knepley }
6845e6139122SMatthew G. Knepley 
6846552f7358SJed Brown /* We can easily have a form that takes an IS instead */
68479886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6848552f7358SJed Brown {
6849552f7358SJed Brown   PetscSection   section, globalSection;
6850552f7358SJed Brown   PetscInt      *numbers, p;
6851552f7358SJed Brown   PetscErrorCode ierr;
6852552f7358SJed Brown 
6853552f7358SJed Brown   PetscFunctionBegin;
685482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6855552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6856552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6857552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6858552f7358SJed Brown   }
6859552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
686015b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6861854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6862552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6863552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6864ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6865ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6866552f7358SJed Brown   }
686782f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6868ef48cebcSMatthew G. Knepley   if (globalSize) {
6869ef48cebcSMatthew G. Knepley     PetscLayout layout;
6870ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6871ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6872ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6873ef48cebcSMatthew G. Knepley   }
6874552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6875552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6876552f7358SJed Brown   PetscFunctionReturn(0);
6877552f7358SJed Brown }
6878552f7358SJed Brown 
687981ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6880552f7358SJed Brown {
6881552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6882552f7358SJed Brown   PetscErrorCode ierr;
6883552f7358SJed Brown 
6884552f7358SJed Brown   PetscFunctionBegin;
6885552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6886552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
68870298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
688881ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
68899886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
689081ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6891552f7358SJed Brown }
689281ed3555SMatthew G. Knepley 
68938dab3259SMatthew G. Knepley /*@
68947cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
68957cd05799SMatthew G. Knepley 
68967cd05799SMatthew G. Knepley   Input Parameter:
68977cd05799SMatthew G. Knepley . dm   - The DMPlex object
68987cd05799SMatthew G. Knepley 
68997cd05799SMatthew G. Knepley   Output Parameter:
69007cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
69017cd05799SMatthew G. Knepley 
69027cd05799SMatthew G. Knepley   Level: developer
69037cd05799SMatthew G. Knepley 
69047cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
69057cd05799SMatthew G. Knepley @*/
690681ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
690781ed3555SMatthew G. Knepley {
690881ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
690981ed3555SMatthew G. Knepley   PetscErrorCode ierr;
691081ed3555SMatthew G. Knepley 
691181ed3555SMatthew G. Knepley   PetscFunctionBegin;
691281ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
691381ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6914552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6915552f7358SJed Brown   PetscFunctionReturn(0);
6916552f7358SJed Brown }
6917552f7358SJed Brown 
691881ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
691981ed3555SMatthew G. Knepley {
692081ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
692181ed3555SMatthew G. Knepley   PetscErrorCode ierr;
692281ed3555SMatthew G. Knepley 
692381ed3555SMatthew G. Knepley   PetscFunctionBegin;
692481ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
692581ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
692681ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
692781ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
69289886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
692981ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
693081ed3555SMatthew G. Knepley }
693181ed3555SMatthew G. Knepley 
69328dab3259SMatthew G. Knepley /*@
69336aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
69347cd05799SMatthew G. Knepley 
69357cd05799SMatthew G. Knepley   Input Parameter:
69367cd05799SMatthew G. Knepley . dm   - The DMPlex object
69377cd05799SMatthew G. Knepley 
69387cd05799SMatthew G. Knepley   Output Parameter:
69397cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
69407cd05799SMatthew G. Knepley 
69417cd05799SMatthew G. Knepley   Level: developer
69427cd05799SMatthew G. Knepley 
69437cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
69447cd05799SMatthew G. Knepley @*/
6945552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6946552f7358SJed Brown {
6947552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6948552f7358SJed Brown   PetscErrorCode ierr;
6949552f7358SJed Brown 
6950552f7358SJed Brown   PetscFunctionBegin;
6951552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
695281ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6953552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6954552f7358SJed Brown   PetscFunctionReturn(0);
6955552f7358SJed Brown }
6956552f7358SJed Brown 
69578dab3259SMatthew G. Knepley /*@
69587cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
69597cd05799SMatthew G. Knepley 
69607cd05799SMatthew G. Knepley   Input Parameter:
69617cd05799SMatthew G. Knepley . dm   - The DMPlex object
69627cd05799SMatthew G. Knepley 
69637cd05799SMatthew G. Knepley   Output Parameter:
69647cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
69657cd05799SMatthew G. Knepley 
69667cd05799SMatthew G. Knepley   Level: developer
69677cd05799SMatthew G. Knepley 
69687cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
69697cd05799SMatthew G. Knepley @*/
6970ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6971ef48cebcSMatthew G. Knepley {
6972ef48cebcSMatthew G. Knepley   IS             nums[4];
6973862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6974ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6975ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6976ef48cebcSMatthew G. Knepley 
6977ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6978ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6979ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
69808abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
69818abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6982862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6983862913ffSStefano Zampini     PetscInt end;
6984862913ffSStefano Zampini 
6985862913ffSStefano Zampini     depths[d] = depth-d;
6986862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6987862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6988862913ffSStefano Zampini   }
6989862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6990862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6991862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6992862913ffSStefano 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]);
6993862913ffSStefano Zampini   }
6994ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6995ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6996ef48cebcSMatthew G. Knepley 
6997862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
69989886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6999ef48cebcSMatthew G. Knepley     shift += gsize;
7000ef48cebcSMatthew G. Knepley   }
7001302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
7002ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
7003ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
7004ef48cebcSMatthew G. Knepley }
7005ef48cebcSMatthew G. Knepley 
700608a22f4bSMatthew G. Knepley 
700708a22f4bSMatthew G. Knepley /*@
700808a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
700908a22f4bSMatthew G. Knepley 
701008a22f4bSMatthew G. Knepley   Input Parameter:
701108a22f4bSMatthew G. Knepley . dm - The DMPlex object
701208a22f4bSMatthew G. Knepley 
701308a22f4bSMatthew G. Knepley   Output Parameter:
701408a22f4bSMatthew G. Knepley . ranks - The rank field
701508a22f4bSMatthew G. Knepley 
701608a22f4bSMatthew G. Knepley   Options Database Keys:
701708a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
701808a22f4bSMatthew G. Knepley 
701908a22f4bSMatthew G. Knepley   Level: intermediate
702008a22f4bSMatthew G. Knepley 
702108a22f4bSMatthew G. Knepley .seealso: DMView()
702208a22f4bSMatthew G. Knepley @*/
702308a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
702408a22f4bSMatthew G. Knepley {
702508a22f4bSMatthew G. Knepley   DM             rdm;
702608a22f4bSMatthew G. Knepley   PetscFE        fe;
702708a22f4bSMatthew G. Knepley   PetscScalar   *r;
702808a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
702908a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
703008a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
703108a22f4bSMatthew G. Knepley 
703208a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
7033f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7034f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
703508a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
703608a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
703708a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
7038f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
703908a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
7040e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
704108a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7042e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
704308a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
704408a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
704508a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
704608a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
704708a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
704808a22f4bSMatthew G. Knepley     PetscScalar *lr;
704908a22f4bSMatthew G. Knepley 
705008a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
705108a22f4bSMatthew G. Knepley     *lr = rank;
705208a22f4bSMatthew G. Knepley   }
705308a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
705408a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
705508a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
705608a22f4bSMatthew G. Knepley }
705708a22f4bSMatthew G. Knepley 
7058ca8062c8SMatthew G. Knepley /*@
705918e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
706018e14f0cSMatthew G. Knepley 
706118e14f0cSMatthew G. Knepley   Input Parameters:
706218e14f0cSMatthew G. Knepley + dm    - The DMPlex
706318e14f0cSMatthew G. Knepley - label - The DMLabel
706418e14f0cSMatthew G. Knepley 
706518e14f0cSMatthew G. Knepley   Output Parameter:
706618e14f0cSMatthew G. Knepley . val - The label value field
706718e14f0cSMatthew G. Knepley 
706818e14f0cSMatthew G. Knepley   Options Database Keys:
706918e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
707018e14f0cSMatthew G. Knepley 
707118e14f0cSMatthew G. Knepley   Level: intermediate
707218e14f0cSMatthew G. Knepley 
707318e14f0cSMatthew G. Knepley .seealso: DMView()
707418e14f0cSMatthew G. Knepley @*/
707518e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
707618e14f0cSMatthew G. Knepley {
707718e14f0cSMatthew G. Knepley   DM             rdm;
707818e14f0cSMatthew G. Knepley   PetscFE        fe;
707918e14f0cSMatthew G. Knepley   PetscScalar   *v;
708018e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
708118e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
708218e14f0cSMatthew G. Knepley 
708318e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
708418e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
708518e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
708618e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
708718e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
708818e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
708918e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
709018e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7091e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
709218e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7093e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
709418e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
709518e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7096effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
709718e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
709818e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
709918e14f0cSMatthew G. Knepley     PetscScalar *lv;
710018e14f0cSMatthew G. Knepley     PetscInt     cval;
710118e14f0cSMatthew G. Knepley 
710218e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
710318e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
710418e14f0cSMatthew G. Knepley     *lv = cval;
710518e14f0cSMatthew G. Knepley   }
710618e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
710718e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
710818e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
710918e14f0cSMatthew G. Knepley }
711018e14f0cSMatthew G. Knepley 
711118e14f0cSMatthew G. Knepley /*@
7112ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7113ca8062c8SMatthew G. Knepley 
711469916449SMatthew G. Knepley   Input Parameter:
711569916449SMatthew G. Knepley . dm - The DMPlex object
7116ca8062c8SMatthew G. Knepley 
711795eb5ee5SVaclav Hapla   Notes:
711895eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
711995eb5ee5SVaclav Hapla 
712095eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7121ca8062c8SMatthew G. Knepley 
7122ca8062c8SMatthew G. Knepley   Level: developer
7123ca8062c8SMatthew G. Knepley 
712495eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7125ca8062c8SMatthew G. Knepley @*/
7126ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7127ca8062c8SMatthew G. Knepley {
7128ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7129ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7130ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
713157beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
713257beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7133ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7134ca8062c8SMatthew G. Knepley 
7135ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7136ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7137ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7138ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7139ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7140ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7141ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7142ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7143ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7144ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
714542e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
714642e66dfaSMatthew G. Knepley       PetscInt  d;
714742e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
714842e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
714942e66dfaSMatthew G. Knepley       }
7150ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7151ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7152ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7153ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7154ca8062c8SMatthew G. Knepley       }
715542e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
71568ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7157ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
71588ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7159ca8062c8SMatthew G. Knepley         }
7160302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71618ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7162ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
71638ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7164ca8062c8SMatthew G. Knepley         }
7165302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71668ccfff9cSToby 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]);
71678ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7168ca8062c8SMatthew G. Knepley       }
716942e66dfaSMatthew G. Knepley     }
717057beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
717157beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7172ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7173ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7174ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7175ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7176ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7177ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
717857beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
717957beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7180ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7181ca8062c8SMatthew G. Knepley       }
7182ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
71838ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7184ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
71858ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7186ca8062c8SMatthew G. Knepley         }
7187302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71888ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7189ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
71908ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7191ca8062c8SMatthew G. Knepley         }
7192302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71938ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7194ca8062c8SMatthew G. Knepley       }
7195ca8062c8SMatthew G. Knepley     }
7196ca8062c8SMatthew G. Knepley   }
719757beb4faSStefano Zampini   if (storagecheck) {
7198ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7199ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
72008ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
720157beb4faSStefano Zampini   }
7202ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7203ca8062c8SMatthew G. Knepley }
7204ca8062c8SMatthew G. Knepley 
7205ca8062c8SMatthew G. Knepley /*@
7206ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7207ca8062c8SMatthew G. Knepley 
7208ca8062c8SMatthew G. Knepley   Input Parameters:
7209ca8062c8SMatthew G. Knepley + dm - The DMPlex object
721058723a97SMatthew G. Knepley - cellHeight - Normally 0
7211ca8062c8SMatthew G. Knepley 
721295eb5ee5SVaclav Hapla   Notes:
721395eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
721425c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7215ca8062c8SMatthew G. Knepley 
721695eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
721795eb5ee5SVaclav Hapla 
7218ca8062c8SMatthew G. Knepley   Level: developer
7219ca8062c8SMatthew G. Knepley 
722095eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7221ca8062c8SMatthew G. Knepley @*/
722225c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7223ca8062c8SMatthew G. Knepley {
722442363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
722525c50c26SVaclav Hapla   PetscBool      isSimplex = PETSC_FALSE;
7226ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
7227ca8062c8SMatthew G. Knepley 
7228ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7229ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7230c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
723125c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
723225c50c26SVaclav Hapla   if (cStart < cEnd) {
723325c50c26SVaclav Hapla     ierr = DMPlexGetConeSize(dm, cStart, &c);CHKERRQ(ierr);
723425c50c26SVaclav Hapla     isSimplex = c == dim+1 ? PETSC_TRUE : PETSC_FALSE;
723525c50c26SVaclav Hapla   }
7236ca8062c8SMatthew G. Knepley   switch (dim) {
723742363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
723842363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
723942363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
7240ca8062c8SMatthew G. Knepley   default:
72418ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
7242ca8062c8SMatthew G. Knepley   }
724358723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7244ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
7245ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
7246ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
724758723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
724858723a97SMatthew G. Knepley 
724958723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
725058723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
725158723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
725258723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
725358723a97SMatthew G. Knepley     }
725458723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
72558ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
7256ca8062c8SMatthew G. Knepley   }
725742363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
725842363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
725942363296SMatthew G. Knepley 
726042363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
726142363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
726242363296SMatthew G. Knepley       const PetscInt p = closure[cl];
726342363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
726442363296SMatthew G. Knepley     }
726542363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
72668ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
726742363296SMatthew G. Knepley   }
7268ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7269ca8062c8SMatthew G. Knepley }
72709bf0dad6SMatthew G. Knepley 
72719bf0dad6SMatthew G. Knepley /*@
72729bf0dad6SMatthew 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
72739bf0dad6SMatthew G. Knepley 
7274899ea2b8SJacob Faibussowitsch   Not Collective
7275899ea2b8SJacob Faibussowitsch 
72769bf0dad6SMatthew G. Knepley   Input Parameters:
72779bf0dad6SMatthew G. Knepley + dm - The DMPlex object
72789bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
72799bf0dad6SMatthew G. Knepley 
728045da879fSVaclav Hapla   Notes:
728145da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
728245da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
728345da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
728445da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
72859bf0dad6SMatthew G. Knepley 
728695eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
728795eb5ee5SVaclav Hapla 
72889bf0dad6SMatthew G. Knepley   Level: developer
72899bf0dad6SMatthew G. Knepley 
729095eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
72919bf0dad6SMatthew G. Knepley @*/
729225c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
72939bf0dad6SMatthew G. Knepley {
72943554e41dSMatthew G. Knepley   PetscInt       pMax[4];
7295ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
72969bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7297899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
72989bf0dad6SMatthew G. Knepley 
72999bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
73009bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7301899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
730245da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
730345da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7304899ea2b8SJacob Faibussowitsch     PetscMPIInt	rank;
7305899ea2b8SJacob Faibussowitsch     MPI_Comm	comm;
7306899ea2b8SJacob Faibussowitsch 
7307899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7308899ea2b8SJacob Faibussowitsch     ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
730945da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7310899ea2b8SJacob Faibussowitsch   }
7311899ea2b8SJacob Faibussowitsch 
7312c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7313ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
73149bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
731525abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
7316ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
73173554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
73183554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
73199bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
7320ba2698f1SMatthew G. Knepley       DMPolytopeType  ct;
73219bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
73229bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
73239bf0dad6SMatthew G. Knepley 
732425abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
7325ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
73269bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
73279bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
73289bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
73299bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
73309bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
73319bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
73329bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
73339bf0dad6SMatthew G. Knepley       }
7334ba2698f1SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
73358ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
73369bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
73379bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
73389bf0dad6SMatthew G. Knepley 
73399bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
73409bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
73419bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
73429bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
73439bf0dad6SMatthew G. Knepley         }
73448ccfff9cSToby Isaac         if (fnumCorners != faceSize) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%D) of cell %D has %D vertices but should have %D", cone[f], f, c, fnumCorners, faceSize);
73459bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
73468ccfff9cSToby Isaac           if (fclosure[v] != faces[f*faceSize+v]) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%d) of cell %D vertex %D, %D != %D", cone[f], f, c, v, fclosure[v], faces[f*faceSize+v]);
73479bf0dad6SMatthew G. Knepley         }
73489bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
73499bf0dad6SMatthew G. Knepley       }
7350ba2698f1SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, ct, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
73519bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
73529bf0dad6SMatthew G. Knepley     }
73533554e41dSMatthew G. Knepley   }
7354552f7358SJed Brown   PetscFunctionReturn(0);
7355552f7358SJed Brown }
73563913d7c8SMatthew G. Knepley 
7357bb6a34a8SMatthew G. Knepley /*@
7358bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7359bb6a34a8SMatthew G. Knepley 
7360bb6a34a8SMatthew G. Knepley   Input Parameter:
7361bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7362bb6a34a8SMatthew G. Knepley 
736395eb5ee5SVaclav Hapla   Notes:
736495eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
736595eb5ee5SVaclav Hapla 
736695eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7367bb6a34a8SMatthew G. Knepley 
7368bb6a34a8SMatthew G. Knepley   Level: developer
7369bb6a34a8SMatthew G. Knepley 
737095eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7371bb6a34a8SMatthew G. Knepley @*/
7372bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7373bb6a34a8SMatthew G. Knepley {
7374bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7375bb6a34a8SMatthew G. Knepley   PetscReal      vol;
73766210d0f3SStefano Zampini   PetscInt       dim, depth, d, cStart, cEnd, c, cMax;
7377bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7378bb6a34a8SMatthew G. Knepley 
7379bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7380bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7381bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7382bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7383bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
73846210d0f3SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
73856210d0f3SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
73866210d0f3SStefano Zampini   for (c = cStart; c < cMax; ++c) {
7387bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7388bb6a34a8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
7389bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7390bb6a34a8SMatthew G. Knepley     if (depth > 1) {
7391bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7392bb6a34a8SMatthew G. Knepley       if (vol <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
7393bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7394bb6a34a8SMatthew G. Knepley     }
7395bb6a34a8SMatthew G. Knepley   }
7396bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7397bb6a34a8SMatthew G. Knepley }
7398bb6a34a8SMatthew G. Knepley 
739903da9461SVaclav Hapla /*@
7400e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
740103da9461SVaclav Hapla 
740203da9461SVaclav Hapla   Input Parameters:
740303da9461SVaclav Hapla . dm - The DMPlex object
740403da9461SVaclav Hapla 
7405e83a0d2dSVaclav Hapla   Notes:
7406e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
74078918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
740803da9461SVaclav Hapla 
740995eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
741095eb5ee5SVaclav Hapla 
741103da9461SVaclav Hapla   Level: developer
741203da9461SVaclav Hapla 
741395eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
741403da9461SVaclav Hapla @*/
741503da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
741603da9461SVaclav Hapla {
7417f0cfc026SVaclav Hapla   PetscSF         pointSF;
7418f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7419f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
7420f0cfc026SVaclav Hapla   PetscBool       distributed;
742103da9461SVaclav Hapla   PetscErrorCode  ierr;
742203da9461SVaclav Hapla 
742303da9461SVaclav Hapla   PetscFunctionBegin;
742403da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7425f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7426f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7427f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7428f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7429f0cfc026SVaclav Hapla   if (overlap) {
74308918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
74318918e3e2SVaclav Hapla     PetscFunctionReturn(0);
74328918e3e2SVaclav Hapla   }
7433f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7434f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7435f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7436f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
7437f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
743803da9461SVaclav Hapla 
7439ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7440f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7441f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7442f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7443f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
7444f5869d18SMatthew G. Knepley 
7445f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
744603da9461SVaclav Hapla   }
7447ece87651SVaclav Hapla 
7448f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
7449f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7450f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
7451f5869d18SMatthew G. Knepley     const PetscInt *cone;
7452f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
7453f5869d18SMatthew G. Knepley 
7454f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
7455f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
7456f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
7457f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
7458f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
7459f5869d18SMatthew 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]);
7460f5869d18SMatthew G. Knepley       }
7461f5869d18SMatthew G. Knepley     }
7462ece87651SVaclav Hapla   }
746303da9461SVaclav Hapla   PetscFunctionReturn(0);
746403da9461SVaclav Hapla }
746503da9461SVaclav Hapla 
7466068a5610SStefano Zampini typedef struct cell_stats
7467068a5610SStefano Zampini {
7468068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7469068a5610SStefano Zampini   PetscInt  count;
7470068a5610SStefano Zampini } cell_stats_t;
7471068a5610SStefano Zampini 
747225befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7473068a5610SStefano Zampini {
7474068a5610SStefano Zampini   PetscInt i, N = *len;
7475068a5610SStefano Zampini 
7476068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7477068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7478068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7479068a5610SStefano Zampini 
7480068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7481068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7482068a5610SStefano Zampini     B->sum += A->sum;
7483068a5610SStefano Zampini     B->squaresum += A->squaresum;
7484068a5610SStefano Zampini     B->count += A->count;
7485068a5610SStefano Zampini   }
7486068a5610SStefano Zampini }
7487068a5610SStefano Zampini 
7488068a5610SStefano Zampini /*@
748943fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
7490068a5610SStefano Zampini 
74918261a58bSMatthew G. Knepley   Collective on dm
74928261a58bSMatthew G. Knepley 
7493068a5610SStefano Zampini   Input Parameters:
7494068a5610SStefano Zampini + dm        - The DMPlex object
749543fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
749643fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
7497068a5610SStefano Zampini 
749895eb5ee5SVaclav Hapla   Notes:
749995eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
750095eb5ee5SVaclav Hapla 
750195eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7502068a5610SStefano Zampini 
7503068a5610SStefano Zampini   Level: developer
7504068a5610SStefano Zampini 
750595eb5ee5SVaclav Hapla .seealso: DMSetFromOptions()
7506068a5610SStefano Zampini @*/
750743fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7508068a5610SStefano Zampini {
7509068a5610SStefano Zampini   DM             dmCoarse;
751043fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
751143fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
751243fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
751343fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
751443fa8764SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, cMax, c, eStart, eEnd, count = 0;
751543fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
7516068a5610SStefano Zampini   PetscErrorCode ierr;
7517068a5610SStefano Zampini 
7518068a5610SStefano Zampini   PetscFunctionBegin;
7519068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7520068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7521068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7522068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7523068a5610SStefano Zampini   stats.count = 0;
7524068a5610SStefano Zampini 
752543fa8764SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
752643fa8764SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
752743fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
752843fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
7529068a5610SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
753043fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
7531068a5610SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);CHKERRQ(ierr);
7532068a5610SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
7533068a5610SStefano Zampini   for (c = cStart; c < cMax; c++) {
7534068a5610SStefano Zampini     PetscInt  i;
7535068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7536068a5610SStefano Zampini 
7537068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7538068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
753943fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
7540068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7541068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7542068a5610SStefano Zampini     }
7543068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7544068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7545068a5610SStefano Zampini 
7546068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7547068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7548068a5610SStefano Zampini     stats.sum       += cond;
7549068a5610SStefano Zampini     stats.squaresum += cond2;
7550068a5610SStefano Zampini     stats.count++;
75518261a58bSMatthew G. Knepley     if (output && cond > limit) {
755243fa8764SMatthew G. Knepley       PetscSection coordSection;
755343fa8764SMatthew G. Knepley       Vec          coordsLocal;
755443fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
755543fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
755643fa8764SMatthew G. Knepley 
755743fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
755843fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
755943fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
7560087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
756143fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
756243fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
756343fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
756443fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
756548afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
756643fa8764SMatthew G. Knepley         }
756743fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
756843fa8764SMatthew G. Knepley       }
756943fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
757043fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
757143fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
757243fa8764SMatthew G. Knepley 
757343fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
757443fa8764SMatthew G. Knepley           PetscReal len;
757543fa8764SMatthew G. Knepley 
757643fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
7577087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
757843fa8764SMatthew G. Knepley         }
757943fa8764SMatthew G. Knepley       }
758043fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
758143fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
758243fa8764SMatthew G. Knepley     }
7583068a5610SStefano Zampini   }
75848261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
7585068a5610SStefano Zampini 
7586068a5610SStefano Zampini   if (size > 1) {
7587068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7588068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7589068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7590068a5610SStefano Zampini     MPI_Op        statReduce;
7591068a5610SStefano Zampini 
7592068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
7593068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
7594068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
7595068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
7596068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
7597068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
7598068a5610SStefano Zampini   } else {
7599580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
7600068a5610SStefano Zampini   }
7601068a5610SStefano Zampini   if (!rank) {
7602068a5610SStefano Zampini     count = globalStats.count;
7603068a5610SStefano Zampini     min   = globalStats.min;
7604068a5610SStefano Zampini     max   = globalStats.max;
7605068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7606068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7607068a5610SStefano Zampini   }
7608068a5610SStefano Zampini 
7609068a5610SStefano Zampini   if (output) {
7610068a5610SStefano 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);
7611068a5610SStefano Zampini   }
7612068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7613068a5610SStefano Zampini 
7614068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7615068a5610SStefano Zampini   if (dmCoarse) {
7616068a5610SStefano Zampini     PetscBool isplex;
7617068a5610SStefano Zampini 
7618068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7619068a5610SStefano Zampini     if (isplex) {
762043fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
7621068a5610SStefano Zampini     }
7622068a5610SStefano Zampini   }
7623068a5610SStefano Zampini   PetscFunctionReturn(0);
7624068a5610SStefano Zampini }
7625068a5610SStefano Zampini 
7626bceba477SMatthew G. Knepley /* Pointwise interpolation
7627bceba477SMatthew G. Knepley      Just code FEM for now
7628bceba477SMatthew G. Knepley      u^f = I u^c
76294ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
76304ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
76314ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
7632bceba477SMatthew G. Knepley */
7633bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7634bceba477SMatthew G. Knepley {
7635bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
7636bceba477SMatthew G. Knepley   PetscInt       m, n;
7637a063dac3SMatthew G. Knepley   void          *ctx;
763868132eb9SMatthew G. Knepley   DM             cdm;
7639fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
7640bceba477SMatthew G. Knepley   PetscErrorCode ierr;
7641bceba477SMatthew G. Knepley 
7642bceba477SMatthew G. Knepley   PetscFunctionBegin;
7643e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7644bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7645e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7646bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
764768132eb9SMatthew G. Knepley 
7648fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
7649bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
7650bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7651fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
7652a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
765368132eb9SMatthew G. Knepley 
7654a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
765568132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
765668132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
765768132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
765868132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
76594db47ee9SStefano Zampini   if (scaling) {
76605d1c2e58SMatthew G. Knepley     /* Use naive scaling */
76615d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
76624db47ee9SStefano Zampini   }
7663a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
7664a063dac3SMatthew G. Knepley }
7665bceba477SMatthew G. Knepley 
76666dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7667a063dac3SMatthew G. Knepley {
766890748bafSMatthew G. Knepley   PetscErrorCode ierr;
76696dbf9973SLawrence Mitchell   VecScatter     ctx;
767090748bafSMatthew G. Knepley 
7671a063dac3SMatthew G. Knepley   PetscFunctionBegin;
76726dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
76736dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
76746dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
7675bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7676bceba477SMatthew G. Knepley }
7677bceba477SMatthew G. Knepley 
7678bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7679bd041c0cSMatthew G. Knepley {
7680bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
7681bd041c0cSMatthew G. Knepley   PetscInt       m, n;
7682bd041c0cSMatthew G. Knepley   void          *ctx;
7683bd041c0cSMatthew G. Knepley   DM             cdm;
7684bd041c0cSMatthew G. Knepley   PetscBool      regular;
7685bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
7686bd041c0cSMatthew G. Knepley 
7687bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
7688e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7689bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7690e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7691bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
7692bd041c0cSMatthew G. Knepley 
7693bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
7694bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7695bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
7696bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
7697bd041c0cSMatthew G. Knepley 
7698bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
7699bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
7700bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7701bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7702bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
7703bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
7704bd041c0cSMatthew G. Knepley }
7705bd041c0cSMatthew G. Knepley 
77060aef6b92SMatthew G. Knepley /*@
77070aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
77080aef6b92SMatthew G. Knepley 
77090aef6b92SMatthew G. Knepley   Input Parameter:
77100aef6b92SMatthew G. Knepley . dm - The DMPlex object
77110aef6b92SMatthew G. Knepley 
77120aef6b92SMatthew G. Knepley   Output Parameter:
77130aef6b92SMatthew G. Knepley . regular - The flag
77140aef6b92SMatthew G. Knepley 
77150aef6b92SMatthew G. Knepley   Level: intermediate
77160aef6b92SMatthew G. Knepley 
77170aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
77180aef6b92SMatthew G. Knepley @*/
77190aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
77200aef6b92SMatthew G. Knepley {
77210aef6b92SMatthew G. Knepley   PetscFunctionBegin;
77220aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77230aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
77240aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
77250aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
77260aef6b92SMatthew G. Knepley }
77270aef6b92SMatthew G. Knepley 
77280aef6b92SMatthew G. Knepley /*@
77290aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
77300aef6b92SMatthew G. Knepley 
77310aef6b92SMatthew G. Knepley   Input Parameters:
77320aef6b92SMatthew G. Knepley + dm - The DMPlex object
77330aef6b92SMatthew G. Knepley - regular - The flag
77340aef6b92SMatthew G. Knepley 
77350aef6b92SMatthew G. Knepley   Level: intermediate
77360aef6b92SMatthew G. Knepley 
77370aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
77380aef6b92SMatthew G. Knepley @*/
77390aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
77400aef6b92SMatthew G. Knepley {
77410aef6b92SMatthew G. Knepley   PetscFunctionBegin;
77420aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77430aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
77440aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
77450aef6b92SMatthew G. Knepley }
77460aef6b92SMatthew G. Knepley 
7747f7c74593SToby Isaac /* anchors */
7748a68b90caSToby Isaac /*@
7749f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7750f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
7751a68b90caSToby Isaac 
7752e228b242SToby Isaac   not collective
7753a68b90caSToby Isaac 
7754a68b90caSToby Isaac   Input Parameters:
7755a68b90caSToby Isaac . dm - The DMPlex object
7756a68b90caSToby Isaac 
7757a68b90caSToby Isaac   Output Parameters:
7758a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7759a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7760a68b90caSToby Isaac 
7761a68b90caSToby Isaac 
7762a68b90caSToby Isaac   Level: intermediate
7763a68b90caSToby Isaac 
7764f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7765a68b90caSToby Isaac @*/
7766a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7767a68b90caSToby Isaac {
7768a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
776941e6d900SToby Isaac   PetscErrorCode ierr;
7770a68b90caSToby Isaac 
7771a68b90caSToby Isaac   PetscFunctionBegin;
7772a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
777341e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7774a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7775a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7776a68b90caSToby Isaac   PetscFunctionReturn(0);
7777a68b90caSToby Isaac }
7778a68b90caSToby Isaac 
7779a68b90caSToby Isaac /*@
7780f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7781f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7782a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7783a68b90caSToby Isaac 
7784a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7785f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7786a68b90caSToby Isaac 
7787e228b242SToby Isaac   collective on dm
7788a68b90caSToby Isaac 
7789a68b90caSToby Isaac   Input Parameters:
7790a68b90caSToby Isaac + dm - The DMPlex object
7791e228b242SToby 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).
7792e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7793a68b90caSToby Isaac 
7794a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7795a68b90caSToby Isaac 
7796a68b90caSToby Isaac   Level: intermediate
7797a68b90caSToby Isaac 
7798f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7799a68b90caSToby Isaac @*/
7800a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7801a68b90caSToby Isaac {
7802a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7803e228b242SToby Isaac   PetscMPIInt    result;
7804a68b90caSToby Isaac   PetscErrorCode ierr;
7805a68b90caSToby Isaac 
7806a68b90caSToby Isaac   PetscFunctionBegin;
7807a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7808e228b242SToby Isaac   if (anchorSection) {
7809e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7810e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7811f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7812e228b242SToby Isaac   }
7813e228b242SToby Isaac   if (anchorIS) {
7814e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7815e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7816f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7817e228b242SToby Isaac   }
7818a68b90caSToby Isaac 
7819a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7820a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7821a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7822a68b90caSToby Isaac 
7823a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7824a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7825a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7826a68b90caSToby Isaac 
7827a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7828a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7829a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7830a68b90caSToby Isaac     const PetscInt *anchors;
7831a68b90caSToby Isaac 
7832a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7833a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7834a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7835a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7836a68b90caSToby Isaac       PetscInt p;
7837a68b90caSToby Isaac 
7838a68b90caSToby Isaac       p = anchors[a];
7839a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7840a68b90caSToby Isaac         PetscInt dof;
7841a68b90caSToby Isaac 
7842a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7843a68b90caSToby Isaac         if (dof) {
7844a68b90caSToby Isaac           PetscErrorCode ierr2;
7845a68b90caSToby Isaac 
7846a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
78478ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7848a68b90caSToby Isaac         }
7849a68b90caSToby Isaac       }
7850a68b90caSToby Isaac     }
7851a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7852a68b90caSToby Isaac   }
7853a68b90caSToby Isaac #endif
7854f7c74593SToby Isaac   /* reset the generic constraints */
7855f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7856a68b90caSToby Isaac   PetscFunctionReturn(0);
7857a68b90caSToby Isaac }
7858a68b90caSToby Isaac 
7859f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7860a68b90caSToby Isaac {
7861f7c74593SToby Isaac   PetscSection anchorSection;
78626995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7863a68b90caSToby Isaac   PetscErrorCode ierr;
7864a68b90caSToby Isaac 
7865a68b90caSToby Isaac   PetscFunctionBegin;
7866a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7867a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7868e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7869a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
78706995de1eSToby Isaac   if (numFields) {
7871719ab38cSToby Isaac     PetscInt f;
7872a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7873719ab38cSToby Isaac 
7874719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7875719ab38cSToby Isaac       PetscInt numComp;
7876719ab38cSToby Isaac 
7877719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7878719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7879719ab38cSToby Isaac     }
78806995de1eSToby Isaac   }
7881a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
78826995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
78836995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
78846995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
78856995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7886a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7887a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7888a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7889a68b90caSToby Isaac     if (dof) {
7890a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7891a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7892a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7893a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7894a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7895a68b90caSToby Isaac       }
7896a68b90caSToby Isaac     }
7897a68b90caSToby Isaac   }
7898a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7899a68b90caSToby Isaac   PetscFunctionReturn(0);
7900a68b90caSToby Isaac }
7901a68b90caSToby Isaac 
7902f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7903a68b90caSToby Isaac {
7904f7c74593SToby Isaac   PetscSection aSec;
79050ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
79060ac89760SToby Isaac   const PetscInt *anchors;
79070ac89760SToby Isaac   PetscInt numFields, f;
790866ad2231SToby Isaac   IS aIS;
79090ac89760SToby Isaac   PetscErrorCode ierr;
79100ac89760SToby Isaac 
79110ac89760SToby Isaac   PetscFunctionBegin;
79120ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79130ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
79140ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
79150ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
79160ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7917302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7918a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
791966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
79206995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
79216995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
79220ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
79230ac89760SToby Isaac   i[0] = 0;
79240ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
79250ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7926f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7927f19733c5SToby Isaac 
7928f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7929f19733c5SToby Isaac     if (!rDof) continue;
7930f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
79310ac89760SToby Isaac     if (numFields) {
79320ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
79330ac89760SToby Isaac         annz = 0;
7934f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7935f19733c5SToby Isaac           a = anchors[rOff + r];
79360ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
79370ac89760SToby Isaac           annz += aDof;
79380ac89760SToby Isaac         }
79390ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
79400ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
79410ac89760SToby Isaac         for (q = 0; q < dof; q++) {
79420ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
79430ac89760SToby Isaac         }
79440ac89760SToby Isaac       }
79450ac89760SToby Isaac     }
79460ac89760SToby Isaac     else {
79470ac89760SToby Isaac       annz = 0;
79480ac89760SToby Isaac       for (q = 0; q < dof; q++) {
79490ac89760SToby Isaac         a = anchors[off + q];
79500ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
79510ac89760SToby Isaac         annz += aDof;
79520ac89760SToby Isaac       }
79530ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
79540ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
79550ac89760SToby Isaac       for (q = 0; q < dof; q++) {
79560ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
79570ac89760SToby Isaac       }
79580ac89760SToby Isaac     }
79590ac89760SToby Isaac   }
79600ac89760SToby Isaac   nnz = i[m];
79610ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
79620ac89760SToby Isaac   offset = 0;
79630ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
79640ac89760SToby Isaac     if (numFields) {
79650ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
79660ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
79670ac89760SToby Isaac         for (q = 0; q < dof; q++) {
79680ac89760SToby Isaac           PetscInt rDof, rOff, r;
79690ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
79700ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
79710ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
79720ac89760SToby Isaac             PetscInt s;
79730ac89760SToby Isaac 
79740ac89760SToby Isaac             a = anchors[rOff + r];
79750ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
79760ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
79770ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
79780ac89760SToby Isaac               j[offset++] = aOff + s;
79790ac89760SToby Isaac             }
79800ac89760SToby Isaac           }
79810ac89760SToby Isaac         }
79820ac89760SToby Isaac       }
79830ac89760SToby Isaac     }
79840ac89760SToby Isaac     else {
79850ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
79860ac89760SToby Isaac       for (q = 0; q < dof; q++) {
79870ac89760SToby Isaac         PetscInt rDof, rOff, r;
79880ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
79890ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
79900ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
79910ac89760SToby Isaac           PetscInt s;
79920ac89760SToby Isaac 
79930ac89760SToby Isaac           a = anchors[rOff + r];
79940ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
79950ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
79960ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
79970ac89760SToby Isaac             j[offset++] = aOff + s;
79980ac89760SToby Isaac           }
79990ac89760SToby Isaac         }
80000ac89760SToby Isaac       }
80010ac89760SToby Isaac     }
80020ac89760SToby Isaac   }
80030ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
800425570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
800525570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
800666ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
80070ac89760SToby Isaac   PetscFunctionReturn(0);
80080ac89760SToby Isaac }
80090ac89760SToby Isaac 
801066ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
801166ad2231SToby Isaac {
8012f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8013f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
801466ad2231SToby Isaac   Mat            cMat;
801566ad2231SToby Isaac   PetscErrorCode ierr;
801666ad2231SToby Isaac 
801766ad2231SToby Isaac   PetscFunctionBegin;
801866ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8019a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
802066ad2231SToby Isaac   if (anchorSection) {
802144a7f3ddSMatthew G. Knepley     PetscInt Nf;
8022e228b242SToby Isaac 
802392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
8024f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
8025f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
802644a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
802744a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
802866ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
802966ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
803066ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
803166ad2231SToby Isaac   }
803266ad2231SToby Isaac   PetscFunctionReturn(0);
803366ad2231SToby Isaac }
8034a93c429eSMatthew G. Knepley 
8035a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8036a93c429eSMatthew G. Knepley {
8037a93c429eSMatthew G. Knepley   IS             subis;
8038a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
8039a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
8040a93c429eSMatthew G. Knepley 
8041a93c429eSMatthew G. Knepley   PetscFunctionBegin;
804292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
8043a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8044a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8045a93c429eSMatthew G. Knepley   /* Create subdomain */
8046a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
8047a93c429eSMatthew G. Knepley   /* Create submodel */
8048a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
8049a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
8050a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
805192fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
8052a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
8053e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
8054a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
8055a93c429eSMatthew G. Knepley   if (is) {
8056a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
8057a93c429eSMatthew G. Knepley     IS              spIS;
8058a93c429eSMatthew G. Knepley     const PetscInt *spmap;
8059a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
8060a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8061a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
8062a93c429eSMatthew G. Knepley 
8063a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
8064a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
8065a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
80666f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
80676f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
8068a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
8069a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8070a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
8071a93c429eSMatthew G. Knepley 
8072a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
8073a93c429eSMatthew G. Knepley       if (gdof > 0) {
8074a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8075a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
8076a93c429eSMatthew G. Knepley 
8077a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
8078a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
8079a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
8080a93c429eSMatthew G. Knepley         }
8081a93c429eSMatthew G. Knepley         subSize += pSubSize;
8082a93c429eSMatthew G. Knepley         if (pSubSize) {
8083a93c429eSMatthew G. Knepley           if (bs < 0) {
8084a93c429eSMatthew G. Knepley             bs = pSubSize;
8085a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
8086a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
8087a93c429eSMatthew G. Knepley             bs = 1;
8088a93c429eSMatthew G. Knepley           }
8089a93c429eSMatthew G. Knepley         }
8090a93c429eSMatthew G. Knepley       }
8091a93c429eSMatthew G. Knepley     }
8092a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
8093a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8094a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
8095a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8096a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
8097a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
8098a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8099a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
8100a93c429eSMatthew G. Knepley 
8101a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
8102a93c429eSMatthew G. Knepley       if (gdof > 0) {
8103a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
8104a93c429eSMatthew G. Knepley 
8105a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
8106a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8107a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
8108a93c429eSMatthew G. Knepley 
8109a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
8110a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
8111a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
8112a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
8113a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
8114a93c429eSMatthew G. Knepley           }
8115a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
8116a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
8117a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8118a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
8119a93c429eSMatthew G. Knepley           }
8120a93c429eSMatthew G. Knepley         }
8121a93c429eSMatthew G. Knepley       }
8122a93c429eSMatthew G. Knepley     }
8123a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
8124a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
8125a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
8126a93c429eSMatthew G. Knepley     if (bs > 1) {
8127a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
8128a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
8129a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
8130a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
8131a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8132a93c429eSMatthew G. Knepley         }
8133a93c429eSMatthew G. Knepley       }
8134a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
8135a93c429eSMatthew G. Knepley     }
8136a93c429eSMatthew G. Knepley     /* Attach nullspace */
8137a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
8138a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8139a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
8140a93c429eSMatthew G. Knepley     }
8141a93c429eSMatthew G. Knepley     if (f < Nf) {
8142a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
8143a93c429eSMatthew G. Knepley 
8144a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
8145a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
8146a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
8147a93c429eSMatthew G. Knepley     }
8148a93c429eSMatthew G. Knepley   }
8149a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
8150a93c429eSMatthew G. Knepley }
8151c0f0dcc3SMatthew G. Knepley 
8152c0f0dcc3SMatthew G. Knepley /*@
8153c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
8154c0f0dcc3SMatthew G. Knepley 
8155c0f0dcc3SMatthew G. Knepley   Input Parameter:
8156c0f0dcc3SMatthew G. Knepley - dm - The DM
8157c0f0dcc3SMatthew G. Knepley 
8158c0f0dcc3SMatthew G. Knepley   Level: developer
8159c0f0dcc3SMatthew G. Knepley 
8160c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8161c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
8162c0f0dcc3SMatthew G. Knepley 
8163c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
8164c0f0dcc3SMatthew G. Knepley @*/
8165c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8166c0f0dcc3SMatthew G. Knepley {
8167c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
8168c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
8169c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
8170c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
8171c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
8172c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
8173c0f0dcc3SMatthew G. Knepley   const char        *name;
8174c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
8175c0f0dcc3SMatthew G. Knepley 
8176c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8177c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8178c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
8179c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
8180c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8181c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
8182c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
8183c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
8184c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
8185c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
8186c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
8187c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
8188c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
8189*fae64647SBarry 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);
8190c0f0dcc3SMatthew G. Knepley #else
8191c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8192c0f0dcc3SMatthew G. Knepley #endif
8193c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8194c0f0dcc3SMatthew G. Knepley }
8195