xref: /petsc/src/dm/impls/plex/plex.c (revision 412e9a14abbdcfab8bb1cbfb40875fcde8c4ce26)
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 /*@
16*412e9a14SMatthew G. Knepley   DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells
17e5337592SStefano Zampini 
18*412e9a14SMatthew G. Knepley   Input Parameter:
19*412e9a14SMatthew G. Knepley + dm     - The DMPlex object
20*412e9a14SMatthew G. Knepley - height - The cell height in the Plex, 0 is the default
21e5337592SStefano Zampini 
22e5337592SStefano Zampini   Output Parameters:
23*412e9a14SMatthew G. Knepley + cStart - The first "normal" cell
24*412e9a14SMatthew G. Knepley - cEnd   - The upper bound on "normal"" cells
25e5337592SStefano Zampini 
26*412e9a14SMatthew G. Knepley   Note: This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.
27e5337592SStefano Zampini 
28*412e9a14SMatthew G. Knepley   Level: developer
29e5337592SStefano Zampini 
30*412e9a14SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum()
31e5337592SStefano Zampini @*/
32*412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
33e5337592SStefano Zampini {
34*412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
35*412e9a14SMatthew G. Knepley   PetscInt       cS, cE, c;
36e5337592SStefano Zampini   PetscErrorCode ierr;
37e5337592SStefano Zampini 
38e5337592SStefano Zampini   PetscFunctionBegin;
39*412e9a14SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);CHKERRQ(ierr);
40*412e9a14SMatthew G. Knepley   for (c = cS; c < cE; ++c) {
41*412e9a14SMatthew G. Knepley     DMPolytopeType cct;
42*412e9a14SMatthew G. Knepley 
43*412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &cct);CHKERRQ(ierr);
44*412e9a14SMatthew G. Knepley     if ((PetscInt) cct < 0) break;
45*412e9a14SMatthew G. Knepley     switch (cct) {
46ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_POINT:
47ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_SEGMENT:
48ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TRIANGLE:
49ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_QUADRILATERAL:
50ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TETRAHEDRON:
51ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_HEXAHEDRON:
52*412e9a14SMatthew G. Knepley         ct = cct;
53e5337592SStefano Zampini         break;
54*412e9a14SMatthew G. Knepley       default: break;
55e5337592SStefano Zampini     }
56*412e9a14SMatthew G. Knepley     if (ct != DM_POLYTOPE_UNKNOWN) break;
57e5337592SStefano Zampini   }
58*412e9a14SMatthew G. Knepley   if (ct != DM_POLYTOPE_UNKNOWN) {
59*412e9a14SMatthew G. Knepley     DMLabel ctLabel;
60*412e9a14SMatthew G. Knepley 
61*412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
62*412e9a14SMatthew G. Knepley     ierr = DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);CHKERRQ(ierr);
63e5337592SStefano Zampini   }
64*412e9a14SMatthew G. Knepley   if (cStart) *cStart = cS;
65*412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = cE;
66e5337592SStefano Zampini   PetscFunctionReturn(0);
67e5337592SStefano Zampini }
68e5337592SStefano Zampini 
697afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
707e42fee7SMatthew G. Knepley {
71*412e9a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
72a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
737e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
747e42fee7SMatthew G. Knepley 
757e42fee7SMatthew G. Knepley   PetscFunctionBegin;
76e630c359SToby Isaac   *ft  = PETSC_VTK_INVALID;
77f094498dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
787e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
79*412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
807e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
817e42fee7SMatthew G. Knepley   if (field >= 0) {
82a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
83a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
847e42fee7SMatthew G. Knepley   } else {
85a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
86a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
877e42fee7SMatthew G. Knepley   }
885483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
89a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
907e42fee7SMatthew G. Knepley     *sStart = vStart;
917e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
92f094498dSMatthew G. Knepley     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
937e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_POINT_FIELD;
94a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
957e42fee7SMatthew G. Knepley     *sStart = cStart;
967e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
97f094498dSMatthew G. Knepley     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
987e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_CELL_FIELD;
99e630c359SToby Isaac   } else {
100e630c359SToby Isaac     if (field >= 0) {
101e630c359SToby Isaac       const char *fieldname;
102e630c359SToby Isaac 
103e630c359SToby Isaac       ierr = PetscSectionGetFieldName(section, field, &fieldname);CHKERRQ(ierr);
104e630c359SToby Isaac       ierr = PetscInfo2((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);CHKERRQ(ierr);
105e630c359SToby Isaac     } else {
106e630c359SToby Isaac       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output typp of section\"%s\"\n");CHKERRQ(ierr);
107e630c359SToby Isaac     }
108e630c359SToby Isaac   }
1097e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1107e42fee7SMatthew G. Knepley }
1117e42fee7SMatthew G. Knepley 
1127cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
113e412dcbdSMatthew G. Knepley {
114e412dcbdSMatthew G. Knepley   DM                 dm;
115d1df6f1dSMatthew G. Knepley   PetscSection       s;
116e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
117e412dcbdSMatthew G. Knepley   DM                 cdm;
118e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
119e412dcbdSMatthew G. Knepley   Vec                coordinates;
120e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
121e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
122339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
123339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
124d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
125e412dcbdSMatthew G. Knepley   const char        *name;
126339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
127e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
128e412dcbdSMatthew G. Knepley 
129e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
130d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
131d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
132d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
133d1df6f1dSMatthew G. Knepley 
134e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
135e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1368135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
13792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
138d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
139e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
140e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
14192fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
142e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
143e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
144e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
145e412dcbdSMatthew G. Knepley 
146e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
147339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
148e412dcbdSMatthew G. Knepley 
149e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
150e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
151e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1520c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1530c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
154e412dcbdSMatthew G. Knepley   }
155e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
156e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley 
158d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
159d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
160d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
161d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
162d1df6f1dSMatthew G. Knepley     IS   fis;
163d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
164d1df6f1dSMatthew G. Knepley     const char *fname;
165d1df6f1dSMatthew G. Knepley 
166d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
167d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
168d1df6f1dSMatthew G. Knepley 
169a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
170d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
171d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
172d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
173d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
174a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
175a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
176d1df6f1dSMatthew G. Knepley     }
177d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
178d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
179d1df6f1dSMatthew G. Knepley 
180d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
181d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
182d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
183d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
184d1df6f1dSMatthew G. Knepley 
185d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
186d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
187339e3443SMatthew G. Knepley       if (!flg) {
188d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
189d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
190d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
191339e3443SMatthew G. Knepley       }
192e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
193339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
194d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
195e412dcbdSMatthew G. Knepley 
196d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
197e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
19899a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
199e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
200e412dcbdSMatthew G. Knepley 
201d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
202339e3443SMatthew G. Knepley         if (a) {
203d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
204339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
205339e3443SMatthew G. Knepley         } else {
206339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
207339e3443SMatthew G. Knepley           PetscInt     numVals, va;
208339e3443SMatthew G. Knepley 
209d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
210d1df6f1dSMatthew G. Knepley           if (numVals % Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %D does not divide the number of values in the closure %D", Nc, numVals);
211d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
212d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
213d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
214d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
215339e3443SMatthew G. Knepley             break;
216d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
217d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
218d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/(Nc*2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp + numVals/(Nc*2)]), vbound[0], vbound[1]);
219d1df6f1dSMatthew G. Knepley             break;
220d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
221339e3443SMatthew G. Knepley           }
222d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
223339e3443SMatthew G. Knepley         }
224e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
225e412dcbdSMatthew G. Knepley         switch (numCoords) {
226e412dcbdSMatthew G. Knepley         case 6:
227339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
228e412dcbdSMatthew G. Knepley           break;
229e412dcbdSMatthew G. Knepley         case 8:
230339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
231339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);CHKERRQ(ierr);
232e412dcbdSMatthew G. Knepley           break;
233e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
234e412dcbdSMatthew G. Knepley         }
235e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
236e412dcbdSMatthew G. Knepley       }
237d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
238e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
239e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
240e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
241d1df6f1dSMatthew G. Knepley     }
242d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
243d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
244d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
245d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
246d1df6f1dSMatthew G. Knepley     }
247d1df6f1dSMatthew G. Knepley   }
248e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
249e412dcbdSMatthew G. Knepley }
250e412dcbdSMatthew G. Knepley 
251684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
252684b87d9SLisandro Dalcin {
253684b87d9SLisandro Dalcin   DM                      dm;
254684b87d9SLisandro Dalcin   Vec                     locv;
255684b87d9SLisandro Dalcin   const char              *name;
256684b87d9SLisandro Dalcin   PetscSection            section;
257684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
258e630c359SToby Isaac   PetscInt                numFields;
259684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
260684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
261684b87d9SLisandro Dalcin 
262684b87d9SLisandro Dalcin   PetscFunctionBegin;
263684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
264684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
265684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
266684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
267684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
26892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
269e630c359SToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
270e630c359SToby Isaac   if (!numFields) {
271684b87d9SLisandro Dalcin     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
272e630c359SToby Isaac     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
273e630c359SToby Isaac   } else {
274e630c359SToby Isaac     PetscInt f;
275e630c359SToby Isaac 
276e630c359SToby Isaac     for (f = 0; f < numFields; f++) {
277e630c359SToby Isaac       ierr = DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);CHKERRQ(ierr);
278e630c359SToby Isaac       if (ft == PETSC_VTK_INVALID) continue;
279e630c359SToby Isaac       ierr = PetscObjectReference((PetscObject)locv);CHKERRQ(ierr);
280e630c359SToby Isaac       ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
281e630c359SToby Isaac     }
282e630c359SToby Isaac     ierr = VecDestroy(&locv);CHKERRQ(ierr);
283e630c359SToby Isaac   }
284684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
285684b87d9SLisandro Dalcin }
286684b87d9SLisandro Dalcin 
287552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
288552f7358SJed Brown {
289552f7358SJed Brown   DM             dm;
290684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
291552f7358SJed Brown   PetscErrorCode ierr;
292552f7358SJed Brown 
293552f7358SJed Brown   PetscFunctionBegin;
294552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
29582f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
296552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
297b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
298f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
2998135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
300684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
301684b87d9SLisandro Dalcin     PetscInt    i,numFields;
302684b87d9SLisandro Dalcin     PetscObject fe;
303ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
304684b87d9SLisandro Dalcin     Vec         locv = v;
305684b87d9SLisandro Dalcin     const char  *name;
306684b87d9SLisandro Dalcin     PetscInt    step;
307684b87d9SLisandro Dalcin     PetscReal   time;
308ef31f671SMatthew G. Knepley 
309ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
310684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
31144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &fe);CHKERRQ(ierr);
312684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
313ef31f671SMatthew G. Knepley     }
314684b87d9SLisandro Dalcin     if (fem) {
315684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
316684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
317684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
318684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
319684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
320684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
321ef31f671SMatthew G. Knepley     }
322552f7358SJed Brown     if (isvtk) {
323684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
324b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
325b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
326684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
327b136c2c9SMatthew G. Knepley #else
328b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
329b136c2c9SMatthew G. Knepley #endif
330f13a32a3SMatthew G. Knepley     } else if (isdraw) {
331684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
332684b87d9SLisandro Dalcin     } else if (isglvis) {
333684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
334684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
335684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
336684b87d9SLisandro Dalcin     }
337684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
338552f7358SJed Brown   } else {
339684b87d9SLisandro Dalcin     PetscBool isseq;
340684b87d9SLisandro Dalcin 
341684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
34255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
34355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
344552f7358SJed Brown   }
345552f7358SJed Brown   PetscFunctionReturn(0);
346552f7358SJed Brown }
347552f7358SJed Brown 
348552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
349552f7358SJed Brown {
350552f7358SJed Brown   DM             dm;
351684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
352552f7358SJed Brown   PetscErrorCode ierr;
353552f7358SJed Brown 
354552f7358SJed Brown   PetscFunctionBegin;
355552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
35682f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
357552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
35833c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
359e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3608135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
361684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
362552f7358SJed Brown     Vec         locv;
363552f7358SJed Brown     const char *name;
364552f7358SJed Brown 
365c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
366552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
367552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
368552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
369552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
370552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
371c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
372b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
373b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
37439d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
375b136c2c9SMatthew G. Knepley #else
376b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
377b136c2c9SMatthew G. Knepley #endif
378552f7358SJed Brown   } else {
379684b87d9SLisandro Dalcin     PetscBool isseq;
380684b87d9SLisandro Dalcin 
381684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
38255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
38355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
384552f7358SJed Brown   }
385552f7358SJed Brown   PetscFunctionReturn(0);
386552f7358SJed Brown }
387552f7358SJed Brown 
388d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
389d930f514SMatthew G. Knepley {
390d930f514SMatthew G. Knepley   DM                dm;
391d930f514SMatthew G. Knepley   MPI_Comm          comm;
392d930f514SMatthew G. Knepley   PetscViewerFormat format;
393d930f514SMatthew G. Knepley   Vec               v;
394d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
395d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
396d930f514SMatthew G. Knepley 
397d930f514SMatthew G. Knepley   PetscFunctionBegin;
398d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
399d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4002c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
401d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
402d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
403d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
404d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
405a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
406a8ad634aSStefano Zampini     /* this need a better fix */
407a8ad634aSStefano Zampini     if (dm->useNatural) {
408a8ad634aSStefano Zampini       if (dm->sfNatural) {
409d930f514SMatthew G. Knepley         const char *vecname;
410d930f514SMatthew G. Knepley         PetscInt    n, nroots;
411d930f514SMatthew G. Knepley 
412ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
413d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley         if (n == nroots) {
415d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
417d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
418d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
419d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
421d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
422a8ad634aSStefano Zampini     } else v = originalv;
423a8ad634aSStefano Zampini   } else v = originalv;
424a8ad634aSStefano Zampini 
425d930f514SMatthew G. Knepley   if (ishdf5) {
426d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
42739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley #else
429d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
430d930f514SMatthew G. Knepley #endif
431d930f514SMatthew G. Knepley   } else if (isvtk) {
432d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
433d930f514SMatthew G. Knepley   } else {
434d930f514SMatthew G. Knepley     PetscBool isseq;
435d930f514SMatthew G. Knepley 
436d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
437d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
438d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
439d930f514SMatthew G. Knepley   }
440a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
441d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
442d930f514SMatthew G. Knepley }
443d930f514SMatthew G. Knepley 
4442c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4452c40f234SMatthew G. Knepley {
4462c40f234SMatthew G. Knepley   DM             dm;
4472c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4482c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4492c40f234SMatthew G. Knepley 
4502c40f234SMatthew G. Knepley   PetscFunctionBegin;
4512c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4522c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4532c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4542c40f234SMatthew G. Knepley   if (ishdf5) {
4552c40f234SMatthew G. Knepley     DM          dmBC;
4562c40f234SMatthew G. Knepley     Vec         gv;
4572c40f234SMatthew G. Knepley     const char *name;
4582c40f234SMatthew G. Knepley 
4592c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4602c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4612c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4632c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4652c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4672c40f234SMatthew G. Knepley   } else {
4682c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4692c40f234SMatthew G. Knepley   }
4702c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4712c40f234SMatthew G. Knepley }
4722c40f234SMatthew G. Knepley 
4732c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4742c40f234SMatthew G. Knepley {
4752c40f234SMatthew G. Knepley   DM             dm;
4762c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4772c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4782c40f234SMatthew G. Knepley 
4792c40f234SMatthew G. Knepley   PetscFunctionBegin;
4802c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4812c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4822c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4832c40f234SMatthew G. Knepley   if (ishdf5) {
484878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
48539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
486b136c2c9SMatthew G. Knepley #else
487b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
488878b459fSMatthew G. Knepley #endif
4892c40f234SMatthew G. Knepley   } else {
4902c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
491552f7358SJed Brown   }
492552f7358SJed Brown   PetscFunctionReturn(0);
493552f7358SJed Brown }
494552f7358SJed Brown 
495d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
496d930f514SMatthew G. Knepley {
497d930f514SMatthew G. Knepley   DM                dm;
498d930f514SMatthew G. Knepley   PetscViewerFormat format;
499d930f514SMatthew G. Knepley   PetscBool         ishdf5;
500d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
501d930f514SMatthew G. Knepley 
502d930f514SMatthew G. Knepley   PetscFunctionBegin;
503d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
504d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
505d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
506d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
507d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
508a8ad634aSStefano Zampini     if (dm->useNatural) {
509d930f514SMatthew G. Knepley       if (dm->sfNatural) {
510d930f514SMatthew G. Knepley         if (ishdf5) {
511d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
512d930f514SMatthew G. Knepley           Vec         v;
513d930f514SMatthew G. Knepley           const char *vecname;
514d930f514SMatthew G. Knepley 
515d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
51839d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
519d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
520d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
521d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
522d930f514SMatthew G. Knepley #else
523d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
524d930f514SMatthew G. Knepley #endif
525d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
526d930f514SMatthew G. Knepley       }
527a8ad634aSStefano Zampini     } else {
528a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
529a8ad634aSStefano Zampini     }
530d930f514SMatthew G. Knepley   }
531d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
532d930f514SMatthew G. Knepley }
533d930f514SMatthew G. Knepley 
5347cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
535731e8ddeSMatthew G. Knepley {
536731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
537731e8ddeSMatthew G. Knepley   Vec                coordinates;
538ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
539731e8ddeSMatthew G. Knepley   const char        *name[4];
540731e8ddeSMatthew G. Knepley   const PetscScalar *a;
541731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
542731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
543731e8ddeSMatthew G. Knepley 
544731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
545731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
546731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
547731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
548731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
549ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
550731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
551731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
554731e8ddeSMatthew G. Knepley   name[1]     = "edge";
555731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
556731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
557731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
558731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
559ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
560731e8ddeSMatthew G. Knepley 
561ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
562ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
564731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
565731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
566731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
567731e8ddeSMatthew G. Knepley 
568731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
569731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
570731e8ddeSMatthew G. Knepley       if (!dof) continue;
571731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
572731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
573f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
575731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
577731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
578087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
579731e8ddeSMatthew G. Knepley         }
580731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
581731e8ddeSMatthew G. Knepley       }
582731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley     }
584731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
586731e8ddeSMatthew G. Knepley   }
587731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
588731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
589731e8ddeSMatthew G. Knepley }
590731e8ddeSMatthew G. Knepley 
5917cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
592552f7358SJed Brown {
593552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
594552f7358SJed Brown   DM                cdm;
595*412e9a14SMatthew G. Knepley   DMLabel           markers, celltypes;
596552f7358SJed Brown   PetscSection      coordSection;
597552f7358SJed Brown   Vec               coordinates;
598552f7358SJed Brown   PetscViewerFormat format;
599552f7358SJed Brown   PetscErrorCode    ierr;
600552f7358SJed Brown 
601552f7358SJed Brown   PetscFunctionBegin;
602552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
60392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
604552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
605552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
606552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
607552f7358SJed Brown     const char *name;
608f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
609552f7358SJed Brown     PetscInt    pStart, pEnd, p;
610552f7358SJed Brown     PetscMPIInt rank, size;
611552f7358SJed Brown 
61282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
61382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
614552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
615552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
616552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
617f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
618f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
619f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
620f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
621f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
622e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6234d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
624e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
625552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
626552f7358SJed Brown       PetscInt dof, off, s;
627552f7358SJed Brown 
628552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
629552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
630552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
631e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
632552f7358SJed Brown       }
633552f7358SJed Brown     }
634552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
635e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
636e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
637552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
638552f7358SJed Brown       PetscInt dof, off, c;
639552f7358SJed Brown 
640552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
641552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
642552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
643e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
644552f7358SJed Brown       }
645552f7358SJed Brown     }
646552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6474d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6483d2e540fSStefano Zampini     if (coordSection && coordinates) {
64980180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6503d2e540fSStefano Zampini     }
651c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6527422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
653*412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &celltypes);CHKERRQ(ierr);
654*412e9a14SMatthew G. Knepley     if (celltypes) {ierr = DMLabelView(celltypes, viewer);CHKERRQ(ierr);}
655552f7358SJed Brown     if (size > 1) {
656552f7358SJed Brown       PetscSF sf;
657552f7358SJed Brown 
658552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
659552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
660552f7358SJed Brown     }
661552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
662552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6630588280cSMatthew G. Knepley     const char  *name, *color;
6640588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6650588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
666fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
667552f7358SJed Brown     PetscReal    scale         = 2.0;
66878081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6690588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6700588280cSMatthew G. Knepley     double       tcoords[3];
671552f7358SJed Brown     PetscScalar *coords;
6720588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
673552f7358SJed Brown     PetscMPIInt  rank, size;
6740588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
675fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
676fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
677fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
678552f7358SJed Brown 
6790588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
680552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
681c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6820588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6830588280cSMatthew G. Knepley     numColors  = 10;
6840588280cSMatthew G. Knepley     numLColors = 10;
6850588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
686c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
68778081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
688c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
689c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6900588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
691c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6920588280cSMatthew G. Knepley     if (!useColors) {
6930588280cSMatthew G. Knepley       numColors = 3;
6940588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6950588280cSMatthew G. Knepley     }
696c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6970588280cSMatthew G. Knepley     if (!useColors) {
6980588280cSMatthew G. Knepley       numLColors = 4;
6990588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7000588280cSMatthew G. Knepley     }
701fe1cc32dSStefano Zampini     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, PETSC_MAX_PATH_LEN, &lflg);CHKERRQ(ierr);
702202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
703202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
704202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
705202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
706fe1cc32dSStefano Zampini 
707fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
708fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
709fe1cc32dSStefano Zampini     if (lflg) {
710fe1cc32dSStefano Zampini       DMLabel lbl;
711fe1cc32dSStefano Zampini 
712fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
713fe1cc32dSStefano Zampini       if (lbl) {
714fe1cc32dSStefano Zampini         PetscInt val, defval;
715fe1cc32dSStefano Zampini 
716fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
717fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
718fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
719fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
720fe1cc32dSStefano Zampini           PetscInt  closureSize;
721fe1cc32dSStefano Zampini 
722fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
723fe1cc32dSStefano Zampini           if (val == defval) continue;
724fe1cc32dSStefano Zampini 
725fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
726fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
727fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
728fe1cc32dSStefano Zampini           }
729fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
730fe1cc32dSStefano Zampini         }
731fe1cc32dSStefano Zampini       }
732fe1cc32dSStefano Zampini     }
733fe1cc32dSStefano Zampini 
73482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
73582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
736552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
737770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7380588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
739552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
740552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
741552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7420588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7430588280cSMatthew G. Knepley     if (size > 1) {
744f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
745770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
746770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
747770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
748770b213bSMatthew G Knepley         } else if (p > 0) {
749770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
750770b213bSMatthew G Knepley         }
751770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
752770b213bSMatthew G Knepley       }
7530588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7540588280cSMatthew G. Knepley     }
755087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
756fe1cc32dSStefano Zampini 
757552f7358SJed Brown     /* Plot vertices */
758552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
759552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7604d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
761552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
762552f7358SJed Brown       PetscInt  off, dof, d;
7630588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
764552f7358SJed Brown 
765fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
766552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
767552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7680588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
769f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7700588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7710588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
772c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7730588280cSMatthew G. Knepley       }
7740588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7750588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
776552f7358SJed Brown       for (d = 0; d < dof; ++d) {
777552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
778087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
779552f7358SJed Brown       }
7800588280cSMatthew G. Knepley       color = colors[rank%numColors];
7810588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7820588280cSMatthew G. Knepley         PetscInt val;
783c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7840588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7850588280cSMatthew G. Knepley       }
7860588280cSMatthew G. Knepley       if (useNumbers) {
787e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7880588280cSMatthew G. Knepley       } else {
789e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7900588280cSMatthew G. Knepley       }
791552f7358SJed Brown     }
792552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
793552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
794846a3e8bSMatthew G. Knepley     /* Plot cells */
795846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
79678081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
797846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
798846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
799846a3e8bSMatthew G. Knepley         const PetscInt *cone;
800846a3e8bSMatthew G. Knepley 
801fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
802846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
803846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
804846a3e8bSMatthew G. Knepley           PetscInt val;
805846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
806846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
807846a3e8bSMatthew G. Knepley         }
808846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
809846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
810846a3e8bSMatthew G. Knepley       }
811846a3e8bSMatthew G. Knepley     } else {
812846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
813846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
814846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
815846a3e8bSMatthew G. Knepley 
816fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
817846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
818846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
819846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
820846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
821846a3e8bSMatthew G. Knepley 
822846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
823846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
824846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
825846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
826846a3e8bSMatthew G. Knepley         }
827846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
828846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
829846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
830846a3e8bSMatthew G. Knepley       }
831846a3e8bSMatthew G. Knepley     }
832846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
833846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
834846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
835846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
836846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
837846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
838846a3e8bSMatthew G. Knepley 
839fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
840846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
841846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
842846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
843846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
844846a3e8bSMatthew G. Knepley         PetscInt       off;
845846a3e8bSMatthew G. Knepley 
846846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
847846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
848846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
849846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
850846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
851846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
852846a3e8bSMatthew G. Knepley         }
853846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
854846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
855846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
856846a3e8bSMatthew G. Knepley         ++n;
857846a3e8bSMatthew G. Knepley       }
858846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
859846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
860846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
861846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
862087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
863846a3e8bSMatthew G. Knepley       }
864846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
865846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
866846a3e8bSMatthew G. Knepley         PetscInt val;
867846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
868846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
869846a3e8bSMatthew G. Knepley       }
870846a3e8bSMatthew G. Knepley       if (useNumbers) {
871846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
872846a3e8bSMatthew G. Knepley       } else {
873846a3e8bSMatthew 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);
874846a3e8bSMatthew G. Knepley       }
875846a3e8bSMatthew G. Knepley     }
876846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
877552f7358SJed Brown     /* Plot edges */
878202fd40aSStefano Zampini     if (plotEdges) {
879552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
880552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
881552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
882552f7358SJed Brown         const PetscInt *cone;
883552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
884552f7358SJed Brown 
885fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
886552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
887f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
888552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
889552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
890552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
891552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
892552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
893552f7358SJed Brown         for (d = 0; d < dof; ++d) {
894202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
895c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
896552f7358SJed Brown         }
8970588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
8980588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
8990588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9000588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
901f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9020588280cSMatthew G. Knepley         }
9030588280cSMatthew G. Knepley         color = colors[rank%numColors];
9040588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9050588280cSMatthew G. Knepley           PetscInt val;
906c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9070750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9080588280cSMatthew G. Knepley         }
909e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
910552f7358SJed Brown       }
911552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
912552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
913552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9140588280cSMatthew G. Knepley     }
915552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9164d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9170588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
918770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9190588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9200588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9210588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9220588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
923fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9240f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9250f7d6e4aSStefano Zampini     Vec                    cown,acown;
9260f7d6e4aSStefano Zampini     VecScatter             sct;
9270f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9280f7d6e4aSStefano Zampini     IS                     gid,acis;
9290f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9300f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9310f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9320f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9330f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9340f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9350f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9360f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
9370f7d6e4aSStefano Zampini 
9380f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
9390f7d6e4aSStefano Zampini     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
940b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
9410f7d6e4aSStefano Zampini     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRQ(ierr);
9420f7d6e4aSStefano Zampini #endif
9430f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
9440f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(comm,&ggroup);CHKERRQ(ierr);
9450f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRQ(ierr);
9460f7d6e4aSStefano Zampini       d1   = 0;
9470f7d6e4aSStefano Zampini       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRQ(ierr);
9480f7d6e4aSStefano Zampini       nid  = d2;
9490f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ggroup);CHKERRQ(ierr);
9500f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ngroup);CHKERRQ(ierr);
9510f7d6e4aSStefano Zampini       ierr = MPI_Comm_free(&ncomm);CHKERRQ(ierr);
9520f7d6e4aSStefano Zampini     } else nid = 0.0;
9530f7d6e4aSStefano Zampini 
9540f7d6e4aSStefano Zampini     /* Get connectivity */
9550f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
9560f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
9570f7d6e4aSStefano Zampini 
9580f7d6e4aSStefano Zampini     /* filter overlapped local cells */
9590f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
9600f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
9610f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
9620f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
9630f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
9640f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
9650f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
9660f7d6e4aSStefano Zampini     }
9670f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
9680f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
9690f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9700f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
9710f7d6e4aSStefano Zampini 
9720f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
9730f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
9740f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
9750f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
9760f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
9770f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
9780f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
9790f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
9800f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9810f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9820f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
9830f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
9840f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
9850f7d6e4aSStefano Zampini 
9860f7d6e4aSStefano Zampini     /* compute edgeCut */
9870f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
9880f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
9890f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
9900f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
9910f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9920f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
9930f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
9940f7d6e4aSStefano Zampini       PetscInt totl;
9950f7d6e4aSStefano Zampini 
9960f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
9970f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
9980f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
9990f7d6e4aSStefano Zampini         if (work[i] < 0) {
10000f7d6e4aSStefano Zampini           ect  += 1;
10010f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10020f7d6e4aSStefano Zampini         }
10030f7d6e4aSStefano Zampini       }
10040f7d6e4aSStefano Zampini     }
10050f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10060f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10070f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10080f7d6e4aSStefano Zampini     lm[1] = -numVertices;
10090f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRQ(ierr);
10100f7d6e4aSStefano 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);
10110f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10120f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10130f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
10140f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRQ(ierr);
10150f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1016b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
10170f7d6e4aSStefano 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);
10180f7d6e4aSStefano Zampini #else
10190f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10200f7d6e4aSStefano Zampini #endif
10210f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10220f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10230f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10240f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1025552f7358SJed Brown   } else {
1026*412e9a14SMatthew G. Knepley     const char    *name;
1027d80ece95SMatthew G. Knepley     PetscInt      *sizes, *hybsizes, *ghostsizes;
1028*412e9a14SMatthew G. Knepley     PetscInt       locDepth, depth, cellHeight, dim, d;
1029d80ece95SMatthew G. Knepley     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1030a57dd577SMatthew G Knepley     PetscInt       numLabels, l;
1031*412e9a14SMatthew G. Knepley     DMPolytopeType ct0;
1032*412e9a14SMatthew G. Knepley     MPI_Comm       comm;
1033*412e9a14SMatthew G. Knepley     PetscMPIInt    size, rank;
1034552f7358SJed Brown 
103582f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
1036552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1037*412e9a14SMatthew G. Knepley     ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1038c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1039f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10405f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1041f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1042f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1043f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1044552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1045b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
1046d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1047d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1048d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1049*412e9a14SMatthew G. Knepley     for (d = 0; d <= depth; d++) {
1050*412e9a14SMatthew G. Knepley       PetscInt Nc[2] = {0, 0}, ict;
1051*412e9a14SMatthew G. Knepley 
1052552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1053*412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, pStart, &ct0);CHKERRQ(ierr);
1054*412e9a14SMatthew G. Knepley       ict  = ct0;
1055*412e9a14SMatthew G. Knepley       ierr = MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1056*412e9a14SMatthew G. Knepley       ct0  = (DMPolytopeType) ict;
1057*412e9a14SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1058*412e9a14SMatthew G. Knepley         DMPolytopeType ct;
1059*412e9a14SMatthew G. Knepley 
1060*412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
1061*412e9a14SMatthew G. Knepley         if (ct == ct0) ++Nc[0];
1062*412e9a14SMatthew G. Knepley         else           ++Nc[1];
1063*412e9a14SMatthew G. Knepley       }
1064*412e9a14SMatthew G. Knepley       ierr = MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1065*412e9a14SMatthew G. Knepley       ierr = MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1066*412e9a14SMatthew G. Knepley       if (d == depth) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);}
1067*412e9a14SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1068834065abSMatthew G. Knepley       for (p = 0; p < size; ++p) {
1069cbb7f117SMark Adams         if (!rank) {
1070*412e9a14SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);CHKERRQ(ierr);
1071*412e9a14SMatthew G. Knepley           if (hybsizes[p]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);CHKERRQ(ierr);}
1072*412e9a14SMatthew G. Knepley           if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1073834065abSMatthew G. Knepley         }
1074cbb7f117SMark Adams       }
1075552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1076552f7358SJed Brown     }
1077d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
1078c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1079a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1080a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1081a57dd577SMatthew G Knepley       DMLabel         label;
1082a57dd577SMatthew G Knepley       const char     *name;
1083a57dd577SMatthew G Knepley       IS              valueIS;
1084a57dd577SMatthew G Knepley       const PetscInt *values;
1085a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1086a57dd577SMatthew G Knepley 
1087c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1088c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1089a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1090d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1091a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1092a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1093120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1094a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1095a57dd577SMatthew G Knepley         PetscInt size;
1096a57dd577SMatthew G Knepley 
1097a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1098a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1099d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1100a57dd577SMatthew G Knepley       }
1101a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1102120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1103a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11044d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1105a57dd577SMatthew G Knepley     }
110634aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
110734aa8a36SMatthew G. Knepley     if (dm->Nf) {
110834aa8a36SMatthew G. Knepley       PetscInt f;
110934aa8a36SMatthew G. Knepley 
111034aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
111134aa8a36SMatthew G. Knepley         const char *name;
111234aa8a36SMatthew G. Knepley 
111334aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
111434aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
111534aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
111634aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
111734aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
111834aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1119ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
112034aa8a36SMatthew G. Knepley         } else {
112134aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
112234aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
112334aa8a36SMatthew G. Knepley         }
112434aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
112534aa8a36SMatthew G. Knepley       }
112634aa8a36SMatthew G. Knepley     }
1127a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11288e7ff633SMatthew G. Knepley     if (cdm) {
11298e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11308e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11318e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11328e7ff633SMatthew G. Knepley     }
1133552f7358SJed Brown   }
1134552f7358SJed Brown   PetscFunctionReturn(0);
1135552f7358SJed Brown }
1136552f7358SJed Brown 
11377cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1138e412dcbdSMatthew G. Knepley {
1139e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1140e412dcbdSMatthew G. Knepley   DM                 cdm;
1141e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1142e412dcbdSMatthew G. Knepley   Vec                coordinates;
1143e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
114429494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1145e412dcbdSMatthew G. Knepley   PetscBool          isnull;
1146e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1147cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
1148e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1149e412dcbdSMatthew G. Knepley 
1150e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1151e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1152e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1153e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
115492fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1155e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1156e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1157e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1158e412dcbdSMatthew G. Knepley 
1159e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1160e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1161e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1162e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1163e412dcbdSMatthew G. Knepley 
1164e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1165e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1166e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
11670c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
11680c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1169e412dcbdSMatthew G. Knepley   }
1170e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
117129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
117229494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
117329494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1174e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1175e412dcbdSMatthew G. Knepley 
1176cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1177cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1178cf3064d3SMatthew G. Knepley     PetscScalar   *coords = NULL;
1179ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
1180ba2698f1SMatthew G. Knepley     PetscInt       numCoords;
1181cf3064d3SMatthew G. Knepley 
1182ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
1183cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1184ba2698f1SMatthew G. Knepley     switch (ct) {
1185ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
1186cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1187cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1188cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1189cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1190cf3064d3SMatthew G. Knepley       break;
1191ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
1192a46c2b5aSMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1193cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1194cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1195a46c2b5aSMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1196a46c2b5aSMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1197a46c2b5aSMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1198cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1199cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1200cf3064d3SMatthew G. Knepley       break;
1201ba2698f1SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1202cf3064d3SMatthew G. Knepley     }
1203cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1204cf3064d3SMatthew G. Knepley   }
1205e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1206e412dcbdSMatthew G. Knepley     PetscScalar   *coords = NULL;
1207ba2698f1SMatthew G. Knepley     DMPolytopeType ct;
1208ba2698f1SMatthew G. Knepley     PetscInt       numCoords;
1209e412dcbdSMatthew G. Knepley 
1210ba2698f1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
1211e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1212ba2698f1SMatthew G. Knepley     switch (ct) {
1213ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
12140c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12150c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12160c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1217e412dcbdSMatthew G. Knepley       break;
1218ba2698f1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
12190c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12200c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12210c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12220c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1223e412dcbdSMatthew G. Knepley       break;
1224ba2698f1SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1225e412dcbdSMatthew G. Knepley     }
1226e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1227e412dcbdSMatthew G. Knepley   }
1228e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1229e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1230e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1231e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1232e412dcbdSMatthew G. Knepley }
1233e412dcbdSMatthew G. Knepley 
12341e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
12351e50132fSMatthew G. Knepley #include <exodusII.h>
12361e50132fSMatthew G. Knepley #endif
12371e50132fSMatthew G. Knepley 
1238552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1239552f7358SJed Brown {
12401e50132fSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1241002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1242552f7358SJed Brown   PetscErrorCode ierr;
1243552f7358SJed Brown 
1244552f7358SJed Brown   PetscFunctionBegin;
1245552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1246552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1247552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);CHKERRQ(ierr);
1248fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
1249c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
1250e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
12518135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
12521e50132fSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);CHKERRQ(ierr);
1253552f7358SJed Brown   if (iascii) {
12548135c375SStefano Zampini     PetscViewerFormat format;
12558135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12568135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
12578135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12588135c375SStefano Zampini     } else {
1259552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
12608135c375SStefano Zampini     }
1261c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1262c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
126339d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1264c6ccd67eSMatthew G. Knepley #else
1265c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1266552f7358SJed Brown #endif
1267e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1268fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1269e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1270e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
12718135c375SStefano Zampini   } else if (isglvis) {
12728135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12731e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
12741e50132fSMatthew G. Knepley   } else if (isexodus) {
12751e50132fSMatthew G. Knepley     int exoid;
12761e50132fSMatthew G. Knepley     PetscInt cStart, cEnd, c;
12771e50132fSMatthew G. Knepley 
12781e50132fSMatthew G. Knepley     ierr = DMCreateLabel(dm, "Cell Sets");CHKERRQ(ierr);
12791e50132fSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
12801e50132fSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {ierr = DMSetLabelValue(dm, "Cell Sets", c, 1);CHKERRQ(ierr);}
12811e50132fSMatthew G. Knepley     ierr = PetscViewerExodusIIGetId(viewer, &exoid);CHKERRQ(ierr);
12821e50132fSMatthew G. Knepley     ierr = DMPlexView_ExodusII_Internal(dm, exoid, 1);CHKERRQ(ierr);
12831e50132fSMatthew G. Knepley #endif
128462201deeSVaclav Hapla   } else {
1285a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1286fcf6c8fdSToby Isaac   }
1287cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1288cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1289cb3ba0daSMatthew G. Knepley   if (flg) {
1290cb3ba0daSMatthew G. Knepley     Vec ranks;
1291cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1292cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1293cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1294cb3ba0daSMatthew G. Knepley   }
1295002a2709SMatthew G. Knepley   /* Optionally view a label */
1296002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1297002a2709SMatthew G. Knepley   if (flg) {
1298002a2709SMatthew G. Knepley     DMLabel label;
1299002a2709SMatthew G. Knepley     Vec     val;
1300002a2709SMatthew G. Knepley 
1301002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1302002a2709SMatthew 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);
1303002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1304002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1305002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1306002a2709SMatthew G. Knepley   }
1307552f7358SJed Brown   PetscFunctionReturn(0);
1308552f7358SJed Brown }
1309552f7358SJed Brown 
13102c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13112c40f234SMatthew G. Knepley {
1312d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13132c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13142c40f234SMatthew G. Knepley 
13152c40f234SMatthew G. Knepley   PetscFunctionBegin;
13162c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13172c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13182c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1319d4f5a9a0SVaclav Hapla   if (ishdf5) {
13202c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13219c48423bSVaclav Hapla     PetscViewerFormat format;
13229c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13239c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13249c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1325509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
132639d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1327509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
13282c40f234SMatthew G. Knepley #else
13292c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1330552f7358SJed Brown #endif
1331d4f5a9a0SVaclav Hapla   } else {
1332d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1333552f7358SJed Brown   }
1334552f7358SJed Brown   PetscFunctionReturn(0);
1335552f7358SJed Brown }
1336552f7358SJed Brown 
1337552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1338552f7358SJed Brown {
1339552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1340552f7358SJed Brown   PetscErrorCode ierr;
1341552f7358SJed Brown 
1342552f7358SJed Brown   PetscFunctionBegin;
13438135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13448135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
134528d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
13460d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1347552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1348552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1349552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1350552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1351be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1352552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1353552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1354d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1355d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
135677623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1357a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1358552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1359552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1360a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1361a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1362d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1363d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1364d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1365d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1366d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1367d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1368fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1369552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1370552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1371552f7358SJed Brown   PetscFunctionReturn(0);
1372552f7358SJed Brown }
1373552f7358SJed Brown 
1374b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1375552f7358SJed Brown {
13768d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1377acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1378552f7358SJed Brown   PetscInt               localSize;
1379837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1380552f7358SJed Brown   PetscErrorCode         ierr;
1381b412c318SBarry Smith   MatType                mtype;
13821428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1383552f7358SJed Brown 
1384552f7358SJed Brown   PetscFunctionBegin;
1385607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1386b412c318SBarry Smith   mtype = dm->mattype;
1387e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1388552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1389552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
139082f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1391552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1392552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1393552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1394acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1395acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1396552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1397552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1398552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1399552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1400552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1401552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1402552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1403837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1404552f7358SJed Brown   if (!isShell) {
1405be36d101SStefano Zampini     PetscSection subSection;
1406837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
14070be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1408fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1409552f7358SJed Brown 
141000140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1411be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1412be36d101SStefano Zampini       PetscSection section;
1413be36d101SStefano Zampini       PetscInt     size;
1414be36d101SStefano Zampini 
141592fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1416be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1417be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1418be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1419be36d101SStefano Zampini     } else {
142000140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1421be36d101SStefano Zampini     }
1422552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1423be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1424a9d99c84SMatthew G. Knepley       PetscInt bdof;
1425a9d99c84SMatthew G. Knepley 
1426552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1427fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
14281d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
14291d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
14301d17a0a3SMatthew G. Knepley       if (dof) {
14311d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1432be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1433be36d101SStefano Zampini       }
1434be36d101SStefano Zampini       if (isMatIS) {
1435be36d101SStefano Zampini         PetscInt loff,c,off;
1436be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1437be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1438be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1439552f7358SJed Brown       }
14402a28c762SMatthew G Knepley     }
14412a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14420be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14430be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14440be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14450be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14466fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
14476fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
14484fa26246SMatthew G. Knepley       PetscInt l;
14496fd5c86aSStefano Zampini 
14506fd5c86aSStefano Zampini       lsize = lsize/bs;
14516fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
14524fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1453be36d101SStefano Zampini     }
1454be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1455be36d101SStefano Zampini     if (isMatIS) {
1456be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1457be36d101SStefano Zampini     }
14581795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
14598d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1460552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1461552f7358SJed Brown   }
1462b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1463552f7358SJed Brown   PetscFunctionReturn(0);
1464552f7358SJed Brown }
1465552f7358SJed Brown 
14667cd05799SMatthew G. Knepley /*@
1467a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1468be36d101SStefano Zampini 
1469be36d101SStefano Zampini   Not collective
1470be36d101SStefano Zampini 
1471be36d101SStefano Zampini   Input Parameter:
1472be36d101SStefano Zampini . mesh - The DMPlex
1473be36d101SStefano Zampini 
1474be36d101SStefano Zampini   Output Parameters:
1475be36d101SStefano Zampini . subsection - The subdomain section
1476be36d101SStefano Zampini 
1477be36d101SStefano Zampini   Level: developer
1478be36d101SStefano Zampini 
1479be36d101SStefano Zampini .seealso:
14807cd05799SMatthew G. Knepley @*/
1481be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1482be36d101SStefano Zampini {
1483be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1484be36d101SStefano Zampini   PetscErrorCode ierr;
1485be36d101SStefano Zampini 
1486be36d101SStefano Zampini   PetscFunctionBegin;
1487be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1488be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1489be36d101SStefano Zampini     PetscSection section;
1490be36d101SStefano Zampini     PetscSF      sf;
1491be36d101SStefano Zampini 
1492be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
149392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1494be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1495be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1496be36d101SStefano Zampini   }
1497be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1498be36d101SStefano Zampini   PetscFunctionReturn(0);
1499be36d101SStefano Zampini }
1500be36d101SStefano Zampini 
1501552f7358SJed Brown /*@
1502552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1503552f7358SJed Brown 
1504552f7358SJed Brown   Not collective
1505552f7358SJed Brown 
1506552f7358SJed Brown   Input Parameter:
1507552f7358SJed Brown . mesh - The DMPlex
1508552f7358SJed Brown 
1509552f7358SJed Brown   Output Parameters:
1510552f7358SJed Brown + pStart - The first mesh point
1511552f7358SJed Brown - pEnd   - The upper bound for mesh points
1512552f7358SJed Brown 
1513552f7358SJed Brown   Level: beginner
1514552f7358SJed Brown 
1515552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1516552f7358SJed Brown @*/
1517552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1518552f7358SJed Brown {
1519552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1520552f7358SJed Brown   PetscErrorCode ierr;
1521552f7358SJed Brown 
1522552f7358SJed Brown   PetscFunctionBegin;
1523552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1524552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1525552f7358SJed Brown   PetscFunctionReturn(0);
1526552f7358SJed Brown }
1527552f7358SJed Brown 
1528552f7358SJed Brown /*@
1529552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1530552f7358SJed Brown 
1531552f7358SJed Brown   Not collective
1532552f7358SJed Brown 
1533552f7358SJed Brown   Input Parameters:
1534552f7358SJed Brown + mesh - The DMPlex
1535552f7358SJed Brown . pStart - The first mesh point
1536552f7358SJed Brown - pEnd   - The upper bound for mesh points
1537552f7358SJed Brown 
1538552f7358SJed Brown   Output Parameters:
1539552f7358SJed Brown 
1540552f7358SJed Brown   Level: beginner
1541552f7358SJed Brown 
1542552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1543552f7358SJed Brown @*/
1544552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1545552f7358SJed Brown {
1546552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1547552f7358SJed Brown   PetscErrorCode ierr;
1548552f7358SJed Brown 
1549552f7358SJed Brown   PetscFunctionBegin;
1550552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1551552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1552552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1553552f7358SJed Brown   PetscFunctionReturn(0);
1554552f7358SJed Brown }
1555552f7358SJed Brown 
1556552f7358SJed Brown /*@
1557eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1558552f7358SJed Brown 
1559552f7358SJed Brown   Not collective
1560552f7358SJed Brown 
1561552f7358SJed Brown   Input Parameters:
1562552f7358SJed Brown + mesh - The DMPlex
1563eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1564552f7358SJed Brown 
1565552f7358SJed Brown   Output Parameter:
1566552f7358SJed Brown . size - The cone size for point p
1567552f7358SJed Brown 
1568552f7358SJed Brown   Level: beginner
1569552f7358SJed Brown 
1570552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1571552f7358SJed Brown @*/
1572552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1573552f7358SJed Brown {
1574552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1575552f7358SJed Brown   PetscErrorCode ierr;
1576552f7358SJed Brown 
1577552f7358SJed Brown   PetscFunctionBegin;
1578552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1579552f7358SJed Brown   PetscValidPointer(size, 3);
1580552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1581552f7358SJed Brown   PetscFunctionReturn(0);
1582552f7358SJed Brown }
1583552f7358SJed Brown 
1584552f7358SJed Brown /*@
1585eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1586552f7358SJed Brown 
1587552f7358SJed Brown   Not collective
1588552f7358SJed Brown 
1589552f7358SJed Brown   Input Parameters:
1590552f7358SJed Brown + mesh - The DMPlex
1591eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1592552f7358SJed Brown - size - The cone size for point p
1593552f7358SJed Brown 
1594552f7358SJed Brown   Output Parameter:
1595552f7358SJed Brown 
1596552f7358SJed Brown   Note:
1597552f7358SJed Brown   This should be called after DMPlexSetChart().
1598552f7358SJed Brown 
1599552f7358SJed Brown   Level: beginner
1600552f7358SJed Brown 
1601552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1602552f7358SJed Brown @*/
1603552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1604552f7358SJed Brown {
1605552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1606552f7358SJed Brown   PetscErrorCode ierr;
1607552f7358SJed Brown 
1608552f7358SJed Brown   PetscFunctionBegin;
1609552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1610552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
16110d644c17SKarl Rupp 
1612552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1613552f7358SJed Brown   PetscFunctionReturn(0);
1614552f7358SJed Brown }
1615552f7358SJed Brown 
1616f5a469b9SMatthew G. Knepley /*@
1617eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1618f5a469b9SMatthew G. Knepley 
1619f5a469b9SMatthew G. Knepley   Not collective
1620f5a469b9SMatthew G. Knepley 
1621f5a469b9SMatthew G. Knepley   Input Parameters:
1622f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1623eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1624f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1625f5a469b9SMatthew G. Knepley 
1626f5a469b9SMatthew G. Knepley   Output Parameter:
1627f5a469b9SMatthew G. Knepley 
1628f5a469b9SMatthew G. Knepley   Note:
1629f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1630f5a469b9SMatthew G. Knepley 
1631f5a469b9SMatthew G. Knepley   Level: beginner
1632f5a469b9SMatthew G. Knepley 
1633f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1634f5a469b9SMatthew G. Knepley @*/
1635f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1636f5a469b9SMatthew G. Knepley {
1637f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1638f5a469b9SMatthew G. Knepley   PetscInt       csize;
1639f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1640f5a469b9SMatthew G. Knepley 
1641f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1642f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1643f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1644f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1645f5a469b9SMatthew G. Knepley 
1646f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1647f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1648f5a469b9SMatthew G. Knepley }
1649f5a469b9SMatthew G. Knepley 
1650552f7358SJed Brown /*@C
1651eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1652552f7358SJed Brown 
1653552f7358SJed Brown   Not collective
1654552f7358SJed Brown 
1655552f7358SJed Brown   Input Parameters:
1656833c876bSVaclav Hapla + dm - The DMPlex
1657eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1658552f7358SJed Brown 
1659552f7358SJed Brown   Output Parameter:
1660552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1661552f7358SJed Brown 
1662552f7358SJed Brown   Level: beginner
1663552f7358SJed Brown 
16643813dfbdSMatthew G Knepley   Fortran Notes:
16653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1667922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1668922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
16693813dfbdSMatthew G Knepley 
16700ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1671552f7358SJed Brown @*/
1672552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1673552f7358SJed Brown {
1674552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1675552f7358SJed Brown   PetscInt       off;
1676552f7358SJed Brown   PetscErrorCode ierr;
1677552f7358SJed Brown 
1678552f7358SJed Brown   PetscFunctionBegin;
1679552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1680552f7358SJed Brown   PetscValidPointer(cone, 3);
1681552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1682552f7358SJed Brown   *cone = &mesh->cones[off];
1683552f7358SJed Brown   PetscFunctionReturn(0);
1684552f7358SJed Brown }
1685552f7358SJed Brown 
16860ce7577fSVaclav Hapla /*@C
16870ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
16880ce7577fSVaclav Hapla 
16890ce7577fSVaclav Hapla   Not collective
16900ce7577fSVaclav Hapla 
16910ce7577fSVaclav Hapla   Input Parameters:
16920ce7577fSVaclav Hapla + dm - The DMPlex
16930ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
16940ce7577fSVaclav Hapla 
16950ce7577fSVaclav Hapla   Output Parameter:
16960ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
16970ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
16980ce7577fSVaclav Hapla 
16990ce7577fSVaclav Hapla   Level: intermediate
17000ce7577fSVaclav Hapla 
1701d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
17020ce7577fSVaclav Hapla @*/
17030ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
17040ce7577fSVaclav Hapla {
17050ce7577fSVaclav Hapla   PetscSection        cs, newcs;
17060ce7577fSVaclav Hapla   PetscInt            *cones;
17070ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
17080ce7577fSVaclav Hapla   PetscInt            n;
17090ce7577fSVaclav Hapla   PetscErrorCode      ierr;
17100ce7577fSVaclav Hapla 
17110ce7577fSVaclav Hapla   PetscFunctionBegin;
17120ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
17130ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
17140ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
17150ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
17160ce7577fSVaclav Hapla   if (pCones) {
17170ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
17180ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
17190ce7577fSVaclav Hapla   }
17200ce7577fSVaclav Hapla   PetscFunctionReturn(0);
17210ce7577fSVaclav Hapla }
17220ce7577fSVaclav Hapla 
1723af9eab45SVaclav Hapla /*@
1724af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1725d4636a37SVaclav Hapla 
1726d4636a37SVaclav Hapla   Not collective
1727d4636a37SVaclav Hapla 
1728d4636a37SVaclav Hapla   Input Parameters:
1729d4636a37SVaclav Hapla + dm - The DMPlex
1730af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1731d4636a37SVaclav Hapla 
1732d4636a37SVaclav Hapla   Output Parameter:
1733af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1734d4636a37SVaclav Hapla 
1735d4636a37SVaclav Hapla   Level: advanced
1736d4636a37SVaclav Hapla 
1737af9eab45SVaclav Hapla   Notes:
1738af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1739af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1740af9eab45SVaclav Hapla 
1741af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1742d4636a37SVaclav Hapla @*/
1743af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1744d4636a37SVaclav Hapla {
1745af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1746af9eab45SVaclav Hapla   PetscInt            depth;
1747d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1748d4636a37SVaclav Hapla 
1749d4636a37SVaclav Hapla   PetscFunctionBegin;
1750af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1751af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1752af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1753af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1754af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1755af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1756af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1757af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1758af9eab45SVaclav Hapla }
1759af9eab45SVaclav Hapla 
1760af9eab45SVaclav Hapla /*@
1761af9eab45SVaclav 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).
1762af9eab45SVaclav Hapla 
1763af9eab45SVaclav Hapla   Not collective
1764af9eab45SVaclav Hapla 
1765af9eab45SVaclav Hapla   Input Parameters:
1766af9eab45SVaclav Hapla + dm - The DMPlex
1767af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1768af9eab45SVaclav Hapla 
1769af9eab45SVaclav Hapla   Output Parameter:
1770af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1771af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1772af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1773af9eab45SVaclav Hapla 
1774af9eab45SVaclav Hapla   Level: advanced
1775af9eab45SVaclav Hapla 
1776af9eab45SVaclav Hapla   Notes:
1777af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1778af9eab45SVaclav Hapla 
1779af9eab45SVaclav 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.
1780af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1781af9eab45SVaclav Hapla 
1782af9eab45SVaclav 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:
1783af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1784af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1785af9eab45SVaclav Hapla 
1786af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1787af9eab45SVaclav Hapla @*/
1788af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1789af9eab45SVaclav Hapla {
1790af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1791af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1792af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1793af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1794af9eab45SVaclav Hapla   PetscSection        *sections_;
1795af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1796af9eab45SVaclav Hapla 
1797af9eab45SVaclav Hapla   PetscFunctionBegin;
1798af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1799af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1800af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1801af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1802af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1803af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1804af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1805af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1806af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1807af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1808af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1809af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1810af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1811af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1812af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1813af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
1814af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
1815af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
1816af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
1817af9eab45SVaclav Hapla       } else {
1818af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
1819af9eab45SVaclav Hapla       }
1820af9eab45SVaclav Hapla     }
1821af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
1822af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
1823af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
1824af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1825af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
1826af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
1827af9eab45SVaclav Hapla       if (cn > 1) {
1828af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
1829af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
1830af9eab45SVaclav Hapla       } else {
1831af9eab45SVaclav Hapla         newarr[co] = arr[i];
1832af9eab45SVaclav Hapla       }
1833af9eab45SVaclav Hapla     }
1834af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
1835af9eab45SVaclav Hapla     arr = newarr;
1836af9eab45SVaclav Hapla     n = newn;
1837af9eab45SVaclav Hapla   }
1838ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
1839af9eab45SVaclav Hapla   *depth = depth_;
1840af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
1841af9eab45SVaclav Hapla   else {
1842af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
1843af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
1844af9eab45SVaclav Hapla   }
1845af9eab45SVaclav Hapla   if (sections) *sections = sections_;
1846af9eab45SVaclav Hapla   else {
1847af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
1848af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
1849af9eab45SVaclav Hapla   }
1850af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1851af9eab45SVaclav Hapla }
1852af9eab45SVaclav Hapla 
1853af9eab45SVaclav Hapla /*@
1854af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
1855af9eab45SVaclav Hapla 
1856af9eab45SVaclav Hapla   Not collective
1857af9eab45SVaclav Hapla 
1858af9eab45SVaclav Hapla   Input Parameters:
1859af9eab45SVaclav Hapla + dm - The DMPlex
1860af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1861af9eab45SVaclav Hapla 
1862af9eab45SVaclav Hapla   Output Parameter:
1863af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1864af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
1865af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1866af9eab45SVaclav Hapla 
1867af9eab45SVaclav Hapla   Level: advanced
1868af9eab45SVaclav Hapla 
1869af9eab45SVaclav Hapla   Notes:
1870af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
1871af9eab45SVaclav Hapla 
1872af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1873af9eab45SVaclav Hapla @*/
1874af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1875af9eab45SVaclav Hapla {
1876af9eab45SVaclav Hapla   PetscInt            d, depth_;
1877af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1878af9eab45SVaclav Hapla 
1879af9eab45SVaclav Hapla   PetscFunctionBegin;
1880af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1881af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1882af9eab45SVaclav Hapla   if (depth) *depth = 0;
1883af9eab45SVaclav Hapla   if (expandedPoints) {
1884af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
1885af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
1886af9eab45SVaclav Hapla   }
1887af9eab45SVaclav Hapla   if (sections)  {
1888af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
1889af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
1890af9eab45SVaclav Hapla   }
1891d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1892d4636a37SVaclav Hapla }
1893d4636a37SVaclav Hapla 
1894552f7358SJed Brown /*@
189592371b87SBarry 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
1896552f7358SJed Brown 
1897552f7358SJed Brown   Not collective
1898552f7358SJed Brown 
1899552f7358SJed Brown   Input Parameters:
1900552f7358SJed Brown + mesh - The DMPlex
1901eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1902552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1903552f7358SJed Brown 
1904552f7358SJed Brown   Output Parameter:
1905552f7358SJed Brown 
1906552f7358SJed Brown   Note:
1907552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1908552f7358SJed Brown 
190992371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
191092371b87SBarry Smith 
1911552f7358SJed Brown   Level: beginner
1912552f7358SJed Brown 
191392371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1914552f7358SJed Brown @*/
1915552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1916552f7358SJed Brown {
1917552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1918552f7358SJed Brown   PetscInt       pStart, pEnd;
1919552f7358SJed Brown   PetscInt       dof, off, c;
1920552f7358SJed Brown   PetscErrorCode ierr;
1921552f7358SJed Brown 
1922552f7358SJed Brown   PetscFunctionBegin;
1923552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1924552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1925552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1926552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1927552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
192882f516ccSBarry 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);
1929552f7358SJed Brown   for (c = 0; c < dof; ++c) {
193082f516ccSBarry 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);
1931552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1932552f7358SJed Brown   }
1933552f7358SJed Brown   PetscFunctionReturn(0);
1934552f7358SJed Brown }
1935552f7358SJed Brown 
1936552f7358SJed Brown /*@C
1937eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1938552f7358SJed Brown 
1939552f7358SJed Brown   Not collective
1940552f7358SJed Brown 
1941552f7358SJed Brown   Input Parameters:
1942552f7358SJed Brown + mesh - The DMPlex
1943eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1944552f7358SJed Brown 
1945552f7358SJed Brown   Output Parameter:
1946552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1947552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1948552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1949552f7358SJed Brown                     the index of the cone point on which to start.
1950552f7358SJed Brown 
1951552f7358SJed Brown   Level: beginner
1952552f7358SJed Brown 
19533813dfbdSMatthew G Knepley   Fortran Notes:
19543813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19553813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19563b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1957922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
19583813dfbdSMatthew G Knepley 
1959552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1960552f7358SJed Brown @*/
1961552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1962552f7358SJed Brown {
1963552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1964552f7358SJed Brown   PetscInt       off;
1965552f7358SJed Brown   PetscErrorCode ierr;
1966552f7358SJed Brown 
1967552f7358SJed Brown   PetscFunctionBegin;
1968552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1969552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1970552f7358SJed Brown   {
1971552f7358SJed Brown     PetscInt dof;
1972552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1973552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1974552f7358SJed Brown   }
1975552f7358SJed Brown #endif
1976552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
19770d644c17SKarl Rupp 
1978552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1979552f7358SJed Brown   PetscFunctionReturn(0);
1980552f7358SJed Brown }
1981552f7358SJed Brown 
1982552f7358SJed Brown /*@
1983eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1984552f7358SJed Brown 
1985552f7358SJed Brown   Not collective
1986552f7358SJed Brown 
1987552f7358SJed Brown   Input Parameters:
1988552f7358SJed Brown + mesh - The DMPlex
1989eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1990552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1991552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1992552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1993552f7358SJed Brown                     the index of the cone point on which to start.
1994552f7358SJed Brown 
1995552f7358SJed Brown   Output Parameter:
1996552f7358SJed Brown 
1997552f7358SJed Brown   Note:
1998552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1999552f7358SJed Brown 
2000552f7358SJed Brown   Level: beginner
2001552f7358SJed Brown 
2002552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2003552f7358SJed Brown @*/
2004552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2005552f7358SJed Brown {
2006552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2007552f7358SJed Brown   PetscInt       pStart, pEnd;
2008552f7358SJed Brown   PetscInt       dof, off, c;
2009552f7358SJed Brown   PetscErrorCode ierr;
2010552f7358SJed Brown 
2011552f7358SJed Brown   PetscFunctionBegin;
2012552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2013552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2014552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2015552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2016552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
201782f516ccSBarry 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);
2018552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2019552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2020552f7358SJed Brown 
2021552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
202282f516ccSBarry 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);
2023552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2024552f7358SJed Brown   }
2025552f7358SJed Brown   PetscFunctionReturn(0);
2026552f7358SJed Brown }
2027552f7358SJed Brown 
20287cd05799SMatthew G. Knepley /*@
2029eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
20307cd05799SMatthew G. Knepley 
20317cd05799SMatthew G. Knepley   Not collective
20327cd05799SMatthew G. Knepley 
20337cd05799SMatthew G. Knepley   Input Parameters:
20347cd05799SMatthew G. Knepley + mesh - The DMPlex
2035eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20367cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20377cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
20387cd05799SMatthew G. Knepley 
20397cd05799SMatthew G. Knepley   Level: beginner
20407cd05799SMatthew G. Knepley 
20417cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20427cd05799SMatthew G. Knepley @*/
2043552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2044552f7358SJed Brown {
2045552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2046552f7358SJed Brown   PetscInt       pStart, pEnd;
2047552f7358SJed Brown   PetscInt       dof, off;
2048552f7358SJed Brown   PetscErrorCode ierr;
2049552f7358SJed Brown 
2050552f7358SJed Brown   PetscFunctionBegin;
2051552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2052552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
205382f516ccSBarry 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);
205482f516ccSBarry 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);
205577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
205677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
205777c88f5bSMatthew 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);
2058552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2059552f7358SJed Brown   PetscFunctionReturn(0);
2060552f7358SJed Brown }
2061552f7358SJed Brown 
20627cd05799SMatthew G. Knepley /*@
2063eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
20647cd05799SMatthew G. Knepley 
20657cd05799SMatthew G. Knepley   Not collective
20667cd05799SMatthew G. Knepley 
20677cd05799SMatthew G. Knepley   Input Parameters:
20687cd05799SMatthew G. Knepley + mesh - The DMPlex
2069eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20707cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20717cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
20727cd05799SMatthew G. Knepley 
20737cd05799SMatthew G. Knepley   Level: beginner
20747cd05799SMatthew G. Knepley 
20757cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20767cd05799SMatthew G. Knepley @*/
207777c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
207877c88f5bSMatthew G Knepley {
207977c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
208077c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
208177c88f5bSMatthew G Knepley   PetscInt       dof, off;
208277c88f5bSMatthew G Knepley   PetscErrorCode ierr;
208377c88f5bSMatthew G Knepley 
208477c88f5bSMatthew G Knepley   PetscFunctionBegin;
208577c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
208677c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
208777c88f5bSMatthew 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);
208877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
208977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
209077c88f5bSMatthew 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);
209177c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
209277c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
209377c88f5bSMatthew G Knepley }
209477c88f5bSMatthew G Knepley 
2095552f7358SJed Brown /*@
2096eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2097552f7358SJed Brown 
2098552f7358SJed Brown   Not collective
2099552f7358SJed Brown 
2100552f7358SJed Brown   Input Parameters:
2101552f7358SJed Brown + mesh - The DMPlex
2102eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2103552f7358SJed Brown 
2104552f7358SJed Brown   Output Parameter:
2105552f7358SJed Brown . size - The support size for point p
2106552f7358SJed Brown 
2107552f7358SJed Brown   Level: beginner
2108552f7358SJed Brown 
2109552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2110552f7358SJed Brown @*/
2111552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2112552f7358SJed Brown {
2113552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2114552f7358SJed Brown   PetscErrorCode ierr;
2115552f7358SJed Brown 
2116552f7358SJed Brown   PetscFunctionBegin;
2117552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2118552f7358SJed Brown   PetscValidPointer(size, 3);
2119552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2120552f7358SJed Brown   PetscFunctionReturn(0);
2121552f7358SJed Brown }
2122552f7358SJed Brown 
2123552f7358SJed Brown /*@
2124eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2125552f7358SJed Brown 
2126552f7358SJed Brown   Not collective
2127552f7358SJed Brown 
2128552f7358SJed Brown   Input Parameters:
2129552f7358SJed Brown + mesh - The DMPlex
2130eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2131552f7358SJed Brown - size - The support size for point p
2132552f7358SJed Brown 
2133552f7358SJed Brown   Output Parameter:
2134552f7358SJed Brown 
2135552f7358SJed Brown   Note:
2136552f7358SJed Brown   This should be called after DMPlexSetChart().
2137552f7358SJed Brown 
2138552f7358SJed Brown   Level: beginner
2139552f7358SJed Brown 
2140552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2141552f7358SJed Brown @*/
2142552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2143552f7358SJed Brown {
2144552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2145552f7358SJed Brown   PetscErrorCode ierr;
2146552f7358SJed Brown 
2147552f7358SJed Brown   PetscFunctionBegin;
2148552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2149552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
21500d644c17SKarl Rupp 
2151552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2152552f7358SJed Brown   PetscFunctionReturn(0);
2153552f7358SJed Brown }
2154552f7358SJed Brown 
2155552f7358SJed Brown /*@C
2156eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2157552f7358SJed Brown 
2158552f7358SJed Brown   Not collective
2159552f7358SJed Brown 
2160552f7358SJed Brown   Input Parameters:
2161552f7358SJed Brown + mesh - The DMPlex
2162eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2163552f7358SJed Brown 
2164552f7358SJed Brown   Output Parameter:
2165552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2166552f7358SJed Brown 
2167552f7358SJed Brown   Level: beginner
2168552f7358SJed Brown 
21693813dfbdSMatthew G Knepley   Fortran Notes:
21703813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21713813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21723b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2173922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
21743813dfbdSMatthew G Knepley 
2175552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2176552f7358SJed Brown @*/
2177552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2178552f7358SJed Brown {
2179552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2180552f7358SJed Brown   PetscInt       off;
2181552f7358SJed Brown   PetscErrorCode ierr;
2182552f7358SJed Brown 
2183552f7358SJed Brown   PetscFunctionBegin;
2184552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2185552f7358SJed Brown   PetscValidPointer(support, 3);
2186552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2187552f7358SJed Brown   *support = &mesh->supports[off];
2188552f7358SJed Brown   PetscFunctionReturn(0);
2189552f7358SJed Brown }
2190552f7358SJed Brown 
2191552f7358SJed Brown /*@
219292371b87SBarry 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
2193552f7358SJed Brown 
2194552f7358SJed Brown   Not collective
2195552f7358SJed Brown 
2196552f7358SJed Brown   Input Parameters:
2197552f7358SJed Brown + mesh - The DMPlex
2198eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
219992371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2200552f7358SJed Brown 
2201552f7358SJed Brown   Output Parameter:
2202552f7358SJed Brown 
2203552f7358SJed Brown   Note:
2204552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2205552f7358SJed Brown 
2206552f7358SJed Brown   Level: beginner
2207552f7358SJed Brown 
220892371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2209552f7358SJed Brown @*/
2210552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2211552f7358SJed Brown {
2212552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2213552f7358SJed Brown   PetscInt       pStart, pEnd;
2214552f7358SJed Brown   PetscInt       dof, off, c;
2215552f7358SJed Brown   PetscErrorCode ierr;
2216552f7358SJed Brown 
2217552f7358SJed Brown   PetscFunctionBegin;
2218552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2219552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2220552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2221552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2222552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
222382f516ccSBarry 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);
2224552f7358SJed Brown   for (c = 0; c < dof; ++c) {
222582f516ccSBarry 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);
2226552f7358SJed Brown     mesh->supports[off+c] = support[c];
2227552f7358SJed Brown   }
2228552f7358SJed Brown   PetscFunctionReturn(0);
2229552f7358SJed Brown }
2230552f7358SJed Brown 
22317cd05799SMatthew G. Knepley /*@
2232eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
22337cd05799SMatthew G. Knepley 
22347cd05799SMatthew G. Knepley   Not collective
22357cd05799SMatthew G. Knepley 
22367cd05799SMatthew G. Knepley   Input Parameters:
22377cd05799SMatthew G. Knepley + mesh - The DMPlex
2238eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22397cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
22407cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
22417cd05799SMatthew G. Knepley 
22427cd05799SMatthew G. Knepley   Level: beginner
22437cd05799SMatthew G. Knepley 
22447cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22457cd05799SMatthew G. Knepley @*/
2246552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2247552f7358SJed Brown {
2248552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2249552f7358SJed Brown   PetscInt       pStart, pEnd;
2250552f7358SJed Brown   PetscInt       dof, off;
2251552f7358SJed Brown   PetscErrorCode ierr;
2252552f7358SJed Brown 
2253552f7358SJed Brown   PetscFunctionBegin;
2254552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2255552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2256552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2257552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
225882f516ccSBarry 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);
225982f516ccSBarry 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);
226082f516ccSBarry 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);
2261552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2262552f7358SJed Brown   PetscFunctionReturn(0);
2263552f7358SJed Brown }
2264552f7358SJed Brown 
2265552f7358SJed Brown /*@C
2266eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2267552f7358SJed Brown 
2268552f7358SJed Brown   Not collective
2269552f7358SJed Brown 
2270552f7358SJed Brown   Input Parameters:
2271552f7358SJed Brown + mesh - The DMPlex
2272eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2273552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
22740298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2275552f7358SJed Brown 
2276552f7358SJed Brown   Output Parameters:
2277552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2278552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2279552f7358SJed Brown 
2280552f7358SJed Brown   Note:
22810298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2282552f7358SJed Brown 
22833813dfbdSMatthew G Knepley   Fortran Notes:
22843813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22853813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22863813dfbdSMatthew G Knepley 
22873813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22883813dfbdSMatthew G Knepley 
2289552f7358SJed Brown   Level: beginner
2290552f7358SJed Brown 
2291552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2292552f7358SJed Brown @*/
2293552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2294552f7358SJed Brown {
2295552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2296552f7358SJed Brown   PetscInt       *closure, *fifo;
22970298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2298552f7358SJed Brown   PetscInt        tmpSize, t;
2299552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2300552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2301552f7358SJed Brown   PetscErrorCode  ierr;
2302552f7358SJed Brown 
2303552f7358SJed Brown   PetscFunctionBegin;
2304552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2305552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2306552f7358SJed Brown   /* This is only 1-level */
2307552f7358SJed Brown   if (useCone) {
2308552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2309552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2310552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2311552f7358SJed Brown   } else {
2312552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2313552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2314552f7358SJed Brown   }
2315bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2316bfbcdd7aSMatthew G. Knepley     if (*points) {
2317bfbcdd7aSMatthew G. Knepley       closure = *points;
2318bfbcdd7aSMatthew G. Knepley     } else {
2319bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
232069291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2321bfbcdd7aSMatthew G. Knepley     }
2322bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2323bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2324bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2325bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2326bfbcdd7aSMatthew G. Knepley     }
2327bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2328bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2329bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2330bfbcdd7aSMatthew G. Knepley   }
233124c766afSToby Isaac   {
233224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
233324c766afSToby Isaac 
233424c766afSToby Isaac     c = mesh->maxConeSize;
233524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
233624c766afSToby Isaac     s = mesh->maxSupportSize;
233724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
233824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
233924c766afSToby Isaac   }
234069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2341bfbcdd7aSMatthew G. Knepley   if (*points) {
2342bfbcdd7aSMatthew G. Knepley     closure = *points;
2343bfbcdd7aSMatthew G. Knepley   } else {
234469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2345bfbcdd7aSMatthew G. Knepley   }
2346bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2347552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2348552f7358SJed Brown     const PetscInt cp = tmp[t];
2349552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2350552f7358SJed Brown 
2351552f7358SJed Brown     closure[closureSize]   = cp;
2352552f7358SJed Brown     closure[closureSize+1] = co;
2353552f7358SJed Brown     fifo[fifoSize]         = cp;
2354552f7358SJed Brown     fifo[fifoSize+1]       = co;
2355552f7358SJed Brown   }
2356bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2357552f7358SJed Brown   while (fifoSize - fifoStart) {
2358552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2359552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2360552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2361552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2362552f7358SJed Brown 
2363552f7358SJed Brown     if (useCone) {
2364552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2365552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2366552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2367552f7358SJed Brown     } else {
2368552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2369552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
23700298fd71SBarry Smith       tmpO = NULL;
2371552f7358SJed Brown     }
2372552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2373552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2374552f7358SJed Brown       const PetscInt cp = tmp[i];
23752e1b13c2SMatthew 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. */
23762e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
23772e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
23782e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2379552f7358SJed Brown       PetscInt       c;
2380552f7358SJed Brown 
23812e1b13c2SMatthew G. Knepley       if (rev) {
23822e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
23832e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
23842e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
23852e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
23862e1b13c2SMatthew G. Knepley       }
2387552f7358SJed Brown       /* Check for duplicate */
2388552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2389552f7358SJed Brown         if (closure[c] == cp) break;
2390552f7358SJed Brown       }
2391552f7358SJed Brown       if (c == closureSize) {
2392552f7358SJed Brown         closure[closureSize]   = cp;
2393552f7358SJed Brown         closure[closureSize+1] = co;
2394552f7358SJed Brown         fifo[fifoSize]         = cp;
2395552f7358SJed Brown         fifo[fifoSize+1]       = co;
2396552f7358SJed Brown         closureSize           += 2;
2397552f7358SJed Brown         fifoSize              += 2;
2398552f7358SJed Brown       }
2399552f7358SJed Brown     }
2400552f7358SJed Brown     fifoStart += 2;
2401552f7358SJed Brown   }
2402552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2403552f7358SJed Brown   if (points)    *points    = closure;
240469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2405552f7358SJed Brown   PetscFunctionReturn(0);
2406552f7358SJed Brown }
2407552f7358SJed Brown 
24089bf0dad6SMatthew G. Knepley /*@C
2409eaf898f9SPatrick 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
24109bf0dad6SMatthew G. Knepley 
24119bf0dad6SMatthew G. Knepley   Not collective
24129bf0dad6SMatthew G. Knepley 
24139bf0dad6SMatthew G. Knepley   Input Parameters:
24149bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2415eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24169bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
24179bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24189bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
24199bf0dad6SMatthew G. Knepley 
24209bf0dad6SMatthew G. Knepley   Output Parameters:
24219bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
24229bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
24239bf0dad6SMatthew G. Knepley 
24249bf0dad6SMatthew G. Knepley   Note:
24259bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
24269bf0dad6SMatthew G. Knepley 
24279bf0dad6SMatthew G. Knepley   Fortran Notes:
24289bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24299bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
24309bf0dad6SMatthew G. Knepley 
24319bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24329bf0dad6SMatthew G. Knepley 
24339bf0dad6SMatthew G. Knepley   Level: beginner
24349bf0dad6SMatthew G. Knepley 
24359bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
24369bf0dad6SMatthew G. Knepley @*/
24379bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
24389bf0dad6SMatthew G. Knepley {
24399bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
24409bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
24419bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
24429bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
24439bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
24449bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
24459bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
24469bf0dad6SMatthew G. Knepley 
24479bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
24489bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24499bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24509bf0dad6SMatthew G. Knepley   /* This is only 1-level */
24519bf0dad6SMatthew G. Knepley   if (useCone) {
24529bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
24539bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
24549bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
24559bf0dad6SMatthew G. Knepley   } else {
24569bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
24579bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
24589bf0dad6SMatthew G. Knepley   }
24599bf0dad6SMatthew G. Knepley   if (depth == 1) {
24609bf0dad6SMatthew G. Knepley     if (*points) {
24619bf0dad6SMatthew G. Knepley       closure = *points;
24629bf0dad6SMatthew G. Knepley     } else {
24639bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
246469291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24659bf0dad6SMatthew G. Knepley     }
24669bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
24679bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
24689bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
24699bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
24709bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
24719bf0dad6SMatthew G. Knepley     }
24729bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
24739bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
24749bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
24759bf0dad6SMatthew G. Knepley   }
247624c766afSToby Isaac   {
247724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
247824c766afSToby Isaac 
247924c766afSToby Isaac     c = mesh->maxConeSize;
248024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
248124c766afSToby Isaac     s = mesh->maxSupportSize;
248224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
248324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
248424c766afSToby Isaac   }
248569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
24869bf0dad6SMatthew G. Knepley   if (*points) {
24879bf0dad6SMatthew G. Knepley     closure = *points;
24889bf0dad6SMatthew G. Knepley   } else {
248969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24909bf0dad6SMatthew G. Knepley   }
24919bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
24929bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
24939bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
24949bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
24959bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
24969bf0dad6SMatthew G. Knepley 
24979bf0dad6SMatthew G. Knepley     if (ornt < 0) {
24989bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
24999bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
250086b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
25019bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25029bf0dad6SMatthew G. Knepley     }
25039bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
25049bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
25059bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
25069bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
25079bf0dad6SMatthew G. Knepley   }
25089bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
25099bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
25109bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
25119bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
25129bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
25139bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
25149bf0dad6SMatthew G. Knepley 
25159bf0dad6SMatthew G. Knepley     if (useCone) {
25169bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
25179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
25189bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
25199bf0dad6SMatthew G. Knepley     } else {
25209bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
25219bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25229bf0dad6SMatthew G. Knepley       tmpO = NULL;
25239bf0dad6SMatthew G. Knepley     }
25249bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
25259bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
25269bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
25279bf0dad6SMatthew 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. */
25289bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25299bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25309bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
25319bf0dad6SMatthew G. Knepley       PetscInt       c;
25329bf0dad6SMatthew G. Knepley 
25339bf0dad6SMatthew G. Knepley       if (rev) {
25349bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
25359bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25369bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25379bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25389bf0dad6SMatthew G. Knepley       }
25399bf0dad6SMatthew G. Knepley       /* Check for duplicate */
25409bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
25419bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
25429bf0dad6SMatthew G. Knepley       }
25439bf0dad6SMatthew G. Knepley       if (c == closureSize) {
25449bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
25459bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
25469bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
25479bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
25489bf0dad6SMatthew G. Knepley         closureSize           += 2;
25499bf0dad6SMatthew G. Knepley         fifoSize              += 2;
25509bf0dad6SMatthew G. Knepley       }
25519bf0dad6SMatthew G. Knepley     }
25529bf0dad6SMatthew G. Knepley     fifoStart += 2;
25539bf0dad6SMatthew G. Knepley   }
25549bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
25559bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
255669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25579bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
25589bf0dad6SMatthew G. Knepley }
25599bf0dad6SMatthew G. Knepley 
2560552f7358SJed Brown /*@C
2561eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2562552f7358SJed Brown 
2563552f7358SJed Brown   Not collective
2564552f7358SJed Brown 
2565552f7358SJed Brown   Input Parameters:
2566552f7358SJed Brown + mesh - The DMPlex
2567eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2568552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2569e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2570e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2571552f7358SJed Brown 
2572552f7358SJed Brown   Note:
25730298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2574552f7358SJed Brown 
25753813dfbdSMatthew G Knepley   Fortran Notes:
25763813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25773813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25783813dfbdSMatthew G Knepley 
25793813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25803813dfbdSMatthew G Knepley 
2581552f7358SJed Brown   Level: beginner
2582552f7358SJed Brown 
2583552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2584552f7358SJed Brown @*/
2585552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2586552f7358SJed Brown {
2587552f7358SJed Brown   PetscErrorCode ierr;
2588552f7358SJed Brown 
2589552f7358SJed Brown   PetscFunctionBegin;
2590552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2591e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2592e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
259369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
25944ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2595552f7358SJed Brown   PetscFunctionReturn(0);
2596552f7358SJed Brown }
2597552f7358SJed Brown 
2598552f7358SJed Brown /*@
2599eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2600552f7358SJed Brown 
2601552f7358SJed Brown   Not collective
2602552f7358SJed Brown 
2603552f7358SJed Brown   Input Parameter:
2604552f7358SJed Brown . mesh - The DMPlex
2605552f7358SJed Brown 
2606552f7358SJed Brown   Output Parameters:
2607552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2608552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2609552f7358SJed Brown 
2610552f7358SJed Brown   Level: beginner
2611552f7358SJed Brown 
2612552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2613552f7358SJed Brown @*/
2614552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2615552f7358SJed Brown {
2616552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2617552f7358SJed Brown 
2618552f7358SJed Brown   PetscFunctionBegin;
2619552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2620552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2621552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2622552f7358SJed Brown   PetscFunctionReturn(0);
2623552f7358SJed Brown }
2624552f7358SJed Brown 
2625552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2626552f7358SJed Brown {
2627552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2628552f7358SJed Brown   PetscInt       size;
2629552f7358SJed Brown   PetscErrorCode ierr;
2630552f7358SJed Brown 
2631552f7358SJed Brown   PetscFunctionBegin;
2632552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2633552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2634552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
26351795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
26361795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2637552f7358SJed Brown   if (mesh->maxSupportSize) {
2638552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2639552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
26401795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2641552f7358SJed Brown   }
2642552f7358SJed Brown   PetscFunctionReturn(0);
2643552f7358SJed Brown }
2644552f7358SJed Brown 
2645276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2646552f7358SJed Brown {
2647552f7358SJed Brown   PetscErrorCode ierr;
2648552f7358SJed Brown 
2649552f7358SJed Brown   PetscFunctionBegin;
26504d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2651792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2652c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2653736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2654f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2655f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2656f94b4a02SBlaise Bourdin 
26573dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
26583dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2659c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
2660f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2661f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2662f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2663f94b4a02SBlaise Bourdin 
2664f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2665c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2666f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2667f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2668f94b4a02SBlaise Bourdin   }
2669552f7358SJed Brown   PetscFunctionReturn(0);
2670552f7358SJed Brown }
2671552f7358SJed Brown 
26722adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
26732adcc780SMatthew G. Knepley {
26742adcc780SMatthew G. Knepley   PetscErrorCode ierr;
26753dcd263cSBlaise Bourdin   PetscInt       i = 0;
26762adcc780SMatthew G. Knepley 
26772adcc780SMatthew G. Knepley   PetscFunctionBegin;
2678435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2679792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2680c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
26813dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
26823dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
26833dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
26843dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
26853dcd263cSBlaise Bourdin 
26863dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
26873dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2688c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
268992fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
26903dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
26913dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
26923dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
26933dcd263cSBlaise Bourdin 
26943dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2695c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
26963dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
26973dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
26983dcd263cSBlaise Bourdin       break;
26993dcd263cSBlaise Bourdin     }
27003dcd263cSBlaise Bourdin   }
27012adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
27022adcc780SMatthew G. Knepley }
27032adcc780SMatthew G. Knepley 
2704552f7358SJed Brown /*@
2705eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2706552f7358SJed Brown 
2707552f7358SJed Brown   Not collective
2708552f7358SJed Brown 
2709552f7358SJed Brown   Input Parameter:
2710552f7358SJed Brown . mesh - The DMPlex
2711552f7358SJed Brown 
2712552f7358SJed Brown   Output Parameter:
2713552f7358SJed Brown 
2714552f7358SJed Brown   Note:
2715552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2716552f7358SJed Brown 
2717552f7358SJed Brown   Level: beginner
2718552f7358SJed Brown 
2719552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2720552f7358SJed Brown @*/
2721552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2722552f7358SJed Brown {
2723552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2724552f7358SJed Brown   PetscInt      *offsets;
2725552f7358SJed Brown   PetscInt       supportSize;
2726552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2727552f7358SJed Brown   PetscErrorCode ierr;
2728552f7358SJed Brown 
2729552f7358SJed Brown   PetscFunctionBegin;
2730552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
273182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
273230b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2733552f7358SJed Brown   /* Calculate support sizes */
2734552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2735552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2736552f7358SJed Brown     PetscInt dof, off, c;
2737552f7358SJed Brown 
2738552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2739552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2740552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2741552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2742552f7358SJed Brown     }
2743552f7358SJed Brown   }
2744552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2745552f7358SJed Brown     PetscInt dof;
2746552f7358SJed Brown 
2747552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
27480d644c17SKarl Rupp 
2749552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2750552f7358SJed Brown   }
2751552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2752552f7358SJed Brown   /* Calculate supports */
2753552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
27541795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
27551795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2756552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2757552f7358SJed Brown     PetscInt dof, off, c;
2758552f7358SJed Brown 
2759552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2760552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2761552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2762552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2763552f7358SJed Brown       PetscInt       offS;
2764552f7358SJed Brown 
2765552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
27660d644c17SKarl Rupp 
2767552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2768552f7358SJed Brown       ++offsets[q];
2769552f7358SJed Brown     }
2770552f7358SJed Brown   }
2771552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
277230b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2773552f7358SJed Brown   PetscFunctionReturn(0);
2774552f7358SJed Brown }
2775552f7358SJed Brown 
2776277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2777277ea44aSLisandro Dalcin {
2778277ea44aSLisandro Dalcin   IS             stratumIS;
2779277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2780277ea44aSLisandro Dalcin 
2781277ea44aSLisandro Dalcin   PetscFunctionBegin;
2782277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
2783277ea44aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
2784277ea44aSLisandro Dalcin   {
2785277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2786277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2787277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2788277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2789277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2790277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2791277ea44aSLisandro Dalcin     }
2792277ea44aSLisandro 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);
2793277ea44aSLisandro Dalcin   }
2794277ea44aSLisandro Dalcin #endif
2795277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2796277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2797277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2798277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2799277ea44aSLisandro Dalcin }
2800277ea44aSLisandro Dalcin 
2801552f7358SJed Brown /*@
2802a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
28036dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2804552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2805552f7358SJed Brown   the DAG.
2806552f7358SJed Brown 
2807bf4602e4SToby Isaac   Collective on dm
2808552f7358SJed Brown 
2809552f7358SJed Brown   Input Parameter:
2810552f7358SJed Brown . mesh - The DMPlex
2811552f7358SJed Brown 
2812552f7358SJed Brown   Output Parameter:
2813552f7358SJed Brown 
2814552f7358SJed Brown   Notes:
2815b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
2816b1bb481bSMatthew 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
2817b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2818c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2819150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2820552f7358SJed Brown 
2821b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
2822b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
2823b1bb481bSMatthew 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
2824b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
2825b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
2826b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
2827b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
2828b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
2829b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
2830b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
2831b1bb481bSMatthew Knepley 
2832150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2833552f7358SJed Brown 
2834552f7358SJed Brown   Level: beginner
2835552f7358SJed Brown 
2836ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
2837552f7358SJed Brown @*/
2838552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2839552f7358SJed Brown {
2840df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2841aa50250dSMatthew G. Knepley   DMLabel        label;
2842552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2843552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2844552f7358SJed Brown   PetscErrorCode ierr;
2845552f7358SJed Brown 
2846552f7358SJed Brown   PetscFunctionBegin;
2847552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2848552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2849277ea44aSLisandro Dalcin 
2850277ea44aSLisandro Dalcin   /* Create depth label */
2851aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2852c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2853aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2854277ea44aSLisandro Dalcin 
2855277ea44aSLisandro Dalcin   {
2856552f7358SJed Brown     /* Initialize roots and count leaves */
2857277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2858277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2859552f7358SJed Brown     PetscInt coneSize, supportSize;
2860552f7358SJed Brown 
2861277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2862552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2863552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2864552f7358SJed Brown       if (!coneSize && supportSize) {
2865277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2866277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2867552f7358SJed Brown         ++numRoots;
2868552f7358SJed Brown       } else if (!supportSize && coneSize) {
2869552f7358SJed Brown         ++numLeaves;
2870552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2871552f7358SJed Brown         /* Isolated points */
2872277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2873277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2874552f7358SJed Brown       }
2875552f7358SJed Brown     }
2876277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2877277ea44aSLisandro Dalcin   }
2878277ea44aSLisandro Dalcin 
2879552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2880277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2881277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2882552f7358SJed Brown     PetscInt coneSize, supportSize;
2883552f7358SJed Brown 
2884277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2885552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2886552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2887552f7358SJed Brown       if (!supportSize && coneSize) {
2888277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2889277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2890552f7358SJed Brown       }
2891552f7358SJed Brown     }
2892277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2893552f7358SJed Brown   } else {
2894277ea44aSLisandro Dalcin     PetscInt level = 0;
2895277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2896552f7358SJed Brown 
2897277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2898277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2899277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2900277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
290174ef644bSMatthew G. Knepley 
2902277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
290374ef644bSMatthew G. Knepley         const PetscInt *support;
290474ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
290574ef644bSMatthew G. Knepley 
2906277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2907277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
290874ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2909277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2910277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2911552f7358SJed Brown         }
2912552f7358SJed Brown       }
2913277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2914277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2915277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
291674ef644bSMatthew G. Knepley     }
291774ef644bSMatthew G. Knepley   }
2918bf4602e4SToby Isaac   { /* just in case there is an empty process */
2919bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2920bf4602e4SToby Isaac 
2921bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
29226d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2923bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2924367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2925bf4602e4SToby Isaac     }
2926bf4602e4SToby Isaac   }
2927d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
2928552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2929552f7358SJed Brown   PetscFunctionReturn(0);
2930552f7358SJed Brown }
2931552f7358SJed Brown 
2932*412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
2933ba2698f1SMatthew G. Knepley {
2934*412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
2935*412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
2936ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
2937ba2698f1SMatthew G. Knepley 
2938*412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
2939ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2940ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2941ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2942ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
2943ba2698f1SMatthew G. Knepley   if (depth <= 1) {
2944ba2698f1SMatthew G. Knepley     switch (pdepth) {
2945ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
2946ba2698f1SMatthew G. Knepley       case 1:
2947ba2698f1SMatthew G. Knepley         switch (coneSize) {
2948ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
2949ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
2950ba2698f1SMatthew G. Knepley           case 4:
2951ba2698f1SMatthew G. Knepley           switch (dim) {
2952ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
2953ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
2954ba2698f1SMatthew G. Knepley             default: break;
2955ba2698f1SMatthew G. Knepley           }
2956ba2698f1SMatthew G. Knepley           break;
2957ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
2958ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
2959ba2698f1SMatthew G. Knepley         default: break;
2960ba2698f1SMatthew G. Knepley       }
2961ba2698f1SMatthew G. Knepley     }
2962ba2698f1SMatthew G. Knepley   } else {
2963ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
2964ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
2965ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
2966ba2698f1SMatthew G. Knepley       switch (dim) {
2967ba2698f1SMatthew G. Knepley         case 1:
2968ba2698f1SMatthew G. Knepley           switch (coneSize) {
2969ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
2970ba2698f1SMatthew G. Knepley             default: break;
2971ba2698f1SMatthew G. Knepley           }
2972ba2698f1SMatthew G. Knepley           break;
2973ba2698f1SMatthew G. Knepley         case 2:
2974ba2698f1SMatthew G. Knepley           switch (coneSize) {
2975ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
2976ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
2977ba2698f1SMatthew G. Knepley             default: break;
2978ba2698f1SMatthew G. Knepley           }
2979ba2698f1SMatthew G. Knepley           break;
2980ba2698f1SMatthew G. Knepley         case 3:
2981ba2698f1SMatthew G. Knepley           switch (coneSize) {
2982ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
2983ba2698f1SMatthew G. Knepley             case 5: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
2984ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
2985ba2698f1SMatthew G. Knepley             default: break;
2986ba2698f1SMatthew G. Knepley           }
2987ba2698f1SMatthew G. Knepley           break;
2988ba2698f1SMatthew G. Knepley         default: break;
2989ba2698f1SMatthew G. Knepley       }
2990ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
2991ba2698f1SMatthew G. Knepley       switch (coneSize) {
2992ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
2993ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
2994ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
2995ba2698f1SMatthew G. Knepley         default: break;
2996ba2698f1SMatthew G. Knepley       }
2997ba2698f1SMatthew G. Knepley     }
2998ba2698f1SMatthew G. Knepley   }
2999*412e9a14SMatthew G. Knepley   *pt = ct;
3000*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3001ba2698f1SMatthew G. Knepley }
3002*412e9a14SMatthew G. Knepley 
3003*412e9a14SMatthew G. Knepley /*@
3004*412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
3005*412e9a14SMatthew G. Knepley 
3006*412e9a14SMatthew G. Knepley   Collective on dm
3007*412e9a14SMatthew G. Knepley 
3008*412e9a14SMatthew G. Knepley   Input Parameter:
3009*412e9a14SMatthew G. Knepley . mesh - The DMPlex
3010*412e9a14SMatthew G. Knepley 
3011*412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
3012*412e9a14SMatthew G. Knepley 
3013*412e9a14SMatthew G. Knepley   Level: developer
3014*412e9a14SMatthew G. Knepley 
3015*412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3016*412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3017*412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
3018*412e9a14SMatthew G. Knepley 
3019*412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3020*412e9a14SMatthew G. Knepley @*/
3021*412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
3022*412e9a14SMatthew G. Knepley {
3023*412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
3024*412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
3025*412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3026*412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3027*412e9a14SMatthew G. Knepley 
3028*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3029*412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3030*412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
3031*412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
3032*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
3033*412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3034*412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3035*412e9a14SMatthew G. Knepley     DMPolytopeType ct;
3036*412e9a14SMatthew G. Knepley     PetscInt       pdepth;
3037*412e9a14SMatthew G. Knepley 
3038*412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
3039*412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
3040*412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3041*412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
3042*412e9a14SMatthew G. Knepley   }
3043*412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
3044*412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
3045ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3046ba2698f1SMatthew G. Knepley }
3047ba2698f1SMatthew G. Knepley 
3048552f7358SJed Brown /*@C
3049552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
3050552f7358SJed Brown 
3051552f7358SJed Brown   Not Collective
3052552f7358SJed Brown 
3053552f7358SJed Brown   Input Parameters:
3054552f7358SJed Brown + dm - The DMPlex object
3055552f7358SJed Brown . numPoints - The number of input points for the join
3056552f7358SJed Brown - points - The input points
3057552f7358SJed Brown 
3058552f7358SJed Brown   Output Parameters:
3059552f7358SJed Brown + numCoveredPoints - The number of points in the join
3060552f7358SJed Brown - coveredPoints - The points in the join
3061552f7358SJed Brown 
3062552f7358SJed Brown   Level: intermediate
3063552f7358SJed Brown 
3064552f7358SJed Brown   Note: Currently, this is restricted to a single level join
3065552f7358SJed Brown 
30663813dfbdSMatthew G Knepley   Fortran Notes:
30673813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30683813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30693813dfbdSMatthew G Knepley 
30703813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30713813dfbdSMatthew G Knepley 
3072552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3073552f7358SJed Brown @*/
3074552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3075552f7358SJed Brown {
3076552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3077552f7358SJed Brown   PetscInt      *join[2];
3078552f7358SJed Brown   PetscInt       joinSize, i = 0;
3079552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3080552f7358SJed Brown   PetscErrorCode ierr;
3081552f7358SJed Brown 
3082552f7358SJed Brown   PetscFunctionBegin;
3083552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
308448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
308548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
308648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
308769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
308869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3089552f7358SJed Brown   /* Copy in support of first point */
3090552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
3091552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
3092552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3093552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3094552f7358SJed Brown   }
3095552f7358SJed Brown   /* Check each successive support */
3096552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3097552f7358SJed Brown     PetscInt newJoinSize = 0;
3098552f7358SJed Brown 
3099552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3100552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3101552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3102552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3103552f7358SJed Brown 
3104552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3105552f7358SJed Brown         if (point == join[i][m]) {
3106552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3107552f7358SJed Brown           break;
3108552f7358SJed Brown         }
3109552f7358SJed Brown       }
3110552f7358SJed Brown     }
3111552f7358SJed Brown     joinSize = newJoinSize;
3112552f7358SJed Brown     i        = 1-i;
3113552f7358SJed Brown   }
3114552f7358SJed Brown   *numCoveredPoints = joinSize;
3115552f7358SJed Brown   *coveredPoints    = join[i];
311669291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3117552f7358SJed Brown   PetscFunctionReturn(0);
3118552f7358SJed Brown }
3119552f7358SJed Brown 
3120552f7358SJed Brown /*@C
3121552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3122552f7358SJed Brown 
3123552f7358SJed Brown   Not Collective
3124552f7358SJed Brown 
3125552f7358SJed Brown   Input Parameters:
3126552f7358SJed Brown + dm - The DMPlex object
3127552f7358SJed Brown . numPoints - The number of input points for the join
3128552f7358SJed Brown - points - The input points
3129552f7358SJed Brown 
3130552f7358SJed Brown   Output Parameters:
3131552f7358SJed Brown + numCoveredPoints - The number of points in the join
3132552f7358SJed Brown - coveredPoints - The points in the join
3133552f7358SJed Brown 
31343813dfbdSMatthew G Knepley   Fortran Notes:
31353813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31363813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31373813dfbdSMatthew G Knepley 
31383813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31393813dfbdSMatthew G Knepley 
3140552f7358SJed Brown   Level: intermediate
3141552f7358SJed Brown 
3142552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3143552f7358SJed Brown @*/
3144552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3145552f7358SJed Brown {
3146552f7358SJed Brown   PetscErrorCode ierr;
3147552f7358SJed Brown 
3148552f7358SJed Brown   PetscFunctionBegin;
3149552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3150d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3151d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3152d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
315369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3154d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3155552f7358SJed Brown   PetscFunctionReturn(0);
3156552f7358SJed Brown }
3157552f7358SJed Brown 
3158552f7358SJed Brown /*@C
3159552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3160552f7358SJed Brown 
3161552f7358SJed Brown   Not Collective
3162552f7358SJed Brown 
3163552f7358SJed Brown   Input Parameters:
3164552f7358SJed Brown + dm - The DMPlex object
3165552f7358SJed Brown . numPoints - The number of input points for the join
3166552f7358SJed Brown - points - The input points
3167552f7358SJed Brown 
3168552f7358SJed Brown   Output Parameters:
3169552f7358SJed Brown + numCoveredPoints - The number of points in the join
3170552f7358SJed Brown - coveredPoints - The points in the join
3171552f7358SJed Brown 
31723813dfbdSMatthew G Knepley   Fortran Notes:
31733813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31743813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31753813dfbdSMatthew G Knepley 
31763813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31773813dfbdSMatthew G Knepley 
3178552f7358SJed Brown   Level: intermediate
3179552f7358SJed Brown 
3180552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3181552f7358SJed Brown @*/
3182552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3183552f7358SJed Brown {
3184552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3185552f7358SJed Brown   PetscInt      *offsets, **closures;
3186552f7358SJed Brown   PetscInt      *join[2];
3187552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
318824c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3189552f7358SJed Brown   PetscErrorCode ierr;
3190552f7358SJed Brown 
3191552f7358SJed Brown   PetscFunctionBegin;
3192552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
319348bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
319448bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
319548bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3196552f7358SJed Brown 
3197552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
31981795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
319969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
320024c766afSToby Isaac   ms      = mesh->maxSupportSize;
320124c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
320269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
320369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3204552f7358SJed Brown 
3205552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3206552f7358SJed Brown     PetscInt closureSize;
3207552f7358SJed Brown 
3208552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
32090d644c17SKarl Rupp 
3210552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3211552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3212552f7358SJed Brown       PetscInt pStart, pEnd, i;
3213552f7358SJed Brown 
3214552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3215552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3216552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3217552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3218552f7358SJed Brown           break;
3219552f7358SJed Brown         }
3220552f7358SJed Brown       }
3221552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3222552f7358SJed Brown     }
322382f516ccSBarry 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);
3224552f7358SJed Brown   }
3225552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3226552f7358SJed Brown     PetscInt dof;
3227552f7358SJed Brown 
3228552f7358SJed Brown     /* Copy in support of first point */
3229552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3230552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3231552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3232552f7358SJed Brown     }
3233552f7358SJed Brown     /* Check each successive cone */
3234552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3235552f7358SJed Brown       PetscInt newJoinSize = 0;
3236552f7358SJed Brown 
3237552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3238552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3239552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3240552f7358SJed Brown 
3241552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3242552f7358SJed Brown           if (point == join[i][m]) {
3243552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3244552f7358SJed Brown             break;
3245552f7358SJed Brown           }
3246552f7358SJed Brown         }
3247552f7358SJed Brown       }
3248552f7358SJed Brown       joinSize = newJoinSize;
3249552f7358SJed Brown       i        = 1-i;
3250552f7358SJed Brown     }
3251552f7358SJed Brown     if (joinSize) break;
3252552f7358SJed Brown   }
3253552f7358SJed Brown   *numCoveredPoints = joinSize;
3254552f7358SJed Brown   *coveredPoints    = join[i];
3255552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
32560298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3257552f7358SJed Brown   }
3258552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
325969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
326069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3261552f7358SJed Brown   PetscFunctionReturn(0);
3262552f7358SJed Brown }
3263552f7358SJed Brown 
3264552f7358SJed Brown /*@C
3265552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3266552f7358SJed Brown 
3267552f7358SJed Brown   Not Collective
3268552f7358SJed Brown 
3269552f7358SJed Brown   Input Parameters:
3270552f7358SJed Brown + dm - The DMPlex object
3271552f7358SJed Brown . numPoints - The number of input points for the meet
3272552f7358SJed Brown - points - The input points
3273552f7358SJed Brown 
3274552f7358SJed Brown   Output Parameters:
3275552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3276552f7358SJed Brown - coveredPoints - The points in the meet
3277552f7358SJed Brown 
3278552f7358SJed Brown   Level: intermediate
3279552f7358SJed Brown 
3280552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3281552f7358SJed Brown 
32823813dfbdSMatthew G Knepley   Fortran Notes:
32833813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32843813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32853813dfbdSMatthew G Knepley 
32863813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32873813dfbdSMatthew G Knepley 
3288552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3289552f7358SJed Brown @*/
3290552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3291552f7358SJed Brown {
3292552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3293552f7358SJed Brown   PetscInt      *meet[2];
3294552f7358SJed Brown   PetscInt       meetSize, i = 0;
3295552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3296552f7358SJed Brown   PetscErrorCode ierr;
3297552f7358SJed Brown 
3298552f7358SJed Brown   PetscFunctionBegin;
3299552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3300552f7358SJed Brown   PetscValidPointer(points, 2);
3301552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3302552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
330369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
330469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3305552f7358SJed Brown   /* Copy in cone of first point */
3306552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3307552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3308552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3309552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3310552f7358SJed Brown   }
3311552f7358SJed Brown   /* Check each successive cone */
3312552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3313552f7358SJed Brown     PetscInt newMeetSize = 0;
3314552f7358SJed Brown 
3315552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3316552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3317552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3318552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3319552f7358SJed Brown 
3320552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3321552f7358SJed Brown         if (point == meet[i][m]) {
3322552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3323552f7358SJed Brown           break;
3324552f7358SJed Brown         }
3325552f7358SJed Brown       }
3326552f7358SJed Brown     }
3327552f7358SJed Brown     meetSize = newMeetSize;
3328552f7358SJed Brown     i        = 1-i;
3329552f7358SJed Brown   }
3330552f7358SJed Brown   *numCoveringPoints = meetSize;
3331552f7358SJed Brown   *coveringPoints    = meet[i];
333269291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3333552f7358SJed Brown   PetscFunctionReturn(0);
3334552f7358SJed Brown }
3335552f7358SJed Brown 
3336552f7358SJed Brown /*@C
3337552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3338552f7358SJed Brown 
3339552f7358SJed Brown   Not Collective
3340552f7358SJed Brown 
3341552f7358SJed Brown   Input Parameters:
3342552f7358SJed Brown + dm - The DMPlex object
3343552f7358SJed Brown . numPoints - The number of input points for the meet
3344552f7358SJed Brown - points - The input points
3345552f7358SJed Brown 
3346552f7358SJed Brown   Output Parameters:
3347552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3348552f7358SJed Brown - coveredPoints - The points in the meet
3349552f7358SJed Brown 
3350552f7358SJed Brown   Level: intermediate
3351552f7358SJed Brown 
33523813dfbdSMatthew G Knepley   Fortran Notes:
33533813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33543813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33553813dfbdSMatthew G Knepley 
33563813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33573813dfbdSMatthew G Knepley 
3358552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3359552f7358SJed Brown @*/
3360552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3361552f7358SJed Brown {
3362552f7358SJed Brown   PetscErrorCode ierr;
3363552f7358SJed Brown 
3364552f7358SJed Brown   PetscFunctionBegin;
3365552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3366d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3367d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3368d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
336969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3370d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3371552f7358SJed Brown   PetscFunctionReturn(0);
3372552f7358SJed Brown }
3373552f7358SJed Brown 
3374552f7358SJed Brown /*@C
3375552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3376552f7358SJed Brown 
3377552f7358SJed Brown   Not Collective
3378552f7358SJed Brown 
3379552f7358SJed Brown   Input Parameters:
3380552f7358SJed Brown + dm - The DMPlex object
3381552f7358SJed Brown . numPoints - The number of input points for the meet
3382552f7358SJed Brown - points - The input points
3383552f7358SJed Brown 
3384552f7358SJed Brown   Output Parameters:
3385552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3386552f7358SJed Brown - coveredPoints - The points in the meet
3387552f7358SJed Brown 
3388552f7358SJed Brown   Level: intermediate
3389552f7358SJed Brown 
33903813dfbdSMatthew G Knepley   Fortran Notes:
33913813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
33923813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33933813dfbdSMatthew G Knepley 
33943813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33953813dfbdSMatthew G Knepley 
3396552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3397552f7358SJed Brown @*/
3398552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3399552f7358SJed Brown {
3400552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3401552f7358SJed Brown   PetscInt      *offsets, **closures;
3402552f7358SJed Brown   PetscInt      *meet[2];
3403552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
340424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3405552f7358SJed Brown   PetscErrorCode ierr;
3406552f7358SJed Brown 
3407552f7358SJed Brown   PetscFunctionBegin;
3408552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3409552f7358SJed Brown   PetscValidPointer(points, 2);
3410552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3411552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3412552f7358SJed Brown 
3413552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3414785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
341569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
341624c766afSToby Isaac   mc      = mesh->maxConeSize;
341724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
341869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
341969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3420552f7358SJed Brown 
3421552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3422552f7358SJed Brown     PetscInt closureSize;
3423552f7358SJed Brown 
3424552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
34250d644c17SKarl Rupp 
3426552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3427552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3428552f7358SJed Brown       PetscInt pStart, pEnd, i;
3429552f7358SJed Brown 
3430552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3431552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3432552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3433552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3434552f7358SJed Brown           break;
3435552f7358SJed Brown         }
3436552f7358SJed Brown       }
3437552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3438552f7358SJed Brown     }
343982f516ccSBarry 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);
3440552f7358SJed Brown   }
3441552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3442552f7358SJed Brown     PetscInt dof;
3443552f7358SJed Brown 
3444552f7358SJed Brown     /* Copy in cone of first point */
3445552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3446552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3447552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3448552f7358SJed Brown     }
3449552f7358SJed Brown     /* Check each successive cone */
3450552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3451552f7358SJed Brown       PetscInt newMeetSize = 0;
3452552f7358SJed Brown 
3453552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3454552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3455552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3456552f7358SJed Brown 
3457552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3458552f7358SJed Brown           if (point == meet[i][m]) {
3459552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3460552f7358SJed Brown             break;
3461552f7358SJed Brown           }
3462552f7358SJed Brown         }
3463552f7358SJed Brown       }
3464552f7358SJed Brown       meetSize = newMeetSize;
3465552f7358SJed Brown       i        = 1-i;
3466552f7358SJed Brown     }
3467552f7358SJed Brown     if (meetSize) break;
3468552f7358SJed Brown   }
3469552f7358SJed Brown   *numCoveredPoints = meetSize;
3470552f7358SJed Brown   *coveredPoints    = meet[i];
3471552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
34720298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3473552f7358SJed Brown   }
3474552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
347569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
347669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3477552f7358SJed Brown   PetscFunctionReturn(0);
3478552f7358SJed Brown }
3479552f7358SJed Brown 
34804e3744c5SMatthew G. Knepley /*@C
34814e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
34824e3744c5SMatthew G. Knepley 
34834e3744c5SMatthew G. Knepley   Not Collective
34844e3744c5SMatthew G. Knepley 
34854e3744c5SMatthew G. Knepley   Input Parameters:
34864e3744c5SMatthew G. Knepley + dmA - A DMPlex object
34874e3744c5SMatthew G. Knepley - dmB - A DMPlex object
34884e3744c5SMatthew G. Knepley 
34894e3744c5SMatthew G. Knepley   Output Parameters:
34904e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
34914e3744c5SMatthew G. Knepley 
34924e3744c5SMatthew G. Knepley   Level: intermediate
34934e3744c5SMatthew G. Knepley 
34944e3744c5SMatthew G. Knepley   Notes:
34954e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
34964e3744c5SMatthew G. Knepley 
34974e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
34984e3744c5SMatthew G. Knepley @*/
34994e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
35004e3744c5SMatthew G. Knepley {
35014e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
35024e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
35034e3744c5SMatthew G. Knepley 
35044e3744c5SMatthew G. Knepley   PetscFunctionBegin;
35054e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
35064e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
35074e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
35084e3744c5SMatthew G. Knepley 
35094e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
35104e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
35114e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
35124e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
35134e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
35144e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
35154e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
35164e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35174e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
35184e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
35194e3744c5SMatthew G. Knepley 
35204e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
35214e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
35224e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
35234e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
35244e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
35254e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
35264e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
35274e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
35284e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
35294e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
35304e3744c5SMatthew G. Knepley     }
35314e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
35324e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
35334e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
35344e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
35354e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
35364e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
35374e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
35384e3744c5SMatthew G. Knepley     }
35394e3744c5SMatthew G. Knepley   }
35404e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
35414e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
35424e3744c5SMatthew G. Knepley }
35434e3744c5SMatthew G. Knepley 
35447cd05799SMatthew G. Knepley /*@C
35457cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
35467cd05799SMatthew G. Knepley 
35477cd05799SMatthew G. Knepley   Not Collective
35487cd05799SMatthew G. Knepley 
35497cd05799SMatthew G. Knepley   Input Parameters:
35507cd05799SMatthew G. Knepley + dm         - The DMPlex
35517cd05799SMatthew G. Knepley . cellDim    - The cell dimension
35527cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
35537cd05799SMatthew G. Knepley 
35547cd05799SMatthew G. Knepley   Output Parameters:
35557cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
35567cd05799SMatthew G. Knepley 
35577cd05799SMatthew G. Knepley   Level: developer
35587cd05799SMatthew G. Knepley 
35597cd05799SMatthew G. Knepley   Notes:
35607cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
35617cd05799SMatthew G. Knepley 
35627cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
35637cd05799SMatthew G. Knepley @*/
356418ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3565a6dfd86eSKarl Rupp {
356682f516ccSBarry Smith   MPI_Comm       comm;
3567552f7358SJed Brown   PetscErrorCode ierr;
3568552f7358SJed Brown 
3569552f7358SJed Brown   PetscFunctionBegin;
357082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3571552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3572552f7358SJed Brown   switch (cellDim) {
3573552f7358SJed Brown   case 0:
3574552f7358SJed Brown     *numFaceVertices = 0;
3575552f7358SJed Brown     break;
3576552f7358SJed Brown   case 1:
3577552f7358SJed Brown     *numFaceVertices = 1;
3578552f7358SJed Brown     break;
3579552f7358SJed Brown   case 2:
3580552f7358SJed Brown     switch (numCorners) {
358119436ca2SJed Brown     case 3: /* triangle */
358219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3583552f7358SJed Brown       break;
358419436ca2SJed Brown     case 4: /* quadrilateral */
358519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3586552f7358SJed Brown       break;
358719436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
358819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3589552f7358SJed Brown       break;
359019436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
359119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3592552f7358SJed Brown       break;
3593552f7358SJed Brown     default:
3594f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3595552f7358SJed Brown     }
3596552f7358SJed Brown     break;
3597552f7358SJed Brown   case 3:
3598552f7358SJed Brown     switch (numCorners) {
359919436ca2SJed Brown     case 4: /* tetradehdron */
360019436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3601552f7358SJed Brown       break;
360219436ca2SJed Brown     case 6: /* tet cohesive cells */
360319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3604552f7358SJed Brown       break;
360519436ca2SJed Brown     case 8: /* hexahedron */
360619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3607552f7358SJed Brown       break;
360819436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
360919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3610552f7358SJed Brown       break;
361119436ca2SJed Brown     case 10: /* quadratic tetrahedron */
361219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3613552f7358SJed Brown       break;
361419436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
361519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3616552f7358SJed Brown       break;
361719436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
361819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3619552f7358SJed Brown       break;
362019436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
362119436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 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   default:
3628f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3629552f7358SJed Brown   }
3630552f7358SJed Brown   PetscFunctionReturn(0);
3631552f7358SJed Brown }
3632552f7358SJed Brown 
3633552f7358SJed Brown /*@
3634aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3635552f7358SJed Brown 
3636552f7358SJed Brown   Not Collective
3637552f7358SJed Brown 
3638aa50250dSMatthew G. Knepley   Input Parameter:
3639552f7358SJed Brown . dm    - The DMPlex object
3640552f7358SJed Brown 
3641aa50250dSMatthew G. Knepley   Output Parameter:
3642aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3643552f7358SJed Brown 
3644552f7358SJed Brown   Level: developer
3645552f7358SJed Brown 
3646aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3647aa50250dSMatthew G. Knepley @*/
3648aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3649aa50250dSMatthew G. Knepley {
3650aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3651aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3652aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3653c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3654aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3655aa50250dSMatthew G. Knepley }
3656aa50250dSMatthew G. Knepley 
3657aa50250dSMatthew G. Knepley /*@
3658aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3659aa50250dSMatthew G. Knepley 
3660aa50250dSMatthew G. Knepley   Not Collective
3661aa50250dSMatthew G. Knepley 
3662aa50250dSMatthew G. Knepley   Input Parameter:
3663aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3664aa50250dSMatthew G. Knepley 
3665aa50250dSMatthew G. Knepley   Output Parameter:
3666aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3667aa50250dSMatthew G. Knepley 
3668aa50250dSMatthew G. Knepley   Level: developer
3669552f7358SJed Brown 
3670b1bb481bSMatthew Knepley   Notes:
3671b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3672b1bb481bSMatthew Knepley   The point depth is described more in detail in DMPlexSymmetrize().
3673b1bb481bSMatthew Knepley 
36743dc9a465SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexGetPointHeight(), DMPlexSymmetrize()
3675552f7358SJed Brown @*/
3676552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3677552f7358SJed Brown {
3678aa50250dSMatthew G. Knepley   DMLabel        label;
3679aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3680552f7358SJed Brown   PetscErrorCode ierr;
3681552f7358SJed Brown 
3682552f7358SJed Brown   PetscFunctionBegin;
3683552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3684552f7358SJed Brown   PetscValidPointer(depth, 2);
3685aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3686aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3687552f7358SJed Brown   *depth = d-1;
3688552f7358SJed Brown   PetscFunctionReturn(0);
3689552f7358SJed Brown }
3690552f7358SJed Brown 
3691552f7358SJed Brown /*@
3692552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3693552f7358SJed Brown 
3694552f7358SJed Brown   Not Collective
3695552f7358SJed Brown 
3696552f7358SJed Brown   Input Parameters:
3697552f7358SJed Brown + dm           - The DMPlex object
3698552f7358SJed Brown - stratumValue - The requested depth
3699552f7358SJed Brown 
3700552f7358SJed Brown   Output Parameters:
3701552f7358SJed Brown + start - The first point at this depth
3702552f7358SJed Brown - end   - One beyond the last point at this depth
3703552f7358SJed Brown 
3704647867b2SJed Brown   Notes:
3705647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3706647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3707647867b2SJed Brown   higher dimension, e.g., "edges".
3708647867b2SJed Brown 
3709552f7358SJed Brown   Level: developer
3710552f7358SJed Brown 
37113dc9a465SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetPointDepth()
3712552f7358SJed Brown @*/
37130adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37140adebc6cSBarry Smith {
3715aa50250dSMatthew G. Knepley   DMLabel        label;
371663d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3717552f7358SJed Brown   PetscErrorCode ierr;
3718552f7358SJed Brown 
3719552f7358SJed Brown   PetscFunctionBegin;
3720552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
372163d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
372263d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3723552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
37240d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
372563d1a920SMatthew G. Knepley   if (stratumValue < 0) {
372663d1a920SMatthew G. Knepley     if (start) *start = pStart;
372763d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
372863d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3729552f7358SJed Brown   }
3730aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
373163d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
373263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3733552f7358SJed Brown   PetscFunctionReturn(0);
3734552f7358SJed Brown }
3735552f7358SJed Brown 
3736552f7358SJed Brown /*@
3737552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3738552f7358SJed Brown 
3739552f7358SJed Brown   Not Collective
3740552f7358SJed Brown 
3741552f7358SJed Brown   Input Parameters:
3742552f7358SJed Brown + dm           - The DMPlex object
3743552f7358SJed Brown - stratumValue - The requested height
3744552f7358SJed Brown 
3745552f7358SJed Brown   Output Parameters:
3746552f7358SJed Brown + start - The first point at this height
3747552f7358SJed Brown - end   - One beyond the last point at this height
3748552f7358SJed Brown 
3749647867b2SJed Brown   Notes:
3750647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3751647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3752647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3753647867b2SJed Brown 
3754552f7358SJed Brown   Level: developer
3755552f7358SJed Brown 
37563dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
3757552f7358SJed Brown @*/
37580adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
37590adebc6cSBarry Smith {
3760aa50250dSMatthew G. Knepley   DMLabel        label;
376163d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3762552f7358SJed Brown   PetscErrorCode ierr;
3763552f7358SJed Brown 
3764552f7358SJed Brown   PetscFunctionBegin;
3765552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
376663d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
376763d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3768552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
37690d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
377063d1a920SMatthew G. Knepley   if (stratumValue < 0) {
377163d1a920SMatthew G. Knepley     if (start) *start = pStart;
377263d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
377363d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3774552f7358SJed Brown   }
3775aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
377613903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
377763d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
377863d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3779552f7358SJed Brown   PetscFunctionReturn(0);
3780552f7358SJed Brown }
3781552f7358SJed Brown 
3782ba2698f1SMatthew G. Knepley /*@
3783ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
3784ba2698f1SMatthew G. Knepley 
3785ba2698f1SMatthew G. Knepley   Not Collective
3786ba2698f1SMatthew G. Knepley 
3787ba2698f1SMatthew G. Knepley   Input Parameter:
3788ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
3789ba2698f1SMatthew G. Knepley - point - The point
3790ba2698f1SMatthew G. Knepley 
3791ba2698f1SMatthew G. Knepley   Output Parameter:
3792ba2698f1SMatthew G. Knepley . depth - The depth of the point
3793ba2698f1SMatthew G. Knepley 
3794ba2698f1SMatthew G. Knepley   Level: intermediate
3795ba2698f1SMatthew G. Knepley 
37963dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
3797ba2698f1SMatthew G. Knepley @*/
3798ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
3799ba2698f1SMatthew G. Knepley {
3800ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3801ba2698f1SMatthew G. Knepley 
3802ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3803ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
380440a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
3805ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
3806ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3807ba2698f1SMatthew G. Knepley }
3808ba2698f1SMatthew G. Knepley 
3809ba2698f1SMatthew G. Knepley /*@
38100c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
38110c0a32dcSVaclav Hapla 
38120c0a32dcSVaclav Hapla   Not Collective
38130c0a32dcSVaclav Hapla 
38140c0a32dcSVaclav Hapla   Input Parameter:
38150c0a32dcSVaclav Hapla + dm    - The DMPlex object
38160c0a32dcSVaclav Hapla - point - The point
38170c0a32dcSVaclav Hapla 
38180c0a32dcSVaclav Hapla   Output Parameter:
38190c0a32dcSVaclav Hapla . height - The height of the point
38200c0a32dcSVaclav Hapla 
38210c0a32dcSVaclav Hapla   Level: intermediate
38220c0a32dcSVaclav Hapla 
38233dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
38240c0a32dcSVaclav Hapla @*/
38250c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
38260c0a32dcSVaclav Hapla {
38270c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
38280c0a32dcSVaclav Hapla   PetscErrorCode ierr;
38290c0a32dcSVaclav Hapla 
38300c0a32dcSVaclav Hapla   PetscFunctionBegin;
38310c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38320c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
38330c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
38340c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
38350c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
38360c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
38370c0a32dcSVaclav Hapla }
38380c0a32dcSVaclav Hapla 
38390c0a32dcSVaclav Hapla /*@
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 
3850*412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
3851*412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
3852*412e9a14SMatthew G. Knepley 
3853ba2698f1SMatthew G. Knepley   Level: developer
3854ba2698f1SMatthew G. Knepley 
3855*412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMCreateLabel()
3856ba2698f1SMatthew G. Knepley @*/
3857ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
3858ba2698f1SMatthew G. Knepley {
3859ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3860ba2698f1SMatthew G. Knepley 
3861ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3862ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3863ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
3864ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
3865ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
3866ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3867ba2698f1SMatthew G. Knepley }
3868ba2698f1SMatthew G. Knepley 
3869ba2698f1SMatthew G. Knepley /*@
3870ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
3871ba2698f1SMatthew G. Knepley 
3872ba2698f1SMatthew G. Knepley   Not Collective
3873ba2698f1SMatthew G. Knepley 
3874ba2698f1SMatthew G. Knepley   Input Parameter:
3875ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
3876ba2698f1SMatthew G. Knepley - cell - The cell
3877ba2698f1SMatthew G. Knepley 
3878ba2698f1SMatthew G. Knepley   Output Parameter:
3879ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
3880ba2698f1SMatthew G. Knepley 
3881ba2698f1SMatthew G. Knepley   Level: intermediate
3882ba2698f1SMatthew G. Knepley 
3883ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3884ba2698f1SMatthew G. Knepley @*/
3885ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
3886ba2698f1SMatthew G. Knepley {
3887ba2698f1SMatthew G. Knepley   DMLabel        label;
3888ba2698f1SMatthew G. Knepley   PetscInt       ct;
3889ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3890ba2698f1SMatthew G. Knepley 
3891ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
3892ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3893ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
3894ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3895ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
3896ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
3897ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
3898ba2698f1SMatthew G. Knepley }
3899ba2698f1SMatthew G. Knepley 
3900*412e9a14SMatthew G. Knepley /*@
3901*412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
3902*412e9a14SMatthew G. Knepley 
3903*412e9a14SMatthew G. Knepley   Not Collective
3904*412e9a14SMatthew G. Knepley 
3905*412e9a14SMatthew G. Knepley   Input Parameters:
3906*412e9a14SMatthew G. Knepley + dm   - The DMPlex object
3907*412e9a14SMatthew G. Knepley . cell - The cell
3908*412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
3909*412e9a14SMatthew G. Knepley 
3910*412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
3911*412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
3912*412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
3913*412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
3914*412e9a14SMatthew G. Knepley 
3915*412e9a14SMatthew G. Knepley   Level: advanced
3916*412e9a14SMatthew G. Knepley 
3917*412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
3918*412e9a14SMatthew G. Knepley @*/
3919*412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
3920*412e9a14SMatthew G. Knepley {
3921*412e9a14SMatthew G. Knepley   DMLabel        label;
3922*412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
3923*412e9a14SMatthew G. Knepley 
3924*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
3925*412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3926*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
3927*412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
3928*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
3929*412e9a14SMatthew G. Knepley }
3930*412e9a14SMatthew G. Knepley 
39310adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
39320adebc6cSBarry Smith {
3933efe440bfSMatthew G. Knepley   PetscSection   section, s;
3934efe440bfSMatthew G. Knepley   Mat            m;
39353e922f36SToby Isaac   PetscInt       maxHeight;
3936552f7358SJed Brown   PetscErrorCode ierr;
3937552f7358SJed Brown 
3938552f7358SJed Brown   PetscFunctionBegin;
393938221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
39403e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
39413e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
394282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
394392fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
39441d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3945efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3946efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3947efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3948efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3949efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
39508f4c458bSMatthew G. Knepley 
39518f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
39528f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3953552f7358SJed Brown   PetscFunctionReturn(0);
3954552f7358SJed Brown }
3955552f7358SJed Brown 
3956f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3957f19dbd58SToby Isaac {
3958f19dbd58SToby Isaac   Vec            coordsLocal;
3959f19dbd58SToby Isaac   DM             coordsDM;
3960f19dbd58SToby Isaac   PetscErrorCode ierr;
3961f19dbd58SToby Isaac 
3962f19dbd58SToby Isaac   PetscFunctionBegin;
3963f19dbd58SToby Isaac   *field = NULL;
3964f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3965f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3966f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3967f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3968f19dbd58SToby Isaac   }
3969f19dbd58SToby Isaac   PetscFunctionReturn(0);
3970f19dbd58SToby Isaac }
3971f19dbd58SToby Isaac 
39727cd05799SMatthew G. Knepley /*@C
39737cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
39747cd05799SMatthew G. Knepley 
39757cd05799SMatthew G. Knepley   Not Collective
39767cd05799SMatthew G. Knepley 
39777cd05799SMatthew G. Knepley   Input Parameters:
39787cd05799SMatthew G. Knepley . dm        - The DMPlex object
39797cd05799SMatthew G. Knepley 
39807cd05799SMatthew G. Knepley   Output Parameter:
39817cd05799SMatthew G. Knepley . section - The PetscSection object
39827cd05799SMatthew G. Knepley 
39837cd05799SMatthew G. Knepley   Level: developer
39847cd05799SMatthew G. Knepley 
39857cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
39867cd05799SMatthew G. Knepley @*/
39870adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
39880adebc6cSBarry Smith {
3989552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3990552f7358SJed Brown 
3991552f7358SJed Brown   PetscFunctionBegin;
3992552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3993552f7358SJed Brown   if (section) *section = mesh->coneSection;
3994552f7358SJed Brown   PetscFunctionReturn(0);
3995552f7358SJed Brown }
3996552f7358SJed Brown 
39977cd05799SMatthew G. Knepley /*@C
39987cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
39997cd05799SMatthew G. Knepley 
40007cd05799SMatthew G. Knepley   Not Collective
40017cd05799SMatthew G. Knepley 
40027cd05799SMatthew G. Knepley   Input Parameters:
40037cd05799SMatthew G. Knepley . dm        - The DMPlex object
40047cd05799SMatthew G. Knepley 
40057cd05799SMatthew G. Knepley   Output Parameter:
40067cd05799SMatthew G. Knepley . section - The PetscSection object
40077cd05799SMatthew G. Knepley 
40087cd05799SMatthew G. Knepley   Level: developer
40097cd05799SMatthew G. Knepley 
40107cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40117cd05799SMatthew G. Knepley @*/
40128cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
40138cb4d582SMatthew G. Knepley {
40148cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
40158cb4d582SMatthew G. Knepley 
40168cb4d582SMatthew G. Knepley   PetscFunctionBegin;
40178cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40188cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
40198cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
40208cb4d582SMatthew G. Knepley }
40218cb4d582SMatthew G. Knepley 
40227cd05799SMatthew G. Knepley /*@C
40237cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
40247cd05799SMatthew G. Knepley 
40257cd05799SMatthew G. Knepley   Not Collective
40267cd05799SMatthew G. Knepley 
40277cd05799SMatthew G. Knepley   Input Parameters:
40287cd05799SMatthew G. Knepley . dm        - The DMPlex object
40297cd05799SMatthew G. Knepley 
40307cd05799SMatthew G. Knepley   Output Parameter:
40317cd05799SMatthew G. Knepley . cones - The cone for each point
40327cd05799SMatthew G. Knepley 
40337cd05799SMatthew G. Knepley   Level: developer
40347cd05799SMatthew G. Knepley 
40357cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40367cd05799SMatthew G. Knepley @*/
4037a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4038a6dfd86eSKarl Rupp {
4039552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4040552f7358SJed Brown 
4041552f7358SJed Brown   PetscFunctionBegin;
4042552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4043552f7358SJed Brown   if (cones) *cones = mesh->cones;
4044552f7358SJed Brown   PetscFunctionReturn(0);
4045552f7358SJed Brown }
4046552f7358SJed Brown 
40477cd05799SMatthew G. Knepley /*@C
40487cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
40497cd05799SMatthew G. Knepley 
40507cd05799SMatthew G. Knepley   Not Collective
40517cd05799SMatthew G. Knepley 
40527cd05799SMatthew G. Knepley   Input Parameters:
40537cd05799SMatthew G. Knepley . dm        - The DMPlex object
40547cd05799SMatthew G. Knepley 
40557cd05799SMatthew G. Knepley   Output Parameter:
40567cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
40577cd05799SMatthew G. Knepley 
40587cd05799SMatthew G. Knepley   Level: developer
40597cd05799SMatthew G. Knepley 
40607cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
40617cd05799SMatthew G. Knepley @*/
4062a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4063a6dfd86eSKarl Rupp {
4064552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4065552f7358SJed Brown 
4066552f7358SJed Brown   PetscFunctionBegin;
4067552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4068552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4069552f7358SJed Brown   PetscFunctionReturn(0);
4070552f7358SJed Brown }
4071552f7358SJed Brown 
4072552f7358SJed Brown /******************************** FEM Support **********************************/
4073552f7358SJed Brown 
40749e8305c2SJed Brown /*
40759e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
40769e8305c2SJed Brown  representing a line in the section.
40779e8305c2SJed Brown */
40789e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
40799e8305c2SJed Brown {
40809e8305c2SJed Brown   PetscErrorCode ierr;
40819e8305c2SJed Brown 
40829e8305c2SJed Brown   PetscFunctionBeginHot;
40839e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
4084a433471fSStefano Zampini   if (line < 0) {
4085a433471fSStefano Zampini     *k = 0;
4086a433471fSStefano Zampini     *Nc = 0;
4087a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
40889e8305c2SJed Brown     *k = 1;
40899e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
40909e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
40919e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
40929e8305c2SJed Brown     *k = *k / *Nc + 1;
40939e8305c2SJed Brown   }
40949e8305c2SJed Brown   PetscFunctionReturn(0);
40959e8305c2SJed Brown }
40969e8305c2SJed Brown 
4097a4355906SMatthew Knepley /*@
4098bc1eb3faSJed Brown 
4099bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
4100bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
41011bb6d2a8SBarry Smith   section provided (or the section of the DM).
4102a4355906SMatthew Knepley 
4103a4355906SMatthew Knepley   Input Parameters:
4104a4355906SMatthew Knepley + dm      - The DM
4105a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4106a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
4107a4355906SMatthew Knepley 
4108a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
4109a4355906SMatthew Knepley   degree of the basis.
4110a4355906SMatthew Knepley 
4111bc1eb3faSJed Brown   Example:
4112bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
4113bc1eb3faSJed Brown .vb
4114bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
4115bc1eb3faSJed Brown 
4116bc1eb3faSJed Brown   v4 -- e6 -- v3
4117bc1eb3faSJed Brown   |           |
4118bc1eb3faSJed Brown   e7    c0    e8
4119bc1eb3faSJed Brown   |           |
4120bc1eb3faSJed Brown   v1 -- e5 -- v2
4121bc1eb3faSJed Brown .ve
4122bc1eb3faSJed Brown 
4123bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4124bc1eb3faSJed Brown   dofs in the order of points, e.g.,
4125bc1eb3faSJed Brown .vb
4126bc1eb3faSJed Brown     c0 -> [0,1,2,3]
4127bc1eb3faSJed Brown     v1 -> [4]
4128bc1eb3faSJed Brown     ...
4129bc1eb3faSJed Brown     e5 -> [8, 9]
4130bc1eb3faSJed Brown .ve
4131bc1eb3faSJed Brown 
4132bc1eb3faSJed Brown   which corresponds to the dofs
4133bc1eb3faSJed Brown .vb
4134bc1eb3faSJed Brown     6   10  11  7
4135bc1eb3faSJed Brown     13  2   3   15
4136bc1eb3faSJed Brown     12  0   1   14
4137bc1eb3faSJed Brown     4   8   9   5
4138bc1eb3faSJed Brown .ve
4139bc1eb3faSJed Brown 
4140bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
4141bc1eb3faSJed Brown .vb
4142bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
4143bc1eb3faSJed Brown .ve
4144bc1eb3faSJed Brown 
4145bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
4146bc1eb3faSJed Brown .vb
4147bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
4148bc1eb3faSJed Brown .ve
4149bc1eb3faSJed Brown 
4150a4355906SMatthew Knepley   Level: developer
4151a4355906SMatthew Knepley 
41529df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4153a4355906SMatthew Knepley @*/
4154bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
41553194fc30SMatthew G. Knepley {
41567391a63aSMatthew G. Knepley   DMLabel        label;
41573194fc30SMatthew G. Knepley   PetscInt      *perm;
4158a433471fSStefano Zampini   PetscInt       dim, depth = -1, eStart = -1, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
41599e8305c2SJed Brown   PetscBool      vertexchart;
41603194fc30SMatthew G. Knepley   PetscErrorCode ierr;
41613194fc30SMatthew G. Knepley 
41623194fc30SMatthew G. Knepley   PetscFunctionBegin;
41633194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4164a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
4165a433471fSStefano Zampini   if (point < 0) {
4166a433471fSStefano Zampini     PetscInt sStart,sEnd;
4167a433471fSStefano Zampini 
4168a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
4169a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
4170a433471fSStefano Zampini   }
41717391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4172a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
4173a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
41747391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
41757391a63aSMatthew G. Knepley   else if  (depth == dim) {
41767391a63aSMatthew G. Knepley     const PetscInt *cone;
41777391a63aSMatthew G. Knepley 
41787391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4179d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
4180d4e6627bSStefano Zampini     else if (dim == 3) {
4181d4e6627bSStefano Zampini       const PetscInt *cone2;
4182d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
4183d4e6627bSStefano Zampini       eStart = cone2[0];
4184d4e6627bSStefano 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);
4185a433471fSStefano 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);
41869e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
41879e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
41889e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
41899e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
41909e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
41919e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
41929e8305c2SJed Brown   }
41933194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
41943194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
41959e8305c2SJed Brown     ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
419689eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
41973194fc30SMatthew G. Knepley   }
41983194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
41993194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
420089eabcffSMatthew G. Knepley     switch (dim) {
4201babf31e0SJed Brown     case 1:
42029e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
4203babf31e0SJed Brown       /*
4204babf31e0SJed Brown         Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4205babf31e0SJed Brown         We want              [ vtx0; edge of length k-1; vtx1 ]
4206babf31e0SJed Brown       */
4207babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4208babf31e0SJed Brown       for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4209babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4210babf31e0SJed Brown       foffset = offset;
4211babf31e0SJed Brown       break;
421289eabcffSMatthew G. Knepley     case 2:
42133194fc30SMatthew 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} */
42149e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
42153194fc30SMatthew G. Knepley       /* The SEM order is
42163194fc30SMatthew G. Knepley 
42173194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
421889eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
42193194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
42203194fc30SMatthew G. Knepley       */
42213194fc30SMatthew G. Knepley       {
42223194fc30SMatthew G. Knepley         const PetscInt of   = 0;
42233194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
42243194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
42253194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
42263194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
42273194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
42283194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
42293194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
42303194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
42313194fc30SMatthew G. Knepley         PetscInt       o;
42323194fc30SMatthew G. Knepley 
42333194fc30SMatthew G. Knepley         /* bottom */
42343194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
42353194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
42363194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
42373194fc30SMatthew G. Knepley         /* middle */
42383194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
42393194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
42403194fc30SMatthew 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;
42413194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
42423194fc30SMatthew G. Knepley         }
42433194fc30SMatthew G. Knepley         /* top */
42443194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
42453194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
42463194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
42473194fc30SMatthew G. Knepley         foffset = offset;
42483194fc30SMatthew G. Knepley       }
424989eabcffSMatthew G. Knepley       break;
425089eabcffSMatthew G. Knepley     case 3:
425189eabcffSMatthew G. Knepley       /* The original hex closure is
425289eabcffSMatthew G. Knepley 
425389eabcffSMatthew G. Knepley          {c,
425489eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
425589eabcffSMatthew 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,
425689eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
425789eabcffSMatthew G. Knepley       */
42589e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
425989eabcffSMatthew G. Knepley       /* The SEM order is
426089eabcffSMatthew G. Knepley          Bottom Slice
426189eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
426289eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
426389eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
426489eabcffSMatthew G. Knepley 
426589eabcffSMatthew G. Knepley          Middle Slice (j)
426689eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
426789eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
426889eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
426989eabcffSMatthew G. Knepley 
427089eabcffSMatthew G. Knepley          Top Slice
427189eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
427289eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
427389eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
427489eabcffSMatthew G. Knepley       */
427589eabcffSMatthew G. Knepley       {
427689eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
427789eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
427889eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
427989eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
428089eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
428189eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
428289eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
428389eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
428489eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
428589eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
428689eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
428789eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
428889eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
428989eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
429089eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
429189eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
429289eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
429389eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
429489eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
429589eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
429689eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
429789eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
429889eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
429989eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
430089eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
430189eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
430289eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
430389eabcffSMatthew G. Knepley         PetscInt       o, n;
430489eabcffSMatthew G. Knepley 
430589eabcffSMatthew G. Knepley         /* Bottom Slice */
430689eabcffSMatthew G. Knepley         /*   bottom */
430789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
430889eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --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] = ovbrf*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] = (oebl+i)*Nc + c + foffset;
4313316b7f87SMax 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;}
431489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
43153194fc30SMatthew G. Knepley         }
431689eabcffSMatthew G. Knepley         /*   top */
431789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
431889eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++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] = ovbrb*Nc + c + foffset;
432089eabcffSMatthew G. Knepley 
432189eabcffSMatthew G. Knepley         /* Middle Slice */
432289eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
432389eabcffSMatthew G. Knepley           /*   bottom */
432489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
432589eabcffSMatthew 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;
432689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
432789eabcffSMatthew G. Knepley           /*   middle */
432889eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
432989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
433089eabcffSMatthew 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;
433189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
433289eabcffSMatthew G. Knepley           }
433389eabcffSMatthew G. Knepley           /*   top */
433489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
433589eabcffSMatthew 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;
433689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
433789eabcffSMatthew G. Knepley         }
433889eabcffSMatthew G. Knepley 
433989eabcffSMatthew G. Knepley         /* Top Slice */
434089eabcffSMatthew G. Knepley         /*   bottom */
434189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
434289eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
434389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
434489eabcffSMatthew G. Knepley         /*   middle */
434589eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
434689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
434789eabcffSMatthew 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;
434889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
434989eabcffSMatthew G. Knepley         }
435089eabcffSMatthew G. Knepley         /*   top */
435189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
435289eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
435389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
435489eabcffSMatthew G. Knepley 
435589eabcffSMatthew G. Knepley         foffset = offset;
435689eabcffSMatthew G. Knepley       }
435789eabcffSMatthew G. Knepley       break;
435889eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
435989eabcffSMatthew G. Knepley     }
436089eabcffSMatthew G. Knepley   }
436189eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
43623194fc30SMatthew G. Knepley   /* Check permutation */
43633194fc30SMatthew G. Knepley   {
43643194fc30SMatthew G. Knepley     PetscInt *check;
43653194fc30SMatthew G. Knepley 
43663194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
43673194fc30SMatthew 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]);}
43683194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
43693194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
43703194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
43713194fc30SMatthew G. Knepley   }
43721fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
43733194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
43743194fc30SMatthew G. Knepley }
43753194fc30SMatthew G. Knepley 
4376e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4377e071409bSToby Isaac {
4378e071409bSToby Isaac   PetscDS        prob;
4379e071409bSToby Isaac   PetscInt       depth, Nf, h;
4380e071409bSToby Isaac   DMLabel        label;
4381e071409bSToby Isaac   PetscErrorCode ierr;
4382e071409bSToby Isaac 
4383e071409bSToby Isaac   PetscFunctionBeginHot;
4384e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4385e071409bSToby Isaac   Nf      = prob->Nf;
4386e071409bSToby Isaac   label   = dm->depthLabel;
4387e071409bSToby Isaac   *dspace = NULL;
4388e071409bSToby Isaac   if (field < Nf) {
4389e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4390e071409bSToby Isaac 
4391e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4392e071409bSToby Isaac       PetscDualSpace dsp;
4393e071409bSToby Isaac 
4394e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4395e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4396e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4397e071409bSToby Isaac       h    = depth - 1 - h;
4398e071409bSToby Isaac       if (h) {
4399e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4400e071409bSToby Isaac       } else {
4401e071409bSToby Isaac         *dspace = dsp;
4402e071409bSToby Isaac       }
4403e071409bSToby Isaac     }
4404e071409bSToby Isaac   }
4405e071409bSToby Isaac   PetscFunctionReturn(0);
4406e071409bSToby Isaac }
4407e071409bSToby Isaac 
4408e071409bSToby Isaac 
44091a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4410a6dfd86eSKarl Rupp {
4411552f7358SJed Brown   PetscScalar    *array, *vArray;
4412d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
44131a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4414552f7358SJed Brown   PetscErrorCode  ierr;
4415552f7358SJed Brown 
44161b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
44172a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
44185a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
44195a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
44205a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
44213f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
44229df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
44239df71ca4SMatthew G. Knepley       PetscInt dof;
4424d9917b9dSMatthew G. Knepley 
44259df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
44269df71ca4SMatthew G. Knepley       size += dof;
44279df71ca4SMatthew G. Knepley     }
44289df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
44299df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
44302a3aaacfSMatthew G. Knepley       PetscInt       dof;
44315a1bb5cfSMatthew G. Knepley 
44325a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
44332a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
44345a1bb5cfSMatthew G. Knepley       size += dof;
44355a1bb5cfSMatthew G. Knepley     }
44363f7cbbe7SMatthew G. Knepley     if (!values) {
44373f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
44383f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
44393f7cbbe7SMatthew G. Knepley     }
444069291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4441982e9ed1SMatthew G. Knepley   } else {
4442982e9ed1SMatthew G. Knepley     array = *values;
4443982e9ed1SMatthew G. Knepley   }
44449df71ca4SMatthew G. Knepley   size = 0;
44455a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
44469df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
44479df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
44489df71ca4SMatthew G. Knepley     PetscScalar *varr;
4449d9917b9dSMatthew G. Knepley 
44509df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
44519df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
44529df71ca4SMatthew G. Knepley     varr = &vArray[off];
44531a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
44541a271a75SMatthew G. Knepley       array[offset] = varr[d];
44559df71ca4SMatthew G. Knepley     }
44569df71ca4SMatthew G. Knepley     size += dof;
44579df71ca4SMatthew G. Knepley   }
44589df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
44599df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
44609df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
44615a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
44625a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
44635a1bb5cfSMatthew G. Knepley 
446452ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
44655a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
44665a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
44675a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
44685a1bb5cfSMatthew G. Knepley     if (o >= 0) {
44691a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
44701a271a75SMatthew G. Knepley         array[offset] = varr[d];
44715a1bb5cfSMatthew G. Knepley       }
44725a1bb5cfSMatthew G. Knepley     } else {
44731a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
44741a271a75SMatthew G. Knepley         array[offset] = varr[d];
44755a1bb5cfSMatthew G. Knepley       }
44765a1bb5cfSMatthew G. Knepley     }
44779df71ca4SMatthew G. Knepley     size += dof;
44785a1bb5cfSMatthew G. Knepley   }
44795a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
44809df71ca4SMatthew G. Knepley   if (!*values) {
44815a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
44825a1bb5cfSMatthew G. Knepley     *values = array;
44839df71ca4SMatthew G. Knepley   } else {
44848ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
44858c312ff3SMatthew G. Knepley     *csize = size;
44869df71ca4SMatthew G. Knepley   }
44875a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
44885a1bb5cfSMatthew G. Knepley }
4489d9917b9dSMatthew G. Knepley 
449097529cf3SJed Brown /* Compressed closure does not apply closure permutation */
44911dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4492923c78e0SToby Isaac {
4493923c78e0SToby Isaac   const PetscInt *cla;
4494923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4495923c78e0SToby Isaac   PetscErrorCode ierr;
4496923c78e0SToby Isaac 
4497923c78e0SToby Isaac   PetscFunctionBeginHot;
4498923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
4499923c78e0SToby Isaac   if (!*clPoints) {
4500923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
4501923c78e0SToby Isaac 
4502923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4503923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
4504923c78e0SToby Isaac     /* Compress out points not in the section */
4505923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
4506923c78e0SToby Isaac       PetscInt r = pts[2*p];
4507923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
4508923c78e0SToby Isaac         pts[q*2]   = r;
4509923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
4510923c78e0SToby Isaac         ++q;
4511923c78e0SToby Isaac       }
4512923c78e0SToby Isaac     }
4513923c78e0SToby Isaac     np = q;
4514923c78e0SToby Isaac     cla = NULL;
4515923c78e0SToby Isaac   } else {
4516923c78e0SToby Isaac     PetscInt dof, off;
4517923c78e0SToby Isaac 
4518923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4519923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4520923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4521923c78e0SToby Isaac     np   = dof/2;
4522923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4523923c78e0SToby Isaac   }
4524923c78e0SToby Isaac   *numPoints = np;
4525923c78e0SToby Isaac   *points    = pts;
4526923c78e0SToby Isaac   *clp       = cla;
4527923c78e0SToby Isaac 
4528923c78e0SToby Isaac   PetscFunctionReturn(0);
4529923c78e0SToby Isaac }
4530923c78e0SToby Isaac 
45311dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4532923c78e0SToby Isaac {
4533923c78e0SToby Isaac   PetscErrorCode ierr;
4534923c78e0SToby Isaac 
4535923c78e0SToby Isaac   PetscFunctionBeginHot;
4536923c78e0SToby Isaac   if (!*clPoints) {
4537923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4538923c78e0SToby Isaac   } else {
4539923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4540923c78e0SToby Isaac   }
4541923c78e0SToby Isaac   *numPoints = 0;
4542923c78e0SToby Isaac   *points    = NULL;
4543923c78e0SToby Isaac   *clSec     = NULL;
4544923c78e0SToby Isaac   *clPoints  = NULL;
4545923c78e0SToby Isaac   *clp       = NULL;
4546923c78e0SToby Isaac   PetscFunctionReturn(0);
4547923c78e0SToby Isaac }
4548923c78e0SToby Isaac 
454997e99dd9SToby 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[])
45501a271a75SMatthew G. Knepley {
45511a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
455297e99dd9SToby Isaac   const PetscInt    **perms = NULL;
455397e99dd9SToby Isaac   const PetscScalar **flips = NULL;
45541a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
45551a271a75SMatthew G. Knepley 
45561a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4557fe02ba77SJed Brown   *size = 0;
455897e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
455997e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
456097e99dd9SToby Isaac     const PetscInt    point = points[2*p];
456197e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
456297e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
45631a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
45641a271a75SMatthew G. Knepley     const PetscScalar *varr;
45651a271a75SMatthew G. Knepley 
45661a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
45671a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
45681a271a75SMatthew G. Knepley     varr = &vArray[off];
456997e99dd9SToby Isaac     if (clperm) {
457097e99dd9SToby Isaac       if (perm) {
457197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
45721a271a75SMatthew G. Knepley       } else {
457397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
457497e99dd9SToby Isaac       }
457597e99dd9SToby Isaac       if (flip) {
457697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
457797e99dd9SToby Isaac       }
457897e99dd9SToby Isaac     } else {
457997e99dd9SToby Isaac       if (perm) {
458097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
458197e99dd9SToby Isaac       } else {
458297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
458397e99dd9SToby Isaac       }
458497e99dd9SToby Isaac       if (flip) {
458597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
45861a271a75SMatthew G. Knepley       }
45871a271a75SMatthew G. Knepley     }
458897e99dd9SToby Isaac     offset += dof;
458997e99dd9SToby Isaac   }
459097e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45911a271a75SMatthew G. Knepley   *size = offset;
45921a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
45931a271a75SMatthew G. Knepley }
45941a271a75SMatthew G. Knepley 
459597e99dd9SToby 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[])
45961a271a75SMatthew G. Knepley {
45971a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
45981a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
45991a271a75SMatthew G. Knepley 
46001a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4601fe02ba77SJed Brown   *size = 0;
46021a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
460397e99dd9SToby Isaac     PetscInt          p;
460497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
460597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
46061a271a75SMatthew G. Knepley 
460797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
460897e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
460997e99dd9SToby Isaac       const PetscInt    point = points[2*p];
461097e99dd9SToby Isaac       PetscInt          fdof, foff, b;
46111a271a75SMatthew G. Knepley       const PetscScalar *varr;
461297e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
461397e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
46141a271a75SMatthew G. Knepley 
46151a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
46161a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
46171a271a75SMatthew G. Knepley       varr = &vArray[foff];
461897e99dd9SToby Isaac       if (clperm) {
461997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
462097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
462197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
46221a271a75SMatthew G. Knepley       } else {
462397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
462497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
462597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
46261a271a75SMatthew G. Knepley       }
462797e99dd9SToby Isaac       offset += fdof;
46281a271a75SMatthew G. Knepley     }
462997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
46301a271a75SMatthew G. Knepley   }
46311a271a75SMatthew G. Knepley   *size = offset;
46321a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
46331a271a75SMatthew G. Knepley }
46341a271a75SMatthew G. Knepley 
4635552f7358SJed Brown /*@C
4636552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4637552f7358SJed Brown 
4638552f7358SJed Brown   Not collective
4639552f7358SJed Brown 
4640552f7358SJed Brown   Input Parameters:
4641552f7358SJed Brown + dm - The DM
4642552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4643552f7358SJed Brown . v - The local vector
464422c1ee49SMatthew G. Knepley . point - The point in the DM
464522c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
464622c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4647552f7358SJed Brown 
4648552f7358SJed Brown   Output Parameters:
464922c1ee49SMatthew G. Knepley + csize - The number of values in the closure
465022c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
465122c1ee49SMatthew G. Knepley 
465222c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
465322c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
465422c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
465522c1ee49SMatthew G. Knepley $
465622c1ee49SMatthew G. Knepley $ A typical use could be
465722c1ee49SMatthew G. Knepley $
465822c1ee49SMatthew G. Knepley $  values = NULL;
465922c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
466022c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
466122c1ee49SMatthew G. Knepley $    <Compute on closure>
466222c1ee49SMatthew G. Knepley $  }
466322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
466422c1ee49SMatthew G. Knepley $
466522c1ee49SMatthew G. Knepley $ or
466622c1ee49SMatthew G. Knepley $
466722c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
466822c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
466922c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
467022c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
467122c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
467222c1ee49SMatthew G. Knepley $      <Compute on closure>
467322c1ee49SMatthew G. Knepley $    }
467422c1ee49SMatthew G. Knepley $  }
467522c1ee49SMatthew G. Knepley $  PetscFree(values);
4676552f7358SJed Brown 
4677552f7358SJed Brown   Fortran Notes:
4678552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4679552f7358SJed Brown   include petsc.h90 in your code.
4680552f7358SJed Brown 
4681552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4682552f7358SJed Brown 
4683552f7358SJed Brown   Level: intermediate
4684552f7358SJed Brown 
4685552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4686552f7358SJed Brown @*/
4687552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4688552f7358SJed Brown {
4689552f7358SJed Brown   PetscSection       clSection;
4690d9917b9dSMatthew G. Knepley   IS                 clPoints;
46918a84db2dSMatthew G. Knepley   PetscScalar       *array;
46928a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4693552f7358SJed Brown   PetscInt          *points = NULL;
46948f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
46951a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4696552f7358SJed Brown   PetscErrorCode     ierr;
4697552f7358SJed Brown 
4698d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4699552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
470092fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
47011a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
47021a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4703552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4704552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4705552f7358SJed Brown   if (depth == 1 && numFields < 2) {
47061a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4707552f7358SJed Brown     PetscFunctionReturn(0);
4708552f7358SJed Brown   }
47091a271a75SMatthew G. Knepley   /* Get points */
4710923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47111fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
47121a271a75SMatthew G. Knepley   /* Get array */
4713bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
47141a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4715552f7358SJed Brown 
47161a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4717552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
47181a271a75SMatthew G. Knepley       asize += dof;
4719552f7358SJed Brown     }
4720bd6c0d32SMatthew G. Knepley     if (!values) {
4721923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47221a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4723bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4724bd6c0d32SMatthew G. Knepley     }
472569291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4726d0f6b257SMatthew G. Knepley   } else {
4727d0f6b257SMatthew G. Knepley     array = *values;
4728d0f6b257SMatthew G. Knepley   }
47298a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
47301a271a75SMatthew G. Knepley   /* Get values */
473197e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
473297e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
47331a271a75SMatthew G. Knepley   /* Cleanup points */
4734923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47351a271a75SMatthew G. Knepley   /* Cleanup array */
47368a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4737d0f6b257SMatthew G. Knepley   if (!*values) {
4738552f7358SJed Brown     if (csize) *csize = size;
4739552f7358SJed Brown     *values = array;
4740d0f6b257SMatthew G. Knepley   } else {
4741f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4742d0f6b257SMatthew G. Knepley     *csize = size;
4743d0f6b257SMatthew G. Knepley   }
4744552f7358SJed Brown   PetscFunctionReturn(0);
4745552f7358SJed Brown }
4746552f7358SJed Brown 
4747552f7358SJed Brown /*@C
4748552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4749552f7358SJed Brown 
4750552f7358SJed Brown   Not collective
4751552f7358SJed Brown 
4752552f7358SJed Brown   Input Parameters:
4753552f7358SJed Brown + dm - The DM
47540298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4755552f7358SJed Brown . v - The local vector
4756eaf898f9SPatrick Sanan . point - The point in the DM
47570298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4758552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4759552f7358SJed Brown 
476022c1ee49SMatthew 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()
476122c1ee49SMatthew G. Knepley 
47623813dfbdSMatthew G Knepley   Fortran Notes:
47633813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
47643813dfbdSMatthew G Knepley   include petsc.h90 in your code.
47653813dfbdSMatthew G Knepley 
47663813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
47673813dfbdSMatthew G Knepley 
4768552f7358SJed Brown   Level: intermediate
4769552f7358SJed Brown 
4770552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4771552f7358SJed Brown @*/
47727c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4773a6dfd86eSKarl Rupp {
4774552f7358SJed Brown   PetscInt       size = 0;
4775552f7358SJed Brown   PetscErrorCode ierr;
4776552f7358SJed Brown 
4777552f7358SJed Brown   PetscFunctionBegin;
4778552f7358SJed Brown   /* Should work without recalculating size */
477969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4780c9fdaa05SMatthew G. Knepley   *values = NULL;
4781552f7358SJed Brown   PetscFunctionReturn(0);
4782552f7358SJed Brown }
4783552f7358SJed Brown 
4784552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4785552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4786552f7358SJed Brown 
478797e99dd9SToby 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[])
4788552f7358SJed Brown {
4789552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4790552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4791552f7358SJed Brown   PetscScalar    *a;
4792552f7358SJed Brown   PetscInt        off, cind = 0, k;
4793552f7358SJed Brown   PetscErrorCode  ierr;
4794552f7358SJed Brown 
4795552f7358SJed Brown   PetscFunctionBegin;
4796552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4797552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4798552f7358SJed Brown   a    = &array[off];
4799552f7358SJed Brown   if (!cdof || setBC) {
480097e99dd9SToby Isaac     if (clperm) {
480197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
480297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4803552f7358SJed Brown     } else {
480497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
480597e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4806552f7358SJed Brown     }
4807552f7358SJed Brown   } else {
4808552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
480997e99dd9SToby Isaac     if (clperm) {
481097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4811552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
481297e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4813552f7358SJed Brown         }
4814552f7358SJed Brown       } else {
4815552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4816552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
481797e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
481897e99dd9SToby Isaac         }
481997e99dd9SToby Isaac       }
482097e99dd9SToby Isaac     } else {
482197e99dd9SToby Isaac       if (perm) {
482297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
482397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
482497e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
482597e99dd9SToby Isaac         }
482697e99dd9SToby Isaac       } else {
482797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
482897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
482997e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
483097e99dd9SToby Isaac         }
4831552f7358SJed Brown       }
4832552f7358SJed Brown     }
4833552f7358SJed Brown   }
4834552f7358SJed Brown   PetscFunctionReturn(0);
4835552f7358SJed Brown }
4836552f7358SJed Brown 
483797e99dd9SToby 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[])
4838a5e93ea8SMatthew G. Knepley {
4839a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4840a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4841a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4842a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4843a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4844a5e93ea8SMatthew G. Knepley 
4845a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4846a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4847a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4848a5e93ea8SMatthew G. Knepley   a    = &array[off];
4849a5e93ea8SMatthew G. Knepley   if (cdof) {
4850a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
485197e99dd9SToby Isaac     if (clperm) {
485297e99dd9SToby Isaac       if (perm) {
4853a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4854a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
485597e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
485697e99dd9SToby Isaac             cind++;
4857a5e93ea8SMatthew G. Knepley           }
4858a5e93ea8SMatthew G. Knepley         }
4859a5e93ea8SMatthew G. Knepley       } else {
4860a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4861a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
486297e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
486397e99dd9SToby Isaac             cind++;
486497e99dd9SToby Isaac           }
486597e99dd9SToby Isaac         }
486697e99dd9SToby Isaac       }
486797e99dd9SToby Isaac     } else {
486897e99dd9SToby Isaac       if (perm) {
486997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
487097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
487197e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
487297e99dd9SToby Isaac             cind++;
487397e99dd9SToby Isaac           }
487497e99dd9SToby Isaac         }
487597e99dd9SToby Isaac       } else {
487697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
487797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
487897e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
487997e99dd9SToby Isaac             cind++;
488097e99dd9SToby Isaac           }
4881a5e93ea8SMatthew G. Knepley         }
4882a5e93ea8SMatthew G. Knepley       }
4883a5e93ea8SMatthew G. Knepley     }
4884a5e93ea8SMatthew G. Knepley   }
4885a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4886a5e93ea8SMatthew G. Knepley }
4887a5e93ea8SMatthew G. Knepley 
488897e99dd9SToby 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[])
4889a6dfd86eSKarl Rupp {
4890552f7358SJed Brown   PetscScalar    *a;
48911a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
48921a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
489397e99dd9SToby Isaac   PetscInt        cind = 0, b;
4894552f7358SJed Brown   PetscErrorCode  ierr;
4895552f7358SJed Brown 
4896552f7358SJed Brown   PetscFunctionBegin;
4897552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4898552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
48991a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
49001a271a75SMatthew G. Knepley   a    = &array[foff];
4901552f7358SJed Brown   if (!fcdof || setBC) {
490297e99dd9SToby Isaac     if (clperm) {
490397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
490497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4905552f7358SJed Brown     } else {
490697e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
490797e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4908552f7358SJed Brown     }
4909552f7358SJed Brown   } else {
4910552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
491197e99dd9SToby Isaac     if (clperm) {
491297e99dd9SToby Isaac       if (perm) {
491397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
491497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
491597e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4916552f7358SJed Brown         }
4917552f7358SJed Brown       } else {
491897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
491997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
492097e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
492197e99dd9SToby Isaac         }
492297e99dd9SToby Isaac       }
492397e99dd9SToby Isaac     } else {
492497e99dd9SToby Isaac       if (perm) {
492597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
492697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
492797e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
492897e99dd9SToby Isaac         }
492997e99dd9SToby Isaac       } else {
493097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
493197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
493297e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4933552f7358SJed Brown         }
4934552f7358SJed Brown       }
4935552f7358SJed Brown     }
4936552f7358SJed Brown   }
49371a271a75SMatthew G. Knepley   *offset += fdof;
4938552f7358SJed Brown   PetscFunctionReturn(0);
4939552f7358SJed Brown }
4940552f7358SJed Brown 
4941ba322698SMatthew 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[])
4942a5e93ea8SMatthew G. Knepley {
4943a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
49441a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
49451a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
49465da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
4947ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4948a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4949a5e93ea8SMatthew G. Knepley 
4950a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
49515da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
4952a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4953a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
49541a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
49551a271a75SMatthew G. Knepley   a    = &array[foff];
4956a5e93ea8SMatthew G. Knepley   if (fcdof) {
4957ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4958a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
495997e99dd9SToby Isaac     if (clperm) {
496097e99dd9SToby Isaac       if (perm) {
4961ba322698SMatthew G. Knepley         if (comps) {
4962ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4963ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
49645da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
4965ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4966ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4967ba322698SMatthew G. Knepley           }
4968ba322698SMatthew G. Knepley         } else {
496997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
497097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
497197e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4972a5e93ea8SMatthew G. Knepley               ++cind;
4973a5e93ea8SMatthew G. Knepley             }
4974a5e93ea8SMatthew G. Knepley           }
4975ba322698SMatthew G. Knepley         }
4976ba322698SMatthew G. Knepley       } else {
4977ba322698SMatthew G. Knepley         if (comps) {
4978ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4979ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
49805da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
4981ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4982ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4983ba322698SMatthew G. Knepley           }
4984a5e93ea8SMatthew G. Knepley         } else {
498597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
498697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
498797e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
498897e99dd9SToby Isaac               ++cind;
498997e99dd9SToby Isaac             }
499097e99dd9SToby Isaac           }
499197e99dd9SToby Isaac         }
4992ba322698SMatthew G. Knepley       }
499397e99dd9SToby Isaac     } else {
499497e99dd9SToby Isaac       if (perm) {
4995ba322698SMatthew G. Knepley         if (comps) {
4996ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4997ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
49985da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
4999ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5000ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5001ba322698SMatthew G. Knepley           }
5002ba322698SMatthew G. Knepley         } else {
500397e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
500497e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
500597e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
500697e99dd9SToby Isaac               ++cind;
500797e99dd9SToby Isaac             }
500897e99dd9SToby Isaac           }
5009ba322698SMatthew G. Knepley         }
5010ba322698SMatthew G. Knepley       } else {
5011ba322698SMatthew G. Knepley         if (comps) {
5012ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
5013ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
50145da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5015ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5016ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5017ba322698SMatthew G. Knepley           }
501897e99dd9SToby Isaac         } else {
501997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
502097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
502197e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5022a5e93ea8SMatthew G. Knepley               ++cind;
5023a5e93ea8SMatthew G. Knepley             }
5024a5e93ea8SMatthew G. Knepley           }
5025a5e93ea8SMatthew G. Knepley         }
5026a5e93ea8SMatthew G. Knepley       }
5027a5e93ea8SMatthew G. Knepley     }
5028ba322698SMatthew G. Knepley   }
50291a271a75SMatthew G. Knepley   *offset += fdof;
5030a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5031a5e93ea8SMatthew G. Knepley }
5032a5e93ea8SMatthew G. Knepley 
50338f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5034a6dfd86eSKarl Rupp {
5035552f7358SJed Brown   PetscScalar    *array;
50361b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
50371b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5038552f7358SJed Brown   PetscErrorCode  ierr;
5039552f7358SJed Brown 
50401b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
5041b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5042b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5043b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5044b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5045b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5046b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5047b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
5048b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
5049b6ebb6e6SMatthew G. Knepley 
5050b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5051b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5052b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
5053b6ebb6e6SMatthew G. Knepley     {
5054b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5055b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
5056b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
5057b6ebb6e6SMatthew G. Knepley 
5058b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5059b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5060b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
5061b6ebb6e6SMatthew G. Knepley       if (!cdof) {
5062b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5063b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5064b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5065b6ebb6e6SMatthew G. Knepley           }
5066b6ebb6e6SMatthew G. Knepley         } else {
5067b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5068b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5069b6ebb6e6SMatthew G. Knepley           }
5070b6ebb6e6SMatthew G. Knepley         }
5071b6ebb6e6SMatthew G. Knepley       } else {
5072b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5073b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
5074b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5075b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5076b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
5077b6ebb6e6SMatthew G. Knepley           }
5078b6ebb6e6SMatthew G. Knepley         } else {
5079b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
5080b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5081b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
5082b6ebb6e6SMatthew G. Knepley           }
5083b6ebb6e6SMatthew G. Knepley         }
5084b6ebb6e6SMatthew G. Knepley       }
5085b6ebb6e6SMatthew G. Knepley     }
5086b6ebb6e6SMatthew G. Knepley   }
5087b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5088b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
5089b6ebb6e6SMatthew G. Knepley }
50901b406b76SMatthew G. Knepley 
50911b406b76SMatthew G. Knepley /*@C
50921b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
50931b406b76SMatthew G. Knepley 
50941b406b76SMatthew G. Knepley   Not collective
50951b406b76SMatthew G. Knepley 
50961b406b76SMatthew G. Knepley   Input Parameters:
50971b406b76SMatthew G. Knepley + dm - The DM
50981b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
50991b406b76SMatthew G. Knepley . v - The local vector
5100eaf898f9SPatrick Sanan . point - The point in the DM
51011b406b76SMatthew G. Knepley . values - The array of values
510222c1ee49SMatthew 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,
510322c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
51041b406b76SMatthew G. Knepley 
51051b406b76SMatthew G. Knepley   Fortran Notes:
51061b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
51071b406b76SMatthew G. Knepley 
51081b406b76SMatthew G. Knepley   Level: intermediate
51091b406b76SMatthew G. Knepley 
51101b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
51111b406b76SMatthew G. Knepley @*/
51121b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
51131b406b76SMatthew G. Knepley {
51141b406b76SMatthew G. Knepley   PetscSection    clSection;
51151b406b76SMatthew G. Knepley   IS              clPoints;
51161b406b76SMatthew G. Knepley   PetscScalar    *array;
51171b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
511897e99dd9SToby Isaac   const PetscInt *clp, *clperm;
51191a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
51201b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
51211b406b76SMatthew G. Knepley 
51221a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
51231b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
512492fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
51251a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
51261a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
51271b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
51281b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
51291b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
51308f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
51311b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
51321b406b76SMatthew G. Knepley   }
51331a271a75SMatthew G. Knepley   /* Get points */
513497e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5135923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51361a271a75SMatthew G. Knepley   /* Get array */
5137552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
51381a271a75SMatthew G. Knepley   /* Get values */
5139ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
514097e99dd9SToby Isaac     PetscInt offset = 0, f;
5141552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
514297e99dd9SToby Isaac       const PetscInt    **perms = NULL;
514397e99dd9SToby Isaac       const PetscScalar **flips = NULL;
514497e99dd9SToby Isaac 
514597e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5146552f7358SJed Brown       switch (mode) {
5147552f7358SJed Brown       case INSERT_VALUES:
514897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
514997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
515097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
515197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
515297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5153552f7358SJed Brown         } break;
5154552f7358SJed Brown       case INSERT_ALL_VALUES:
515597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
515697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
515797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
515897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
515997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5160552f7358SJed Brown         } break;
5161a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
516297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
516397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
516497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
516597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5166ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5167a5e93ea8SMatthew G. Knepley         } break;
5168552f7358SJed Brown       case ADD_VALUES:
516997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
517097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
517197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
517297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
517397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5174552f7358SJed Brown         } break;
5175552f7358SJed Brown       case ADD_ALL_VALUES:
517697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
517797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
517897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
517997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
518097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5181552f7358SJed Brown         } break;
5182304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
518397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
518497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
518597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
518697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
5187ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5188304ab55fSMatthew G. Knepley         } break;
5189552f7358SJed Brown       default:
5190f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5191552f7358SJed Brown       }
519297e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
51931a271a75SMatthew G. Knepley     }
5194552f7358SJed Brown   } else {
51951a271a75SMatthew G. Knepley     PetscInt dof, off;
519697e99dd9SToby Isaac     const PetscInt    **perms = NULL;
519797e99dd9SToby Isaac     const PetscScalar **flips = NULL;
51981a271a75SMatthew G. Knepley 
519997e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5200552f7358SJed Brown     switch (mode) {
5201552f7358SJed Brown     case INSERT_VALUES:
520297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
520397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
520497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
520597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
520697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
520797e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5208552f7358SJed Brown       } break;
5209552f7358SJed Brown     case INSERT_ALL_VALUES:
521097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
521197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
521297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
521397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
521497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
521597e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5216552f7358SJed Brown       } break;
5217a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
521897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
521997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
522097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
522197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
522297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
522397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5224a5e93ea8SMatthew G. Knepley       } break;
5225552f7358SJed Brown     case ADD_VALUES:
522697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
522797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
522897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
522997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
523097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
523197e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5232552f7358SJed Brown       } break;
5233552f7358SJed Brown     case ADD_ALL_VALUES:
523497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
523597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
523697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
523797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
523897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
523997e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5240552f7358SJed Brown       } break;
5241304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
524297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
524397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
524497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
524597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
524697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
524797e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5248304ab55fSMatthew G. Knepley       } break;
5249552f7358SJed Brown     default:
5250f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5251552f7358SJed Brown     }
525297e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5253552f7358SJed Brown   }
52541a271a75SMatthew G. Knepley   /* Cleanup points */
5255923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52561a271a75SMatthew G. Knepley   /* Cleanup array */
5257552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5258552f7358SJed Brown   PetscFunctionReturn(0);
5259552f7358SJed Brown }
5260552f7358SJed Brown 
526197529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
5262ba322698SMatthew 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)
5263e07394fbSMatthew G. Knepley {
5264e07394fbSMatthew G. Knepley   PetscSection      clSection;
5265e07394fbSMatthew G. Knepley   IS                clPoints;
5266e07394fbSMatthew G. Knepley   PetscScalar       *array;
5267e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
526897529cf3SJed Brown   const PetscInt    *clp;
5269e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
527097e99dd9SToby Isaac   PetscInt          offset = 0, f;
5271e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5272e07394fbSMatthew G. Knepley 
5273e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5274e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
527592fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5276e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5277e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5278e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5279e07394fbSMatthew G. Knepley   /* Get points */
5280923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5281e07394fbSMatthew G. Knepley   /* Get array */
5282e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5283e07394fbSMatthew G. Knepley   /* Get values */
5284e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
528597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
528697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
528797e99dd9SToby Isaac 
5288e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5289e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5290e07394fbSMatthew G. Knepley         PetscInt fdof;
5291e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5292e07394fbSMatthew G. Knepley         offset += fdof;
5293e07394fbSMatthew G. Knepley       }
5294e07394fbSMatthew G. Knepley       continue;
5295e07394fbSMatthew G. Knepley     }
529697e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5297e07394fbSMatthew G. Knepley     switch (mode) {
5298e07394fbSMatthew G. Knepley     case INSERT_VALUES:
529997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
530097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
530197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
530297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
530397529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5304e07394fbSMatthew G. Knepley       } break;
5305e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
530697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
530797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
530897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
530997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
531097529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5311e07394fbSMatthew G. Knepley         } break;
5312e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
531397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
531497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
531597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
531697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
531797529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5318e07394fbSMatthew G. Knepley       } break;
5319e07394fbSMatthew G. Knepley     case ADD_VALUES:
532097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
532197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
532297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
532397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
532497529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5325e07394fbSMatthew G. Knepley       } break;
5326e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
532797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
532897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
532997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
533097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
533197529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5332e07394fbSMatthew G. Knepley       } break;
5333e07394fbSMatthew G. Knepley     default:
5334f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5335e07394fbSMatthew G. Knepley     }
533697e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5337e07394fbSMatthew G. Knepley   }
5338e07394fbSMatthew G. Knepley   /* Cleanup points */
5339923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5340e07394fbSMatthew G. Knepley   /* Cleanup array */
5341e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5342e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5343e07394fbSMatthew G. Knepley }
5344e07394fbSMatthew G. Knepley 
53457cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5346552f7358SJed Brown {
5347552f7358SJed Brown   PetscMPIInt    rank;
5348552f7358SJed Brown   PetscInt       i, j;
5349552f7358SJed Brown   PetscErrorCode ierr;
5350552f7358SJed Brown 
5351552f7358SJed Brown   PetscFunctionBegin;
535282f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
5353eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5354e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5355e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5356b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5357b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5358e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5359b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5360519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
53617eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5362552f7358SJed Brown #else
5363b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5364552f7358SJed Brown #endif
5365552f7358SJed Brown     }
536677a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5367552f7358SJed Brown   }
5368552f7358SJed Brown   PetscFunctionReturn(0);
5369552f7358SJed Brown }
5370552f7358SJed Brown 
537105586334SMatthew G. Knepley /*
537205586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
537305586334SMatthew G. Knepley 
537405586334SMatthew G. Knepley   Input Parameters:
537505586334SMatthew G. Knepley + section - The section for this data layout
537636fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
537705586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
537805586334SMatthew G. Knepley . off     - The global offset of this point
537905586334SMatthew G. Knepley . loff    - The local offset of each field
538005586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
538105586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
538205586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
538305586334SMatthew G. Knepley 
538405586334SMatthew G. Knepley   Output Parameter:
538505586334SMatthew G. Knepley . indices - Indices for dofs on this point
538605586334SMatthew G. Knepley 
538705586334SMatthew G. Knepley   Level: developer
538805586334SMatthew G. Knepley 
538905586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
539005586334SMatthew G. Knepley */
539136fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5392a6dfd86eSKarl Rupp {
5393e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5394552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5395552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5396552f7358SJed Brown   PetscInt        cind = 0, k;
5397552f7358SJed Brown   PetscErrorCode  ierr;
5398552f7358SJed Brown 
5399552f7358SJed Brown   PetscFunctionBegin;
540036fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5401552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5402552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5403552f7358SJed Brown   if (!cdof || setBC) {
540405586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
540505586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
540605586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
540705586334SMatthew G. Knepley 
540805586334SMatthew G. Knepley       indices[ind] = off + k;
5409552f7358SJed Brown     }
5410552f7358SJed Brown   } else {
5411552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
54124acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
541305586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
541405586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
541505586334SMatthew G. Knepley 
54164acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
54174acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
541805586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
54194acb8e1eSToby Isaac         ++cind;
54204acb8e1eSToby Isaac       } else {
542136fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
5422552f7358SJed Brown       }
5423552f7358SJed Brown     }
5424552f7358SJed Brown   }
5425e6ccafaeSMatthew G Knepley   *loff += dof;
5426552f7358SJed Brown   PetscFunctionReturn(0);
5427552f7358SJed Brown }
5428552f7358SJed Brown 
54297e29afd2SMatthew G. Knepley /*
543036fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
54317e29afd2SMatthew G. Knepley 
543236fa2b79SJed Brown  Input Parameters:
543336fa2b79SJed Brown + section - a section (global or local)
543436fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
543536fa2b79SJed Brown . point - point within section
543636fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
543736fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
543836fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
543936fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
544036fa2b79SJed Brown . permsoff - offset
544136fa2b79SJed Brown - indperm - index permutation
544236fa2b79SJed Brown 
544336fa2b79SJed Brown  Output Parameter:
544436fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
544536fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
544636fa2b79SJed Brown 
544736fa2b79SJed Brown  Notes:
544836fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
544936fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
545036fa2b79SJed Brown  in the local vector.
545136fa2b79SJed Brown 
545236fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
545336fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
545436fa2b79SJed Brown 
545536fa2b79SJed Brown  Developer Note:
545636fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
545736fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
545836fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
545936fa2b79SJed Brown 
546036fa2b79SJed Brown  Example:
546136fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
546236fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
546336fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
546436fa2b79SJed Brown  The global vector does not store constrained dofs, so when this function returns global indices, say {110, -112, 111}, the value of -112 is an arbitrary flag that should not be interpreted beyond its sign.
546536fa2b79SJed Brown 
546636fa2b79SJed Brown  Level: developer
54677e29afd2SMatthew G. Knepley */
546836fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5469a6dfd86eSKarl Rupp {
5470552f7358SJed Brown   PetscInt       numFields, foff, f;
5471552f7358SJed Brown   PetscErrorCode ierr;
5472552f7358SJed Brown 
5473552f7358SJed Brown   PetscFunctionBegin;
547436fa2b79SJed Brown   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5475552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5476552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
54774acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5478552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
54794acb8e1eSToby Isaac     PetscInt        cind = 0, b;
54804acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5481552f7358SJed Brown 
5482552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5483552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5484552f7358SJed Brown     if (!cfdof || setBC) {
548505586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
548605586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
548705586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
548805586334SMatthew G. Knepley 
548905586334SMatthew G. Knepley         indices[ind] = off+foff+b;
549005586334SMatthew G. Knepley       }
5491552f7358SJed Brown     } else {
5492552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
549305586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
549405586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
549505586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
549605586334SMatthew G. Knepley 
54974acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
549805586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5499552f7358SJed Brown           ++cind;
5500552f7358SJed Brown         } else {
550136fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
5502552f7358SJed Brown         }
5503552f7358SJed Brown       }
5504552f7358SJed Brown     }
550536fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
5506552f7358SJed Brown     foffs[f] += fdof;
5507552f7358SJed Brown   }
5508552f7358SJed Brown   PetscFunctionReturn(0);
5509552f7358SJed Brown }
5510552f7358SJed Brown 
55117e29afd2SMatthew G. Knepley /*
55127e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
55137e29afd2SMatthew G. Knepley 
55147e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
5515645102dcSJed Brown 
5516645102dcSJed Brown  Notes:
5517645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
5518645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
55197e29afd2SMatthew G. Knepley */
5520645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
55217e29afd2SMatthew G. Knepley {
55227e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
55237e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
55247e29afd2SMatthew G. Knepley 
55257e29afd2SMatthew G. Knepley   PetscFunctionBegin;
55267e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
55277e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
55287e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
55297e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
55307e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
55317e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
55327e29afd2SMatthew G. Knepley 
55337e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
55347e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
55357e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
5536645102dcSJed Brown     if (!cfdof) {
553705586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
553805586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
553905586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
554005586334SMatthew G. Knepley 
554105586334SMatthew G. Knepley         indices[ind] = foff+b;
554205586334SMatthew G. Knepley       }
55437e29afd2SMatthew G. Knepley     } else {
55447e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
554505586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
554605586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
554705586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
554805586334SMatthew G. Knepley 
55497e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
555005586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
55517e29afd2SMatthew G. Knepley           ++cind;
55527e29afd2SMatthew G. Knepley         } else {
555305586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
55547e29afd2SMatthew G. Knepley         }
55557e29afd2SMatthew G. Knepley       }
55567e29afd2SMatthew G. Knepley     }
55577e29afd2SMatthew G. Knepley     foffs[f] += fdof;
55587e29afd2SMatthew G. Knepley   }
55597e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
55607e29afd2SMatthew G. Knepley }
55617e29afd2SMatthew G. Knepley 
55624acb8e1eSToby 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)
5563d3d1a6afSToby Isaac {
5564d3d1a6afSToby Isaac   Mat             cMat;
5565d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5566d3d1a6afSToby Isaac   IS              aIS;
5567d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5568d3d1a6afSToby Isaac   const PetscInt  *anchors;
5569e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5570d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5571d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5572d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5573d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5574d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5575d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5576d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
55776ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5578d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5579d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5580d3d1a6afSToby Isaac 
5581d3d1a6afSToby Isaac   PetscFunctionBegin;
5582d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5583d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5584d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5585d3d1a6afSToby Isaac 
5586a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5587d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5588d3d1a6afSToby Isaac   if (aSec) {
5589580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5590d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5591d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5592d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5593d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5594d3d1a6afSToby Isaac      * into the global matrix anyway) */
5595d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5596d3d1a6afSToby Isaac       PetscInt b    = points[p];
55974b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5598d3d1a6afSToby Isaac 
55994b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
56004b2f2278SToby Isaac       if (!bSecDof) {
56014b2f2278SToby Isaac         continue;
56024b2f2278SToby Isaac       }
5603d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5604d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5605d3d1a6afSToby Isaac       }
5606d3d1a6afSToby Isaac       if (bDof) {
5607d3d1a6afSToby Isaac         /* this point is constrained */
5608d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5609d3d1a6afSToby Isaac         PetscInt bOff, q;
5610d3d1a6afSToby Isaac 
5611d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5612d3d1a6afSToby Isaac         newNumPoints  += bDof;
5613d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5614d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5615d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5616d3d1a6afSToby Isaac           PetscInt aDof;
5617d3d1a6afSToby Isaac 
5618d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5619d3d1a6afSToby Isaac           newNumIndices += aDof;
5620d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5621d3d1a6afSToby Isaac             PetscInt fDof;
5622d3d1a6afSToby Isaac 
5623d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5624d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5625d3d1a6afSToby Isaac           }
5626d3d1a6afSToby Isaac         }
5627d3d1a6afSToby Isaac       }
5628d3d1a6afSToby Isaac       else {
5629d3d1a6afSToby Isaac         /* this point is not constrained */
5630d3d1a6afSToby Isaac         newNumPoints++;
56314b2f2278SToby Isaac         newNumIndices += bSecDof;
5632d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5633d3d1a6afSToby Isaac           PetscInt fDof;
5634d3d1a6afSToby Isaac 
5635d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5636d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5637d3d1a6afSToby Isaac         }
5638d3d1a6afSToby Isaac       }
5639d3d1a6afSToby Isaac     }
5640d3d1a6afSToby Isaac   }
5641d3d1a6afSToby Isaac   if (!anyConstrained) {
564272b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
564372b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
564472b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
564572b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
564672b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5647d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5648d3d1a6afSToby Isaac   }
5649d3d1a6afSToby Isaac 
56506ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
56516ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
56526ecaa68aSToby Isaac 
5653f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5654d3d1a6afSToby Isaac 
56556ecaa68aSToby Isaac   if (!outPoints && !outValues) {
56566ecaa68aSToby Isaac     if (offsets) {
56576ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
56586ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
56596ecaa68aSToby Isaac       }
56606ecaa68aSToby Isaac     }
56616ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
56626ecaa68aSToby Isaac     PetscFunctionReturn(0);
56636ecaa68aSToby Isaac   }
56646ecaa68aSToby Isaac 
5665f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5666d3d1a6afSToby Isaac 
5667f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5668d3d1a6afSToby Isaac 
5669d3d1a6afSToby Isaac   /* workspaces */
5670d3d1a6afSToby Isaac   if (numFields) {
5671d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
567269291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
567369291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5674d3d1a6afSToby Isaac     }
5675d3d1a6afSToby Isaac   }
5676d3d1a6afSToby Isaac   else {
567769291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
567869291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5679d3d1a6afSToby Isaac   }
5680d3d1a6afSToby Isaac 
5681d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5682d3d1a6afSToby Isaac   if (numFields) {
56834b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
56844b2f2278SToby Isaac 
5685d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5686d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
56874b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5688d3d1a6afSToby Isaac 
56894b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
56904b2f2278SToby Isaac       if (!bSecDof) {
56914b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
56924b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
56934b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
56944b2f2278SToby Isaac         }
56954b2f2278SToby Isaac         continue;
56964b2f2278SToby Isaac       }
5697d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5698d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5699d3d1a6afSToby Isaac       }
5700d3d1a6afSToby Isaac       if (bDof) {
5701d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5702d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5703d3d1a6afSToby Isaac 
5704d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5705d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5706d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5707d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5708d3d1a6afSToby Isaac             PetscInt aFDof;
5709d3d1a6afSToby Isaac 
5710d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5711d3d1a6afSToby Isaac             allFDof += aFDof;
5712d3d1a6afSToby Isaac           }
5713d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5714d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5715d3d1a6afSToby Isaac         }
5716d3d1a6afSToby Isaac       }
5717d3d1a6afSToby Isaac       else {
5718d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5719d3d1a6afSToby Isaac           PetscInt fDof;
5720d3d1a6afSToby Isaac 
5721d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5722d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5723d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5724d3d1a6afSToby Isaac         }
5725d3d1a6afSToby Isaac       }
5726d3d1a6afSToby Isaac     }
57274b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
57284b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
57294b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5730d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5731d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5732d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5733d3d1a6afSToby Isaac       }
573419f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
573519f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
573669291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5737d3d1a6afSToby Isaac     }
5738d3d1a6afSToby Isaac   }
5739d3d1a6afSToby Isaac   else {
5740d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5741d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
57424b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5743d3d1a6afSToby Isaac 
57444b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
57454b2f2278SToby Isaac       if (!bSecDof) {
57464b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
57474b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
57484b2f2278SToby Isaac         continue;
57494b2f2278SToby Isaac       }
5750d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5751d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5752d3d1a6afSToby Isaac       }
5753d3d1a6afSToby Isaac       if (bDof) {
57544b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5755d3d1a6afSToby Isaac 
5756d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5757d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5758d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5759d3d1a6afSToby Isaac 
5760d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5761d3d1a6afSToby Isaac           allDof += aDof;
5762d3d1a6afSToby Isaac         }
5763d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
57644b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5765d3d1a6afSToby Isaac       }
5766d3d1a6afSToby Isaac       else {
57674b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5768d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5769d3d1a6afSToby Isaac       }
5770d3d1a6afSToby Isaac     }
5771d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5772d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5773d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5774d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5775d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5776d3d1a6afSToby Isaac     }
577769291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5778d3d1a6afSToby Isaac   }
5779d3d1a6afSToby Isaac 
57806ecaa68aSToby Isaac   /* output arrays */
578169291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
57826ecaa68aSToby Isaac 
5783d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5784d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5785d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
578669291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
578769291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5788d3d1a6afSToby Isaac   if (numFields) {
5789d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5790d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5791d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
57924b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5793d3d1a6afSToby Isaac 
57944b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
57954b2f2278SToby Isaac       if (!bSecDof) {
57964b2f2278SToby Isaac         continue;
57974b2f2278SToby Isaac       }
5798d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5799d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5800d3d1a6afSToby Isaac       }
5801d3d1a6afSToby Isaac       if (bDof) {
5802d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5803d3d1a6afSToby Isaac 
5804d3d1a6afSToby Isaac         fStart[0] = 0;
5805d3d1a6afSToby Isaac         fEnd[0]   = 0;
5806d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5807d3d1a6afSToby Isaac           PetscInt fDof;
5808d3d1a6afSToby Isaac 
5809d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5810d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5811d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5812d3d1a6afSToby Isaac         }
5813d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
581436fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
5815d3d1a6afSToby Isaac 
5816d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5817d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5818d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5819d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5820d3d1a6afSToby Isaac 
5821d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5822d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5823d3d1a6afSToby Isaac         }
5824d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5825d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5826d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5827d3d1a6afSToby Isaac 
5828d3d1a6afSToby 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 */
5829d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5830d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5831302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
583236fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
5833d3d1a6afSToby Isaac         }
5834d3d1a6afSToby Isaac         newP += bDof;
5835d3d1a6afSToby Isaac 
58366ecaa68aSToby Isaac         if (outValues) {
5837d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5838d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5839d3d1a6afSToby 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);
5840d3d1a6afSToby Isaac           }
5841d3d1a6afSToby Isaac         }
58426ecaa68aSToby Isaac       }
5843d3d1a6afSToby Isaac       else {
5844d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5845d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5846d3d1a6afSToby Isaac         newP++;
5847d3d1a6afSToby Isaac       }
5848d3d1a6afSToby Isaac     }
5849d3d1a6afSToby Isaac   } else {
5850d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5851d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5852d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
58534b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5854d3d1a6afSToby Isaac 
58554b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
58564b2f2278SToby Isaac       if (!bSecDof) {
58574b2f2278SToby Isaac         continue;
58584b2f2278SToby Isaac       }
5859d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5860d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5861d3d1a6afSToby Isaac       }
5862d3d1a6afSToby Isaac       if (bDof) {
5863d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5864d3d1a6afSToby Isaac 
5865d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
586636fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
5867d3d1a6afSToby Isaac 
5868d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5869d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5870d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5871d3d1a6afSToby Isaac 
5872d3d1a6afSToby 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 */
5873d3d1a6afSToby Isaac 
5874d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5875d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5876302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
587736fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
5878d3d1a6afSToby Isaac         }
5879d3d1a6afSToby Isaac         newP += bDof;
5880d3d1a6afSToby Isaac 
5881d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
58826ecaa68aSToby Isaac         if (outValues) {
5883d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5884d3d1a6afSToby Isaac         }
58856ecaa68aSToby Isaac       }
5886d3d1a6afSToby Isaac       else {
5887d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5888d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5889d3d1a6afSToby Isaac         newP++;
5890d3d1a6afSToby Isaac       }
5891d3d1a6afSToby Isaac     }
5892d3d1a6afSToby Isaac   }
5893d3d1a6afSToby Isaac 
58946ecaa68aSToby Isaac   if (outValues) {
589569291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5896580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
5897d3d1a6afSToby Isaac     /* multiply constraints on the right */
5898d3d1a6afSToby Isaac     if (numFields) {
5899d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5900d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5901d3d1a6afSToby Isaac 
5902d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5903d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5904d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5905d3d1a6afSToby Isaac           PetscInt c, r, k;
5906d3d1a6afSToby Isaac           PetscInt dof;
5907d3d1a6afSToby Isaac 
5908d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
59094b2f2278SToby Isaac           if (!dof) {
59104b2f2278SToby Isaac             continue;
59114b2f2278SToby Isaac           }
5912d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5913d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5914d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5915d3d1a6afSToby Isaac 
5916d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5917d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5918d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
59194acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5920d3d1a6afSToby Isaac                 }
5921d3d1a6afSToby Isaac               }
5922d3d1a6afSToby Isaac             }
5923d3d1a6afSToby Isaac           }
5924d3d1a6afSToby Isaac           else {
5925d3d1a6afSToby Isaac             /* copy this column as is */
5926d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5927d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5928d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5929d3d1a6afSToby Isaac               }
5930d3d1a6afSToby Isaac             }
5931d3d1a6afSToby Isaac           }
5932d3d1a6afSToby Isaac           oldOff += dof;
5933d3d1a6afSToby Isaac         }
5934d3d1a6afSToby Isaac       }
5935d3d1a6afSToby Isaac     }
5936d3d1a6afSToby Isaac     else {
5937d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5938d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5939d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5940d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5941d3d1a6afSToby Isaac         PetscInt c, r, k;
5942d3d1a6afSToby Isaac         PetscInt dof;
5943d3d1a6afSToby Isaac 
5944d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
59454b2f2278SToby Isaac         if (!dof) {
59464b2f2278SToby Isaac           continue;
59474b2f2278SToby Isaac         }
5948d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5949d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5950d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5951d3d1a6afSToby Isaac 
5952d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5953d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5954d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5955d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5956d3d1a6afSToby Isaac               }
5957d3d1a6afSToby Isaac             }
5958d3d1a6afSToby Isaac           }
5959d3d1a6afSToby Isaac         }
5960d3d1a6afSToby Isaac         else {
5961d3d1a6afSToby Isaac           /* copy this column as is */
5962d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5963d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5964d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5965d3d1a6afSToby Isaac             }
5966d3d1a6afSToby Isaac           }
5967d3d1a6afSToby Isaac         }
5968d3d1a6afSToby Isaac         oldOff += dof;
5969d3d1a6afSToby Isaac       }
5970d3d1a6afSToby Isaac     }
5971d3d1a6afSToby Isaac 
59726ecaa68aSToby Isaac     if (multiplyLeft) {
597369291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5974580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
5975d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5976d3d1a6afSToby Isaac       if (numFields) {
5977d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5978d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5979d3d1a6afSToby Isaac 
5980d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5981d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5982d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5983d3d1a6afSToby Isaac             PetscInt c, r, k;
5984d3d1a6afSToby Isaac             PetscInt dof;
5985d3d1a6afSToby Isaac 
5986d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5987d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5988d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5989d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5990d3d1a6afSToby Isaac 
5991d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5992d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5993d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5994d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5995d3d1a6afSToby Isaac                   }
5996d3d1a6afSToby Isaac                 }
5997d3d1a6afSToby Isaac               }
5998d3d1a6afSToby Isaac             }
5999d3d1a6afSToby Isaac             else {
6000d3d1a6afSToby Isaac               /* copy this row as is */
6001d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
6002d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
6003d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6004d3d1a6afSToby Isaac                 }
6005d3d1a6afSToby Isaac               }
6006d3d1a6afSToby Isaac             }
6007d3d1a6afSToby Isaac             oldOff += dof;
6008d3d1a6afSToby Isaac           }
6009d3d1a6afSToby Isaac         }
6010d3d1a6afSToby Isaac       }
6011d3d1a6afSToby Isaac       else {
6012d3d1a6afSToby Isaac         PetscInt oldOff = 0;
6013d3d1a6afSToby Isaac 
6014d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
6015d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
6016d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
6017d3d1a6afSToby Isaac           PetscInt c, r, k;
6018d3d1a6afSToby Isaac           PetscInt dof;
6019d3d1a6afSToby Isaac 
6020d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
6021d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6022d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
6023d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
6024d3d1a6afSToby Isaac 
6025d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
6026d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6027d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
6028d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6029d3d1a6afSToby Isaac                 }
6030d3d1a6afSToby Isaac               }
6031d3d1a6afSToby Isaac             }
6032d3d1a6afSToby Isaac           }
6033d3d1a6afSToby Isaac           else {
6034d3d1a6afSToby Isaac             /* copy this row as is */
60359fc93327SToby Isaac             for (r = 0; r < dof; r++) {
6036d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
6037d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6038d3d1a6afSToby Isaac               }
6039d3d1a6afSToby Isaac             }
6040d3d1a6afSToby Isaac           }
6041d3d1a6afSToby Isaac           oldOff += dof;
6042d3d1a6afSToby Isaac         }
6043d3d1a6afSToby Isaac       }
6044d3d1a6afSToby Isaac 
604569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
60466ecaa68aSToby Isaac     }
60476ecaa68aSToby Isaac     else {
60486ecaa68aSToby Isaac       newValues = tmpValues;
60496ecaa68aSToby Isaac     }
60506ecaa68aSToby Isaac   }
60516ecaa68aSToby Isaac 
6052d3d1a6afSToby Isaac   /* clean up */
605369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
605469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
60556ecaa68aSToby Isaac 
6056d3d1a6afSToby Isaac   if (numFields) {
6057d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
605869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
605969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
606069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6061d3d1a6afSToby Isaac     }
6062d3d1a6afSToby Isaac   }
6063d3d1a6afSToby Isaac   else {
606469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
606569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
606669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6067d3d1a6afSToby Isaac   }
6068d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
6069d3d1a6afSToby Isaac 
6070d3d1a6afSToby Isaac   /* output */
60716ecaa68aSToby Isaac   if (outPoints) {
6072d3d1a6afSToby Isaac     *outPoints = newPoints;
60736ecaa68aSToby Isaac   }
60746ecaa68aSToby Isaac   else {
607569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
60766ecaa68aSToby Isaac   }
607731620726SToby Isaac   if (outValues) {
6078d3d1a6afSToby Isaac     *outValues = newValues;
60796ecaa68aSToby Isaac   }
60806ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
6081d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
6082d3d1a6afSToby Isaac   }
6083d3d1a6afSToby Isaac   PetscFunctionReturn(0);
6084d3d1a6afSToby Isaac }
6085d3d1a6afSToby Isaac 
60867cd05799SMatthew G. Knepley /*@C
60872cdc749cSStefano Zampini   DMPlexGetClosureIndices - Get the global indices for all local points in the closure of the given point
60887cd05799SMatthew G. Knepley 
60897cd05799SMatthew G. Knepley   Not collective
60907cd05799SMatthew G. Knepley 
60917cd05799SMatthew G. Knepley   Input Parameters:
60927cd05799SMatthew G. Knepley + dm - The DM
609336fa2b79SJed Brown . section - The section describing the points (a local section)
609436fa2b79SJed Brown . idxSection - The section on which to obtain indices (may be local or global)
60957cd05799SMatthew G. Knepley - point - The mesh point
60967cd05799SMatthew G. Knepley 
60977cd05799SMatthew G. Knepley   Output parameters:
60987cd05799SMatthew G. Knepley + numIndices - The number of indices
60997cd05799SMatthew G. Knepley . indices - The indices
61007cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
61017cd05799SMatthew G. Knepley 
610236fa2b79SJed Brown   Notes:
610336fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
610436fa2b79SJed Brown 
610536fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
610636fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
610736fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
610836fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
610936fa2b79SJed Brown   indices (with the above semantics) are implied.
61107cd05799SMatthew G. Knepley 
61117cd05799SMatthew G. Knepley   Level: advanced
61127cd05799SMatthew G. Knepley 
611336fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
61147cd05799SMatthew G. Knepley @*/
611536fa2b79SJed Brown PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
61167773e69fSMatthew G. Knepley {
611736fa2b79SJed Brown   PetscBool       isLocal = (PetscBool)(section == idxSection);
61187773e69fSMatthew G. Knepley   PetscSection    clSection;
61197773e69fSMatthew G. Knepley   IS              clPoints;
612005586334SMatthew G. Knepley   const PetscInt *clp, *clperm;
61214acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
61227773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
61237773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
61247773e69fSMatthew G. Knepley   PetscInt        offsets[32];
612536fa2b79SJed Brown   PetscInt        Nf, Nind, NindNew, off, idxOff, f, p;
61267773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
61277773e69fSMatthew G. Knepley 
61287773e69fSMatthew G. Knepley   PetscFunctionBegin;
61297773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61307773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
613136fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
61327773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
61337773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
61347773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
61357773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6136580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
61377773e69fSMatthew G. Knepley   /* Get points in closure */
6138923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
613905586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
61407773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
61417773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
61427773e69fSMatthew G. Knepley     PetscInt dof, fdof;
61437773e69fSMatthew G. Knepley 
61447773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
61457773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
61467773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
61477773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
61487773e69fSMatthew G. Knepley     }
61497773e69fSMatthew G. Knepley     Nind += dof;
61507773e69fSMatthew G. Knepley   }
61517773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
61528ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
61534acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
61544acb8e1eSToby Isaac   /* Get dual space symmetries */
61554acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
61564acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
61574acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
61584acb8e1eSToby Isaac   }
61597773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
61607773e69fSMatthew G. Knepley   {
61614acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
61627773e69fSMatthew G. Knepley     if (numPointsNew) {
61634acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
61644acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
61654acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
61664acb8e1eSToby Isaac       }
61674acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
61684acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
61694acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
61704acb8e1eSToby Isaac       }
6171923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
61727773e69fSMatthew G. Knepley       numPoints = numPointsNew;
61737773e69fSMatthew G. Knepley       Nind      = NindNew;
61747773e69fSMatthew G. Knepley       points    = pointsNew;
61757773e69fSMatthew G. Knepley     }
61767773e69fSMatthew G. Knepley   }
61777773e69fSMatthew G. Knepley   /* Calculate indices */
617869291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
61797773e69fSMatthew G. Knepley   if (Nf) {
61806ecaa68aSToby Isaac     if (outOffsets) {
61816ecaa68aSToby Isaac       PetscInt f;
61826ecaa68aSToby Isaac 
618346bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
61846ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
61856ecaa68aSToby Isaac       }
61866ecaa68aSToby Isaac     }
61874acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
618836fa2b79SJed Brown       ierr = PetscSectionGetOffset(idxSection, points[2*p], &idxOff);CHKERRQ(ierr);
618936fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, points[2*p], idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, *indices);CHKERRQ(ierr);
61907773e69fSMatthew G. Knepley     }
61917773e69fSMatthew G. Knepley   } else {
61924acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
61934acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
61944acb8e1eSToby Isaac 
619536fa2b79SJed Brown       ierr = PetscSectionGetOffset(idxSection, points[2*p], &idxOff);CHKERRQ(ierr);
619636fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, points[2*p], idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, *indices);CHKERRQ(ierr);
61977773e69fSMatthew G. Knepley     }
61987773e69fSMatthew G. Knepley   }
61997773e69fSMatthew G. Knepley   /* Cleanup points */
62004acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
62014acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
62024acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
62034acb8e1eSToby Isaac   }
62047773e69fSMatthew G. Knepley   if (numPointsNew) {
620569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
62067773e69fSMatthew G. Knepley   } else {
6207923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
62087773e69fSMatthew G. Knepley   }
62097773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
62107773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
62117773e69fSMatthew G. Knepley }
62127773e69fSMatthew G. Knepley 
62137cd05799SMatthew G. Knepley /*@C
62147cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
62157cd05799SMatthew G. Knepley 
62167cd05799SMatthew G. Knepley   Not collective
62177cd05799SMatthew G. Knepley 
62187cd05799SMatthew G. Knepley   Input Parameters:
62197cd05799SMatthew G. Knepley + dm - The DM
62207cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
62217cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
62227cd05799SMatthew G. Knepley . point - The mesh point
62237cd05799SMatthew G. Knepley . numIndices - The number of indices
62247cd05799SMatthew G. Knepley . indices - The indices
62257cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
62267cd05799SMatthew G. Knepley 
62277cd05799SMatthew G. Knepley   Level: advanced
62287cd05799SMatthew G. Knepley 
62297cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
62307cd05799SMatthew G. Knepley @*/
623146bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
62327773e69fSMatthew G. Knepley {
62337773e69fSMatthew G. Knepley   PetscErrorCode ierr;
62347773e69fSMatthew G. Knepley 
62357773e69fSMatthew G. Knepley   PetscFunctionBegin;
62367773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62377773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
623869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
62397773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
62407773e69fSMatthew G. Knepley }
62417773e69fSMatthew G. Knepley 
62427f5d1fdeSMatthew G. Knepley /*@C
62437f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
62447f5d1fdeSMatthew G. Knepley 
62457f5d1fdeSMatthew G. Knepley   Not collective
62467f5d1fdeSMatthew G. Knepley 
62477f5d1fdeSMatthew G. Knepley   Input Parameters:
62487f5d1fdeSMatthew G. Knepley + dm - The DM
6249ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
6250ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
62517f5d1fdeSMatthew G. Knepley . A - The matrix
6252eaf898f9SPatrick Sanan . point - The point in the DM
62537f5d1fdeSMatthew G. Knepley . values - The array of values
62547f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
62557f5d1fdeSMatthew G. Knepley 
62567f5d1fdeSMatthew G. Knepley   Fortran Notes:
62577f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
62587f5d1fdeSMatthew G. Knepley 
62597f5d1fdeSMatthew G. Knepley   Level: intermediate
62607f5d1fdeSMatthew G. Knepley 
62617f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
62627f5d1fdeSMatthew G. Knepley @*/
62637c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6264552f7358SJed Brown {
6265552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
62661b406b76SMatthew G. Knepley   PetscSection        clSection;
62671b406b76SMatthew G. Knepley   IS                  clPoints;
6268d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
626905586334SMatthew G. Knepley   const PetscInt     *clp, *clperm;
6270552f7358SJed Brown   PetscInt           *indices;
6271552f7358SJed Brown   PetscInt            offsets[32];
62724acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
62734acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
6274923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
62754acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
6276d3d1a6afSToby Isaac   PetscScalar        *newValues;
6277552f7358SJed Brown   PetscErrorCode      ierr;
6278552f7358SJed Brown 
6279552f7358SJed Brown   PetscFunctionBegin;
6280552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
628192fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
62823dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6283e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
62843dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
62853dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
6286552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
628782f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6288580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
628905586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
6290923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6291552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
6292552f7358SJed Brown     PetscInt fdof;
6293552f7358SJed Brown 
6294552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
6295552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
6296552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
6297552f7358SJed Brown       offsets[f+1] += fdof;
6298552f7358SJed Brown     }
6299552f7358SJed Brown     numIndices += dof;
6300552f7358SJed Brown   }
63010d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
63020d644c17SKarl Rupp 
63038ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
63044acb8e1eSToby Isaac   /* Get symmetries */
63054acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
63064acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
63074acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
63084acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
63094acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
63104acb8e1eSToby Isaac 
63114acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
63124acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
63134acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
63144acb8e1eSToby Isaac 
63154acb8e1eSToby Isaac         if (!numFields) {
63164acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
63174acb8e1eSToby Isaac         } else {
63184acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
63194acb8e1eSToby Isaac         }
63204acb8e1eSToby Isaac         if (flip) {
63214acb8e1eSToby Isaac           PetscInt i, j, k;
63224acb8e1eSToby Isaac 
63234acb8e1eSToby Isaac           if (!valCopy) {
632469291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
63254acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
63264acb8e1eSToby Isaac             values = valCopy;
63274acb8e1eSToby Isaac           }
63284acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
6329775f7be2SToby Isaac             PetscScalar fval = flip[i];
63304acb8e1eSToby Isaac 
63314acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
63324acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
63334acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
63344acb8e1eSToby Isaac             }
63354acb8e1eSToby Isaac           }
63364acb8e1eSToby Isaac         }
63374acb8e1eSToby Isaac         foffset += fdof;
63384acb8e1eSToby Isaac       }
63394acb8e1eSToby Isaac     }
63404acb8e1eSToby Isaac   }
63414acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
6342d3d1a6afSToby Isaac   if (newNumPoints) {
63434acb8e1eSToby Isaac     if (valCopy) {
634469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
63454acb8e1eSToby Isaac     }
63464acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
63474acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
63484acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
63494acb8e1eSToby Isaac     }
63504acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
63514acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
63524acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
63534acb8e1eSToby Isaac     }
6354923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6355d3d1a6afSToby Isaac     numPoints  = newNumPoints;
6356d3d1a6afSToby Isaac     numIndices = newNumIndices;
6357d3d1a6afSToby Isaac     points     = newPoints;
6358d3d1a6afSToby Isaac     values     = newValues;
6359d3d1a6afSToby Isaac   }
636069291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6361552f7358SJed Brown   if (numFields) {
63627e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
63637e29afd2SMatthew G. Knepley 
63647e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
63657e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
63667e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
6367645102dcSJed Brown         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, perms, p, clperm, indices);CHKERRQ(ierr);
63687e29afd2SMatthew G. Knepley       }
63697e29afd2SMatthew G. Knepley     } else {
63704acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
63714acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
637236fa2b79SJed Brown         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
637336fa2b79SJed Brown          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
637436fa2b79SJed Brown          * global section. */
637536fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_FALSE, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, indices);CHKERRQ(ierr);
6376552f7358SJed Brown       }
63777e29afd2SMatthew G. Knepley     }
6378552f7358SJed Brown   } else {
63794acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
63804acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
63814acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
638236fa2b79SJed Brown       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
638336fa2b79SJed Brown        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
638436fa2b79SJed Brown        * global section. */
638536fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_FALSE, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, indices);CHKERRQ(ierr);
6386552f7358SJed Brown     }
6387552f7358SJed Brown   }
6388b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
638936fa2b79SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);CHKERRQ(ierr);
6390ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
6391ab1d0545SMatthew G. Knepley     PetscInt i;
6392ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
63938ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
6394ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
6395ab1d0545SMatthew G. Knepley   }
6396552f7358SJed Brown   if (ierr) {
6397552f7358SJed Brown     PetscMPIInt    rank;
6398552f7358SJed Brown     PetscErrorCode ierr2;
6399552f7358SJed Brown 
640082f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6401e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6402b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
640369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
6404552f7358SJed Brown     CHKERRQ(ierr);
6405552f7358SJed Brown   }
64064acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
64074acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
64084acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
64094acb8e1eSToby Isaac   }
6410d3d1a6afSToby Isaac   if (newNumPoints) {
641169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
641269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
6413d3d1a6afSToby Isaac   }
6414d3d1a6afSToby Isaac   else {
64154acb8e1eSToby Isaac     if (valCopy) {
641669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
64174acb8e1eSToby Isaac     }
6418923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6419d3d1a6afSToby Isaac   }
642069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6421552f7358SJed Brown   PetscFunctionReturn(0);
6422552f7358SJed Brown }
6423552f7358SJed Brown 
6424de41b84cSMatthew 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)
6425de41b84cSMatthew G. Knepley {
6426de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6427de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6428de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6429de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
643017c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6431de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6432*412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
64334ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6434de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6435de41b84cSMatthew G. Knepley 
6436de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6437de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6438de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
643992fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6440de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
644192fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6442de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6443e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6444de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6445e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6446de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6447de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6448de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6449de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6450580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6451580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
6452de41b84cSMatthew G. Knepley   /* Column indices */
6453de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
64544ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6455de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6456de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6457de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6458de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6459de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6460de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6461de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6462de41b84cSMatthew G. Knepley       ++q;
6463de41b84cSMatthew G. Knepley     }
6464de41b84cSMatthew G. Knepley   }
6465de41b84cSMatthew G. Knepley   numCPoints = q;
6466de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6467de41b84cSMatthew G. Knepley     PetscInt fdof;
6468de41b84cSMatthew G. Knepley 
6469de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
64704ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6471de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6472de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6473de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6474de41b84cSMatthew G. Knepley     }
6475de41b84cSMatthew G. Knepley     numCIndices += dof;
6476de41b84cSMatthew G. Knepley   }
6477de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6478de41b84cSMatthew G. Knepley   /* Row indices */
6479*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6480*412e9a14SMatthew G. Knepley   {
6481*412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6482*412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6483*412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6484*412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6485*412e9a14SMatthew G. Knepley   }
648669291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6487de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6488de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6489de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6490de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6491de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6492de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6493de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
64944ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
64954ca5e9f5SMatthew G. Knepley         if (!dof) continue;
64964ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
64974ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6498de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6499de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6500de41b84cSMatthew G. Knepley         ++q;
6501de41b84cSMatthew G. Knepley       }
6502de41b84cSMatthew G. Knepley     }
6503de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6504de41b84cSMatthew G. Knepley   }
6505de41b84cSMatthew G. Knepley   numFPoints = q;
6506de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6507de41b84cSMatthew G. Knepley     PetscInt fdof;
6508de41b84cSMatthew G. Knepley 
6509de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
65104ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6511de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6512de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6513de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6514de41b84cSMatthew G. Knepley     }
6515de41b84cSMatthew G. Knepley     numFIndices += dof;
6516de41b84cSMatthew G. Knepley   }
6517de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6518de41b84cSMatthew G. Knepley 
65198ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
65208ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
652169291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
652269291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6523de41b84cSMatthew G. Knepley   if (numFields) {
65244acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
65254acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
65264acb8e1eSToby Isaac 
65274acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
65284acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
65294acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6530de41b84cSMatthew G. Knepley     }
65314acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
65324acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
653336fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
65344acb8e1eSToby Isaac     }
65354acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
65364acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
653736fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
65384acb8e1eSToby Isaac     }
65394acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
65404acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
65414acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6542de41b84cSMatthew G. Knepley     }
6543de41b84cSMatthew G. Knepley   } else {
65444acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
65454acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
65464acb8e1eSToby Isaac 
65474acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
65484acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
65494acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
65504acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
65514acb8e1eSToby Isaac 
65524acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
655336fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6554de41b84cSMatthew G. Knepley     }
65554acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
65564acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
65574acb8e1eSToby Isaac 
65584acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
655936fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6560de41b84cSMatthew G. Knepley     }
65614acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
65624acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6563de41b84cSMatthew G. Knepley   }
6564de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
65654acb8e1eSToby Isaac   /* TODO: flips */
6566de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6567de41b84cSMatthew G. Knepley   if (ierr) {
6568de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6569de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6570de41b84cSMatthew G. Knepley 
6571de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6572e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6573de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
657469291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
657569291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6576de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6577de41b84cSMatthew G. Knepley   }
657869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6579de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
658069291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
658169291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6582de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6583de41b84cSMatthew G. Knepley }
6584de41b84cSMatthew G. Knepley 
65857c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
65867c927364SMatthew G. Knepley {
65877c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
65887c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
65897c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
659017c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6591*412e9a14SMatthew G. Knepley   DMPolytopeType ct;
65927c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
65937c927364SMatthew G. Knepley   PetscErrorCode ierr;
65947c927364SMatthew G. Knepley 
65957c927364SMatthew G. Knepley   PetscFunctionBegin;
65967c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
65977c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
659892fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
65997c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
660092fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
66017c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6602e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
66037c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6604e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
66057c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
66067c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
66077c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6608580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6609580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
66107c927364SMatthew G. Knepley   /* Column indices */
66117c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
66127c927364SMatthew G. Knepley   maxFPoints = numCPoints;
66137c927364SMatthew G. Knepley   /* Compress out points not in the section */
66147c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
66157c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
66167c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
66177c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
66187c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
66197c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
66207c927364SMatthew G. Knepley       ++q;
66217c927364SMatthew G. Knepley     }
66227c927364SMatthew G. Knepley   }
66237c927364SMatthew G. Knepley   numCPoints = q;
66247c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
66257c927364SMatthew G. Knepley     PetscInt fdof;
66267c927364SMatthew G. Knepley 
66277c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
66287c927364SMatthew G. Knepley     if (!dof) continue;
66297c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
66307c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
66317c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
66327c927364SMatthew G. Knepley     }
66337c927364SMatthew G. Knepley     numCIndices += dof;
66347c927364SMatthew G. Knepley   }
66357c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
66367c927364SMatthew G. Knepley   /* Row indices */
6637*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
6638*412e9a14SMatthew G. Knepley   {
6639*412e9a14SMatthew G. Knepley     DMPlexCellRefiner cr;
6640*412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerCreate(dmc, &cr);CHKERRQ(ierr);
6641*412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
6642*412e9a14SMatthew G. Knepley     ierr = DMPlexCellRefinerDestroy(&cr);CHKERRQ(ierr);
6643*412e9a14SMatthew G. Knepley   }
664469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
66457c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
66467c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
66477c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
66487c927364SMatthew G. Knepley     /* Compress out points not in the section */
66497c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
66507c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
66517c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
66527c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
66537c927364SMatthew G. Knepley         if (!dof) continue;
66547c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
66557c927364SMatthew G. Knepley         if (s < q) continue;
66567c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
66577c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
66587c927364SMatthew G. Knepley         ++q;
66597c927364SMatthew G. Knepley       }
66607c927364SMatthew G. Knepley     }
66617c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
66627c927364SMatthew G. Knepley   }
66637c927364SMatthew G. Knepley   numFPoints = q;
66647c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
66657c927364SMatthew G. Knepley     PetscInt fdof;
66667c927364SMatthew G. Knepley 
66677c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
66687c927364SMatthew G. Knepley     if (!dof) continue;
66697c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
66707c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
66717c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
66727c927364SMatthew G. Knepley     }
66737c927364SMatthew G. Knepley     numFIndices += dof;
66747c927364SMatthew G. Knepley   }
66757c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
66767c927364SMatthew G. Knepley 
66778ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
66788ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
66797c927364SMatthew G. Knepley   if (numFields) {
66804acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
66814acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
66824acb8e1eSToby Isaac 
66834acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
66844acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
66854acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
66867c927364SMatthew G. Knepley     }
66874acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
66884acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
668936fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
66904acb8e1eSToby Isaac     }
66914acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
66924acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
669336fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
66944acb8e1eSToby Isaac     }
66954acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
66964acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
66974acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
66987c927364SMatthew G. Knepley     }
66997c927364SMatthew G. Knepley   } else {
67004acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
67014acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
67024acb8e1eSToby Isaac 
67034acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
67044acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
67054acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
67064acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
67074acb8e1eSToby Isaac 
67084acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
670936fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
67107c927364SMatthew G. Knepley     }
67114acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
67124acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
67134acb8e1eSToby Isaac 
67144acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
671536fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
67167c927364SMatthew G. Knepley     }
67174acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
67184acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
67197c927364SMatthew G. Knepley   }
672069291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
67217c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
67227c927364SMatthew G. Knepley   PetscFunctionReturn(0);
67237c927364SMatthew G. Knepley }
67247c927364SMatthew G. Knepley 
67257cd05799SMatthew G. Knepley /*@C
67267cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
67277cd05799SMatthew G. Knepley 
67287cd05799SMatthew G. Knepley   Input Parameter:
67297cd05799SMatthew G. Knepley . dm   - The DMPlex object
67307cd05799SMatthew G. Knepley 
67317cd05799SMatthew G. Knepley   Output Parameter:
67327cd05799SMatthew G. Knepley . cellHeight - The height of a cell
67337cd05799SMatthew G. Knepley 
67347cd05799SMatthew G. Knepley   Level: developer
67357cd05799SMatthew G. Knepley 
67367cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
67377cd05799SMatthew G. Knepley @*/
6738552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6739552f7358SJed Brown {
6740552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6741552f7358SJed Brown 
6742552f7358SJed Brown   PetscFunctionBegin;
6743552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6744552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6745552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6746552f7358SJed Brown   PetscFunctionReturn(0);
6747552f7358SJed Brown }
6748552f7358SJed Brown 
67497cd05799SMatthew G. Knepley /*@C
67507cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
67517cd05799SMatthew G. Knepley 
67527cd05799SMatthew G. Knepley   Input Parameters:
67537cd05799SMatthew G. Knepley + dm   - The DMPlex object
67547cd05799SMatthew G. Knepley - cellHeight - The height of a cell
67557cd05799SMatthew G. Knepley 
67567cd05799SMatthew G. Knepley   Level: developer
67577cd05799SMatthew G. Knepley 
67587cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
67597cd05799SMatthew G. Knepley @*/
6760552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6761552f7358SJed Brown {
6762552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6763552f7358SJed Brown 
6764552f7358SJed Brown   PetscFunctionBegin;
6765552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6766552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6767552f7358SJed Brown   PetscFunctionReturn(0);
6768552f7358SJed Brown }
6769552f7358SJed Brown 
6770e6139122SMatthew G. Knepley /*@
6771e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
6772e6139122SMatthew G. Knepley 
6773e6139122SMatthew G. Knepley   Input Parameter:
6774e6139122SMatthew G. Knepley . dm - The DMPlex object
6775e6139122SMatthew G. Knepley 
6776e6139122SMatthew G. Knepley   Output Parameters:
67772a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
67782a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
6779e6139122SMatthew G. Knepley 
67802a9f31c0SMatthew G. Knepley   Level: advanced
6781e6139122SMatthew G. Knepley 
6782*412e9a14SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum()
6783e6139122SMatthew G. Knepley @*/
6784e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
6785e6139122SMatthew G. Knepley {
6786*412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
6787e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6788e6139122SMatthew G. Knepley 
6789e6139122SMatthew G. Knepley   PetscFunctionBegin;
6790e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6791*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
6792*412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
6793e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6794e6139122SMatthew G. Knepley }
6795e6139122SMatthew G. Knepley 
6796552f7358SJed Brown /* We can easily have a form that takes an IS instead */
67979886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6798552f7358SJed Brown {
6799552f7358SJed Brown   PetscSection   section, globalSection;
6800552f7358SJed Brown   PetscInt      *numbers, p;
6801552f7358SJed Brown   PetscErrorCode ierr;
6802552f7358SJed Brown 
6803552f7358SJed Brown   PetscFunctionBegin;
680482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6805552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6806552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6807552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6808552f7358SJed Brown   }
6809552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
681015b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6811854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6812552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6813552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6814ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6815ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6816552f7358SJed Brown   }
681782f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6818ef48cebcSMatthew G. Knepley   if (globalSize) {
6819ef48cebcSMatthew G. Knepley     PetscLayout layout;
6820ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6821ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6822ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6823ef48cebcSMatthew G. Knepley   }
6824552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6825552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6826552f7358SJed Brown   PetscFunctionReturn(0);
6827552f7358SJed Brown }
6828552f7358SJed Brown 
682981ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6830552f7358SJed Brown {
6831*412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
6832552f7358SJed Brown   PetscErrorCode ierr;
6833552f7358SJed Brown 
6834552f7358SJed Brown   PetscFunctionBegin;
6835552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6836*412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
6837*412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
68389886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
683981ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6840552f7358SJed Brown }
684181ed3555SMatthew G. Knepley 
68428dab3259SMatthew G. Knepley /*@
68437cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
68447cd05799SMatthew G. Knepley 
68457cd05799SMatthew G. Knepley   Input Parameter:
68467cd05799SMatthew G. Knepley . dm   - The DMPlex object
68477cd05799SMatthew G. Knepley 
68487cd05799SMatthew G. Knepley   Output Parameter:
68497cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
68507cd05799SMatthew G. Knepley 
68517cd05799SMatthew G. Knepley   Level: developer
68527cd05799SMatthew G. Knepley 
68537cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
68547cd05799SMatthew G. Knepley @*/
685581ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
685681ed3555SMatthew G. Knepley {
685781ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
685881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
685981ed3555SMatthew G. Knepley 
686081ed3555SMatthew G. Knepley   PetscFunctionBegin;
686181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
686281ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6863552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6864552f7358SJed Brown   PetscFunctionReturn(0);
6865552f7358SJed Brown }
6866552f7358SJed Brown 
686781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
686881ed3555SMatthew G. Knepley {
6869*412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
687081ed3555SMatthew G. Knepley   PetscErrorCode ierr;
687181ed3555SMatthew G. Knepley 
687281ed3555SMatthew G. Knepley   PetscFunctionBegin;
687381ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
687481ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
68759886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
687681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
687781ed3555SMatthew G. Knepley }
687881ed3555SMatthew G. Knepley 
68798dab3259SMatthew G. Knepley /*@
68806aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
68817cd05799SMatthew G. Knepley 
68827cd05799SMatthew G. Knepley   Input Parameter:
68837cd05799SMatthew G. Knepley . dm   - The DMPlex object
68847cd05799SMatthew G. Knepley 
68857cd05799SMatthew G. Knepley   Output Parameter:
68867cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
68877cd05799SMatthew G. Knepley 
68887cd05799SMatthew G. Knepley   Level: developer
68897cd05799SMatthew G. Knepley 
68907cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
68917cd05799SMatthew G. Knepley @*/
6892552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6893552f7358SJed Brown {
6894552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6895552f7358SJed Brown   PetscErrorCode ierr;
6896552f7358SJed Brown 
6897552f7358SJed Brown   PetscFunctionBegin;
6898552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
689981ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6900552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6901552f7358SJed Brown   PetscFunctionReturn(0);
6902552f7358SJed Brown }
6903552f7358SJed Brown 
69048dab3259SMatthew G. Knepley /*@
69057cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
69067cd05799SMatthew G. Knepley 
69077cd05799SMatthew G. Knepley   Input Parameter:
69087cd05799SMatthew G. Knepley . dm   - The DMPlex object
69097cd05799SMatthew G. Knepley 
69107cd05799SMatthew G. Knepley   Output Parameter:
69117cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
69127cd05799SMatthew G. Knepley 
69137cd05799SMatthew G. Knepley   Level: developer
69147cd05799SMatthew G. Knepley 
69157cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
69167cd05799SMatthew G. Knepley @*/
6917ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6918ef48cebcSMatthew G. Knepley {
6919ef48cebcSMatthew G. Knepley   IS             nums[4];
6920862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6921ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6922ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6923ef48cebcSMatthew G. Knepley 
6924ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6925ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6926ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
69278abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
69288abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6929862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6930862913ffSStefano Zampini     PetscInt end;
6931862913ffSStefano Zampini 
6932862913ffSStefano Zampini     depths[d] = depth-d;
6933862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6934862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6935862913ffSStefano Zampini   }
6936862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6937862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6938862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6939862913ffSStefano 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]);
6940862913ffSStefano Zampini   }
6941ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6942ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6943ef48cebcSMatthew G. Knepley 
6944862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
69459886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6946ef48cebcSMatthew G. Knepley     shift += gsize;
6947ef48cebcSMatthew G. Knepley   }
6948302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6949ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6950ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6951ef48cebcSMatthew G. Knepley }
6952ef48cebcSMatthew G. Knepley 
695308a22f4bSMatthew G. Knepley 
695408a22f4bSMatthew G. Knepley /*@
695508a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
695608a22f4bSMatthew G. Knepley 
695708a22f4bSMatthew G. Knepley   Input Parameter:
695808a22f4bSMatthew G. Knepley . dm - The DMPlex object
695908a22f4bSMatthew G. Knepley 
696008a22f4bSMatthew G. Knepley   Output Parameter:
696108a22f4bSMatthew G. Knepley . ranks - The rank field
696208a22f4bSMatthew G. Knepley 
696308a22f4bSMatthew G. Knepley   Options Database Keys:
696408a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
696508a22f4bSMatthew G. Knepley 
696608a22f4bSMatthew G. Knepley   Level: intermediate
696708a22f4bSMatthew G. Knepley 
696808a22f4bSMatthew G. Knepley .seealso: DMView()
696908a22f4bSMatthew G. Knepley @*/
697008a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
697108a22f4bSMatthew G. Knepley {
697208a22f4bSMatthew G. Knepley   DM             rdm;
697308a22f4bSMatthew G. Knepley   PetscFE        fe;
697408a22f4bSMatthew G. Knepley   PetscScalar   *r;
697508a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
697608a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
697708a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
697808a22f4bSMatthew G. Knepley 
697908a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6980f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6981f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
698208a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
698308a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
698408a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6985f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
698608a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
6987e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
698808a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6989e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
699008a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
699108a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
699208a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
699308a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
699408a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
699508a22f4bSMatthew G. Knepley     PetscScalar *lr;
699608a22f4bSMatthew G. Knepley 
699708a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
699871f09efeSPierre Jolivet     if (lr) *lr = rank;
699908a22f4bSMatthew G. Knepley   }
700008a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
700108a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
700208a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
700308a22f4bSMatthew G. Knepley }
700408a22f4bSMatthew G. Knepley 
7005ca8062c8SMatthew G. Knepley /*@
700618e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
700718e14f0cSMatthew G. Knepley 
700818e14f0cSMatthew G. Knepley   Input Parameters:
700918e14f0cSMatthew G. Knepley + dm    - The DMPlex
701018e14f0cSMatthew G. Knepley - label - The DMLabel
701118e14f0cSMatthew G. Knepley 
701218e14f0cSMatthew G. Knepley   Output Parameter:
701318e14f0cSMatthew G. Knepley . val - The label value field
701418e14f0cSMatthew G. Knepley 
701518e14f0cSMatthew G. Knepley   Options Database Keys:
701618e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
701718e14f0cSMatthew G. Knepley 
701818e14f0cSMatthew G. Knepley   Level: intermediate
701918e14f0cSMatthew G. Knepley 
702018e14f0cSMatthew G. Knepley .seealso: DMView()
702118e14f0cSMatthew G. Knepley @*/
702218e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
702318e14f0cSMatthew G. Knepley {
702418e14f0cSMatthew G. Knepley   DM             rdm;
702518e14f0cSMatthew G. Knepley   PetscFE        fe;
702618e14f0cSMatthew G. Knepley   PetscScalar   *v;
702718e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
702818e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
702918e14f0cSMatthew G. Knepley 
703018e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
703118e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
703218e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
703318e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
703418e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
703518e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
703618e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
703718e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
7038e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
703918e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
7040e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
704118e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
704218e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
7043effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
704418e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
704518e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
704618e14f0cSMatthew G. Knepley     PetscScalar *lv;
704718e14f0cSMatthew G. Knepley     PetscInt     cval;
704818e14f0cSMatthew G. Knepley 
704918e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
705018e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
705118e14f0cSMatthew G. Knepley     *lv = cval;
705218e14f0cSMatthew G. Knepley   }
705318e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
705418e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
705518e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
705618e14f0cSMatthew G. Knepley }
705718e14f0cSMatthew G. Knepley 
705818e14f0cSMatthew G. Knepley /*@
7059ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
7060ca8062c8SMatthew G. Knepley 
706169916449SMatthew G. Knepley   Input Parameter:
706269916449SMatthew G. Knepley . dm - The DMPlex object
7063ca8062c8SMatthew G. Knepley 
706495eb5ee5SVaclav Hapla   Notes:
706595eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
706695eb5ee5SVaclav Hapla 
706795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7068ca8062c8SMatthew G. Knepley 
7069ca8062c8SMatthew G. Knepley   Level: developer
7070ca8062c8SMatthew G. Knepley 
707195eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7072ca8062c8SMatthew G. Knepley @*/
7073ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
7074ca8062c8SMatthew G. Knepley {
7075ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
7076ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
7077ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
707857beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
707957beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
7080ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
7081ca8062c8SMatthew G. Knepley 
7082ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7083ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7084*412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
7085ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
7086ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
7087ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
7088ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
7089ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
7090ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
7091ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
7092ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
709342e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
709442e66dfaSMatthew G. Knepley       PetscInt  d;
709542e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
709642e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
709742e66dfaSMatthew G. Knepley       }
7098ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
7099ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
7100ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
7101ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
7102ca8062c8SMatthew G. Knepley       }
710342e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
71048ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
7105ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
71068ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
7107ca8062c8SMatthew G. Knepley         }
7108302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71098ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
7110ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
71118ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
7112ca8062c8SMatthew G. Knepley         }
7113302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71148ccfff9cSToby 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]);
71158ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7116ca8062c8SMatthew G. Knepley       }
711742e66dfaSMatthew G. Knepley     }
711857beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
711957beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7120ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
7121ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
7122ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
7123ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
7124ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
7125ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
712657beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
712757beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
7128ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
7129ca8062c8SMatthew G. Knepley       }
7130ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
71318ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
7132ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
71338ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
7134ca8062c8SMatthew G. Knepley         }
7135302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71368ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
7137ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
71388ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
7139ca8062c8SMatthew G. Knepley         }
7140302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
71418ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7142ca8062c8SMatthew G. Knepley       }
7143ca8062c8SMatthew G. Knepley     }
7144ca8062c8SMatthew G. Knepley   }
714557beb4faSStefano Zampini   if (storagecheck) {
7146ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
7147ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
71488ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
714957beb4faSStefano Zampini   }
7150ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7151ca8062c8SMatthew G. Knepley }
7152ca8062c8SMatthew G. Knepley 
7153*412e9a14SMatthew G. Knepley /*
7154*412e9a14SMatthew G. Knepley   For submeshes with cohesive cells (see DMPlexConstructCohesiveCells()), we allow a special case where some of the boundary of a face (edges and vertices) are not duplicated. We call these special boundary points "unsplit", since the same edge or vertex appears in both copies of the face. These unsplit points throw off our counting, so we have to explicitly account for them here.
7155*412e9a14SMatthew G. Knepley */
7156*412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7157*412e9a14SMatthew G. Knepley {
7158*412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
7159*412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
7160*412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
7161*412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7162*412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
7163*412e9a14SMatthew G. Knepley 
7164*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7165*412e9a14SMatthew G. Knepley   *unsplit = 0;
7166*412e9a14SMatthew G. Knepley   switch (ct) {
7167*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7168*412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7169*412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7170*412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7171*412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
7172*412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7173*412e9a14SMatthew G. Knepley       }
7174*412e9a14SMatthew G. Knepley       break;
7175*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7176*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7177*412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
7178*412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7179*412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
7180*412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
7181*412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
7182*412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
7183*412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
7184*412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7185*412e9a14SMatthew G. Knepley             PetscInt p;
7186*412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7187*412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
7188*412e9a14SMatthew G. Knepley           }
7189*412e9a14SMatthew G. Knepley         }
7190*412e9a14SMatthew G. Knepley       }
7191*412e9a14SMatthew G. Knepley       break;
7192*412e9a14SMatthew G. Knepley     default: break;
7193*412e9a14SMatthew G. Knepley   }
7194*412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
7195*412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
7196*412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7197*412e9a14SMatthew G. Knepley   }
7198*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7199*412e9a14SMatthew G. Knepley }
7200*412e9a14SMatthew G. Knepley 
7201ca8062c8SMatthew G. Knepley /*@
7202ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7203ca8062c8SMatthew G. Knepley 
7204ca8062c8SMatthew G. Knepley   Input Parameters:
7205ca8062c8SMatthew G. Knepley + dm - The DMPlex object
720658723a97SMatthew G. Knepley - cellHeight - Normally 0
7207ca8062c8SMatthew G. Knepley 
720895eb5ee5SVaclav Hapla   Notes:
720995eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
721025c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7211ca8062c8SMatthew G. Knepley 
721295eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
721395eb5ee5SVaclav Hapla 
7214ca8062c8SMatthew G. Knepley   Level: developer
7215ca8062c8SMatthew G. Knepley 
721695eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7217ca8062c8SMatthew G. Knepley @*/
721825c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7219ca8062c8SMatthew G. Knepley {
7220*412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
7221*412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
7222*412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
7223ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
7224ca8062c8SMatthew G. Knepley 
7225ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7226ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7227*412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
722825c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
722958723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7230*412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7231*412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
7232*412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
723358723a97SMatthew G. Knepley 
7234*412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7235*412e9a14SMatthew G. Knepley     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7236*412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7237*412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
7238*412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
7239*412e9a14SMatthew G. Knepley       if (coneSize != DMPolytopeTypeGetConeSize(ct)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has cone size %D != %D", c, coneSize, DMPolytopeTypeGetConeSize(ct));
7240*412e9a14SMatthew G. Knepley     }
724158723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
724258723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
724358723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
7244*412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
724558723a97SMatthew G. Knepley     }
724658723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7247*412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
7248*412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7249*412e9a14SMatthew G. Knepley       PetscInt unsplit;
725042363296SMatthew G. Knepley 
7251*412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7252*412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
725342363296SMatthew G. Knepley     }
7254*412e9a14SMatthew G. Knepley     if (Nv != DMPolytopeTypeGetNumVertices(ct)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D vertices != %D", c, Nv, DMPolytopeTypeGetNumVertices(ct));
725542363296SMatthew G. Knepley   }
7256ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7257ca8062c8SMatthew G. Knepley }
72589bf0dad6SMatthew G. Knepley 
72599bf0dad6SMatthew G. Knepley /*@
72609bf0dad6SMatthew 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
72619bf0dad6SMatthew G. Knepley 
7262899ea2b8SJacob Faibussowitsch   Not Collective
7263899ea2b8SJacob Faibussowitsch 
72649bf0dad6SMatthew G. Knepley   Input Parameters:
72659bf0dad6SMatthew G. Knepley + dm - The DMPlex object
72669bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
72679bf0dad6SMatthew G. Knepley 
726845da879fSVaclav Hapla   Notes:
726945da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
727045da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
727145da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
727245da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
72739bf0dad6SMatthew G. Knepley 
727495eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
727595eb5ee5SVaclav Hapla 
72769bf0dad6SMatthew G. Knepley   Level: developer
72779bf0dad6SMatthew G. Knepley 
727895eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
72799bf0dad6SMatthew G. Knepley @*/
728025c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
72819bf0dad6SMatthew G. Knepley {
7282ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
72839bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
7284899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
72859bf0dad6SMatthew G. Knepley 
72869bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
72879bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7288899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
728945da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
729045da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7291899ea2b8SJacob Faibussowitsch     PetscMPIInt	rank;
7292899ea2b8SJacob Faibussowitsch     MPI_Comm	comm;
7293899ea2b8SJacob Faibussowitsch 
7294899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
7295899ea2b8SJacob Faibussowitsch     ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
729645da879fSVaclav Hapla     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7297899ea2b8SJacob Faibussowitsch   }
7298899ea2b8SJacob Faibussowitsch 
7299c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7300ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
73019bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7302ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
73033554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
73043554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
7305*412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7306*412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
7307ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
7308*412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
7309*412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
73109bf0dad6SMatthew G. Knepley 
7311ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7312*412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7313*412e9a14SMatthew G. Knepley       if (unsplit) continue;
73149bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
73159bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
73169bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
73179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
73189bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
73199bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
73209bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
73219bf0dad6SMatthew G. Knepley       }
7322*412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
73238ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
73249bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
73259bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
73269bf0dad6SMatthew G. Knepley 
73279bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
73289bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
73299bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
73309bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
73319bf0dad6SMatthew G. Knepley         }
7332*412e9a14SMatthew G. Knepley         if (fnumCorners != faceSizes[f]) 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, faceSizes[f]);
73339bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
7334*412e9a14SMatthew G. Knepley           if (fclosure[v] != faces[fOff+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[fOff+v]);
73359bf0dad6SMatthew G. Knepley         }
73369bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
7337*412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
73389bf0dad6SMatthew G. Knepley       }
7339*412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
73409bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
73419bf0dad6SMatthew G. Knepley     }
73423554e41dSMatthew G. Knepley   }
7343552f7358SJed Brown   PetscFunctionReturn(0);
7344552f7358SJed Brown }
73453913d7c8SMatthew G. Knepley 
7346bb6a34a8SMatthew G. Knepley /*@
7347bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7348bb6a34a8SMatthew G. Knepley 
7349bb6a34a8SMatthew G. Knepley   Input Parameter:
7350bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7351bb6a34a8SMatthew G. Knepley 
735295eb5ee5SVaclav Hapla   Notes:
735395eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
735495eb5ee5SVaclav Hapla 
735595eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7356bb6a34a8SMatthew G. Knepley 
7357bb6a34a8SMatthew G. Knepley   Level: developer
7358bb6a34a8SMatthew G. Knepley 
735995eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
7360bb6a34a8SMatthew G. Knepley @*/
7361bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7362bb6a34a8SMatthew G. Knepley {
7363bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7364bb6a34a8SMatthew G. Knepley   PetscReal      vol;
7365*412e9a14SMatthew G. Knepley   PetscBool      periodic;
7366*412e9a14SMatthew G. Knepley   PetscInt       dim, depth, d, cStart, cEnd, c;
7367bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7368bb6a34a8SMatthew G. Knepley 
7369bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7370bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7371bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7372*412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
7373bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7374bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7375*412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
7376*412e9a14SMatthew G. Knepley     DMPolytopeType ct;
7377*412e9a14SMatthew G. Knepley     PetscInt       unsplit;
7378*412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
7379*412e9a14SMatthew G. Knepley 
7380*412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
7381*412e9a14SMatthew G. Knepley     switch (ct) {
7382*412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7383*412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7384*412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7385*412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
7386*412e9a14SMatthew G. Knepley       default: break;
7387*412e9a14SMatthew G. Knepley     }
7388*412e9a14SMatthew G. Knepley     switch (ct) {
7389*412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
7390*412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7391*412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7392*412e9a14SMatthew G. Knepley         continue;
7393*412e9a14SMatthew G. Knepley       default: break;
7394*412e9a14SMatthew G. Knepley     }
7395*412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
7396*412e9a14SMatthew G. Knepley     if (unsplit) continue;
7397bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7398*412e9a14SMatthew G. Knepley     if (detJ < -PETSC_SMALL || (detJ <= 0.0 && !ignoreZeroVol)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
7399bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7400*412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
7401bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7402*412e9a14SMatthew G. Knepley       if (vol < -PETSC_SMALL || (vol <= 0.0 && !ignoreZeroVol)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
7403bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7404bb6a34a8SMatthew G. Knepley     }
7405bb6a34a8SMatthew G. Knepley   }
7406bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7407bb6a34a8SMatthew G. Knepley }
7408bb6a34a8SMatthew G. Knepley 
740903da9461SVaclav Hapla /*@
7410e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
741103da9461SVaclav Hapla 
741203da9461SVaclav Hapla   Input Parameters:
741303da9461SVaclav Hapla . dm - The DMPlex object
741403da9461SVaclav Hapla 
7415e83a0d2dSVaclav Hapla   Notes:
7416e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
74178918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
741803da9461SVaclav Hapla 
741995eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
742095eb5ee5SVaclav Hapla 
742103da9461SVaclav Hapla   Level: developer
742203da9461SVaclav Hapla 
742395eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
742403da9461SVaclav Hapla @*/
742503da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
742603da9461SVaclav Hapla {
7427f0cfc026SVaclav Hapla   PetscSF         pointSF;
7428f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7429f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
7430f0cfc026SVaclav Hapla   PetscBool       distributed;
743103da9461SVaclav Hapla   PetscErrorCode  ierr;
743203da9461SVaclav Hapla 
743303da9461SVaclav Hapla   PetscFunctionBegin;
743403da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7435f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7436f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7437f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7438f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7439f0cfc026SVaclav Hapla   if (overlap) {
74408918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
74418918e3e2SVaclav Hapla     PetscFunctionReturn(0);
74428918e3e2SVaclav Hapla   }
7443f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7444f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7445f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7446f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
7447f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
744803da9461SVaclav Hapla 
7449ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7450f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
7451f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7452f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7453f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
7454f5869d18SMatthew G. Knepley 
7455f5869d18SMatthew G. Knepley     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
745603da9461SVaclav Hapla   }
7457ece87651SVaclav Hapla 
7458f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
7459f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
7460f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
7461f5869d18SMatthew G. Knepley     const PetscInt *cone;
7462f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
7463f5869d18SMatthew G. Knepley 
7464f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
7465f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
7466f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
7467f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
7468f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
7469f5869d18SMatthew 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]);
7470f5869d18SMatthew G. Knepley       }
7471f5869d18SMatthew G. Knepley     }
7472ece87651SVaclav Hapla   }
747303da9461SVaclav Hapla   PetscFunctionReturn(0);
747403da9461SVaclav Hapla }
747503da9461SVaclav Hapla 
7476068a5610SStefano Zampini typedef struct cell_stats
7477068a5610SStefano Zampini {
7478068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7479068a5610SStefano Zampini   PetscInt  count;
7480068a5610SStefano Zampini } cell_stats_t;
7481068a5610SStefano Zampini 
748225befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7483068a5610SStefano Zampini {
7484068a5610SStefano Zampini   PetscInt i, N = *len;
7485068a5610SStefano Zampini 
7486068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7487068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7488068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7489068a5610SStefano Zampini 
7490068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7491068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7492068a5610SStefano Zampini     B->sum += A->sum;
7493068a5610SStefano Zampini     B->squaresum += A->squaresum;
7494068a5610SStefano Zampini     B->count += A->count;
7495068a5610SStefano Zampini   }
7496068a5610SStefano Zampini }
7497068a5610SStefano Zampini 
7498068a5610SStefano Zampini /*@
749943fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
7500068a5610SStefano Zampini 
75018261a58bSMatthew G. Knepley   Collective on dm
75028261a58bSMatthew G. Knepley 
7503068a5610SStefano Zampini   Input Parameters:
7504068a5610SStefano Zampini + dm        - The DMPlex object
750543fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
750643fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
7507068a5610SStefano Zampini 
750895eb5ee5SVaclav Hapla   Notes:
750995eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
751095eb5ee5SVaclav Hapla 
751195eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
7512068a5610SStefano Zampini 
7513068a5610SStefano Zampini   Level: developer
7514068a5610SStefano Zampini 
751595eb5ee5SVaclav Hapla .seealso: DMSetFromOptions()
7516068a5610SStefano Zampini @*/
751743fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7518068a5610SStefano Zampini {
7519068a5610SStefano Zampini   DM             dmCoarse;
752043fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
752143fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
752243fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
752343fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
7524*412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
752543fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
7526068a5610SStefano Zampini   PetscErrorCode ierr;
7527068a5610SStefano Zampini 
7528068a5610SStefano Zampini   PetscFunctionBegin;
7529068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7530068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7531068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7532068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7533068a5610SStefano Zampini   stats.count = 0;
7534068a5610SStefano Zampini 
753543fa8764SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
753643fa8764SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
753743fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
753843fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
7539*412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
754043fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
7541*412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
7542068a5610SStefano Zampini     PetscInt  i;
7543068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7544068a5610SStefano Zampini 
7545068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7546068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
754743fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
7548068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7549068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7550068a5610SStefano Zampini     }
7551068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7552068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7553068a5610SStefano Zampini 
7554068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7555068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7556068a5610SStefano Zampini     stats.sum       += cond;
7557068a5610SStefano Zampini     stats.squaresum += cond2;
7558068a5610SStefano Zampini     stats.count++;
75598261a58bSMatthew G. Knepley     if (output && cond > limit) {
756043fa8764SMatthew G. Knepley       PetscSection coordSection;
756143fa8764SMatthew G. Knepley       Vec          coordsLocal;
756243fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
756343fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
756443fa8764SMatthew G. Knepley 
756543fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
756643fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
756743fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
7568087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
756943fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
757043fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
757143fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
757243fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
757348afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
757443fa8764SMatthew G. Knepley         }
757543fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
757643fa8764SMatthew G. Knepley       }
757743fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
757843fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
757943fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
758043fa8764SMatthew G. Knepley 
758143fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
758243fa8764SMatthew G. Knepley           PetscReal len;
758343fa8764SMatthew G. Knepley 
758443fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
7585087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
758643fa8764SMatthew G. Knepley         }
758743fa8764SMatthew G. Knepley       }
758843fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
758943fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
759043fa8764SMatthew G. Knepley     }
7591068a5610SStefano Zampini   }
75928261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
7593068a5610SStefano Zampini 
7594068a5610SStefano Zampini   if (size > 1) {
7595068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7596068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7597068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7598068a5610SStefano Zampini     MPI_Op        statReduce;
7599068a5610SStefano Zampini 
7600068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
7601068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
7602068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
7603068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
7604068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
7605068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
7606068a5610SStefano Zampini   } else {
7607580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
7608068a5610SStefano Zampini   }
7609068a5610SStefano Zampini   if (!rank) {
7610068a5610SStefano Zampini     count = globalStats.count;
7611068a5610SStefano Zampini     min   = globalStats.min;
7612068a5610SStefano Zampini     max   = globalStats.max;
7613068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7614068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7615068a5610SStefano Zampini   }
7616068a5610SStefano Zampini 
7617068a5610SStefano Zampini   if (output) {
7618068a5610SStefano 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);
7619068a5610SStefano Zampini   }
7620068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7621068a5610SStefano Zampini 
7622068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7623068a5610SStefano Zampini   if (dmCoarse) {
7624068a5610SStefano Zampini     PetscBool isplex;
7625068a5610SStefano Zampini 
7626068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7627068a5610SStefano Zampini     if (isplex) {
762843fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
7629068a5610SStefano Zampini     }
7630068a5610SStefano Zampini   }
7631068a5610SStefano Zampini   PetscFunctionReturn(0);
7632068a5610SStefano Zampini }
7633068a5610SStefano Zampini 
7634bceba477SMatthew G. Knepley /* Pointwise interpolation
7635bceba477SMatthew G. Knepley      Just code FEM for now
7636bceba477SMatthew G. Knepley      u^f = I u^c
76374ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
76384ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
76394ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
7640bceba477SMatthew G. Knepley */
7641bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7642bceba477SMatthew G. Knepley {
7643bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
7644bceba477SMatthew G. Knepley   PetscInt       m, n;
7645a063dac3SMatthew G. Knepley   void          *ctx;
764668132eb9SMatthew G. Knepley   DM             cdm;
7647fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
7648bceba477SMatthew G. Knepley   PetscErrorCode ierr;
7649bceba477SMatthew G. Knepley 
7650bceba477SMatthew G. Knepley   PetscFunctionBegin;
7651e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7652bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7653e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7654bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
765568132eb9SMatthew G. Knepley 
7656fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
7657bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
7658bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7659fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
7660a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
766168132eb9SMatthew G. Knepley 
7662a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
766368132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
766468132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
766568132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
766668132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
76674db47ee9SStefano Zampini   if (scaling) {
76685d1c2e58SMatthew G. Knepley     /* Use naive scaling */
76695d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
76704db47ee9SStefano Zampini   }
7671a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
7672a063dac3SMatthew G. Knepley }
7673bceba477SMatthew G. Knepley 
76746dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7675a063dac3SMatthew G. Knepley {
767690748bafSMatthew G. Knepley   PetscErrorCode ierr;
76776dbf9973SLawrence Mitchell   VecScatter     ctx;
767890748bafSMatthew G. Knepley 
7679a063dac3SMatthew G. Knepley   PetscFunctionBegin;
76806dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
76816dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
76826dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
7683bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7684bceba477SMatthew G. Knepley }
7685bceba477SMatthew G. Knepley 
7686bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7687bd041c0cSMatthew G. Knepley {
7688bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
7689bd041c0cSMatthew G. Knepley   PetscInt       m, n;
7690bd041c0cSMatthew G. Knepley   void          *ctx;
7691bd041c0cSMatthew G. Knepley   DM             cdm;
7692bd041c0cSMatthew G. Knepley   PetscBool      regular;
7693bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
7694bd041c0cSMatthew G. Knepley 
7695bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
7696e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7697bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7698e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7699bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
7700bd041c0cSMatthew G. Knepley 
7701bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
7702bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7703bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
7704bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
7705bd041c0cSMatthew G. Knepley 
7706bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
7707bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
7708bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7709bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7710bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
7711bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
7712bd041c0cSMatthew G. Knepley }
7713bd041c0cSMatthew G. Knepley 
77140aef6b92SMatthew G. Knepley /*@
77150aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
77160aef6b92SMatthew G. Knepley 
77170aef6b92SMatthew G. Knepley   Input Parameter:
77180aef6b92SMatthew G. Knepley . dm - The DMPlex object
77190aef6b92SMatthew G. Knepley 
77200aef6b92SMatthew G. Knepley   Output Parameter:
77210aef6b92SMatthew G. Knepley . regular - The flag
77220aef6b92SMatthew G. Knepley 
77230aef6b92SMatthew G. Knepley   Level: intermediate
77240aef6b92SMatthew G. Knepley 
77250aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
77260aef6b92SMatthew G. Knepley @*/
77270aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
77280aef6b92SMatthew G. Knepley {
77290aef6b92SMatthew G. Knepley   PetscFunctionBegin;
77300aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77310aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
77320aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
77330aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
77340aef6b92SMatthew G. Knepley }
77350aef6b92SMatthew G. Knepley 
77360aef6b92SMatthew G. Knepley /*@
77370aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
77380aef6b92SMatthew G. Knepley 
77390aef6b92SMatthew G. Knepley   Input Parameters:
77400aef6b92SMatthew G. Knepley + dm - The DMPlex object
77410aef6b92SMatthew G. Knepley - regular - The flag
77420aef6b92SMatthew G. Knepley 
77430aef6b92SMatthew G. Knepley   Level: intermediate
77440aef6b92SMatthew G. Knepley 
77450aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
77460aef6b92SMatthew G. Knepley @*/
77470aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
77480aef6b92SMatthew G. Knepley {
77490aef6b92SMatthew G. Knepley   PetscFunctionBegin;
77500aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77510aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
77520aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
77530aef6b92SMatthew G. Knepley }
77540aef6b92SMatthew G. Knepley 
7755*412e9a14SMatthew G. Knepley /*@
7756*412e9a14SMatthew G. Knepley   DMPlexGetCellRefinerType - Get the strategy for refining a cell
7757*412e9a14SMatthew G. Knepley 
7758*412e9a14SMatthew G. Knepley   Input Parameter:
7759*412e9a14SMatthew G. Knepley . dm - The DMPlex object
7760*412e9a14SMatthew G. Knepley 
7761*412e9a14SMatthew G. Knepley   Output Parameter:
7762*412e9a14SMatthew G. Knepley . cr - The strategy number
7763*412e9a14SMatthew G. Knepley 
7764*412e9a14SMatthew G. Knepley   Level: intermediate
7765*412e9a14SMatthew G. Knepley 
7766*412e9a14SMatthew G. Knepley .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
7767*412e9a14SMatthew G. Knepley @*/
7768*412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
7769*412e9a14SMatthew G. Knepley {
7770*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7771*412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7772*412e9a14SMatthew G. Knepley   PetscValidPointer(cr, 2);
7773*412e9a14SMatthew G. Knepley   *cr = ((DM_Plex *) dm->data)->cellRefiner;
7774*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7775*412e9a14SMatthew G. Knepley }
7776*412e9a14SMatthew G. Knepley 
7777*412e9a14SMatthew G. Knepley /*@
7778*412e9a14SMatthew G. Knepley   DMPlexSetCellRefinerType - Set the strategy for refining a cell
7779*412e9a14SMatthew G. Knepley 
7780*412e9a14SMatthew G. Knepley   Input Parameters:
7781*412e9a14SMatthew G. Knepley + dm - The DMPlex object
7782*412e9a14SMatthew G. Knepley - cr - The strategy number
7783*412e9a14SMatthew G. Knepley 
7784*412e9a14SMatthew G. Knepley   Level: intermediate
7785*412e9a14SMatthew G. Knepley 
7786*412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
7787*412e9a14SMatthew G. Knepley @*/
7788*412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
7789*412e9a14SMatthew G. Knepley {
7790*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
7791*412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7792*412e9a14SMatthew G. Knepley   ((DM_Plex *) dm->data)->cellRefiner = cr;
7793*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
7794*412e9a14SMatthew G. Knepley }
7795*412e9a14SMatthew G. Knepley 
7796f7c74593SToby Isaac /* anchors */
7797a68b90caSToby Isaac /*@
7798f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7799f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
7800a68b90caSToby Isaac 
7801e228b242SToby Isaac   not collective
7802a68b90caSToby Isaac 
7803a68b90caSToby Isaac   Input Parameters:
7804a68b90caSToby Isaac . dm - The DMPlex object
7805a68b90caSToby Isaac 
7806a68b90caSToby Isaac   Output Parameters:
7807a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7808a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7809a68b90caSToby Isaac 
7810a68b90caSToby Isaac 
7811a68b90caSToby Isaac   Level: intermediate
7812a68b90caSToby Isaac 
7813f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7814a68b90caSToby Isaac @*/
7815a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7816a68b90caSToby Isaac {
7817a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
781841e6d900SToby Isaac   PetscErrorCode ierr;
7819a68b90caSToby Isaac 
7820a68b90caSToby Isaac   PetscFunctionBegin;
7821a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
782241e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7823a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7824a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7825a68b90caSToby Isaac   PetscFunctionReturn(0);
7826a68b90caSToby Isaac }
7827a68b90caSToby Isaac 
7828a68b90caSToby Isaac /*@
7829f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7830f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7831a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7832a68b90caSToby Isaac 
7833a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7834f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7835a68b90caSToby Isaac 
7836e228b242SToby Isaac   collective on dm
7837a68b90caSToby Isaac 
7838a68b90caSToby Isaac   Input Parameters:
7839a68b90caSToby Isaac + dm - The DMPlex object
7840e228b242SToby 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).
7841e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7842a68b90caSToby Isaac 
7843a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7844a68b90caSToby Isaac 
7845a68b90caSToby Isaac   Level: intermediate
7846a68b90caSToby Isaac 
7847f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7848a68b90caSToby Isaac @*/
7849a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7850a68b90caSToby Isaac {
7851a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7852e228b242SToby Isaac   PetscMPIInt    result;
7853a68b90caSToby Isaac   PetscErrorCode ierr;
7854a68b90caSToby Isaac 
7855a68b90caSToby Isaac   PetscFunctionBegin;
7856a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7857e228b242SToby Isaac   if (anchorSection) {
7858e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7859e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7860f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7861e228b242SToby Isaac   }
7862e228b242SToby Isaac   if (anchorIS) {
7863e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7864e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7865f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7866e228b242SToby Isaac   }
7867a68b90caSToby Isaac 
7868a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7869a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7870a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7871a68b90caSToby Isaac 
7872a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7873a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7874a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7875a68b90caSToby Isaac 
7876a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7877a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7878a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7879a68b90caSToby Isaac     const PetscInt *anchors;
7880a68b90caSToby Isaac 
7881a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7882a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7883a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7884a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7885a68b90caSToby Isaac       PetscInt p;
7886a68b90caSToby Isaac 
7887a68b90caSToby Isaac       p = anchors[a];
7888a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7889a68b90caSToby Isaac         PetscInt dof;
7890a68b90caSToby Isaac 
7891a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7892a68b90caSToby Isaac         if (dof) {
7893a68b90caSToby Isaac           PetscErrorCode ierr2;
7894a68b90caSToby Isaac 
7895a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
78968ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7897a68b90caSToby Isaac         }
7898a68b90caSToby Isaac       }
7899a68b90caSToby Isaac     }
7900a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7901a68b90caSToby Isaac   }
7902a68b90caSToby Isaac #endif
7903f7c74593SToby Isaac   /* reset the generic constraints */
7904f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7905a68b90caSToby Isaac   PetscFunctionReturn(0);
7906a68b90caSToby Isaac }
7907a68b90caSToby Isaac 
7908f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7909a68b90caSToby Isaac {
7910f7c74593SToby Isaac   PetscSection anchorSection;
79116995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7912a68b90caSToby Isaac   PetscErrorCode ierr;
7913a68b90caSToby Isaac 
7914a68b90caSToby Isaac   PetscFunctionBegin;
7915a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7916a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7917e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7918a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
79196995de1eSToby Isaac   if (numFields) {
7920719ab38cSToby Isaac     PetscInt f;
7921a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7922719ab38cSToby Isaac 
7923719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7924719ab38cSToby Isaac       PetscInt numComp;
7925719ab38cSToby Isaac 
7926719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7927719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7928719ab38cSToby Isaac     }
79296995de1eSToby Isaac   }
7930a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
79316995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
79326995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
79336995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
79346995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7935a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7936a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7937a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7938a68b90caSToby Isaac     if (dof) {
7939a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7940a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7941a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7942a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7943a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7944a68b90caSToby Isaac       }
7945a68b90caSToby Isaac     }
7946a68b90caSToby Isaac   }
7947a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7948a68b90caSToby Isaac   PetscFunctionReturn(0);
7949a68b90caSToby Isaac }
7950a68b90caSToby Isaac 
7951f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7952a68b90caSToby Isaac {
7953f7c74593SToby Isaac   PetscSection aSec;
79540ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
79550ac89760SToby Isaac   const PetscInt *anchors;
79560ac89760SToby Isaac   PetscInt numFields, f;
795766ad2231SToby Isaac   IS aIS;
79580ac89760SToby Isaac   PetscErrorCode ierr;
79590ac89760SToby Isaac 
79600ac89760SToby Isaac   PetscFunctionBegin;
79610ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79620ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
79630ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
79640ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
79650ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7966302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7967a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
796866ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
79696995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
79706995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
79710ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
79720ac89760SToby Isaac   i[0] = 0;
79730ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
79740ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7975f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7976f19733c5SToby Isaac 
7977f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7978f19733c5SToby Isaac     if (!rDof) continue;
7979f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
79800ac89760SToby Isaac     if (numFields) {
79810ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
79820ac89760SToby Isaac         annz = 0;
7983f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7984f19733c5SToby Isaac           a = anchors[rOff + r];
79850ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
79860ac89760SToby Isaac           annz += aDof;
79870ac89760SToby Isaac         }
79880ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
79890ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
79900ac89760SToby Isaac         for (q = 0; q < dof; q++) {
79910ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
79920ac89760SToby Isaac         }
79930ac89760SToby Isaac       }
79940ac89760SToby Isaac     }
79950ac89760SToby Isaac     else {
79960ac89760SToby Isaac       annz = 0;
79970ac89760SToby Isaac       for (q = 0; q < dof; q++) {
79980ac89760SToby Isaac         a = anchors[off + q];
79990ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
80000ac89760SToby Isaac         annz += aDof;
80010ac89760SToby Isaac       }
80020ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
80030ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
80040ac89760SToby Isaac       for (q = 0; q < dof; q++) {
80050ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
80060ac89760SToby Isaac       }
80070ac89760SToby Isaac     }
80080ac89760SToby Isaac   }
80090ac89760SToby Isaac   nnz = i[m];
80100ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
80110ac89760SToby Isaac   offset = 0;
80120ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
80130ac89760SToby Isaac     if (numFields) {
80140ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
80150ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
80160ac89760SToby Isaac         for (q = 0; q < dof; q++) {
80170ac89760SToby Isaac           PetscInt rDof, rOff, r;
80180ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
80190ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
80200ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
80210ac89760SToby Isaac             PetscInt s;
80220ac89760SToby Isaac 
80230ac89760SToby Isaac             a = anchors[rOff + r];
80240ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
80250ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
80260ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
80270ac89760SToby Isaac               j[offset++] = aOff + s;
80280ac89760SToby Isaac             }
80290ac89760SToby Isaac           }
80300ac89760SToby Isaac         }
80310ac89760SToby Isaac       }
80320ac89760SToby Isaac     }
80330ac89760SToby Isaac     else {
80340ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
80350ac89760SToby Isaac       for (q = 0; q < dof; q++) {
80360ac89760SToby Isaac         PetscInt rDof, rOff, r;
80370ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
80380ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
80390ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
80400ac89760SToby Isaac           PetscInt s;
80410ac89760SToby Isaac 
80420ac89760SToby Isaac           a = anchors[rOff + r];
80430ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
80440ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
80450ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
80460ac89760SToby Isaac             j[offset++] = aOff + s;
80470ac89760SToby Isaac           }
80480ac89760SToby Isaac         }
80490ac89760SToby Isaac       }
80500ac89760SToby Isaac     }
80510ac89760SToby Isaac   }
80520ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
805325570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
805425570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
805566ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
80560ac89760SToby Isaac   PetscFunctionReturn(0);
80570ac89760SToby Isaac }
80580ac89760SToby Isaac 
805966ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
806066ad2231SToby Isaac {
8061f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
8062f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
806366ad2231SToby Isaac   Mat            cMat;
806466ad2231SToby Isaac   PetscErrorCode ierr;
806566ad2231SToby Isaac 
806666ad2231SToby Isaac   PetscFunctionBegin;
806766ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8068a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
806966ad2231SToby Isaac   if (anchorSection) {
807044a7f3ddSMatthew G. Knepley     PetscInt Nf;
8071e228b242SToby Isaac 
807292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
8073f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
8074f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
807544a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
807644a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
807766ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
807866ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
807966ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
808066ad2231SToby Isaac   }
808166ad2231SToby Isaac   PetscFunctionReturn(0);
808266ad2231SToby Isaac }
8083a93c429eSMatthew G. Knepley 
8084a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8085a93c429eSMatthew G. Knepley {
8086a93c429eSMatthew G. Knepley   IS             subis;
8087a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
8088a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
8089a93c429eSMatthew G. Knepley 
8090a93c429eSMatthew G. Knepley   PetscFunctionBegin;
809192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
8092a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8093a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8094a93c429eSMatthew G. Knepley   /* Create subdomain */
8095a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
8096a93c429eSMatthew G. Knepley   /* Create submodel */
8097a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
8098a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
8099a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
810092fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
8101a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
8102e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
8103a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
8104a93c429eSMatthew G. Knepley   if (is) {
8105a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
8106a93c429eSMatthew G. Knepley     IS              spIS;
8107a93c429eSMatthew G. Knepley     const PetscInt *spmap;
8108a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
8109a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8110a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
8111a93c429eSMatthew G. Knepley 
8112a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
8113a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
8114a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
81156f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
81166f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
8117a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
8118a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8119a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
8120a93c429eSMatthew G. Knepley 
8121a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
8122a93c429eSMatthew G. Knepley       if (gdof > 0) {
8123a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8124a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
8125a93c429eSMatthew G. Knepley 
8126a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
8127a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
8128a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
8129a93c429eSMatthew G. Knepley         }
8130a93c429eSMatthew G. Knepley         subSize += pSubSize;
8131a93c429eSMatthew G. Knepley         if (pSubSize) {
8132a93c429eSMatthew G. Knepley           if (bs < 0) {
8133a93c429eSMatthew G. Knepley             bs = pSubSize;
8134a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
8135a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
8136a93c429eSMatthew G. Knepley             bs = 1;
8137a93c429eSMatthew G. Knepley           }
8138a93c429eSMatthew G. Knepley         }
8139a93c429eSMatthew G. Knepley       }
8140a93c429eSMatthew G. Knepley     }
8141a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
8142a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8143a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
8144a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8145a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
8146a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
8147a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
8148a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
8149a93c429eSMatthew G. Knepley 
8150a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
8151a93c429eSMatthew G. Knepley       if (gdof > 0) {
8152a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
8153a93c429eSMatthew G. Knepley 
8154a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
8155a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
8156a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
8157a93c429eSMatthew G. Knepley 
8158a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
8159a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
8160a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
8161a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
8162a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
8163a93c429eSMatthew G. Knepley           }
8164a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
8165a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
8166a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8167a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
8168a93c429eSMatthew G. Knepley           }
8169a93c429eSMatthew G. Knepley         }
8170a93c429eSMatthew G. Knepley       }
8171a93c429eSMatthew G. Knepley     }
8172a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
8173a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
8174a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
8175a93c429eSMatthew G. Knepley     if (bs > 1) {
8176a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
8177a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
8178a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
8179a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
8180a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8181a93c429eSMatthew G. Knepley         }
8182a93c429eSMatthew G. Knepley       }
8183a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
8184a93c429eSMatthew G. Knepley     }
8185a93c429eSMatthew G. Knepley     /* Attach nullspace */
8186a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
8187a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8188a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
8189a93c429eSMatthew G. Knepley     }
8190a93c429eSMatthew G. Knepley     if (f < Nf) {
8191a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
8192a93c429eSMatthew G. Knepley 
8193a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
8194a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
8195a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
8196a93c429eSMatthew G. Knepley     }
8197a93c429eSMatthew G. Knepley   }
8198a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
8199a93c429eSMatthew G. Knepley }
8200c0f0dcc3SMatthew G. Knepley 
8201c0f0dcc3SMatthew G. Knepley /*@
8202c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
8203c0f0dcc3SMatthew G. Knepley 
8204c0f0dcc3SMatthew G. Knepley   Input Parameter:
8205c0f0dcc3SMatthew G. Knepley - dm - The DM
8206c0f0dcc3SMatthew G. Knepley 
8207c0f0dcc3SMatthew G. Knepley   Level: developer
8208c0f0dcc3SMatthew G. Knepley 
8209c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8210c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
8211c0f0dcc3SMatthew G. Knepley 
8212c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
8213c0f0dcc3SMatthew G. Knepley @*/
8214c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8215c0f0dcc3SMatthew G. Knepley {
8216c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
8217c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
8218c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
8219c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
8220c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
8221c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
8222c0f0dcc3SMatthew G. Knepley   const char        *name;
8223c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
8224c0f0dcc3SMatthew G. Knepley 
8225c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8226c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8227c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
8228c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
8229c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8230c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
8231c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
8232c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
8233c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
8234c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
8235c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
8236c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
8237c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
8238fae64647SBarry 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);
8239c0f0dcc3SMatthew G. Knepley #else
8240c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8241c0f0dcc3SMatthew G. Knepley #endif
8242c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8243c0f0dcc3SMatthew G. Knepley }
8244