xref: /petsc/src/dm/impls/plex/plex.c (revision e432b41dc02bf0734625665660df6f79e3b54e76)
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>
9012bc364SMatthew G. Knepley #include <petscdmplextransform.h>
10552f7358SJed Brown 
11552f7358SJed Brown /* Logging support */
1202f7d72cSksagiyam PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Symmetrize, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh, DMPLEX_RebalanceSharedPoints, DMPLEX_PartSelf, DMPLEX_PartLabelInvert, DMPLEX_PartLabelCreateSF, DMPLEX_PartStratSF, DMPLEX_CreatePointSF,DMPLEX_LocatePoints,DMPLEX_TopologyView,DMPLEX_LabelsView,DMPLEX_CoordinatesView,DMPLEX_SectionView,DMPLEX_GlobalVectorView,DMPLEX_LocalVectorView,DMPLEX_TopologyLoad,DMPLEX_LabelsLoad,DMPLEX_CoordinatesLoad,DMPLEX_SectionLoad,DMPLEX_GlobalVectorLoad,DMPLEX_LocalVectorLoad;
13552f7358SJed Brown 
145a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
15552f7358SJed Brown 
16e5337592SStefano Zampini /*@
179318fe57SMatthew G. Knepley   DMPlexIsSimplex - Is the first cell in this mesh a simplex?
189318fe57SMatthew G. Knepley 
199318fe57SMatthew G. Knepley   Input Parameter:
209318fe57SMatthew G. Knepley . dm      - The DMPlex object
219318fe57SMatthew G. Knepley 
229318fe57SMatthew G. Knepley   Output Parameter:
239318fe57SMatthew G. Knepley . simplex - Flag checking for a simplex
249318fe57SMatthew G. Knepley 
259318fe57SMatthew 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.
269318fe57SMatthew G. Knepley   If the mesh has no cells, this returns PETSC_FALSE.
279318fe57SMatthew G. Knepley 
289318fe57SMatthew G. Knepley   Level: intermediate
299318fe57SMatthew G. Knepley 
309318fe57SMatthew G. Knepley .seealso DMPlexGetSimplexOrBoxCells(), DMPlexGetCellType(), DMPlexGetHeightStratum(), DMPolytopeTypeGetNumVertices()
319318fe57SMatthew G. Knepley @*/
329318fe57SMatthew G. Knepley PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
339318fe57SMatthew G. Knepley {
349318fe57SMatthew G. Knepley   DMPolytopeType ct;
359318fe57SMatthew G. Knepley   PetscInt       cStart, cEnd;
369318fe57SMatthew G. Knepley   PetscErrorCode ierr;
379318fe57SMatthew G. Knepley 
389318fe57SMatthew G. Knepley   PetscFunctionBegin;
399318fe57SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
409318fe57SMatthew G. Knepley   if (cEnd <= cStart) {*simplex = PETSC_FALSE; PetscFunctionReturn(0);}
419318fe57SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
429318fe57SMatthew G. Knepley   *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
439318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
449318fe57SMatthew G. Knepley }
459318fe57SMatthew G. Knepley 
469318fe57SMatthew G. Knepley /*@
47412e9a14SMatthew G. Knepley   DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells
48e5337592SStefano Zampini 
49d8d19677SJose E. Roman   Input Parameters:
50412e9a14SMatthew G. Knepley + dm     - The DMPlex object
51412e9a14SMatthew G. Knepley - height - The cell height in the Plex, 0 is the default
52e5337592SStefano Zampini 
53e5337592SStefano Zampini   Output Parameters:
54412e9a14SMatthew G. Knepley + cStart - The first "normal" cell
55412e9a14SMatthew G. Knepley - cEnd   - The upper bound on "normal"" cells
56e5337592SStefano Zampini 
57412e9a14SMatthew 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.
58e5337592SStefano Zampini 
59412e9a14SMatthew G. Knepley   Level: developer
60e5337592SStefano Zampini 
618065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
62e5337592SStefano Zampini @*/
63412e9a14SMatthew G. Knepley PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
64e5337592SStefano Zampini {
65412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
66412e9a14SMatthew G. Knepley   PetscInt       cS, cE, c;
67e5337592SStefano Zampini   PetscErrorCode ierr;
68e5337592SStefano Zampini 
69e5337592SStefano Zampini   PetscFunctionBegin;
70412e9a14SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);CHKERRQ(ierr);
71412e9a14SMatthew G. Knepley   for (c = cS; c < cE; ++c) {
72412e9a14SMatthew G. Knepley     DMPolytopeType cct;
73412e9a14SMatthew G. Knepley 
74412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &cct);CHKERRQ(ierr);
75412e9a14SMatthew G. Knepley     if ((PetscInt) cct < 0) break;
76412e9a14SMatthew G. Knepley     switch (cct) {
77ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_POINT:
78ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_SEGMENT:
79ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TRIANGLE:
80ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_QUADRILATERAL:
81ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_TETRAHEDRON:
82ba2698f1SMatthew G. Knepley       case DM_POLYTOPE_HEXAHEDRON:
83412e9a14SMatthew G. Knepley         ct = cct;
84e5337592SStefano Zampini         break;
85412e9a14SMatthew G. Knepley       default: break;
86e5337592SStefano Zampini     }
87412e9a14SMatthew G. Knepley     if (ct != DM_POLYTOPE_UNKNOWN) break;
88e5337592SStefano Zampini   }
89412e9a14SMatthew G. Knepley   if (ct != DM_POLYTOPE_UNKNOWN) {
90412e9a14SMatthew G. Knepley     DMLabel ctLabel;
91412e9a14SMatthew G. Knepley 
92412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
93412e9a14SMatthew G. Knepley     ierr = DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);CHKERRQ(ierr);
94e5337592SStefano Zampini   }
95412e9a14SMatthew G. Knepley   if (cStart) *cStart = cS;
96412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = cE;
97e5337592SStefano Zampini   PetscFunctionReturn(0);
98e5337592SStefano Zampini }
99e5337592SStefano Zampini 
1007afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
1017e42fee7SMatthew G. Knepley {
102412e9a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
103a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
1047e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
1057e42fee7SMatthew G. Knepley 
1067e42fee7SMatthew G. Knepley   PetscFunctionBegin;
107e630c359SToby Isaac   *ft  = PETSC_VTK_INVALID;
108f094498dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1097e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
110412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1117e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1127e42fee7SMatthew G. Knepley   if (field >= 0) {
113a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
114a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1157e42fee7SMatthew G. Knepley   } else {
116a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
117a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1187e42fee7SMatthew G. Knepley   }
119ffc4695bSBarry Smith   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
120a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1217e42fee7SMatthew G. Knepley     *sStart = vStart;
1227e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
123f094498dSMatthew G. Knepley     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1247e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_POINT_FIELD;
125a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1267e42fee7SMatthew G. Knepley     *sStart = cStart;
1277e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
128f094498dSMatthew G. Knepley     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1297e42fee7SMatthew G. Knepley     else                        *ft = PETSC_VTK_CELL_FIELD;
130e630c359SToby Isaac   } else {
131e630c359SToby Isaac     if (field >= 0) {
132e630c359SToby Isaac       const char *fieldname;
133e630c359SToby Isaac 
134e630c359SToby Isaac       ierr = PetscSectionGetFieldName(section, field, &fieldname);CHKERRQ(ierr);
1357d3de750SJacob Faibussowitsch       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);CHKERRQ(ierr);
136e630c359SToby Isaac     } else {
1376823f3c5SBlaise Bourdin       ierr = PetscInfo((PetscObject) dm, "Could not classify VTK output type of section\"%s\"\n");CHKERRQ(ierr);
138e630c359SToby Isaac     }
139e630c359SToby Isaac   }
1407e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1417e42fee7SMatthew G. Knepley }
1427e42fee7SMatthew G. Knepley 
1437cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
144e412dcbdSMatthew G. Knepley {
145e412dcbdSMatthew G. Knepley   DM                 dm;
146d1df6f1dSMatthew G. Knepley   PetscSection       s;
147e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
148e412dcbdSMatthew G. Knepley   DM                 cdm;
149e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
150e412dcbdSMatthew G. Knepley   Vec                coordinates;
151e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
152e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
153339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
154339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
155d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
156e412dcbdSMatthew G. Knepley   const char        *name;
157339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
158e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
159e412dcbdSMatthew G. Knepley 
160e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
161d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
162d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
163d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
164d1df6f1dSMatthew G. Knepley 
165e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1672c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dim != 2,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
16892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
169d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
17292fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
173e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
175e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
176e412dcbdSMatthew G. Knepley 
177e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
178339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
179e412dcbdSMatthew G. Knepley 
180e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
181e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
182e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1830c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1840c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
185e412dcbdSMatthew G. Knepley   }
186e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
187e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
188e412dcbdSMatthew G. Knepley 
189d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
190d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
191d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
192d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
193d1df6f1dSMatthew G. Knepley     IS   fis;
194d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
195d1df6f1dSMatthew G. Knepley     const char *fname;
196d1df6f1dSMatthew G. Knepley 
197d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
198d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
199d1df6f1dSMatthew G. Knepley 
200a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
201d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
202d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
203d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
204d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
205a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
206a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
207d1df6f1dSMatthew G. Knepley     }
208d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
209d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
210d1df6f1dSMatthew G. Knepley 
211d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
212d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
213d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
214d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
215d1df6f1dSMatthew G. Knepley 
216d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
217d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
218339e3443SMatthew G. Knepley       if (!flg) {
219d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
220d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
221d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
222339e3443SMatthew G. Knepley       }
223e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
224339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
225d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
226e412dcbdSMatthew G. Knepley 
227d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
228e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22999a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
230e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
231e412dcbdSMatthew G. Knepley 
232d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
233339e3443SMatthew G. Knepley         if (a) {
234d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
235339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
236339e3443SMatthew G. Knepley         } else {
237339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
238339e3443SMatthew G. Knepley           PetscInt     numVals, va;
239339e3443SMatthew G. Knepley 
240d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
2412c71b3e2SJacob Faibussowitsch           PetscCheckFalse(numVals % Nc,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);
242d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
243d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
244d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
245d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
246339e3443SMatthew G. Knepley             break;
247d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
248d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
249d1df6f1dSMatthew 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]);
250d1df6f1dSMatthew G. Knepley             break;
25198921bdaSJacob Faibussowitsch           default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
252339e3443SMatthew G. Knepley           }
253d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
254339e3443SMatthew G. Knepley         }
255e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
256e412dcbdSMatthew G. Knepley         switch (numCoords) {
257e412dcbdSMatthew G. Knepley         case 6:
2589edc3542SMatthew Knepley         case 12: /* Localized triangle */
259339e3443SMatthew 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);
260e412dcbdSMatthew G. Knepley           break;
261e412dcbdSMatthew G. Knepley         case 8:
2629edc3542SMatthew Knepley         case 16: /* Localized quadrilateral */
263339e3443SMatthew 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);
264339e3443SMatthew 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);
265e412dcbdSMatthew G. Knepley           break;
26698921bdaSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
267e412dcbdSMatthew G. Knepley         }
268e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
269e412dcbdSMatthew G. Knepley       }
270d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
271e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
272e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
273e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
274d1df6f1dSMatthew G. Knepley     }
275d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
276d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
277d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
278d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
279d1df6f1dSMatthew G. Knepley     }
280d1df6f1dSMatthew G. Knepley   }
281e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
282e412dcbdSMatthew G. Knepley }
283e412dcbdSMatthew G. Knepley 
284684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
285684b87d9SLisandro Dalcin {
286684b87d9SLisandro Dalcin   DM                      dm;
287684b87d9SLisandro Dalcin   Vec                     locv;
288684b87d9SLisandro Dalcin   const char              *name;
289684b87d9SLisandro Dalcin   PetscSection            section;
290684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
291e630c359SToby Isaac   PetscInt                numFields;
292684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
293684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
294684b87d9SLisandro Dalcin 
295684b87d9SLisandro Dalcin   PetscFunctionBegin;
296684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
297684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
298684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
299684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
300684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
30192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
302e630c359SToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
303e630c359SToby Isaac   if (!numFields) {
304684b87d9SLisandro Dalcin     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
305e630c359SToby Isaac     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
306e630c359SToby Isaac   } else {
307e630c359SToby Isaac     PetscInt f;
308e630c359SToby Isaac 
309e630c359SToby Isaac     for (f = 0; f < numFields; f++) {
310e630c359SToby Isaac       ierr = DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);CHKERRQ(ierr);
311e630c359SToby Isaac       if (ft == PETSC_VTK_INVALID) continue;
312e630c359SToby Isaac       ierr = PetscObjectReference((PetscObject)locv);CHKERRQ(ierr);
313e630c359SToby Isaac       ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
314e630c359SToby Isaac     }
315e630c359SToby Isaac     ierr = VecDestroy(&locv);CHKERRQ(ierr);
316e630c359SToby Isaac   }
317684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
318684b87d9SLisandro Dalcin }
319684b87d9SLisandro Dalcin 
320552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
321552f7358SJed Brown {
322552f7358SJed Brown   DM             dm;
323684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
324552f7358SJed Brown   PetscErrorCode ierr;
325552f7358SJed Brown 
326552f7358SJed Brown   PetscFunctionBegin;
327552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3282c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm,PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
329552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
330b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
331f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3328135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
333684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
334684b87d9SLisandro Dalcin     PetscInt    i,numFields;
335684b87d9SLisandro Dalcin     PetscObject fe;
336ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
337684b87d9SLisandro Dalcin     Vec         locv = v;
338684b87d9SLisandro Dalcin     const char  *name;
339684b87d9SLisandro Dalcin     PetscInt    step;
340684b87d9SLisandro Dalcin     PetscReal   time;
341ef31f671SMatthew G. Knepley 
342ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
343684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
34444a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &fe);CHKERRQ(ierr);
345684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
346ef31f671SMatthew G. Knepley     }
347684b87d9SLisandro Dalcin     if (fem) {
348798534f6SMatthew G. Knepley       PetscObject isZero;
349798534f6SMatthew G. Knepley 
350684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
351684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
352684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
353798534f6SMatthew G. Knepley       ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
354798534f6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
355684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
356684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
357684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
358ef31f671SMatthew G. Knepley     }
359552f7358SJed Brown     if (isvtk) {
360684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
361b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
362b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
363684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
364b136c2c9SMatthew G. Knepley #else
365b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
366b136c2c9SMatthew G. Knepley #endif
367f13a32a3SMatthew G. Knepley     } else if (isdraw) {
368684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
369684b87d9SLisandro Dalcin     } else if (isglvis) {
370684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
371684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
372684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
373684b87d9SLisandro Dalcin     }
374798534f6SMatthew G. Knepley     if (fem) {
375798534f6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
376798534f6SMatthew G. Knepley       ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
377798534f6SMatthew G. Knepley     }
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 
388552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
389552f7358SJed Brown {
390552f7358SJed Brown   DM             dm;
3916823f3c5SBlaise Bourdin   PetscBool      isvtk, ishdf5, isdraw, isglvis, isexodusii;
392552f7358SJed Brown   PetscErrorCode ierr;
393552f7358SJed Brown 
394552f7358SJed Brown   PetscFunctionBegin;
395552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3962c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm,PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
397552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
39833c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
399e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
4008135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
4016823f3c5SBlaise Bourdin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);CHKERRQ(ierr);
402684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
403552f7358SJed Brown     Vec         locv;
404798534f6SMatthew G. Knepley     PetscObject isZero;
405552f7358SJed Brown     const char *name;
406552f7358SJed Brown 
407c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
408552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
409552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
410552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
411552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
412798534f6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
413798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);CHKERRQ(ierr);
414552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
415798534f6SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);CHKERRQ(ierr);
416c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
417b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
418b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
41939d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
420b136c2c9SMatthew G. Knepley #else
421b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
422b136c2c9SMatthew G. Knepley #endif
4236823f3c5SBlaise Bourdin   } else if (isexodusii) {
4246823f3c5SBlaise Bourdin #if defined(PETSC_HAVE_EXODUSII)
4256823f3c5SBlaise Bourdin     ierr = VecView_PlexExodusII_Internal(v, viewer);CHKERRQ(ierr);
4266823f3c5SBlaise Bourdin #else
4276823f3c5SBlaise Bourdin     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
4286823f3c5SBlaise Bourdin #endif
429552f7358SJed Brown   } else {
430684b87d9SLisandro Dalcin     PetscBool isseq;
431684b87d9SLisandro Dalcin 
432684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
43355f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
43455f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
435552f7358SJed Brown   }
436552f7358SJed Brown   PetscFunctionReturn(0);
437552f7358SJed Brown }
438552f7358SJed Brown 
439d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
440d930f514SMatthew G. Knepley {
441d930f514SMatthew G. Knepley   DM                dm;
442d930f514SMatthew G. Knepley   MPI_Comm          comm;
443d930f514SMatthew G. Knepley   PetscViewerFormat format;
444d930f514SMatthew G. Knepley   Vec               v;
445d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
446d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
447d930f514SMatthew G. Knepley 
448d930f514SMatthew G. Knepley   PetscFunctionBegin;
449d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
450d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4512c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm,comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
452d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
453d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
454d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
455d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
456a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
457a8ad634aSStefano Zampini     /* this need a better fix */
458a8ad634aSStefano Zampini     if (dm->useNatural) {
459a8ad634aSStefano Zampini       if (dm->sfNatural) {
460d930f514SMatthew G. Knepley         const char *vecname;
461d930f514SMatthew G. Knepley         PetscInt    n, nroots;
462d930f514SMatthew G. Knepley 
463ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
464d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
465d930f514SMatthew G. Knepley         if (n == nroots) {
466d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
467d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
468d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
469d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
470d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
471d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
472d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
473a8ad634aSStefano Zampini     } else v = originalv;
474a8ad634aSStefano Zampini   } else v = originalv;
475a8ad634aSStefano Zampini 
476d930f514SMatthew G. Knepley   if (ishdf5) {
477d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
47839d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
479d930f514SMatthew G. Knepley #else
480d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
481d930f514SMatthew G. Knepley #endif
482d930f514SMatthew G. Knepley   } else if (isvtk) {
483d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
484d930f514SMatthew G. Knepley   } else {
485d930f514SMatthew G. Knepley     PetscBool isseq;
486d930f514SMatthew G. Knepley 
487d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
488d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
489d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
490d930f514SMatthew G. Knepley   }
491a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
492d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
493d930f514SMatthew G. Knepley }
494d930f514SMatthew G. Knepley 
4952c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4962c40f234SMatthew G. Knepley {
4972c40f234SMatthew G. Knepley   DM             dm;
4982c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4992c40f234SMatthew G. Knepley   PetscErrorCode ierr;
5002c40f234SMatthew G. Knepley 
5012c40f234SMatthew G. Knepley   PetscFunctionBegin;
5022c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
5032c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm,PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
5042c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5052c40f234SMatthew G. Knepley   if (ishdf5) {
5062c40f234SMatthew G. Knepley     DM          dmBC;
5072c40f234SMatthew G. Knepley     Vec         gv;
5082c40f234SMatthew G. Knepley     const char *name;
5092c40f234SMatthew G. Knepley 
5102c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
5112c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
5122c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
5132c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
5142c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
5152c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
5162c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
5172c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
5182c40f234SMatthew G. Knepley   } else {
5192c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
5202c40f234SMatthew G. Knepley   }
5212c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
5222c40f234SMatthew G. Knepley }
5232c40f234SMatthew G. Knepley 
5242c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
5252c40f234SMatthew G. Knepley {
5262c40f234SMatthew G. Knepley   DM             dm;
5276823f3c5SBlaise Bourdin   PetscBool      ishdf5,isexodusii;
5282c40f234SMatthew G. Knepley   PetscErrorCode ierr;
5292c40f234SMatthew G. Knepley 
5302c40f234SMatthew G. Knepley   PetscFunctionBegin;
5312c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
5322c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm,PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
5332c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
5346823f3c5SBlaise Bourdin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);CHKERRQ(ierr);
5352c40f234SMatthew G. Knepley   if (ishdf5) {
536878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
53739d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
538b136c2c9SMatthew G. Knepley #else
539b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
540878b459fSMatthew G. Knepley #endif
5416823f3c5SBlaise Bourdin   } else if (isexodusii) {
5426823f3c5SBlaise Bourdin #if defined(PETSC_HAVE_EXODUSII)
5436823f3c5SBlaise Bourdin     ierr = VecLoad_PlexExodusII_Internal(v, viewer);CHKERRQ(ierr);
5446823f3c5SBlaise Bourdin #else
5456823f3c5SBlaise Bourdin     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
5466823f3c5SBlaise Bourdin #endif
5472c40f234SMatthew G. Knepley   } else {
5482c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
549552f7358SJed Brown   }
550552f7358SJed Brown   PetscFunctionReturn(0);
551552f7358SJed Brown }
552552f7358SJed Brown 
553d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
554d930f514SMatthew G. Knepley {
555d930f514SMatthew G. Knepley   DM                dm;
556d930f514SMatthew G. Knepley   PetscViewerFormat format;
557d930f514SMatthew G. Knepley   PetscBool         ishdf5;
558d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
559d930f514SMatthew G. Knepley 
560d930f514SMatthew G. Knepley   PetscFunctionBegin;
561d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
5622c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm,PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
563d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
564d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
565d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
566a8ad634aSStefano Zampini     if (dm->useNatural) {
567d930f514SMatthew G. Knepley       if (dm->sfNatural) {
568d930f514SMatthew G. Knepley         if (ishdf5) {
569d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
570d930f514SMatthew G. Knepley           Vec         v;
571d930f514SMatthew G. Knepley           const char *vecname;
572d930f514SMatthew G. Knepley 
573d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
574d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
575d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
57639d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
577d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
578d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
579d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
580d930f514SMatthew G. Knepley #else
581d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
582d930f514SMatthew G. Knepley #endif
583d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
584d930f514SMatthew G. Knepley       }
585a8ad634aSStefano Zampini     } else {
586a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
587a8ad634aSStefano Zampini     }
588d930f514SMatthew G. Knepley   }
589d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
590d930f514SMatthew G. Knepley }
591d930f514SMatthew G. Knepley 
5927cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
593731e8ddeSMatthew G. Knepley {
594731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
595731e8ddeSMatthew G. Knepley   Vec                coordinates;
596ba2698f1SMatthew G. Knepley   DMLabel            depthLabel, celltypeLabel;
597731e8ddeSMatthew G. Knepley   const char        *name[4];
598731e8ddeSMatthew G. Knepley   const PetscScalar *a;
599731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
600731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
601731e8ddeSMatthew G. Knepley 
602731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
603731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
604731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
605731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
606731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
607ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &celltypeLabel);CHKERRQ(ierr);
608731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
609731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
610731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
611731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
612731e8ddeSMatthew G. Knepley   name[1]     = "edge";
613731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
614731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
615731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
616731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
617ba2698f1SMatthew G. Knepley     PetscInt  closureSize, cl, ct;
618731e8ddeSMatthew G. Knepley 
619ba2698f1SMatthew G. Knepley     ierr = DMLabelGetValue(celltypeLabel, c, &ct);CHKERRQ(ierr);
620ba2698f1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);CHKERRQ(ierr);
621731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
622731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
623731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
624731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
625731e8ddeSMatthew G. Knepley 
626731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
627731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
628731e8ddeSMatthew G. Knepley       if (!dof) continue;
629731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
630731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
631f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
632731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
633731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
634731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
635731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
636087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
637731e8ddeSMatthew G. Knepley         }
638731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
639731e8ddeSMatthew G. Knepley       }
640731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
641731e8ddeSMatthew G. Knepley     }
642731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
643731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
644731e8ddeSMatthew G. Knepley   }
645731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
646731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
647731e8ddeSMatthew G. Knepley }
648731e8ddeSMatthew G. Knepley 
64919ad8254SMatthew G. Knepley typedef enum {CS_CARTESIAN, CS_POLAR, CS_CYLINDRICAL, CS_SPHERICAL} CoordSystem;
65019ad8254SMatthew G. Knepley const char *CoordSystems[] = {"cartesian", "polar", "cylindrical", "spherical", "CoordSystem", "CS_", NULL};
65119ad8254SMatthew G. Knepley 
65219ad8254SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii_Coordinates(PetscViewer viewer, CoordSystem cs, PetscInt dim, const PetscScalar x[])
65319ad8254SMatthew G. Knepley {
65419ad8254SMatthew G. Knepley   PetscInt       i;
65519ad8254SMatthew G. Knepley   PetscErrorCode ierr;
65619ad8254SMatthew G. Knepley 
65719ad8254SMatthew G. Knepley   PetscFunctionBegin;
65819ad8254SMatthew G. Knepley   if (dim > 3) {
65919ad8254SMatthew G. Knepley     for (i = 0; i < dim; ++i) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double) PetscRealPart(x[i]));CHKERRQ(ierr);}
66019ad8254SMatthew G. Knepley   } else {
66119ad8254SMatthew G. Knepley     PetscReal coords[3], trcoords[3];
66219ad8254SMatthew G. Knepley 
66319ad8254SMatthew G. Knepley     for (i = 0; i < dim; ++i) coords[i] = PetscRealPart(x[i]);
66419ad8254SMatthew G. Knepley     switch (cs) {
66519ad8254SMatthew G. Knepley       case CS_CARTESIAN: for (i = 0; i < dim; ++i) trcoords[i] = coords[i];break;
66619ad8254SMatthew G. Knepley       case CS_POLAR:
6672c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dim != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Polar coordinates are for 2 dimension, not %D", dim);
66819ad8254SMatthew G. Knepley         trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
66919ad8254SMatthew G. Knepley         trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
67019ad8254SMatthew G. Knepley         break;
67119ad8254SMatthew G. Knepley       case CS_CYLINDRICAL:
6722c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dim != 3,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cylindrical coordinates are for 3 dimension, not %D", dim);
67319ad8254SMatthew G. Knepley         trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
67419ad8254SMatthew G. Knepley         trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
67519ad8254SMatthew G. Knepley         trcoords[2] = coords[2];
67619ad8254SMatthew G. Knepley         break;
67719ad8254SMatthew G. Knepley       case CS_SPHERICAL:
6782c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dim != 3,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Spherical coordinates are for 3 dimension, not %D", dim);
67919ad8254SMatthew G. Knepley         trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]) + PetscSqr(coords[2]));
68019ad8254SMatthew G. Knepley         trcoords[1] = PetscAtan2Real(PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1])), coords[2]);
68119ad8254SMatthew G. Knepley         trcoords[2] = PetscAtan2Real(coords[1], coords[0]);
68219ad8254SMatthew G. Knepley         break;
68319ad8254SMatthew G. Knepley     }
68419ad8254SMatthew G. Knepley     for (i = 0; i < dim; ++i) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double) trcoords[i]);CHKERRQ(ierr);}
68519ad8254SMatthew G. Knepley   }
68619ad8254SMatthew G. Knepley   PetscFunctionReturn(0);
68719ad8254SMatthew G. Knepley }
68819ad8254SMatthew G. Knepley 
6897cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
690552f7358SJed Brown {
691552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
692552f7358SJed Brown   DM                cdm;
693552f7358SJed Brown   PetscSection      coordSection;
694552f7358SJed Brown   Vec               coordinates;
695552f7358SJed Brown   PetscViewerFormat format;
696552f7358SJed Brown   PetscErrorCode    ierr;
697552f7358SJed Brown 
698552f7358SJed Brown   PetscFunctionBegin;
699552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
70092fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
701552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
702552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
703552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
704552f7358SJed Brown     const char *name;
705f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
7069318fe57SMatthew G. Knepley     PetscInt    pStart, pEnd, p, numLabels, l;
707552f7358SJed Brown     PetscMPIInt rank, size;
708552f7358SJed Brown 
709ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
710ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
711552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
712552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
713552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
714f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
715f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
716f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
717f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
718f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
719e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
7204d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
721e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
722552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
723552f7358SJed Brown       PetscInt dof, off, s;
724552f7358SJed Brown 
725552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
726552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
727552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
728e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
729552f7358SJed Brown       }
730552f7358SJed Brown     }
731552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
732e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
733e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
734552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
735552f7358SJed Brown       PetscInt dof, off, c;
736552f7358SJed Brown 
737552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
738552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
739552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
740e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
741552f7358SJed Brown       }
742552f7358SJed Brown     }
743552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7444d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
7453d2e540fSStefano Zampini     if (coordSection && coordinates) {
74619ad8254SMatthew G. Knepley       CoordSystem        cs = CS_CARTESIAN;
74719ad8254SMatthew G. Knepley       const PetscScalar *array;
74819ad8254SMatthew G. Knepley       PetscInt           Nf, Nc, pStart, pEnd, p;
74919ad8254SMatthew G. Knepley       PetscMPIInt        rank;
75019ad8254SMatthew G. Knepley       const char        *name;
75119ad8254SMatthew G. Knepley 
75219ad8254SMatthew G. Knepley       ierr = PetscOptionsGetEnum(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_coord_system", CoordSystems, (PetscEnum *) &cs, NULL);CHKERRQ(ierr);
75319ad8254SMatthew G. Knepley       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRMPI(ierr);
75419ad8254SMatthew G. Knepley       ierr = PetscSectionGetNumFields(coordSection, &Nf);CHKERRQ(ierr);
7552c71b3e2SJacob Faibussowitsch       PetscCheckFalse(Nf != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Coordinate section should have 1 field, not %D", Nf);
75619ad8254SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
75719ad8254SMatthew G. Knepley       ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
75819ad8254SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) coordinates, &name);CHKERRQ(ierr);
75919ad8254SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s with %D fields\n", name, Nf);CHKERRQ(ierr);
760d410b0cfSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  field 0 with %D components\n", Nc);CHKERRQ(ierr);
76119ad8254SMatthew G. Knepley       if (cs != CS_CARTESIAN) {ierr = PetscViewerASCIIPrintf(viewer, "  output coordinate system: %s\n", CoordSystems[cs]);CHKERRQ(ierr);}
76219ad8254SMatthew G. Knepley 
76319ad8254SMatthew G. Knepley       ierr = VecGetArrayRead(coordinates, &array);CHKERRQ(ierr);
76419ad8254SMatthew G. Knepley       ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
76519ad8254SMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank);CHKERRQ(ierr);
76619ad8254SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
76719ad8254SMatthew G. Knepley         PetscInt dof, off;
76819ad8254SMatthew G. Knepley 
76919ad8254SMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, p, &dof);CHKERRQ(ierr);
77019ad8254SMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, p, &off);CHKERRQ(ierr);
77119ad8254SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "  (%4D) dim %2D offset %3D", p, dof, off);CHKERRQ(ierr);
77219ad8254SMatthew G. Knepley         ierr = DMPlexView_Ascii_Coordinates(viewer, cs, dof, &array[off]);CHKERRQ(ierr);
77319ad8254SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\n");CHKERRQ(ierr);
77419ad8254SMatthew G. Knepley       }
77519ad8254SMatthew G. Knepley       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
77619ad8254SMatthew G. Knepley       ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
77719ad8254SMatthew G. Knepley       ierr = VecRestoreArrayRead(coordinates, &array);CHKERRQ(ierr);
7783d2e540fSStefano Zampini     }
7799318fe57SMatthew G. Knepley     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
7809318fe57SMatthew G. Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
7819318fe57SMatthew G. Knepley     for (l = 0; l < numLabels; ++l) {
7829318fe57SMatthew G. Knepley       DMLabel     label;
7839318fe57SMatthew G. Knepley       PetscBool   isdepth;
7849318fe57SMatthew G. Knepley       const char *name;
7859318fe57SMatthew G. Knepley 
7869318fe57SMatthew G. Knepley       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
7879318fe57SMatthew G. Knepley       ierr = PetscStrcmp(name, "depth", &isdepth);CHKERRQ(ierr);
7889318fe57SMatthew G. Knepley       if (isdepth) continue;
7899318fe57SMatthew G. Knepley       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7909318fe57SMatthew G. Knepley       ierr = DMLabelView(label, viewer);CHKERRQ(ierr);
7919318fe57SMatthew G. Knepley     }
792552f7358SJed Brown     if (size > 1) {
793552f7358SJed Brown       PetscSF sf;
794552f7358SJed Brown 
795552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
796552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
797552f7358SJed Brown     }
798552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
799552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
8000588280cSMatthew G. Knepley     const char  *name, *color;
8010588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
8020588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
803fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
804552f7358SJed Brown     PetscReal    scale         = 2.0;
80578081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
806b7f6ffafSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, drawNumbers[4], drawColors[4], useLabels, useColors, plotEdges, drawHasse = PETSC_FALSE;
8070588280cSMatthew G. Knepley     double       tcoords[3];
808552f7358SJed Brown     PetscScalar *coords;
809b7f6ffafSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, d, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p, n;
810552f7358SJed Brown     PetscMPIInt  rank, size;
8110588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
812b7f6ffafSMatthew G. Knepley     PetscBool    flg, lflg;
813fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
814fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
815552f7358SJed Brown 
8160588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
817552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
818c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
8190588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
8200588280cSMatthew G. Knepley     numColors  = 10;
8210588280cSMatthew G. Knepley     numLColors = 10;
8220588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
823c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
82478081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
825c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
826b7f6ffafSMatthew G. Knepley     for (d = 0; d < 4; ++d) drawNumbers[d] = useNumbers;
827b7f6ffafSMatthew G. Knepley     for (d = 0; d < 4; ++d) drawColors[d]  = PETSC_TRUE;
828b7f6ffafSMatthew G. Knepley     n = 4;
829b7f6ffafSMatthew G. Knepley     ierr = PetscOptionsGetBoolArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers_depth", drawNumbers, &n, &flg);CHKERRQ(ierr);
8302c71b3e2SJacob Faibussowitsch     PetscCheckFalse(flg && n != dim+1,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of flags %D != %D dim+1", n, dim+1);
831b7f6ffafSMatthew G. Knepley     ierr = PetscOptionsGetBoolArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors_depth", drawColors, &n, &flg);CHKERRQ(ierr);
8322c71b3e2SJacob Faibussowitsch     PetscCheckFalse(flg && n != dim+1,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of flags %D != %D dim+1", n, dim+1);
833c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
8340588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
835c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
8360588280cSMatthew G. Knepley     if (!useColors) {
8370588280cSMatthew G. Knepley       numColors = 3;
8380588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
8390588280cSMatthew G. Knepley     }
840c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
8410588280cSMatthew G. Knepley     if (!useColors) {
8420588280cSMatthew G. Knepley       numLColors = 4;
8430588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
8440588280cSMatthew G. Knepley     }
845589a23caSBarry Smith     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);CHKERRQ(ierr);
846b7f6ffafSMatthew G. Knepley     plotEdges = (PetscBool)(depth > 1 && drawNumbers[1] && dim < 3);
847202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
8482c71b3e2SJacob Faibussowitsch     PetscCheckFalse(flg && plotEdges && depth < dim,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
849202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
850b7f6ffafSMatthew G. Knepley     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_hasse", &drawHasse, NULL);CHKERRQ(ierr);
851fe1cc32dSStefano Zampini 
852fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
853fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
854b7f6ffafSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
855b7f6ffafSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
856b7f6ffafSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
857fe1cc32dSStefano Zampini     if (lflg) {
858fe1cc32dSStefano Zampini       DMLabel lbl;
859fe1cc32dSStefano Zampini 
860fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
861fe1cc32dSStefano Zampini       if (lbl) {
862fe1cc32dSStefano Zampini         PetscInt val, defval;
863fe1cc32dSStefano Zampini 
864fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
865fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
866fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
867fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
868fe1cc32dSStefano Zampini           PetscInt  closureSize;
869fe1cc32dSStefano Zampini 
870fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
871fe1cc32dSStefano Zampini           if (val == defval) continue;
872fe1cc32dSStefano Zampini 
873fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
874fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
875fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
876fe1cc32dSStefano Zampini           }
877fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
878fe1cc32dSStefano Zampini         }
879fe1cc32dSStefano Zampini       }
880fe1cc32dSStefano Zampini     }
881fe1cc32dSStefano Zampini 
882ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
883ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRMPI(ierr);
884552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
885770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
8860588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
887552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
888552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
889552f7358SJed Brown \\usetikzlibrary{arrows}\n\
8900588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
8910588280cSMatthew G. Knepley     if (size > 1) {
892f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
893770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
894770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
895770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
896770b213bSMatthew G Knepley         } else if (p > 0) {
897770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
898770b213bSMatthew G Knepley         }
899770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
900770b213bSMatthew G Knepley       }
9010588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
9020588280cSMatthew G. Knepley     }
903b7f6ffafSMatthew G. Knepley     if (drawHasse) {
904b7f6ffafSMatthew G. Knepley       PetscInt maxStratum = PetscMax(vEnd-vStart, PetscMax(eEnd-eStart, cEnd-cStart));
905b7f6ffafSMatthew G. Knepley 
906b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vStart}{%D}\n", vStart);CHKERRQ(ierr);
907b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vEnd}{%D}\n", vEnd-1);CHKERRQ(ierr);
908b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numVertices}{%D}\n", vEnd-vStart);CHKERRQ(ierr);
909b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vShift}{%.2f}\n", 3 + (maxStratum-(vEnd-vStart))/2.);CHKERRQ(ierr);
910b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eStart}{%D}\n", eStart);CHKERRQ(ierr);
911b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eEnd}{%D}\n", eEnd-1);CHKERRQ(ierr);
912b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eShift}{%.2f}\n", 3 + (maxStratum-(eEnd-eStart))/2.);CHKERRQ(ierr);
913b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numEdges}{%D}\n", eEnd-eStart);CHKERRQ(ierr);
914b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cStart}{%D}\n", cStart);CHKERRQ(ierr);
915b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cEnd}{%D}\n", cEnd-1);CHKERRQ(ierr);
916b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numCells}{%D}\n", cEnd-cStart);CHKERRQ(ierr);
917b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cShift}{%.2f}\n", 3 + (maxStratum-(cEnd-cStart))/2.);CHKERRQ(ierr);
918b7f6ffafSMatthew G. Knepley     }
919087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);CHKERRQ(ierr);
920fe1cc32dSStefano Zampini 
921552f7358SJed Brown     /* Plot vertices */
922552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
9234d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
924552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
925552f7358SJed Brown       PetscInt  off, dof, d;
9260588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
927552f7358SJed Brown 
928fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
929552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
930552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
9310588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
9322c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof > 3,PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
9330588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
9340588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
935c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
9360588280cSMatthew G. Knepley       }
9370588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
9380588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
939552f7358SJed Brown       for (d = 0; d < dof; ++d) {
940552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
941087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);CHKERRQ(ierr);
942552f7358SJed Brown       }
943b7f6ffafSMatthew G. Knepley       if (drawHasse) color = colors[0%numColors];
944b7f6ffafSMatthew G. Knepley       else           color = colors[rank%numColors];
9450588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
9460588280cSMatthew G. Knepley         PetscInt val;
947c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9480588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
9490588280cSMatthew G. Knepley       }
950b7f6ffafSMatthew G. Knepley       if (drawNumbers[0]) {
951e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
952b7f6ffafSMatthew G. Knepley       } else if (drawColors[0]) {
953e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
954b7f6ffafSMatthew G. Knepley       } else {
955b7f6ffafSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [] {};\n", v, rank);CHKERRQ(ierr);
9560588280cSMatthew G. Knepley       }
957552f7358SJed Brown     }
958552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
959552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
960b7f6ffafSMatthew G. Knepley     /* Plot edges */
961b7f6ffafSMatthew G. Knepley     if (plotEdges) {
962b7f6ffafSMatthew G. Knepley       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
963b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
964b7f6ffafSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
965b7f6ffafSMatthew G. Knepley         const PetscInt *cone;
966b7f6ffafSMatthew G. Knepley         PetscInt        coneSize, offA, offB, dof, d;
967b7f6ffafSMatthew G. Knepley 
968b7f6ffafSMatthew G. Knepley         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
969b7f6ffafSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
9702c71b3e2SJacob Faibussowitsch         PetscCheckFalse(coneSize != 2,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
971b7f6ffafSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
972b7f6ffafSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
973b7f6ffafSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
974b7f6ffafSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
975b7f6ffafSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
976b7f6ffafSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
977b7f6ffafSMatthew G. Knepley           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
978b7f6ffafSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
979b7f6ffafSMatthew G. Knepley         }
980b7f6ffafSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
981b7f6ffafSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
982b7f6ffafSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
983b7f6ffafSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
984b7f6ffafSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
985b7f6ffafSMatthew G. Knepley         }
986b7f6ffafSMatthew G. Knepley         if (drawHasse) color = colors[1%numColors];
987b7f6ffafSMatthew G. Knepley         else           color = colors[rank%numColors];
988b7f6ffafSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
989b7f6ffafSMatthew G. Knepley           PetscInt val;
990b7f6ffafSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
991b7f6ffafSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
992b7f6ffafSMatthew G. Knepley         }
993b7f6ffafSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
994b7f6ffafSMatthew G. Knepley       }
995b7f6ffafSMatthew G. Knepley       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
996b7f6ffafSMatthew G. Knepley       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
997b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
998b7f6ffafSMatthew G. Knepley     }
999846a3e8bSMatthew G. Knepley     /* Plot cells */
1000b7f6ffafSMatthew G. Knepley     if (dim == 3 || !drawNumbers[1]) {
1001846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
1002846a3e8bSMatthew G. Knepley         const PetscInt *cone;
1003846a3e8bSMatthew G. Knepley 
1004fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
1005846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
1006846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
1007846a3e8bSMatthew G. Knepley           PetscInt val;
1008846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
1009846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
1010846a3e8bSMatthew G. Knepley         }
1011846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
1012846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
1013846a3e8bSMatthew G. Knepley       }
1014846a3e8bSMatthew G. Knepley     } else {
1015b7f6ffafSMatthew G. Knepley        DMPolytopeType ct;
1016846a3e8bSMatthew G. Knepley 
1017b7f6ffafSMatthew G. Knepley       /* Drawing a 2D polygon */
1018b7f6ffafSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
1019fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1020b7f6ffafSMatthew G. Knepley         ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
1021b7f6ffafSMatthew G. Knepley         if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR ||
1022b7f6ffafSMatthew G. Knepley             ct == DM_POLYTOPE_TRI_PRISM_TENSOR ||
1023b7f6ffafSMatthew G. Knepley             ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
1024b7f6ffafSMatthew G. Knepley           const PetscInt *cone;
1025b7f6ffafSMatthew G. Knepley           PetscInt        coneSize, e;
1026b7f6ffafSMatthew G. Knepley 
1027b7f6ffafSMatthew G. Knepley           ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
1028b7f6ffafSMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1029b7f6ffafSMatthew G. Knepley           for (e = 0; e < coneSize; ++e) {
1030b7f6ffafSMatthew G. Knepley             const PetscInt *econe;
1031b7f6ffafSMatthew G. Knepley 
1032b7f6ffafSMatthew G. Knepley             ierr = DMPlexGetCone(dm, cone[e], &econe);CHKERRQ(ierr);
1033b7f6ffafSMatthew G. Knepley             ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d) -- (%D_%d);\n", colors[rank%numColors], econe[0], rank, cone[e], rank, econe[1], rank);CHKERRQ(ierr);
1034b7f6ffafSMatthew G. Knepley           }
1035b7f6ffafSMatthew G. Knepley         } else {
1036b7f6ffafSMatthew G. Knepley           PetscInt *closure = NULL;
1037b7f6ffafSMatthew G. Knepley           PetscInt  closureSize, Nv = 0, v;
1038b7f6ffafSMatthew G. Knepley 
1039846a3e8bSMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1040846a3e8bSMatthew G. Knepley           for (p = 0; p < closureSize*2; p += 2) {
1041846a3e8bSMatthew G. Knepley             const PetscInt point = closure[p];
1042846a3e8bSMatthew G. Knepley 
1043b7f6ffafSMatthew G. Knepley             if ((point >= vStart) && (point < vEnd)) closure[Nv++] = point;
1044846a3e8bSMatthew G. Knepley           }
1045b7f6ffafSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
1046b7f6ffafSMatthew G. Knepley           for (v = 0; v <= Nv; ++v) {
1047b7f6ffafSMatthew G. Knepley             const PetscInt vertex = closure[v%Nv];
1048b7f6ffafSMatthew G. Knepley 
1049b7f6ffafSMatthew G. Knepley             if (v > 0) {
1050b7f6ffafSMatthew G. Knepley               if (plotEdges) {
1051b7f6ffafSMatthew G. Knepley                 const PetscInt *edge;
1052b7f6ffafSMatthew G. Knepley                 PetscInt        endpoints[2], ne;
1053b7f6ffafSMatthew G. Knepley 
1054b7f6ffafSMatthew G. Knepley                 endpoints[0] = closure[v-1]; endpoints[1] = vertex;
1055b7f6ffafSMatthew G. Knepley                 ierr = DMPlexGetJoin(dm, 2, endpoints, &ne, &edge);CHKERRQ(ierr);
10562c71b3e2SJacob Faibussowitsch                 PetscCheckFalse(ne != 1,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find edge for vertices %D, %D", endpoints[0], endpoints[1]);
1057b7f6ffafSMatthew G. Knepley                 ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d) -- ", edge[0], rank);CHKERRQ(ierr);
1058b7f6ffafSMatthew G. Knepley                 ierr = DMPlexRestoreJoin(dm, 2, endpoints, &ne, &edge);CHKERRQ(ierr);
1059b7f6ffafSMatthew G. Knepley               } else {
1060b7f6ffafSMatthew G. Knepley                 ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);
1061b7f6ffafSMatthew G. Knepley               }
1062b7f6ffafSMatthew G. Knepley             }
1063b7f6ffafSMatthew G. Knepley             ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", vertex, rank);CHKERRQ(ierr);
1064b7f6ffafSMatthew G. Knepley           }
1065b7f6ffafSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, ";\n");CHKERRQ(ierr);
1066846a3e8bSMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1067846a3e8bSMatthew G. Knepley         }
1068846a3e8bSMatthew G. Knepley       }
1069b7f6ffafSMatthew G. Knepley     }
1070846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1071846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
1072846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
1073846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
1074846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
1075846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
1076846a3e8bSMatthew G. Knepley 
1077fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
1078846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1079846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
1080846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
1081846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
1082846a3e8bSMatthew G. Knepley         PetscInt       off;
1083846a3e8bSMatthew G. Knepley 
1084846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
1085846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
1086846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
1087846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
1088846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
1089846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1090846a3e8bSMatthew G. Knepley         }
1091846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1092846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
1093846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
1094846a3e8bSMatthew G. Knepley         ++n;
1095846a3e8bSMatthew G. Knepley       }
1096846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
1097846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1098846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
1099846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
1100087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);CHKERRQ(ierr);
1101846a3e8bSMatthew G. Knepley       }
1102b7f6ffafSMatthew G. Knepley       if (drawHasse) color = colors[depth%numColors];
1103b7f6ffafSMatthew G. Knepley       else           color = colors[rank%numColors];
1104846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
1105846a3e8bSMatthew G. Knepley         PetscInt val;
1106846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
1107846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
1108846a3e8bSMatthew G. Knepley       }
1109b7f6ffafSMatthew G. Knepley       if (drawNumbers[dim]) {
1110846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
1111b7f6ffafSMatthew G. Knepley       } else if (drawColors[dim]) {
1112846a3e8bSMatthew 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);
1113b7f6ffafSMatthew G. Knepley       } else {
1114b7f6ffafSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [] {};\n", c, rank);CHKERRQ(ierr);
1115846a3e8bSMatthew G. Knepley       }
1116846a3e8bSMatthew G. Knepley     }
1117846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1118b7f6ffafSMatthew G. Knepley     if (drawHasse) {
1119b7f6ffafSMatthew G. Knepley       color = colors[depth%numColors];
1120b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%% Cells\n");CHKERRQ(ierr);
1121b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\foreach \\c in {\\cStart,...,\\cEnd}\n");CHKERRQ(ierr);
1122b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "{\n");CHKERRQ(ierr);
1123b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  \\node(\\c_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\cShift+\\c-\\cStart,0) {\\c};\n", rank, color);CHKERRQ(ierr);
1124b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "}\n");CHKERRQ(ierr);
1125552f7358SJed Brown 
1126b7f6ffafSMatthew G. Knepley       color = colors[1%numColors];
1127b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%% Edges\n");CHKERRQ(ierr);
1128b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\foreach \\e in {\\eStart,...,\\eEnd}\n");CHKERRQ(ierr);
1129b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "{\n");CHKERRQ(ierr);
1130b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  \\node(\\e_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\eShift+\\e-\\eStart,1) {\\e};\n", rank, color);CHKERRQ(ierr);
1131b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "}\n");CHKERRQ(ierr);
1132b7f6ffafSMatthew G. Knepley 
1133b7f6ffafSMatthew G. Knepley       color = colors[0%numColors];
1134b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%% Vertices\n");CHKERRQ(ierr);
1135b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\\foreach \\v in {\\vStart,...,\\vEnd}\n");CHKERRQ(ierr);
1136b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "{\n");CHKERRQ(ierr);
1137b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  \\node(\\v_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\vShift+\\v-\\vStart,2) {\\v};\n", rank, color);CHKERRQ(ierr);
1138b7f6ffafSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "}\n");CHKERRQ(ierr);
1139b7f6ffafSMatthew G. Knepley 
1140b7f6ffafSMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1141b7f6ffafSMatthew G. Knepley         const PetscInt *cone;
1142b7f6ffafSMatthew G. Knepley         PetscInt        coneSize, cp;
1143b7f6ffafSMatthew G. Knepley 
1144b7f6ffafSMatthew G. Knepley         ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1145b7f6ffafSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1146b7f6ffafSMatthew G. Knepley         for (cp = 0; cp < coneSize; ++cp) {
1147b7f6ffafSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "\\draw[->, shorten >=1pt] (%D_%d) -- (%D_%d);\n", cone[cp], rank, p, rank);CHKERRQ(ierr);
1148552f7358SJed Brown         }
11490588280cSMatthew G. Knepley       }
11500588280cSMatthew G. Knepley     }
1151552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
11524d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
11530588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
1154770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
11550588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
11560588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
11570588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
11580588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
1159fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
11600f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
11610f7d6e4aSStefano Zampini     Vec                    cown,acown;
11620f7d6e4aSStefano Zampini     VecScatter             sct;
11630f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
11640f7d6e4aSStefano Zampini     IS                     gid,acis;
11650f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
11660f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
11670f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
11680f7d6e4aSStefano Zampini     const PetscInt         *idxs;
11690f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
11700f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
11710f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
11720f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
11730f7d6e4aSStefano Zampini 
11740f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1175ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
1176b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1177ffc4695bSBarry Smith     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRMPI(ierr);
11780f7d6e4aSStefano Zampini #endif
11790f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
1180ffc4695bSBarry Smith       ierr = MPI_Comm_group(comm,&ggroup);CHKERRMPI(ierr);
1181ffc4695bSBarry Smith       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRMPI(ierr);
11820f7d6e4aSStefano Zampini       d1   = 0;
1183ffc4695bSBarry Smith       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRMPI(ierr);
11840f7d6e4aSStefano Zampini       nid  = d2;
1185ffc4695bSBarry Smith       ierr = MPI_Group_free(&ggroup);CHKERRMPI(ierr);
1186ffc4695bSBarry Smith       ierr = MPI_Group_free(&ngroup);CHKERRMPI(ierr);
1187ffc4695bSBarry Smith       ierr = MPI_Comm_free(&ncomm);CHKERRMPI(ierr);
11880f7d6e4aSStefano Zampini     } else nid = 0.0;
11890f7d6e4aSStefano Zampini 
11900f7d6e4aSStefano Zampini     /* Get connectivity */
11910f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
11920f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
11930f7d6e4aSStefano Zampini 
11940f7d6e4aSStefano Zampini     /* filter overlapped local cells */
11950f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
11960f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
11970f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
11980f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
11990f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
12000f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
12010f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
12020f7d6e4aSStefano Zampini     }
12030f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
12042c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numVertices != cum,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
12050f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
12060f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
12070f7d6e4aSStefano Zampini 
12080f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
12090f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
12100f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
12110f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
12120f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
12130f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
12140f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
12150f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
12160f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
12170f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
12180f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
12190f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
12200f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
12210f7d6e4aSStefano Zampini 
12220f7d6e4aSStefano Zampini     /* compute edgeCut */
12230f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
12240f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
12250f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
12260f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
12270f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
12280f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
12290f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
12300f7d6e4aSStefano Zampini       PetscInt totl;
12310f7d6e4aSStefano Zampini 
12320f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
12330f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
12340f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
12350f7d6e4aSStefano Zampini         if (work[i] < 0) {
12360f7d6e4aSStefano Zampini           ect  += 1;
12370f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
12380f7d6e4aSStefano Zampini         }
12390f7d6e4aSStefano Zampini       }
12400f7d6e4aSStefano Zampini     }
12410f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
12420f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
12430f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
12440f7d6e4aSStefano Zampini     lm[1] = -numVertices;
1245820f2d46SBarry Smith     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRMPI(ierr);
12460f7d6e4aSStefano 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);
12470f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
12480f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
12490f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1250820f2d46SBarry Smith     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRMPI(ierr);
12510f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
1252b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
12530f7d6e4aSStefano 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);
12540f7d6e4aSStefano Zampini #else
12550f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
12560f7d6e4aSStefano Zampini #endif
12570f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
12580f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
12590f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
12600f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1261552f7358SJed Brown   } else {
1262412e9a14SMatthew G. Knepley     const char    *name;
1263d80ece95SMatthew G. Knepley     PetscInt      *sizes, *hybsizes, *ghostsizes;
1264412e9a14SMatthew G. Knepley     PetscInt       locDepth, depth, cellHeight, dim, d;
1265d80ece95SMatthew G. Knepley     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1266ca7bf7eeSMatthew G. Knepley     PetscInt       numLabels, l, maxSize = 17;
12679318fe57SMatthew G. Knepley     DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1268412e9a14SMatthew G. Knepley     MPI_Comm       comm;
1269412e9a14SMatthew G. Knepley     PetscMPIInt    size, rank;
1270552f7358SJed Brown 
127182f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
1272ffc4695bSBarry Smith     ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
1273ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
1274c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1275f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
12765f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1277f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1278f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1279f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1280552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1281820f2d46SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRMPI(ierr);
1282d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1283d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1284ca7bf7eeSMatthew G. Knepley     if (size < maxSize) {ierr = PetscCalloc3(size, &sizes, size, &hybsizes, size, &ghostsizes);CHKERRQ(ierr);}
1285ca7bf7eeSMatthew G. Knepley     else                {ierr = PetscCalloc3(3,    &sizes, 3,    &hybsizes, 3,    &ghostsizes);CHKERRQ(ierr);}
1286412e9a14SMatthew G. Knepley     for (d = 0; d <= depth; d++) {
1287412e9a14SMatthew G. Knepley       PetscInt Nc[2] = {0, 0}, ict;
1288412e9a14SMatthew G. Knepley 
1289552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
12909318fe57SMatthew G. Knepley       if (pStart < pEnd) {ierr = DMPlexGetCellType(dm, pStart, &ct0);CHKERRQ(ierr);}
1291412e9a14SMatthew G. Knepley       ict  = ct0;
1292ffc4695bSBarry Smith       ierr = MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1293412e9a14SMatthew G. Knepley       ct0  = (DMPolytopeType) ict;
1294412e9a14SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
1295412e9a14SMatthew G. Knepley         DMPolytopeType ct;
1296412e9a14SMatthew G. Knepley 
1297412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
1298412e9a14SMatthew G. Knepley         if (ct == ct0) ++Nc[0];
1299412e9a14SMatthew G. Knepley         else           ++Nc[1];
1300412e9a14SMatthew G. Knepley       }
1301ca7bf7eeSMatthew G. Knepley       if (size < maxSize) {
1302ffc4695bSBarry Smith         ierr = MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1303ffc4695bSBarry Smith         ierr = MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1304ffc4695bSBarry Smith         if (d == depth) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);}
13059880b877SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "  Number of %D-cells per rank:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1306834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
1307dd400576SPatrick Sanan           if (rank == 0) {
1308412e9a14SMatthew G. Knepley             ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);CHKERRQ(ierr);
1309412e9a14SMatthew G. Knepley             if (hybsizes[p]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);CHKERRQ(ierr);}
1310412e9a14SMatthew G. Knepley             if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1311834065abSMatthew G. Knepley           }
1312cbb7f117SMark Adams         }
1313ca7bf7eeSMatthew G. Knepley       } else {
1314ca7bf7eeSMatthew G. Knepley         PetscInt locMinMax[2];
1315ca7bf7eeSMatthew G. Knepley 
1316ca7bf7eeSMatthew G. Knepley         locMinMax[0] = Nc[0]+Nc[1]; locMinMax[1] = Nc[0]+Nc[1];
1317ca7bf7eeSMatthew G. Knepley         ierr = PetscGlobalMinMaxInt(comm, locMinMax, sizes);CHKERRQ(ierr);
1318ca7bf7eeSMatthew G. Knepley         locMinMax[0] = Nc[1]; locMinMax[1] = Nc[1];
1319ca7bf7eeSMatthew G. Knepley         ierr = PetscGlobalMinMaxInt(comm, locMinMax, hybsizes);CHKERRQ(ierr);
1320ca7bf7eeSMatthew G. Knepley         if (d == depth) {
1321ca7bf7eeSMatthew G. Knepley           locMinMax[0] = gcNum; locMinMax[1] = gcNum;
1322ca7bf7eeSMatthew G. Knepley           ierr = PetscGlobalMinMaxInt(comm, locMinMax, ghostsizes);CHKERRQ(ierr);
1323ca7bf7eeSMatthew G. Knepley         }
1324ca7bf7eeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "  Min/Max of %D-cells per rank:", (depth == 1) && d ? dim : d);CHKERRQ(ierr);
1325ca7bf7eeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "/%" PetscInt_FMT, sizes[0], sizes[1]);CHKERRQ(ierr);
1326ca7bf7eeSMatthew G. Knepley         if (hybsizes[0]   > 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT "/%" PetscInt_FMT ")", hybsizes[0], hybsizes[1]);CHKERRQ(ierr);}
1327ca7bf7eeSMatthew G. Knepley         if (ghostsizes[0] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "/%" PetscInt_FMT "]", ghostsizes[0], ghostsizes[1]);CHKERRQ(ierr);}
1328ca7bf7eeSMatthew G. Knepley       }
1329552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1330552f7358SJed Brown     }
1331d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes, hybsizes, ghostsizes);CHKERRQ(ierr);
13329318fe57SMatthew G. Knepley     {
13339318fe57SMatthew G. Knepley       const PetscReal      *maxCell;
13349318fe57SMatthew G. Knepley       const PetscReal      *L;
13359318fe57SMatthew G. Knepley       const DMBoundaryType *bd;
13369318fe57SMatthew G. Knepley       PetscBool             per, localized;
13379318fe57SMatthew G. Knepley 
13389318fe57SMatthew G. Knepley       ierr = DMGetPeriodicity(dm, &per, &maxCell, &L, &bd);CHKERRQ(ierr);
13399318fe57SMatthew G. Knepley       ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
13409318fe57SMatthew G. Knepley       if (per) {
13419318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "Periodic mesh (");CHKERRQ(ierr);
13429318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
13439318fe57SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
13449318fe57SMatthew G. Knepley           if (bd && d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
13459318fe57SMatthew G. Knepley           if (bd)    {ierr = PetscViewerASCIIPrintf(viewer, "%s", DMBoundaryTypes[bd[d]]);CHKERRQ(ierr);}
13469318fe57SMatthew G. Knepley         }
13479318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ") coordinates %s\n", localized ? "localized" : "not localized");CHKERRQ(ierr);
13489318fe57SMatthew G. Knepley         ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
13499318fe57SMatthew G. Knepley       }
13509318fe57SMatthew G. Knepley     }
1351c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1352a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1353a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1354a57dd577SMatthew G Knepley       DMLabel         label;
1355a57dd577SMatthew G Knepley       const char     *name;
1356a57dd577SMatthew G Knepley       IS              valueIS;
1357a57dd577SMatthew G Knepley       const PetscInt *values;
1358a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1359a57dd577SMatthew G Knepley 
1360c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1361c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1362a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1363d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1364a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1365a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1366120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1367a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1368a57dd577SMatthew G Knepley         PetscInt size;
1369a57dd577SMatthew G Knepley 
1370a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1371a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1372d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1373a57dd577SMatthew G Knepley       }
1374a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1375120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1376a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
13774d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1378a57dd577SMatthew G Knepley     }
1379c1cad2e7SMatthew G. Knepley     {
1380c1cad2e7SMatthew G. Knepley       char    **labelNames;
1381c1cad2e7SMatthew G. Knepley       PetscInt  Nl = numLabels;
1382c1cad2e7SMatthew G. Knepley       PetscBool flg;
1383c1cad2e7SMatthew G. Knepley 
1384c1cad2e7SMatthew G. Knepley       ierr = PetscMalloc1(Nl, &labelNames);CHKERRQ(ierr);
1385c1cad2e7SMatthew G. Knepley       ierr = PetscOptionsGetStringArray(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_plex_view_labels", labelNames, &Nl, &flg);CHKERRQ(ierr);
1386c1cad2e7SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {
1387c1cad2e7SMatthew G. Knepley         DMLabel label;
1388c1cad2e7SMatthew G. Knepley 
1389c1cad2e7SMatthew G. Knepley         ierr = DMHasLabel(dm, labelNames[l], &flg);CHKERRQ(ierr);
1390c1cad2e7SMatthew G. Knepley         if (flg) {
1391c1cad2e7SMatthew G. Knepley           ierr = DMGetLabel(dm, labelNames[l], &label);CHKERRQ(ierr);
1392c1cad2e7SMatthew G. Knepley           ierr = DMLabelView(label, viewer);CHKERRQ(ierr);
1393c1cad2e7SMatthew G. Knepley         }
1394c1cad2e7SMatthew G. Knepley         ierr = PetscFree(labelNames[l]);CHKERRQ(ierr);
1395c1cad2e7SMatthew G. Knepley       }
1396c1cad2e7SMatthew G. Knepley       ierr = PetscFree(labelNames);CHKERRQ(ierr);
1397c1cad2e7SMatthew G. Knepley     }
139834aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
139934aa8a36SMatthew G. Knepley     if (dm->Nf) {
140034aa8a36SMatthew G. Knepley       PetscInt f;
140134aa8a36SMatthew G. Knepley 
140234aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
140334aa8a36SMatthew G. Knepley         const char *name;
140434aa8a36SMatthew G. Knepley 
140534aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
140634aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
140734aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
140834aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
140934aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
141034aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1411ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
141234aa8a36SMatthew G. Knepley         } else {
141334aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
141434aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
141534aa8a36SMatthew G. Knepley         }
141634aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
141734aa8a36SMatthew G. Knepley       }
141834aa8a36SMatthew G. Knepley     }
1419a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
14208e7ff633SMatthew G. Knepley     if (cdm) {
14218e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
14228e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
14238e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
14248e7ff633SMatthew G. Knepley     }
1425552f7358SJed Brown   }
1426552f7358SJed Brown   PetscFunctionReturn(0);
1427552f7358SJed Brown }
1428552f7358SJed Brown 
1429e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1430e5c487bfSMatthew G. Knepley {
1431e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1432e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1433a12d352dSMatthew G. Knepley   PetscInt       cdim;
1434e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1435e5c487bfSMatthew G. Knepley 
1436e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1437ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1438e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1439a12d352dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1440e5c487bfSMatthew G. Knepley   switch (ct) {
1441a12d352dSMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
1442a12d352dSMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
1443a12d352dSMatthew G. Knepley     switch (cdim) {
1444a12d352dSMatthew G. Knepley     case 1:
1445a12d352dSMatthew G. Knepley     {
1446a12d352dSMatthew G. Knepley       const PetscReal y  = 0.5;  /* TODO Put it in the middle of the viewport */
1447a12d352dSMatthew G. Knepley       const PetscReal dy = 0.05; /* TODO Make it a fraction of the total length */
1448a12d352dSMatthew G. Knepley 
1449a12d352dSMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), y,    PetscRealPart(coords[1]), y,    PETSC_DRAW_BLACK);CHKERRQ(ierr);
1450a12d352dSMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), y+dy, PetscRealPart(coords[0]), y-dy, PETSC_DRAW_BLACK);CHKERRQ(ierr);
1451a12d352dSMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[1]), y+dy, PetscRealPart(coords[1]), y-dy, PETSC_DRAW_BLACK);CHKERRQ(ierr);
1452a12d352dSMatthew G. Knepley     }
1453a12d352dSMatthew G. Knepley     break;
1454a12d352dSMatthew G. Knepley     case 2:
1455a12d352dSMatthew G. Knepley     {
1456a12d352dSMatthew G. Knepley       const PetscReal dx = (PetscRealPart(coords[3]) - PetscRealPart(coords[1]));
1457a12d352dSMatthew G. Knepley       const PetscReal dy = (PetscRealPart(coords[2]) - PetscRealPart(coords[0]));
1458a12d352dSMatthew G. Knepley       const PetscReal l  = 0.1/PetscSqrtReal(dx*dx + dy*dy);
1459a12d352dSMatthew G. Knepley 
1460a12d352dSMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1461a12d352dSMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0])+l*dx, PetscRealPart(coords[1])+l*dy, PetscRealPart(coords[0])-l*dx, PetscRealPart(coords[1])-l*dy, PETSC_DRAW_BLACK);CHKERRQ(ierr);
1462a12d352dSMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2])+l*dx, PetscRealPart(coords[3])+l*dy, PetscRealPart(coords[2])-l*dx, PetscRealPart(coords[3])-l*dy, PETSC_DRAW_BLACK);CHKERRQ(ierr);
1463a12d352dSMatthew G. Knepley     }
1464a12d352dSMatthew G. Knepley     break;
146598921bdaSJacob Faibussowitsch     default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of dimension %D", cdim);
1466a12d352dSMatthew G. Knepley     }
1467a12d352dSMatthew G. Knepley     break;
1468e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1469e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1470e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1471e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1472e5c487bfSMatthew G. Knepley                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1473e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1474e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1475e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1476e5c487bfSMatthew G. Knepley     break;
1477e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
1478e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1479e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1480e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1481e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1482e5c487bfSMatthew G. Knepley     ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1483e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1484e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1485e5c487bfSMatthew G. Knepley                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1486e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1487e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1488e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1489e5c487bfSMatthew G. Knepley     ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1490e5c487bfSMatthew G. Knepley     break;
149198921bdaSJacob Faibussowitsch   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1492e5c487bfSMatthew G. Knepley   }
1493e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1494e5c487bfSMatthew G. Knepley }
1495e5c487bfSMatthew G. Knepley 
1496e5c487bfSMatthew G. Knepley static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1497e5c487bfSMatthew G. Knepley {
1498e5c487bfSMatthew G. Knepley   DMPolytopeType ct;
1499e5c487bfSMatthew G. Knepley   PetscReal      centroid[2] = {0., 0.};
1500e5c487bfSMatthew G. Knepley   PetscMPIInt    rank;
1501e5c487bfSMatthew G. Knepley   PetscInt       fillColor, v, e, d;
1502e5c487bfSMatthew G. Knepley   PetscErrorCode ierr;
1503e5c487bfSMatthew G. Knepley 
1504e5c487bfSMatthew G. Knepley   PetscFunctionBegin;
1505ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1506e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cell, &ct);CHKERRQ(ierr);
1507e5c487bfSMatthew G. Knepley   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1508e5c487bfSMatthew G. Knepley   switch (ct) {
1509e5c487bfSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1510e5c487bfSMatthew G. Knepley     {
1511e5c487bfSMatthew G. Knepley       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
1512e5c487bfSMatthew G. Knepley 
1513e5c487bfSMatthew G. Knepley       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1514e5c487bfSMatthew G. Knepley       for (e = 0; e < 3; ++e) {
1515e5c487bfSMatthew G. Knepley         refCoords[0] = refVertices[e*2+0];
1516e5c487bfSMatthew G. Knepley         refCoords[1] = refVertices[e*2+1];
1517e5c487bfSMatthew G. Knepley         for (d = 1; d <= edgeDiv; ++d) {
1518e5c487bfSMatthew G. Knepley           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1519e5c487bfSMatthew G. Knepley           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1520e5c487bfSMatthew G. Knepley         }
1521e5c487bfSMatthew G. Knepley         ierr = DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);CHKERRQ(ierr);
1522e5c487bfSMatthew G. Knepley         for (d = 0; d < edgeDiv; ++d) {
1523e5c487bfSMatthew G. Knepley           ierr = PetscDrawTriangle(draw, centroid[0], centroid[1], edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], fillColor, fillColor, fillColor);CHKERRQ(ierr);
1524e5c487bfSMatthew G. Knepley           ierr = PetscDrawLine(draw, edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], PETSC_DRAW_BLACK);CHKERRQ(ierr);
1525e5c487bfSMatthew G. Knepley         }
1526e5c487bfSMatthew G. Knepley       }
1527e5c487bfSMatthew G. Knepley     }
1528e5c487bfSMatthew G. Knepley     break;
152998921bdaSJacob Faibussowitsch   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1530e5c487bfSMatthew G. Knepley   }
1531e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
1532e5c487bfSMatthew G. Knepley }
1533e5c487bfSMatthew G. Knepley 
15347cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1535e412dcbdSMatthew G. Knepley {
1536e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1537e412dcbdSMatthew G. Knepley   DM                 cdm;
1538e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1539e412dcbdSMatthew G. Knepley   Vec                coordinates;
1540e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
154129494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1542e5c487bfSMatthew G. Knepley   PetscReal         *refCoords, *edgeCoords;
1543e5c487bfSMatthew G. Knepley   PetscBool          isnull, drawAffine = PETSC_TRUE;
1544e5c487bfSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1545e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1546e412dcbdSMatthew G. Knepley 
1547e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1548e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
15492c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dim > 2,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1550e5c487bfSMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);CHKERRQ(ierr);
1551e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);CHKERRQ(ierr);}
1552e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
155392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1554e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1555e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1556e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1557e412dcbdSMatthew G. Knepley 
1558e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1559e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1560e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1561e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1562e412dcbdSMatthew G. Knepley 
1563e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1564e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1565e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
15660c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
15670c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1568e412dcbdSMatthew G. Knepley   }
1569e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
1570820f2d46SBarry Smith   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1571820f2d46SBarry Smith   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
157229494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1573e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1574e412dcbdSMatthew G. Knepley 
1575cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1576cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1577ba2698f1SMatthew G. Knepley     PetscInt     numCoords;
1578cf3064d3SMatthew G. Knepley 
1579e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);CHKERRQ(ierr);
1580e5c487bfSMatthew G. Knepley     if (drawAffine) {
1581e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCell(dm, draw, c, coords);CHKERRQ(ierr);
1582e5c487bfSMatthew G. Knepley     } else {
1583e5c487bfSMatthew G. Knepley       ierr = DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);CHKERRQ(ierr);
1584cf3064d3SMatthew G. Knepley     }
1585cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1586cf3064d3SMatthew G. Knepley   }
1587e5c487bfSMatthew G. Knepley   if (!drawAffine) {ierr = PetscFree2(refCoords, edgeCoords);CHKERRQ(ierr);}
1588e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1589e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1590e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1591e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1592e412dcbdSMatthew G. Knepley }
1593e412dcbdSMatthew G. Knepley 
15941e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
15951e50132fSMatthew G. Knepley #include <exodusII.h>
15966823f3c5SBlaise Bourdin #include <petscviewerexodusii.h>
15971e50132fSMatthew G. Knepley #endif
15981e50132fSMatthew G. Knepley 
1599552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1600552f7358SJed Brown {
16011e50132fSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1602002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1603552f7358SJed Brown   PetscErrorCode ierr;
1604552f7358SJed Brown 
1605552f7358SJed Brown   PetscFunctionBegin;
1606552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1607552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1608552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);CHKERRQ(ierr);
1609fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);CHKERRQ(ierr);
1610c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);CHKERRQ(ierr);
1611e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);CHKERRQ(ierr);
16128135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);CHKERRQ(ierr);
16131e50132fSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);CHKERRQ(ierr);
1614552f7358SJed Brown   if (iascii) {
16158135c375SStefano Zampini     PetscViewerFormat format;
16168135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
16178135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
16188135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
16198135c375SStefano Zampini     } else {
1620552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
16218135c375SStefano Zampini     }
1622c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1623c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
162439d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1625c6ccd67eSMatthew G. Knepley #else
1626c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1627552f7358SJed Brown #endif
1628e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1629fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1630e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1631e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
16328135c375SStefano Zampini   } else if (isglvis) {
16338135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
16341e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
16351e50132fSMatthew G. Knepley   } else if (isexodus) {
16366823f3c5SBlaise Bourdin /*
16376823f3c5SBlaise Bourdin       exodusII requires that all sets be part of exactly one cell set.
16386823f3c5SBlaise Bourdin       If the dm does not have a "Cell Sets" label defined, we create one
16396823f3c5SBlaise Bourdin       with ID 1, containig all cells.
16406823f3c5SBlaise Bourdin       Note that if the Cell Sets label is defined but does not cover all cells,
16416823f3c5SBlaise Bourdin       we may still have a problem. This should probably be checked here or in the viewer;
16426823f3c5SBlaise Bourdin     */
16436823f3c5SBlaise Bourdin     PetscInt numCS;
16446823f3c5SBlaise Bourdin     ierr = DMGetLabelSize(dm,"Cell Sets",&numCS);CHKERRQ(ierr);
16456823f3c5SBlaise Bourdin     if (!numCS) {
16461e50132fSMatthew G. Knepley       PetscInt cStart, cEnd, c;
16471e50132fSMatthew G. Knepley       ierr = DMCreateLabel(dm, "Cell Sets");CHKERRQ(ierr);
16481e50132fSMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
16491e50132fSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {ierr = DMSetLabelValue(dm, "Cell Sets", c, 1);CHKERRQ(ierr);}
16506823f3c5SBlaise Bourdin     }
16516823f3c5SBlaise Bourdin     ierr = DMView_PlexExodusII(dm, viewer);CHKERRQ(ierr);
16521e50132fSMatthew G. Knepley #endif
165362201deeSVaclav Hapla   } else {
165498921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1655fcf6c8fdSToby Isaac   }
1656cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1657cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1658cb3ba0daSMatthew G. Knepley   if (flg) {
1659cb3ba0daSMatthew G. Knepley     Vec ranks;
1660cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1661cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1662cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1663cb3ba0daSMatthew G. Knepley   }
1664002a2709SMatthew G. Knepley   /* Optionally view a label */
1665589a23caSBarry Smith   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);CHKERRQ(ierr);
1666002a2709SMatthew G. Knepley   if (flg) {
1667002a2709SMatthew G. Knepley     DMLabel label;
1668002a2709SMatthew G. Knepley     Vec     val;
1669002a2709SMatthew G. Knepley 
1670002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
16712c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!label,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
1672002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1673002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1674002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1675002a2709SMatthew G. Knepley   }
1676552f7358SJed Brown   PetscFunctionReturn(0);
1677552f7358SJed Brown }
1678552f7358SJed Brown 
16797f96f51bSksagiyam /*@
16807f96f51bSksagiyam   DMPlexTopologyView - Saves a DMPlex topology into a file
16817f96f51bSksagiyam 
16827f96f51bSksagiyam   Collective on DM
16837f96f51bSksagiyam 
16847f96f51bSksagiyam   Input Parameters:
16857f96f51bSksagiyam + dm     - The DM whose topology is to be saved
16867f96f51bSksagiyam - viewer - The PetscViewer for saving
16877f96f51bSksagiyam 
16887f96f51bSksagiyam   Level: advanced
16897f96f51bSksagiyam 
16907f96f51bSksagiyam .seealso: DMView(), DMPlexCoordinatesView(), DMPlexLabelsView(), DMPlexTopologyLoad()
16917f96f51bSksagiyam @*/
16927f96f51bSksagiyam PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
16937f96f51bSksagiyam {
16947f96f51bSksagiyam   PetscBool      ishdf5;
16957f96f51bSksagiyam   PetscErrorCode ierr;
16967f96f51bSksagiyam 
16977f96f51bSksagiyam   PetscFunctionBegin;
16987f96f51bSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16997f96f51bSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
17007f96f51bSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
170102f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_TopologyView,viewer,0,0,0);CHKERRQ(ierr);
17027f96f51bSksagiyam   if (ishdf5) {
17037f96f51bSksagiyam #if defined(PETSC_HAVE_HDF5)
17047f96f51bSksagiyam     PetscViewerFormat format;
17057f96f51bSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
17067f96f51bSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
17077f96f51bSksagiyam       IS globalPointNumbering;
17087f96f51bSksagiyam 
17097f96f51bSksagiyam       ierr = DMPlexCreatePointNumbering(dm, &globalPointNumbering);CHKERRQ(ierr);
17107f96f51bSksagiyam       ierr = DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer);CHKERRQ(ierr);
17117f96f51bSksagiyam       ierr = ISDestroy(&globalPointNumbering);CHKERRQ(ierr);
171298921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
17137f96f51bSksagiyam #else
17147f96f51bSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
17157f96f51bSksagiyam #endif
17167f96f51bSksagiyam   }
171702f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_TopologyView,viewer,0,0,0);CHKERRQ(ierr);
17187f96f51bSksagiyam   PetscFunctionReturn(0);
17197f96f51bSksagiyam }
17207f96f51bSksagiyam 
172177b8e257Sksagiyam /*@
172277b8e257Sksagiyam   DMPlexCoordinatesView - Saves DMPlex coordinates into a file
172377b8e257Sksagiyam 
172477b8e257Sksagiyam   Collective on DM
172577b8e257Sksagiyam 
172677b8e257Sksagiyam   Input Parameters:
172777b8e257Sksagiyam + dm     - The DM whose coordinates are to be saved
172877b8e257Sksagiyam - viewer - The PetscViewer for saving
172977b8e257Sksagiyam 
173077b8e257Sksagiyam   Level: advanced
173177b8e257Sksagiyam 
173277b8e257Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexLabelsView(), DMPlexCoordinatesLoad()
173377b8e257Sksagiyam @*/
173477b8e257Sksagiyam PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
173577b8e257Sksagiyam {
173677b8e257Sksagiyam   PetscBool      ishdf5;
173777b8e257Sksagiyam   PetscErrorCode ierr;
173877b8e257Sksagiyam 
173977b8e257Sksagiyam   PetscFunctionBegin;
174077b8e257Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
174177b8e257Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
174277b8e257Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
174302f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_CoordinatesView,viewer,0,0,0);CHKERRQ(ierr);
174477b8e257Sksagiyam   if (ishdf5) {
174577b8e257Sksagiyam #if defined(PETSC_HAVE_HDF5)
174677b8e257Sksagiyam     PetscViewerFormat format;
174777b8e257Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
174877b8e257Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
174977b8e257Sksagiyam       ierr = DMPlexCoordinatesView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
175098921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
175177b8e257Sksagiyam #else
175277b8e257Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
175377b8e257Sksagiyam #endif
175477b8e257Sksagiyam   }
175502f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_CoordinatesView,viewer,0,0,0);CHKERRQ(ierr);
175677b8e257Sksagiyam   PetscFunctionReturn(0);
175777b8e257Sksagiyam }
175877b8e257Sksagiyam 
1759bd6565f1Sksagiyam /*@
1760bd6565f1Sksagiyam   DMPlexLabelsView - Saves DMPlex labels into a file
1761bd6565f1Sksagiyam 
1762bd6565f1Sksagiyam   Collective on DM
1763bd6565f1Sksagiyam 
1764bd6565f1Sksagiyam   Input Parameters:
1765bd6565f1Sksagiyam + dm     - The DM whose labels are to be saved
1766bd6565f1Sksagiyam - viewer - The PetscViewer for saving
1767bd6565f1Sksagiyam 
1768bd6565f1Sksagiyam   Level: advanced
1769bd6565f1Sksagiyam 
1770bd6565f1Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexCoordinatesView(), DMPlexLabelsLoad()
1771bd6565f1Sksagiyam @*/
1772bd6565f1Sksagiyam PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
1773bd6565f1Sksagiyam {
1774bd6565f1Sksagiyam   PetscBool      ishdf5;
1775bd6565f1Sksagiyam   PetscErrorCode ierr;
1776bd6565f1Sksagiyam 
1777bd6565f1Sksagiyam   PetscFunctionBegin;
1778bd6565f1Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1779bd6565f1Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1780bd6565f1Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
178102f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_LabelsView,viewer,0,0,0);CHKERRQ(ierr);
1782bd6565f1Sksagiyam   if (ishdf5) {
1783bd6565f1Sksagiyam #if defined(PETSC_HAVE_HDF5)
1784bd6565f1Sksagiyam     IS                globalPointNumbering;
1785bd6565f1Sksagiyam     PetscViewerFormat format;
1786bd6565f1Sksagiyam 
1787bd6565f1Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
1788bd6565f1Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1789bd6565f1Sksagiyam       ierr = DMPlexCreatePointNumbering(dm, &globalPointNumbering);CHKERRQ(ierr);
1790bd6565f1Sksagiyam       ierr = DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer);CHKERRQ(ierr);
1791bd6565f1Sksagiyam       ierr = ISDestroy(&globalPointNumbering);CHKERRQ(ierr);
179298921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1793bd6565f1Sksagiyam #else
1794bd6565f1Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1795bd6565f1Sksagiyam #endif
1796bd6565f1Sksagiyam   }
179702f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_LabelsView,viewer,0,0,0);CHKERRQ(ierr);
1798bd6565f1Sksagiyam   PetscFunctionReturn(0);
1799bd6565f1Sksagiyam }
1800bd6565f1Sksagiyam 
1801021affd3Sksagiyam /*@
1802021affd3Sksagiyam   DMPlexSectionView - Saves a section associated with a DMPlex
1803021affd3Sksagiyam 
1804021affd3Sksagiyam   Collective on DM
1805021affd3Sksagiyam 
1806021affd3Sksagiyam   Input Parameters:
1807021affd3Sksagiyam + dm         - The DM that contains the topology on which the section to be saved is defined
1808021affd3Sksagiyam . viewer     - The PetscViewer for saving
1809021affd3Sksagiyam - sectiondm  - The DM that contains the section to be saved
1810021affd3Sksagiyam 
1811021affd3Sksagiyam   Level: advanced
1812021affd3Sksagiyam 
1813021affd3Sksagiyam   Notes:
1814021affd3Sksagiyam   This function is a wrapper around PetscSectionView(); in addition to the raw section, it saves information that associates the section points to the topology (dm) points. When the topology (dm) and the section are later loaded with DMPlexTopologyLoad() and DMPlexSectionLoad(), respectively, this information is used to match section points with topology points.
1815021affd3Sksagiyam 
1816021affd3Sksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
1817021affd3Sksagiyam 
1818021affd3Sksagiyam .seealso: DMView(), DMPlexTopologyView(), DMPlexCoordinatesView(), DMPlexLabelsView(), DMPlexGlobalVectorView(), DMPlexLocalVectorView(), PetscSectionView(), DMPlexSectionLoad()
1819021affd3Sksagiyam @*/
1820021affd3Sksagiyam PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
1821021affd3Sksagiyam {
1822021affd3Sksagiyam   PetscBool      ishdf5;
1823021affd3Sksagiyam   PetscErrorCode ierr;
1824021affd3Sksagiyam 
1825021affd3Sksagiyam   PetscFunctionBegin;
1826021affd3Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1827021affd3Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1828021affd3Sksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
1829021affd3Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
183002f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_SectionView,viewer,0,0,0);CHKERRQ(ierr);
1831021affd3Sksagiyam   if (ishdf5) {
1832021affd3Sksagiyam #if defined(PETSC_HAVE_HDF5)
1833021affd3Sksagiyam     ierr = DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm);CHKERRQ(ierr);
1834021affd3Sksagiyam #else
1835021affd3Sksagiyam     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1836021affd3Sksagiyam #endif
1837021affd3Sksagiyam   }
183802f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_SectionView,viewer,0,0,0);CHKERRQ(ierr);
1839021affd3Sksagiyam   PetscFunctionReturn(0);
1840021affd3Sksagiyam }
1841021affd3Sksagiyam 
18423e97647fSksagiyam /*@
18433e97647fSksagiyam   DMPlexGlobalVectorView - Saves a global vector
18443e97647fSksagiyam 
18453e97647fSksagiyam   Collective on DM
18463e97647fSksagiyam 
18473e97647fSksagiyam   Input Parameters:
18483e97647fSksagiyam + dm        - The DM that represents the topology
18493e97647fSksagiyam . viewer    - The PetscViewer to save data with
18503e97647fSksagiyam . sectiondm - The DM that contains the global section on which vec is defined
18513e97647fSksagiyam - vec       - The global vector to be saved
18523e97647fSksagiyam 
18533e97647fSksagiyam   Level: advanced
18543e97647fSksagiyam 
18553e97647fSksagiyam   Notes:
18563e97647fSksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
18573e97647fSksagiyam 
18583e97647fSksagiyam   Typical calling sequence
18593e97647fSksagiyam $       DMCreate(PETSC_COMM_WORLD, &dm);
18603e97647fSksagiyam $       DMSetType(dm, DMPLEX);
18613e97647fSksagiyam $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
18623e97647fSksagiyam $       DMClone(dm, &sectiondm);
18633e97647fSksagiyam $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
18643e97647fSksagiyam $       PetscSectionCreate(PETSC_COMM_WORLD, &section);
18653e97647fSksagiyam $       DMPlexGetChart(sectiondm, &pStart, &pEnd);
18663e97647fSksagiyam $       PetscSectionSetChart(section, pStart, pEnd);
18673e97647fSksagiyam $       PetscSectionSetUp(section);
18683e97647fSksagiyam $       DMSetLocalSection(sectiondm, section);
18693e97647fSksagiyam $       PetscSectionDestroy(&section);
18703e97647fSksagiyam $       DMGetGlobalVector(sectiondm, &vec);
18713e97647fSksagiyam $       PetscObjectSetName((PetscObject)vec, "vec_name");
18723e97647fSksagiyam $       DMPlexTopologyView(dm, viewer);
18733e97647fSksagiyam $       DMPlexSectionView(dm, viewer, sectiondm);
18743e97647fSksagiyam $       DMPlexGlobalVectorView(dm, viewer, sectiondm, vec);
18753e97647fSksagiyam $       DMRestoreGlobalVector(sectiondm, &vec);
18763e97647fSksagiyam $       DMDestroy(&sectiondm);
18773e97647fSksagiyam $       DMDestroy(&dm);
18783e97647fSksagiyam 
18793e97647fSksagiyam .seealso: DMPlexTopologyView(), DMPlexSectionView(), DMPlexLocalVectorView(), DMPlexGlobalVectorLoad(), DMPlexLocalVectorLoad()
18803e97647fSksagiyam @*/
18813e97647fSksagiyam PetscErrorCode DMPlexGlobalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
18823e97647fSksagiyam {
18833e97647fSksagiyam   PetscBool       ishdf5;
18843e97647fSksagiyam   PetscErrorCode  ierr;
18853e97647fSksagiyam 
18863e97647fSksagiyam   PetscFunctionBegin;
18873e97647fSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18883e97647fSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18893e97647fSksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
18903e97647fSksagiyam   PetscValidHeaderSpecific(vec, VEC_CLASSID, 4);
18913e97647fSksagiyam   /* Check consistency */
18923e97647fSksagiyam   {
18933e97647fSksagiyam     PetscSection  section;
18943e97647fSksagiyam     PetscBool     includesConstraints;
18953e97647fSksagiyam     PetscInt      m, m1;
18963e97647fSksagiyam 
18973e97647fSksagiyam     ierr = VecGetLocalSize(vec, &m1);CHKERRQ(ierr);
18983e97647fSksagiyam     ierr = DMGetGlobalSection(sectiondm, &section);CHKERRQ(ierr);
18993e97647fSksagiyam     ierr = PetscSectionGetIncludesConstraints(section, &includesConstraints);CHKERRQ(ierr);
19003e97647fSksagiyam     if (includesConstraints) {ierr = PetscSectionGetStorageSize(section, &m);CHKERRQ(ierr);}
19013e97647fSksagiyam     else {ierr = PetscSectionGetConstrainedStorageSize(section, &m);CHKERRQ(ierr);}
19022c71b3e2SJacob Faibussowitsch     PetscCheckFalse(m1 != m,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global vector size (%D) != global section storage size (%D)", m1, m);
19033e97647fSksagiyam   }
19043e97647fSksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
190502f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_GlobalVectorView,viewer,0,0,0);CHKERRQ(ierr);
19063e97647fSksagiyam   if (ishdf5) {
19073e97647fSksagiyam #if defined(PETSC_HAVE_HDF5)
19083e97647fSksagiyam     ierr = DMPlexGlobalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);CHKERRQ(ierr);
19093e97647fSksagiyam #else
19103e97647fSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
19113e97647fSksagiyam #endif
19123e97647fSksagiyam   }
191302f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_GlobalVectorView,viewer,0,0,0);CHKERRQ(ierr);
19143e97647fSksagiyam   PetscFunctionReturn(0);
19153e97647fSksagiyam }
19163e97647fSksagiyam 
19173e97647fSksagiyam /*@
19183e97647fSksagiyam   DMPlexLocalVectorView - Saves a local vector
19193e97647fSksagiyam 
19203e97647fSksagiyam   Collective on DM
19213e97647fSksagiyam 
19223e97647fSksagiyam   Input Parameters:
19233e97647fSksagiyam + dm        - The DM that represents the topology
19243e97647fSksagiyam . viewer    - The PetscViewer to save data with
19253e97647fSksagiyam . sectiondm - The DM that contains the local section on which vec is defined; may be the same as dm
19263e97647fSksagiyam - vec       - The local vector to be saved
19273e97647fSksagiyam 
19283e97647fSksagiyam   Level: advanced
19293e97647fSksagiyam 
19303e97647fSksagiyam   Notes:
19313e97647fSksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
19323e97647fSksagiyam 
19333e97647fSksagiyam   Typical calling sequence
19343e97647fSksagiyam $       DMCreate(PETSC_COMM_WORLD, &dm);
19353e97647fSksagiyam $       DMSetType(dm, DMPLEX);
19363e97647fSksagiyam $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
19373e97647fSksagiyam $       DMClone(dm, &sectiondm);
19383e97647fSksagiyam $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
19393e97647fSksagiyam $       PetscSectionCreate(PETSC_COMM_WORLD, &section);
19403e97647fSksagiyam $       DMPlexGetChart(sectiondm, &pStart, &pEnd);
19413e97647fSksagiyam $       PetscSectionSetChart(section, pStart, pEnd);
19423e97647fSksagiyam $       PetscSectionSetUp(section);
19433e97647fSksagiyam $       DMSetLocalSection(sectiondm, section);
19443e97647fSksagiyam $       DMGetLocalVector(sectiondm, &vec);
19453e97647fSksagiyam $       PetscObjectSetName((PetscObject)vec, "vec_name");
19463e97647fSksagiyam $       DMPlexTopologyView(dm, viewer);
19473e97647fSksagiyam $       DMPlexSectionView(dm, viewer, sectiondm);
19483e97647fSksagiyam $       DMPlexLocalVectorView(dm, viewer, sectiondm, vec);
19493e97647fSksagiyam $       DMRestoreLocalVector(sectiondm, &vec);
19503e97647fSksagiyam $       DMDestroy(&sectiondm);
19513e97647fSksagiyam $       DMDestroy(&dm);
19523e97647fSksagiyam 
19533e97647fSksagiyam .seealso: DMPlexTopologyView(), DMPlexSectionView(), DMPlexGlobalVectorView(), DMPlexGlobalVectorLoad(), DMPlexLocalVectorLoad()
19543e97647fSksagiyam @*/
19553e97647fSksagiyam PetscErrorCode DMPlexLocalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
19563e97647fSksagiyam {
19573e97647fSksagiyam   PetscBool       ishdf5;
19583e97647fSksagiyam   PetscErrorCode  ierr;
19593e97647fSksagiyam 
19603e97647fSksagiyam   PetscFunctionBegin;
19613e97647fSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19623e97647fSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
19633e97647fSksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
19643e97647fSksagiyam   PetscValidHeaderSpecific(vec, VEC_CLASSID, 4);
19653e97647fSksagiyam   /* Check consistency */
19663e97647fSksagiyam   {
19673e97647fSksagiyam     PetscSection  section;
19683e97647fSksagiyam     PetscBool     includesConstraints;
19693e97647fSksagiyam     PetscInt      m, m1;
19703e97647fSksagiyam 
19713e97647fSksagiyam     ierr = VecGetLocalSize(vec, &m1);CHKERRQ(ierr);
19723e97647fSksagiyam     ierr = DMGetLocalSection(sectiondm, &section);CHKERRQ(ierr);
19733e97647fSksagiyam     ierr = PetscSectionGetIncludesConstraints(section, &includesConstraints);CHKERRQ(ierr);
19743e97647fSksagiyam     if (includesConstraints) {ierr = PetscSectionGetStorageSize(section, &m);CHKERRQ(ierr);}
19753e97647fSksagiyam     else {ierr = PetscSectionGetConstrainedStorageSize(section, &m);CHKERRQ(ierr);}
19762c71b3e2SJacob Faibussowitsch     PetscCheckFalse(m1 != m,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local vector size (%D) != local section storage size (%D)", m1, m);
19773e97647fSksagiyam   }
19783e97647fSksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
197902f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_LocalVectorView,viewer,0,0,0);CHKERRQ(ierr);
19803e97647fSksagiyam   if (ishdf5) {
19813e97647fSksagiyam #if defined(PETSC_HAVE_HDF5)
19823e97647fSksagiyam     ierr = DMPlexLocalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);CHKERRQ(ierr);
19833e97647fSksagiyam #else
19843e97647fSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
19853e97647fSksagiyam #endif
19863e97647fSksagiyam   }
198702f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_LocalVectorView,viewer,0,0,0);CHKERRQ(ierr);
19883e97647fSksagiyam   PetscFunctionReturn(0);
19893e97647fSksagiyam }
19903e97647fSksagiyam 
19912c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
19922c40f234SMatthew G. Knepley {
1993d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
19942c40f234SMatthew G. Knepley   PetscErrorCode ierr;
19952c40f234SMatthew G. Knepley 
19962c40f234SMatthew G. Knepley   PetscFunctionBegin;
19972c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19982c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
19992c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
2000d4f5a9a0SVaclav Hapla   if (ishdf5) {
20012c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
20029c48423bSVaclav Hapla     PetscViewerFormat format;
20039c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
20049c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
20059c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
2006509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
200739d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
200898921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2009b458e8f1SJose E. Roman     PetscFunctionReturn(0);
20102c40f234SMatthew G. Knepley #else
20112c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2012552f7358SJed Brown #endif
201398921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
2014552f7358SJed Brown }
2015552f7358SJed Brown 
2016ea8e1828Sksagiyam /*@
2017ea8e1828Sksagiyam   DMPlexTopologyLoad - Loads a topology into a DMPlex
2018ea8e1828Sksagiyam 
2019ea8e1828Sksagiyam   Collective on DM
2020ea8e1828Sksagiyam 
2021ea8e1828Sksagiyam   Input Parameters:
2022ea8e1828Sksagiyam + dm     - The DM into which the topology is loaded
2023ea8e1828Sksagiyam - viewer - The PetscViewer for the saved topology
2024ea8e1828Sksagiyam 
2025dec9e869Sksagiyam   Output Parameters:
2026f84dd6b4Sksagiyam . globalToLocalPointSF - The PetscSF that pushes points in [0, N) to the associated points in the loaded plex, where N is the global number of points; NULL if unneeded
2027dec9e869Sksagiyam 
2028ea8e1828Sksagiyam   Level: advanced
2029ea8e1828Sksagiyam 
2030b08ad5deSksagiyam .seealso: DMLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
2031ea8e1828Sksagiyam @*/
2032f84dd6b4Sksagiyam PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *globalToLocalPointSF)
2033ea8e1828Sksagiyam {
2034ea8e1828Sksagiyam   PetscBool      ishdf5;
2035ea8e1828Sksagiyam   PetscErrorCode ierr;
2036ea8e1828Sksagiyam 
2037ea8e1828Sksagiyam   PetscFunctionBegin;
2038ea8e1828Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2039ea8e1828Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
2040f84dd6b4Sksagiyam   if (globalToLocalPointSF) PetscValidPointer(globalToLocalPointSF, 3);
2041ea8e1828Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
204202f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_TopologyLoad,viewer,0,0,0);CHKERRQ(ierr);
2043ea8e1828Sksagiyam   if (ishdf5) {
2044ea8e1828Sksagiyam #if defined(PETSC_HAVE_HDF5)
2045ea8e1828Sksagiyam     PetscViewerFormat format;
2046ea8e1828Sksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
2047ea8e1828Sksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2048f84dd6b4Sksagiyam       ierr = DMPlexTopologyLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);CHKERRQ(ierr);
204998921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2050ea8e1828Sksagiyam #else
2051ea8e1828Sksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2052ea8e1828Sksagiyam #endif
2053ea8e1828Sksagiyam   }
205402f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_TopologyLoad,viewer,0,0,0);CHKERRQ(ierr);
2055ea8e1828Sksagiyam   PetscFunctionReturn(0);
2056ea8e1828Sksagiyam }
2057ea8e1828Sksagiyam 
20583e701f1cSksagiyam /*@
20593e701f1cSksagiyam   DMPlexCoordinatesLoad - Loads coordinates into a DMPlex
20603e701f1cSksagiyam 
20613e701f1cSksagiyam   Collective on DM
20623e701f1cSksagiyam 
20633e701f1cSksagiyam   Input Parameters:
20643e701f1cSksagiyam + dm     - The DM into which the coordinates are loaded
2065c9ad657eSksagiyam . viewer - The PetscViewer for the saved coordinates
2066c9ad657eSksagiyam - globalToLocalPointSF - The SF returned by DMPlexTopologyLoad() when loading dm from viewer
20673e701f1cSksagiyam 
20683e701f1cSksagiyam   Level: advanced
20693e701f1cSksagiyam 
2070b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
20713e701f1cSksagiyam @*/
2072c9ad657eSksagiyam PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
20733e701f1cSksagiyam {
20743e701f1cSksagiyam   PetscBool      ishdf5;
20753e701f1cSksagiyam   PetscErrorCode ierr;
20763e701f1cSksagiyam 
20773e701f1cSksagiyam   PetscFunctionBegin;
20783e701f1cSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20793e701f1cSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
2080c9ad657eSksagiyam   PetscValidHeaderSpecific(globalToLocalPointSF, PETSCSF_CLASSID, 3);
20813e701f1cSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
208202f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_CoordinatesLoad,viewer,0,0,0);CHKERRQ(ierr);
20833e701f1cSksagiyam   if (ishdf5) {
20843e701f1cSksagiyam #if defined(PETSC_HAVE_HDF5)
20853e701f1cSksagiyam     PetscViewerFormat format;
20863e701f1cSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
20873e701f1cSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2088c9ad657eSksagiyam       ierr = DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);CHKERRQ(ierr);
208998921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
20903e701f1cSksagiyam #else
20913e701f1cSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
20923e701f1cSksagiyam #endif
20933e701f1cSksagiyam   }
209402f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_CoordinatesLoad,viewer,0,0,0);CHKERRQ(ierr);
20953e701f1cSksagiyam   PetscFunctionReturn(0);
20963e701f1cSksagiyam }
20973e701f1cSksagiyam 
2098b08ad5deSksagiyam /*@
2099b08ad5deSksagiyam   DMPlexLabelsLoad - Loads labels into a DMPlex
2100b08ad5deSksagiyam 
2101b08ad5deSksagiyam   Collective on DM
2102b08ad5deSksagiyam 
2103b08ad5deSksagiyam   Input Parameters:
2104b08ad5deSksagiyam + dm     - The DM into which the labels are loaded
2105e6368b79SVaclav Hapla . viewer - The PetscViewer for the saved labels
2106e6368b79SVaclav Hapla - globalToLocalPointSF - The SF returned by DMPlexTopologyLoad() when loading dm from viewer
2107b08ad5deSksagiyam 
2108b08ad5deSksagiyam   Level: advanced
2109b08ad5deSksagiyam 
2110e6368b79SVaclav Hapla   Notes:
2111e6368b79SVaclav Hapla   The PetscSF argument must not be NULL if the DM is distributed, otherwise an error occurs.
2112e6368b79SVaclav Hapla 
2113b08ad5deSksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
2114b08ad5deSksagiyam @*/
2115e6368b79SVaclav Hapla PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2116b08ad5deSksagiyam {
2117b08ad5deSksagiyam   PetscBool      ishdf5;
2118b08ad5deSksagiyam   PetscErrorCode ierr;
2119b08ad5deSksagiyam 
2120b08ad5deSksagiyam   PetscFunctionBegin;
2121b08ad5deSksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2122b08ad5deSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
2123e6368b79SVaclav Hapla   if (globalToLocalPointSF) PetscValidHeaderSpecific(globalToLocalPointSF, PETSCSF_CLASSID, 3);
2124b08ad5deSksagiyam   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
212502f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_LabelsLoad,viewer,0,0,0);CHKERRQ(ierr);
2126b08ad5deSksagiyam   if (ishdf5) {
2127b08ad5deSksagiyam #if defined(PETSC_HAVE_HDF5)
2128b08ad5deSksagiyam     PetscViewerFormat format;
2129b08ad5deSksagiyam 
2130b08ad5deSksagiyam     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
2131b08ad5deSksagiyam     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2132e6368b79SVaclav Hapla       ierr = DMPlexLabelsLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);CHKERRQ(ierr);
213398921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2134b08ad5deSksagiyam #else
2135b08ad5deSksagiyam     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2136b08ad5deSksagiyam #endif
2137b08ad5deSksagiyam   }
213802f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_LabelsLoad,viewer,0,0,0);CHKERRQ(ierr);
2139b08ad5deSksagiyam   PetscFunctionReturn(0);
2140b08ad5deSksagiyam }
2141b08ad5deSksagiyam 
2142f84dd6b4Sksagiyam /*@
2143f84dd6b4Sksagiyam   DMPlexSectionLoad - Loads section into a DMPlex
2144f84dd6b4Sksagiyam 
2145f84dd6b4Sksagiyam   Collective on DM
2146f84dd6b4Sksagiyam 
2147f84dd6b4Sksagiyam   Input Parameters:
2148f84dd6b4Sksagiyam + dm          - The DM that represents the topology
2149f84dd6b4Sksagiyam . viewer      - The PetscViewer that represents the on-disk section (sectionA)
2150f84dd6b4Sksagiyam . sectiondm   - The DM into which the on-disk section (sectionA) is migrated
2151f84dd6b4Sksagiyam - globalToLocalPointSF - The SF returned by DMPlexTopologyLoad() when loading dm from viewer
2152f84dd6b4Sksagiyam 
2153f84dd6b4Sksagiyam   Output Parameters
2154f84dd6b4Sksagiyam + globalDofSF - The SF that migrates any on-disk Vec data associated with sectionA into a global Vec associated with the sectiondm's global section (NULL if not needed)
2155f84dd6b4Sksagiyam - localDofSF  - The SF that migrates any on-disk Vec data associated with sectionA into a local Vec associated with the sectiondm's local section (NULL if not needed)
2156f84dd6b4Sksagiyam 
2157f84dd6b4Sksagiyam   Level: advanced
2158f84dd6b4Sksagiyam 
2159f84dd6b4Sksagiyam   Notes:
2160f84dd6b4Sksagiyam   This function is a wrapper around PetscSectionLoad(); it loads, in addition to the raw section, a list of global point numbers that associates each on-disk section point with a global point number in [0, NX), where NX is the number of topology points in dm. Noting that globalToLocalPointSF associates each topology point in dm with a global number in [0, NX), one can readily establish an association of the on-disk section points with the topology points.
2161f84dd6b4Sksagiyam 
2162f84dd6b4Sksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
2163f84dd6b4Sksagiyam 
2164f84dd6b4Sksagiyam   The output parameter, globalDofSF (localDofSF), can later be used with DMPlexGlobalVectorLoad() (DMPlexLocalVectorLoad()) to load on-disk vectors into global (local) vectors associated with sectiondm's global (local) section.
2165f84dd6b4Sksagiyam 
2166f84dd6b4Sksagiyam   Example using 2 processes:
2167f84dd6b4Sksagiyam $  NX (number of points on dm): 4
2168f84dd6b4Sksagiyam $  sectionA                   : the on-disk section
2169f84dd6b4Sksagiyam $  vecA                       : a vector associated with sectionA
2170f84dd6b4Sksagiyam $  sectionB                   : sectiondm's local section constructed in this function
2171f84dd6b4Sksagiyam $  vecB (local)               : a vector associated with sectiondm's local section
2172f84dd6b4Sksagiyam $  vecB (global)              : a vector associated with sectiondm's global section
2173f84dd6b4Sksagiyam $
2174f84dd6b4Sksagiyam $                                     rank 0    rank 1
2175f84dd6b4Sksagiyam $  vecA (global)                  : [.0 .4 .1 | .2 .3]        <- to be loaded in DMPlexGlobalVectorLoad() or DMPlexLocalVectorLoad()
2176f84dd6b4Sksagiyam $  sectionA->atlasOff             :       0 2 | 1             <- loaded in PetscSectionLoad()
2177f84dd6b4Sksagiyam $  sectionA->atlasDof             :       1 3 | 1             <- loaded in PetscSectionLoad()
2178f84dd6b4Sksagiyam $  sectionA's global point numbers:       0 2 | 3             <- loaded in DMPlexSectionLoad()
2179f84dd6b4Sksagiyam $  [0, NX)                        :       0 1 | 2 3           <- conceptual partition used in globalToLocalPointSF
2180f84dd6b4Sksagiyam $  sectionB's global point numbers:     0 1 3 | 3 2           <- associated with [0, NX) by globalToLocalPointSF
2181f84dd6b4Sksagiyam $  sectionB->atlasDof             :     1 0 1 | 1 3
2182f84dd6b4Sksagiyam $  sectionB->atlasOff (no perm)   :     0 1 1 | 0 1
2183f84dd6b4Sksagiyam $  vecB (local)                   :   [.0 .4] | [.4 .1 .2 .3] <- to be constructed by calling DMPlexLocalVectorLoad() with localDofSF
2184f84dd6b4Sksagiyam $  vecB (global)                  :    [.0 .4 | .1 .2 .3]     <- to be constructed by calling DMPlexGlobalVectorLoad() with globalDofSF
2185f84dd6b4Sksagiyam $
2186f84dd6b4Sksagiyam $  where "|" represents a partition of loaded data, and global point 3 is assumed to be owned by rank 0.
2187f84dd6b4Sksagiyam 
2188f84dd6b4Sksagiyam .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMPlexGlobalVectorLoad(), DMPlexLocalVectorLoad(), PetscSectionLoad(), DMPlexSectionView()
2189f84dd6b4Sksagiyam @*/
2190f84dd6b4Sksagiyam PetscErrorCode DMPlexSectionLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF globalToLocalPointSF, PetscSF *globalDofSF, PetscSF *localDofSF)
2191f84dd6b4Sksagiyam {
2192f84dd6b4Sksagiyam   PetscBool      ishdf5;
2193f84dd6b4Sksagiyam   PetscErrorCode ierr;
2194f84dd6b4Sksagiyam 
2195f84dd6b4Sksagiyam   PetscFunctionBegin;
2196f84dd6b4Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2197f84dd6b4Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
2198f84dd6b4Sksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
2199f84dd6b4Sksagiyam   PetscValidHeaderSpecific(globalToLocalPointSF, PETSCSF_CLASSID, 4);
2200f84dd6b4Sksagiyam   if (globalDofSF) PetscValidPointer(globalDofSF, 5);
2201f84dd6b4Sksagiyam   if (localDofSF) PetscValidPointer(localDofSF, 6);
2202f84dd6b4Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
220302f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_SectionLoad,viewer,0,0,0);CHKERRQ(ierr);
2204f84dd6b4Sksagiyam   if (ishdf5) {
2205f84dd6b4Sksagiyam #if defined(PETSC_HAVE_HDF5)
2206f84dd6b4Sksagiyam     ierr = DMPlexSectionLoad_HDF5_Internal(dm, viewer, sectiondm, globalToLocalPointSF, globalDofSF, localDofSF);CHKERRQ(ierr);
2207f84dd6b4Sksagiyam #else
2208f84dd6b4Sksagiyam     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2209f84dd6b4Sksagiyam #endif
2210f84dd6b4Sksagiyam   }
221102f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_SectionLoad,viewer,0,0,0);CHKERRQ(ierr);
2212f84dd6b4Sksagiyam   PetscFunctionReturn(0);
2213f84dd6b4Sksagiyam }
2214f84dd6b4Sksagiyam 
22158be3dfe1Sksagiyam /*@
22168be3dfe1Sksagiyam   DMPlexGlobalVectorLoad - Loads on-disk vector data into a global vector
22178be3dfe1Sksagiyam 
22188be3dfe1Sksagiyam   Collective on DM
22198be3dfe1Sksagiyam 
22208be3dfe1Sksagiyam   Input Parameters:
22218be3dfe1Sksagiyam + dm        - The DM that represents the topology
22228be3dfe1Sksagiyam . viewer    - The PetscViewer that represents the on-disk vector data
22238be3dfe1Sksagiyam . sectiondm - The DM that contains the global section on which vec is defined
22248be3dfe1Sksagiyam . sf        - The SF that migrates the on-disk vector data into vec
22258be3dfe1Sksagiyam - vec       - The global vector to set values of
22268be3dfe1Sksagiyam 
22278be3dfe1Sksagiyam   Level: advanced
22288be3dfe1Sksagiyam 
22298be3dfe1Sksagiyam   Notes:
22308be3dfe1Sksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
22318be3dfe1Sksagiyam 
22328be3dfe1Sksagiyam   Typical calling sequence
22338be3dfe1Sksagiyam $       DMCreate(PETSC_COMM_WORLD, &dm);
22348be3dfe1Sksagiyam $       DMSetType(dm, DMPLEX);
22358be3dfe1Sksagiyam $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
22368be3dfe1Sksagiyam $       DMPlexTopologyLoad(dm, viewer, &sfX);
22378be3dfe1Sksagiyam $       DMClone(dm, &sectiondm);
22388be3dfe1Sksagiyam $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
22398be3dfe1Sksagiyam $       DMPlexSectionLoad(dm, viewer, sectiondm, sfX, &gsf, NULL);
22408be3dfe1Sksagiyam $       DMGetGlobalVector(sectiondm, &vec);
22418be3dfe1Sksagiyam $       PetscObjectSetName((PetscObject)vec, "vec_name");
22428be3dfe1Sksagiyam $       DMPlexGlobalVectorLoad(dm, viewer, sectiondm, gsf, vec);
22438be3dfe1Sksagiyam $       DMRestoreGlobalVector(sectiondm, &vec);
22448be3dfe1Sksagiyam $       PetscSFDestroy(&gsf);
22458be3dfe1Sksagiyam $       PetscSFDestroy(&sfX);
22468be3dfe1Sksagiyam $       DMDestroy(&sectiondm);
22478be3dfe1Sksagiyam $       DMDestroy(&dm);
22488be3dfe1Sksagiyam 
22498be3dfe1Sksagiyam .seealso: DMPlexTopologyLoad(), DMPlexSectionLoad(), DMPlexLocalVectorLoad(), DMPlexGlobalVectorView(), DMPlexLocalVectorView()
22508be3dfe1Sksagiyam @*/
22518be3dfe1Sksagiyam PetscErrorCode DMPlexGlobalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
22528be3dfe1Sksagiyam {
22538be3dfe1Sksagiyam   PetscBool       ishdf5;
22548be3dfe1Sksagiyam   PetscErrorCode  ierr;
22558be3dfe1Sksagiyam 
22568be3dfe1Sksagiyam   PetscFunctionBegin;
22578be3dfe1Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22588be3dfe1Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
22598be3dfe1Sksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
22608be3dfe1Sksagiyam   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 4);
22618be3dfe1Sksagiyam   PetscValidHeaderSpecific(vec, VEC_CLASSID, 5);
22628be3dfe1Sksagiyam   /* Check consistency */
22638be3dfe1Sksagiyam   {
22648be3dfe1Sksagiyam     PetscSection  section;
22658be3dfe1Sksagiyam     PetscBool     includesConstraints;
22668be3dfe1Sksagiyam     PetscInt      m, m1;
22678be3dfe1Sksagiyam 
22688be3dfe1Sksagiyam     ierr = VecGetLocalSize(vec, &m1);CHKERRQ(ierr);
22698be3dfe1Sksagiyam     ierr = DMGetGlobalSection(sectiondm, &section);CHKERRQ(ierr);
22708be3dfe1Sksagiyam     ierr = PetscSectionGetIncludesConstraints(section, &includesConstraints);CHKERRQ(ierr);
22718be3dfe1Sksagiyam     if (includesConstraints) {ierr = PetscSectionGetStorageSize(section, &m);CHKERRQ(ierr);}
22728be3dfe1Sksagiyam     else {ierr = PetscSectionGetConstrainedStorageSize(section, &m);CHKERRQ(ierr);}
22732c71b3e2SJacob Faibussowitsch     PetscCheckFalse(m1 != m,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global vector size (%D) != global section storage size (%D)", m1, m);
22748be3dfe1Sksagiyam   }
22758be3dfe1Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
227602f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_GlobalVectorLoad,viewer,0,0,0);CHKERRQ(ierr);
22778be3dfe1Sksagiyam   if (ishdf5) {
22788be3dfe1Sksagiyam #if defined(PETSC_HAVE_HDF5)
22798be3dfe1Sksagiyam     ierr = DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);CHKERRQ(ierr);
22808be3dfe1Sksagiyam #else
22818be3dfe1Sksagiyam     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
22828be3dfe1Sksagiyam #endif
22838be3dfe1Sksagiyam   }
228402f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_GlobalVectorLoad,viewer,0,0,0);CHKERRQ(ierr);
22858be3dfe1Sksagiyam   PetscFunctionReturn(0);
22868be3dfe1Sksagiyam }
22878be3dfe1Sksagiyam 
22888be3dfe1Sksagiyam /*@
22898be3dfe1Sksagiyam   DMPlexLocalVectorLoad - Loads on-disk vector data into a local vector
22908be3dfe1Sksagiyam 
22918be3dfe1Sksagiyam   Collective on DM
22928be3dfe1Sksagiyam 
22938be3dfe1Sksagiyam   Input Parameters:
22948be3dfe1Sksagiyam + dm        - The DM that represents the topology
22958be3dfe1Sksagiyam . viewer    - The PetscViewer that represents the on-disk vector data
22968be3dfe1Sksagiyam . sectiondm - The DM that contains the local section on which vec is defined
22978be3dfe1Sksagiyam . sf        - The SF that migrates the on-disk vector data into vec
22988be3dfe1Sksagiyam - vec       - The local vector to set values of
22998be3dfe1Sksagiyam 
23008be3dfe1Sksagiyam   Level: advanced
23018be3dfe1Sksagiyam 
23028be3dfe1Sksagiyam   Notes:
23038be3dfe1Sksagiyam   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with PetscObjectSetName(). In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
23048be3dfe1Sksagiyam 
23058be3dfe1Sksagiyam   Typical calling sequence
23068be3dfe1Sksagiyam $       DMCreate(PETSC_COMM_WORLD, &dm);
23078be3dfe1Sksagiyam $       DMSetType(dm, DMPLEX);
23088be3dfe1Sksagiyam $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
23098be3dfe1Sksagiyam $       DMPlexTopologyLoad(dm, viewer, &sfX);
23108be3dfe1Sksagiyam $       DMClone(dm, &sectiondm);
23118be3dfe1Sksagiyam $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
23128be3dfe1Sksagiyam $       DMPlexSectionLoad(dm, viewer, sectiondm, sfX, NULL, &lsf);
23138be3dfe1Sksagiyam $       DMGetLocalVector(sectiondm, &vec);
23148be3dfe1Sksagiyam $       PetscObjectSetName((PetscObject)vec, "vec_name");
23158be3dfe1Sksagiyam $       DMPlexLocalVectorLoad(dm, viewer, sectiondm, lsf, vec);
23168be3dfe1Sksagiyam $       DMRestoreLocalVector(sectiondm, &vec);
23178be3dfe1Sksagiyam $       PetscSFDestroy(&lsf);
23188be3dfe1Sksagiyam $       PetscSFDestroy(&sfX);
23198be3dfe1Sksagiyam $       DMDestroy(&sectiondm);
23208be3dfe1Sksagiyam $       DMDestroy(&dm);
23218be3dfe1Sksagiyam 
23228be3dfe1Sksagiyam .seealso: DMPlexTopologyLoad(), DMPlexSectionLoad(), DMPlexGlobalVectorLoad(), DMPlexGlobalVectorView(), DMPlexLocalVectorView()
23238be3dfe1Sksagiyam @*/
23248be3dfe1Sksagiyam PetscErrorCode DMPlexLocalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
23258be3dfe1Sksagiyam {
23268be3dfe1Sksagiyam   PetscBool       ishdf5;
23278be3dfe1Sksagiyam   PetscErrorCode  ierr;
23288be3dfe1Sksagiyam 
23298be3dfe1Sksagiyam   PetscFunctionBegin;
23308be3dfe1Sksagiyam   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23318be3dfe1Sksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
23328be3dfe1Sksagiyam   PetscValidHeaderSpecific(sectiondm, DM_CLASSID, 3);
23338be3dfe1Sksagiyam   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 4);
23348be3dfe1Sksagiyam   PetscValidHeaderSpecific(vec, VEC_CLASSID, 5);
23358be3dfe1Sksagiyam   /* Check consistency */
23368be3dfe1Sksagiyam   {
23378be3dfe1Sksagiyam     PetscSection  section;
23388be3dfe1Sksagiyam     PetscBool     includesConstraints;
23398be3dfe1Sksagiyam     PetscInt      m, m1;
23408be3dfe1Sksagiyam 
23418be3dfe1Sksagiyam     ierr = VecGetLocalSize(vec, &m1);CHKERRQ(ierr);
23428be3dfe1Sksagiyam     ierr = DMGetLocalSection(sectiondm, &section);CHKERRQ(ierr);
23438be3dfe1Sksagiyam     ierr = PetscSectionGetIncludesConstraints(section, &includesConstraints);CHKERRQ(ierr);
23448be3dfe1Sksagiyam     if (includesConstraints) {ierr = PetscSectionGetStorageSize(section, &m);CHKERRQ(ierr);}
23458be3dfe1Sksagiyam     else {ierr = PetscSectionGetConstrainedStorageSize(section, &m);CHKERRQ(ierr);}
23462c71b3e2SJacob Faibussowitsch     PetscCheckFalse(m1 != m,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local vector size (%D) != local section storage size (%D)", m1, m);
23478be3dfe1Sksagiyam   }
23488be3dfe1Sksagiyam   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
234902f7d72cSksagiyam   ierr = PetscLogEventBegin(DMPLEX_LocalVectorLoad,viewer,0,0,0);CHKERRQ(ierr);
23508be3dfe1Sksagiyam   if (ishdf5) {
23518be3dfe1Sksagiyam #if defined(PETSC_HAVE_HDF5)
23528be3dfe1Sksagiyam     ierr = DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);CHKERRQ(ierr);
23538be3dfe1Sksagiyam #else
23548be3dfe1Sksagiyam     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
23558be3dfe1Sksagiyam #endif
23568be3dfe1Sksagiyam   }
235702f7d72cSksagiyam   ierr = PetscLogEventEnd(DMPLEX_LocalVectorLoad,viewer,0,0,0);CHKERRQ(ierr);
23588be3dfe1Sksagiyam   PetscFunctionReturn(0);
23598be3dfe1Sksagiyam }
23608be3dfe1Sksagiyam 
2361552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
2362552f7358SJed Brown {
2363552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2364552f7358SJed Brown   PetscErrorCode ierr;
2365552f7358SJed Brown 
2366552f7358SJed Brown   PetscFunctionBegin;
23678135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
23688135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
236928d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
23701eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);CHKERRQ(ierr);
23710d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
2372552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
2373552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
2374552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
2375552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
2376be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
2377552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
2378552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
2379d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
2380d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
2381012bc364SMatthew G. Knepley   ierr = PetscFree(mesh->transformType);CHKERRQ(ierr);
238277623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
2383a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
238497d8846cSMatthew Knepley   ierr = ISDestroy(&mesh->subpointIS);CHKERRQ(ierr);
2385552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
2386552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
2387a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
2388a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
2389d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
2390d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
2391d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
2392d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
2393d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
2394d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
2395fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
23960a19bb7dSprj-   ierr = PetscFree(mesh->neighbors);CHKERRQ(ierr);
239731b70465SJoe Wallwork   if (mesh->metricCtx) { ierr = PetscFree(mesh->metricCtx);CHKERRQ(ierr); }
2398552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
2399552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
2400552f7358SJed Brown   PetscFunctionReturn(0);
2401552f7358SJed Brown }
2402552f7358SJed Brown 
2403b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
2404552f7358SJed Brown {
24058d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
2406acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
2407552f7358SJed Brown   PetscInt               localSize;
2408837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
2409552f7358SJed Brown   PetscErrorCode         ierr;
2410b412c318SBarry Smith   MatType                mtype;
24111428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
2412552f7358SJed Brown 
2413552f7358SJed Brown   PetscFunctionBegin;
2414607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
2415b412c318SBarry Smith   mtype = dm->mattype;
2416e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
2417552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
2418552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
241982f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
2420552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
2421552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
2422552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
2423acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
2424acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
2425552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
2426552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
2427552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
2428552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
2429552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
2430552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
2431552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
2432837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
2433552f7358SJed Brown   if (!isShell) {
2434837628f4SStefano Zampini     PetscBool fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
2435*e432b41dSStefano Zampini     PetscInt  *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2];
2436fad22124SMatthew G Knepley     PetscInt  pStart, pEnd, p, dof, cdof;
2437552f7358SJed Brown 
243800140cc3SStefano Zampini     ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
2439552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
2440*e432b41dSStefano Zampini     for (p = pStart; p < pEnd; ++p) {
2441a9d99c84SMatthew G. Knepley       PetscInt bdof;
2442a9d99c84SMatthew G. Knepley 
2443552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
2444fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
24451d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
24461d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
24471d17a0a3SMatthew G. Knepley       if (dof) {
24481d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
2449*e432b41dSStefano Zampini         else if (bs != bdof) {bs = 1; break;}
2450552f7358SJed Brown       }
24512a28c762SMatthew G Knepley     }
24522a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
2453*e432b41dSStefano Zampini     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
2454*e432b41dSStefano Zampini     bsLocal[1] = bs;
24550be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
2456*e432b41dSStefano Zampini     if (bsMinMax[0] != bsMinMax[1]) bs = 1;
2457*e432b41dSStefano Zampini     else bs = bsMinMax[0];
24586fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
2459be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
24601795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
24618d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
2462552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
2463552f7358SJed Brown   }
2464b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
2465552f7358SJed Brown   PetscFunctionReturn(0);
2466552f7358SJed Brown }
2467552f7358SJed Brown 
24687cd05799SMatthew G. Knepley /*@
2469a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
2470be36d101SStefano Zampini 
2471be36d101SStefano Zampini   Not collective
2472be36d101SStefano Zampini 
2473be36d101SStefano Zampini   Input Parameter:
2474be36d101SStefano Zampini . mesh - The DMPlex
2475be36d101SStefano Zampini 
2476be36d101SStefano Zampini   Output Parameters:
2477be36d101SStefano Zampini . subsection - The subdomain section
2478be36d101SStefano Zampini 
2479be36d101SStefano Zampini   Level: developer
2480be36d101SStefano Zampini 
2481be36d101SStefano Zampini .seealso:
24827cd05799SMatthew G. Knepley @*/
2483be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
2484be36d101SStefano Zampini {
2485be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
2486be36d101SStefano Zampini   PetscErrorCode ierr;
2487be36d101SStefano Zampini 
2488be36d101SStefano Zampini   PetscFunctionBegin;
2489be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2490be36d101SStefano Zampini   if (!mesh->subdomainSection) {
2491be36d101SStefano Zampini     PetscSection section;
2492be36d101SStefano Zampini     PetscSF      sf;
2493be36d101SStefano Zampini 
2494be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
249592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
2496be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
2497be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
2498be36d101SStefano Zampini   }
2499be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
2500be36d101SStefano Zampini   PetscFunctionReturn(0);
2501be36d101SStefano Zampini }
2502be36d101SStefano Zampini 
2503552f7358SJed Brown /*@
2504552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
2505552f7358SJed Brown 
2506552f7358SJed Brown   Not collective
2507552f7358SJed Brown 
2508552f7358SJed Brown   Input Parameter:
2509552f7358SJed Brown . mesh - The DMPlex
2510552f7358SJed Brown 
2511552f7358SJed Brown   Output Parameters:
2512552f7358SJed Brown + pStart - The first mesh point
2513552f7358SJed Brown - pEnd   - The upper bound for mesh points
2514552f7358SJed Brown 
2515552f7358SJed Brown   Level: beginner
2516552f7358SJed Brown 
2517552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
2518552f7358SJed Brown @*/
2519552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
2520552f7358SJed Brown {
2521552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2522552f7358SJed Brown   PetscErrorCode ierr;
2523552f7358SJed Brown 
2524552f7358SJed Brown   PetscFunctionBegin;
2525552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2526552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
2527552f7358SJed Brown   PetscFunctionReturn(0);
2528552f7358SJed Brown }
2529552f7358SJed Brown 
2530552f7358SJed Brown /*@
2531552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
2532552f7358SJed Brown 
2533552f7358SJed Brown   Not collective
2534552f7358SJed Brown 
2535552f7358SJed Brown   Input Parameters:
2536552f7358SJed Brown + mesh - The DMPlex
2537552f7358SJed Brown . pStart - The first mesh point
2538552f7358SJed Brown - pEnd   - The upper bound for mesh points
2539552f7358SJed Brown 
2540552f7358SJed Brown   Output Parameters:
2541552f7358SJed Brown 
2542552f7358SJed Brown   Level: beginner
2543552f7358SJed Brown 
2544552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
2545552f7358SJed Brown @*/
2546552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
2547552f7358SJed Brown {
2548552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2549552f7358SJed Brown   PetscErrorCode ierr;
2550552f7358SJed Brown 
2551552f7358SJed Brown   PetscFunctionBegin;
2552552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2553552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
2554552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
2555552f7358SJed Brown   PetscFunctionReturn(0);
2556552f7358SJed Brown }
2557552f7358SJed Brown 
2558552f7358SJed Brown /*@
2559eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
2560552f7358SJed Brown 
2561552f7358SJed Brown   Not collective
2562552f7358SJed Brown 
2563552f7358SJed Brown   Input Parameters:
2564552f7358SJed Brown + mesh - The DMPlex
2565eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2566552f7358SJed Brown 
2567552f7358SJed Brown   Output Parameter:
2568552f7358SJed Brown . size - The cone size for point p
2569552f7358SJed Brown 
2570552f7358SJed Brown   Level: beginner
2571552f7358SJed Brown 
2572552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2573552f7358SJed Brown @*/
2574552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
2575552f7358SJed Brown {
2576552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2577552f7358SJed Brown   PetscErrorCode ierr;
2578552f7358SJed Brown 
2579552f7358SJed Brown   PetscFunctionBegin;
2580552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2581552f7358SJed Brown   PetscValidPointer(size, 3);
2582552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
2583552f7358SJed Brown   PetscFunctionReturn(0);
2584552f7358SJed Brown }
2585552f7358SJed Brown 
2586552f7358SJed Brown /*@
2587eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
2588552f7358SJed Brown 
2589552f7358SJed Brown   Not collective
2590552f7358SJed Brown 
2591552f7358SJed Brown   Input Parameters:
2592552f7358SJed Brown + mesh - The DMPlex
2593eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2594552f7358SJed Brown - size - The cone size for point p
2595552f7358SJed Brown 
2596552f7358SJed Brown   Output Parameter:
2597552f7358SJed Brown 
2598552f7358SJed Brown   Note:
2599552f7358SJed Brown   This should be called after DMPlexSetChart().
2600552f7358SJed Brown 
2601552f7358SJed Brown   Level: beginner
2602552f7358SJed Brown 
2603552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
2604552f7358SJed Brown @*/
2605552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
2606552f7358SJed Brown {
2607552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2608552f7358SJed Brown   PetscErrorCode ierr;
2609552f7358SJed Brown 
2610552f7358SJed Brown   PetscFunctionBegin;
2611552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2612552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
26130d644c17SKarl Rupp 
2614552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
2615552f7358SJed Brown   PetscFunctionReturn(0);
2616552f7358SJed Brown }
2617552f7358SJed Brown 
2618f5a469b9SMatthew G. Knepley /*@
2619eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
2620f5a469b9SMatthew G. Knepley 
2621f5a469b9SMatthew G. Knepley   Not collective
2622f5a469b9SMatthew G. Knepley 
2623f5a469b9SMatthew G. Knepley   Input Parameters:
2624f5a469b9SMatthew G. Knepley + mesh - The DMPlex
2625eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2626f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
2627f5a469b9SMatthew G. Knepley 
2628f5a469b9SMatthew G. Knepley   Output Parameter:
2629f5a469b9SMatthew G. Knepley 
2630f5a469b9SMatthew G. Knepley   Note:
2631f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
2632f5a469b9SMatthew G. Knepley 
2633f5a469b9SMatthew G. Knepley   Level: beginner
2634f5a469b9SMatthew G. Knepley 
2635f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
2636f5a469b9SMatthew G. Knepley @*/
2637f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
2638f5a469b9SMatthew G. Knepley {
2639f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2640f5a469b9SMatthew G. Knepley   PetscInt       csize;
2641f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
2642f5a469b9SMatthew G. Knepley 
2643f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
2644f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2645f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
2646f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
2647f5a469b9SMatthew G. Knepley 
2648f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
2649f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
2650f5a469b9SMatthew G. Knepley }
2651f5a469b9SMatthew G. Knepley 
2652552f7358SJed Brown /*@C
2653eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
2654552f7358SJed Brown 
2655552f7358SJed Brown   Not collective
2656552f7358SJed Brown 
2657552f7358SJed Brown   Input Parameters:
2658833c876bSVaclav Hapla + dm - The DMPlex
2659eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2660552f7358SJed Brown 
2661552f7358SJed Brown   Output Parameter:
2662552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
2663552f7358SJed Brown 
2664552f7358SJed Brown   Level: beginner
2665552f7358SJed Brown 
26663813dfbdSMatthew G Knepley   Fortran Notes:
26673813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26683813dfbdSMatthew G Knepley   include petsc.h90 in your code.
2669922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
2670922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
26713813dfbdSMatthew G Knepley 
2672e45f02b0SMatthew G. Knepley .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
2673552f7358SJed Brown @*/
2674552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
2675552f7358SJed Brown {
2676552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2677552f7358SJed Brown   PetscInt       off;
2678552f7358SJed Brown   PetscErrorCode ierr;
2679552f7358SJed Brown 
2680552f7358SJed Brown   PetscFunctionBegin;
2681552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2682552f7358SJed Brown   PetscValidPointer(cone, 3);
2683552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2684552f7358SJed Brown   *cone = &mesh->cones[off];
2685552f7358SJed Brown   PetscFunctionReturn(0);
2686552f7358SJed Brown }
2687552f7358SJed Brown 
26880ce7577fSVaclav Hapla /*@C
26890ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
26900ce7577fSVaclav Hapla 
26910ce7577fSVaclav Hapla   Not collective
26920ce7577fSVaclav Hapla 
26930ce7577fSVaclav Hapla   Input Parameters:
26940ce7577fSVaclav Hapla + dm - The DMPlex
26950ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
26960ce7577fSVaclav Hapla 
2697d8d19677SJose E. Roman   Output Parameters:
26980ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
26990ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
27000ce7577fSVaclav Hapla 
27010ce7577fSVaclav Hapla   Level: intermediate
27020ce7577fSVaclav Hapla 
2703d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
27040ce7577fSVaclav Hapla @*/
27050ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
27060ce7577fSVaclav Hapla {
27070ce7577fSVaclav Hapla   PetscSection        cs, newcs;
27080ce7577fSVaclav Hapla   PetscInt            *cones;
27090ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
27100ce7577fSVaclav Hapla   PetscInt            n;
27110ce7577fSVaclav Hapla   PetscErrorCode      ierr;
27120ce7577fSVaclav Hapla 
27130ce7577fSVaclav Hapla   PetscFunctionBegin;
27140ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
27150ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
27160ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
27170ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
27180ce7577fSVaclav Hapla   if (pCones) {
27190ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
27200ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
27210ce7577fSVaclav Hapla   }
27220ce7577fSVaclav Hapla   PetscFunctionReturn(0);
27230ce7577fSVaclav Hapla }
27240ce7577fSVaclav Hapla 
2725af9eab45SVaclav Hapla /*@
2726af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
2727d4636a37SVaclav Hapla 
2728d4636a37SVaclav Hapla   Not collective
2729d4636a37SVaclav Hapla 
2730d4636a37SVaclav Hapla   Input Parameters:
2731d4636a37SVaclav Hapla + dm - The DMPlex
2732af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2733d4636a37SVaclav Hapla 
2734d4636a37SVaclav Hapla   Output Parameter:
2735af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
2736d4636a37SVaclav Hapla 
2737d4636a37SVaclav Hapla   Level: advanced
2738d4636a37SVaclav Hapla 
2739af9eab45SVaclav Hapla   Notes:
2740af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
2741af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
2742af9eab45SVaclav Hapla 
2743af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
2744d4636a37SVaclav Hapla @*/
2745af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2746d4636a37SVaclav Hapla {
2747af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
2748af9eab45SVaclav Hapla   PetscInt            depth;
2749d4636a37SVaclav Hapla   PetscErrorCode      ierr;
2750d4636a37SVaclav Hapla 
2751d4636a37SVaclav Hapla   PetscFunctionBegin;
2752af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2753af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2754af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
2755af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2756af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
27571e1ea65dSPierre Jolivet   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);CHKERRQ(ierr);
2758af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
2759af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2760af9eab45SVaclav Hapla }
2761af9eab45SVaclav Hapla 
2762af9eab45SVaclav Hapla /*@
2763af9eab45SVaclav 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).
2764af9eab45SVaclav Hapla 
2765af9eab45SVaclav Hapla   Not collective
2766af9eab45SVaclav Hapla 
2767af9eab45SVaclav Hapla   Input Parameters:
2768af9eab45SVaclav Hapla + dm - The DMPlex
2769af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2770af9eab45SVaclav Hapla 
2771d8d19677SJose E. Roman   Output Parameters:
2772af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2773af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
2774af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2775af9eab45SVaclav Hapla 
2776af9eab45SVaclav Hapla   Level: advanced
2777af9eab45SVaclav Hapla 
2778af9eab45SVaclav Hapla   Notes:
2779af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
2780af9eab45SVaclav Hapla 
2781af9eab45SVaclav 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.
2782af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
2783af9eab45SVaclav Hapla 
2784af9eab45SVaclav 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:
2785af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
2786af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
2787af9eab45SVaclav Hapla 
2788af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2789af9eab45SVaclav Hapla @*/
2790af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2791af9eab45SVaclav Hapla {
2792af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
2793af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
2794af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
2795af9eab45SVaclav Hapla   IS                  *expandedPoints_;
2796af9eab45SVaclav Hapla   PetscSection        *sections_;
2797af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2798af9eab45SVaclav Hapla 
2799af9eab45SVaclav Hapla   PetscFunctionBegin;
2800af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2801af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
2802af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
2803af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
2804af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
2805af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
2806af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
2807af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
2808af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
2809af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
2810af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
2811af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
2812af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
2813af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
2814af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2815af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
2816af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
2817af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
2818af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
2819af9eab45SVaclav Hapla       } else {
2820af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
2821af9eab45SVaclav Hapla       }
2822af9eab45SVaclav Hapla     }
2823af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
2824af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
2825af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
2826af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
2827af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
2828af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
2829af9eab45SVaclav Hapla       if (cn > 1) {
2830af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
2831af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
2832af9eab45SVaclav Hapla       } else {
2833af9eab45SVaclav Hapla         newarr[co] = arr[i];
2834af9eab45SVaclav Hapla       }
2835af9eab45SVaclav Hapla     }
2836af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
2837af9eab45SVaclav Hapla     arr = newarr;
2838af9eab45SVaclav Hapla     n = newn;
2839af9eab45SVaclav Hapla   }
2840ba2698f1SMatthew G. Knepley   ierr = ISRestoreIndices(points, &arr0);CHKERRQ(ierr);
2841af9eab45SVaclav Hapla   *depth = depth_;
2842af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
2843af9eab45SVaclav Hapla   else {
2844af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
2845af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
2846af9eab45SVaclav Hapla   }
2847af9eab45SVaclav Hapla   if (sections) *sections = sections_;
2848af9eab45SVaclav Hapla   else {
2849af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
2850af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
2851af9eab45SVaclav Hapla   }
2852af9eab45SVaclav Hapla   PetscFunctionReturn(0);
2853af9eab45SVaclav Hapla }
2854af9eab45SVaclav Hapla 
2855af9eab45SVaclav Hapla /*@
2856af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
2857af9eab45SVaclav Hapla 
2858af9eab45SVaclav Hapla   Not collective
2859af9eab45SVaclav Hapla 
2860af9eab45SVaclav Hapla   Input Parameters:
2861af9eab45SVaclav Hapla + dm - The DMPlex
2862af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
2863af9eab45SVaclav Hapla 
2864d8d19677SJose E. Roman   Output Parameters:
2865af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2866af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
2867af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
2868af9eab45SVaclav Hapla 
2869af9eab45SVaclav Hapla   Level: advanced
2870af9eab45SVaclav Hapla 
2871af9eab45SVaclav Hapla   Notes:
2872af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
2873af9eab45SVaclav Hapla 
2874af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2875af9eab45SVaclav Hapla @*/
2876af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2877af9eab45SVaclav Hapla {
2878af9eab45SVaclav Hapla   PetscInt            d, depth_;
2879af9eab45SVaclav Hapla   PetscErrorCode      ierr;
2880af9eab45SVaclav Hapla 
2881af9eab45SVaclav Hapla   PetscFunctionBegin;
2882af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
28832c71b3e2SJacob Faibussowitsch   PetscCheckFalse(depth && *depth != depth_,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
2884af9eab45SVaclav Hapla   if (depth) *depth = 0;
2885af9eab45SVaclav Hapla   if (expandedPoints) {
2886af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
2887af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
2888af9eab45SVaclav Hapla   }
2889af9eab45SVaclav Hapla   if (sections)  {
2890af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
2891af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
2892af9eab45SVaclav Hapla   }
2893d4636a37SVaclav Hapla   PetscFunctionReturn(0);
2894d4636a37SVaclav Hapla }
2895d4636a37SVaclav Hapla 
2896552f7358SJed Brown /*@
289792371b87SBarry 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
2898552f7358SJed Brown 
2899552f7358SJed Brown   Not collective
2900552f7358SJed Brown 
2901552f7358SJed Brown   Input Parameters:
2902552f7358SJed Brown + mesh - The DMPlex
2903eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2904552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
2905552f7358SJed Brown 
2906552f7358SJed Brown   Output Parameter:
2907552f7358SJed Brown 
2908552f7358SJed Brown   Note:
2909552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2910552f7358SJed Brown 
2911552f7358SJed Brown   Level: beginner
2912552f7358SJed Brown 
291392371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
2914552f7358SJed Brown @*/
2915552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
2916552f7358SJed Brown {
2917552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2918552f7358SJed Brown   PetscInt       pStart, pEnd;
2919552f7358SJed Brown   PetscInt       dof, off, c;
2920552f7358SJed Brown   PetscErrorCode ierr;
2921552f7358SJed Brown 
2922552f7358SJed Brown   PetscFunctionBegin;
2923552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2924552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2925552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2926552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
2927552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
29282c71b3e2SJacob Faibussowitsch   PetscCheckFalse((p < pStart) || (p >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
2929552f7358SJed Brown   for (c = 0; c < dof; ++c) {
29302c71b3e2SJacob Faibussowitsch     PetscCheckFalse((cone[c] < pStart) || (cone[c] >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", cone[c], pStart, pEnd);
2931552f7358SJed Brown     mesh->cones[off+c] = cone[c];
2932552f7358SJed Brown   }
2933552f7358SJed Brown   PetscFunctionReturn(0);
2934552f7358SJed Brown }
2935552f7358SJed Brown 
2936552f7358SJed Brown /*@C
2937eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
2938552f7358SJed Brown 
2939552f7358SJed Brown   Not collective
2940552f7358SJed Brown 
2941552f7358SJed Brown   Input Parameters:
2942552f7358SJed Brown + mesh - The DMPlex
2943eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2944552f7358SJed Brown 
2945552f7358SJed Brown   Output Parameter:
2946552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
2947b5a892a1SMatthew G. Knepley                     integer giving the prescription for cone traversal.
2948552f7358SJed Brown 
2949552f7358SJed Brown   Level: beginner
2950552f7358SJed Brown 
2951b5a892a1SMatthew G. Knepley   Notes:
2952b5a892a1SMatthew G. Knepley   The number indexes the symmetry transformations for the cell type (see manual). Orientation 0 is always
2953b5a892a1SMatthew G. Knepley   the identity transformation. Negative orientation indicates reflection so that -(o+1) is the reflection
2954b5a892a1SMatthew G. Knepley   of o, however it is not necessarily the inverse. To get the inverse, use DMPolytopeTypeComposeOrientationInv()
2955b5a892a1SMatthew G. Knepley   with the identity.
2956b5a892a1SMatthew G. Knepley 
29573813dfbdSMatthew G Knepley   Fortran Notes:
29583813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29593813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29603b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2961922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
29623813dfbdSMatthew G Knepley 
2963b5a892a1SMatthew G. Knepley .seealso: DMPolytopeTypeComposeOrientation(), DMPolytopeTypeComposeOrientationInv(), DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2964552f7358SJed Brown @*/
2965552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2966552f7358SJed Brown {
2967552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2968552f7358SJed Brown   PetscInt       off;
2969552f7358SJed Brown   PetscErrorCode ierr;
2970552f7358SJed Brown 
2971552f7358SJed Brown   PetscFunctionBegin;
2972552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
297376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2974552f7358SJed Brown     PetscInt dof;
2975552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2976552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
2977552f7358SJed Brown   }
2978552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
29790d644c17SKarl Rupp 
2980552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
2981552f7358SJed Brown   PetscFunctionReturn(0);
2982552f7358SJed Brown }
2983552f7358SJed Brown 
2984552f7358SJed Brown /*@
2985eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
2986552f7358SJed Brown 
2987552f7358SJed Brown   Not collective
2988552f7358SJed Brown 
2989552f7358SJed Brown   Input Parameters:
2990552f7358SJed Brown + mesh - The DMPlex
2991eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2992b5a892a1SMatthew G. Knepley - coneOrientation - An array of orientations
2993552f7358SJed Brown   Output Parameter:
2994552f7358SJed Brown 
2995b5a892a1SMatthew G. Knepley   Notes:
2996552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2997552f7358SJed Brown 
2998b5a892a1SMatthew G. Knepley   The meaning of coneOrientation is detailed in DMPlexGetConeOrientation().
2999b5a892a1SMatthew G. Knepley 
3000552f7358SJed Brown   Level: beginner
3001552f7358SJed Brown 
3002552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
3003552f7358SJed Brown @*/
3004552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
3005552f7358SJed Brown {
3006552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3007552f7358SJed Brown   PetscInt       pStart, pEnd;
3008552f7358SJed Brown   PetscInt       dof, off, c;
3009552f7358SJed Brown   PetscErrorCode ierr;
3010552f7358SJed Brown 
3011552f7358SJed Brown   PetscFunctionBegin;
3012552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3013552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
3014552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3015552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
3016552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
30172c71b3e2SJacob Faibussowitsch   PetscCheckFalse((p < pStart) || (p >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
3018552f7358SJed Brown   for (c = 0; c < dof; ++c) {
3019552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
3020552f7358SJed Brown 
3021552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
30222c71b3e2SJacob Faibussowitsch     PetscCheckFalse(o && ((o < -(cdof+1)) || (o >= cdof)),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %D is not in the valid range [%D. %D)", o, -(cdof+1), cdof);
3023552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
3024552f7358SJed Brown   }
3025552f7358SJed Brown   PetscFunctionReturn(0);
3026552f7358SJed Brown }
3027552f7358SJed Brown 
30287cd05799SMatthew G. Knepley /*@
3029eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
30307cd05799SMatthew G. Knepley 
30317cd05799SMatthew G. Knepley   Not collective
30327cd05799SMatthew G. Knepley 
30337cd05799SMatthew G. Knepley   Input Parameters:
30347cd05799SMatthew G. Knepley + mesh - The DMPlex
3035eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
30367cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
30377cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
30387cd05799SMatthew G. Knepley 
30397cd05799SMatthew G. Knepley   Level: beginner
30407cd05799SMatthew G. Knepley 
30417cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
30427cd05799SMatthew G. Knepley @*/
3043552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
3044552f7358SJed Brown {
3045552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3046552f7358SJed Brown   PetscInt       pStart, pEnd;
3047552f7358SJed Brown   PetscInt       dof, off;
3048552f7358SJed Brown   PetscErrorCode ierr;
3049552f7358SJed Brown 
3050552f7358SJed Brown   PetscFunctionBegin;
3051552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3052552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
30532c71b3e2SJacob Faibussowitsch   PetscCheckFalse((p < pStart) || (p >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
30542c71b3e2SJacob Faibussowitsch   PetscCheckFalse((conePoint < pStart) || (conePoint >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", conePoint, pStart, pEnd);
305577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
305677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
30572c71b3e2SJacob Faibussowitsch   PetscCheckFalse((conePos < 0) || (conePos >= dof),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
3058552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
3059552f7358SJed Brown   PetscFunctionReturn(0);
3060552f7358SJed Brown }
3061552f7358SJed Brown 
30627cd05799SMatthew G. Knepley /*@
3063eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
30647cd05799SMatthew G. Knepley 
30657cd05799SMatthew G. Knepley   Not collective
30667cd05799SMatthew G. Knepley 
30677cd05799SMatthew G. Knepley   Input Parameters:
30687cd05799SMatthew G. Knepley + mesh - The DMPlex
3069eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
30707cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
30717cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
30727cd05799SMatthew G. Knepley 
30737cd05799SMatthew G. Knepley   Level: beginner
30747cd05799SMatthew G. Knepley 
3075b5a892a1SMatthew G. Knepley   Notes:
3076b5a892a1SMatthew G. Knepley   The meaning of coneOrientation values is detailed in DMPlexGetConeOrientation().
3077b5a892a1SMatthew G. Knepley 
30787cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
30797cd05799SMatthew G. Knepley @*/
308077c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
308177c88f5bSMatthew G Knepley {
308277c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
308377c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
308477c88f5bSMatthew G Knepley   PetscInt       dof, off;
308577c88f5bSMatthew G Knepley   PetscErrorCode ierr;
308677c88f5bSMatthew G Knepley 
308777c88f5bSMatthew G Knepley   PetscFunctionBegin;
308877c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
308977c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
30902c71b3e2SJacob Faibussowitsch   PetscCheckFalse((p < pStart) || (p >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
309177c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
309277c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
30932c71b3e2SJacob Faibussowitsch   PetscCheckFalse((conePos < 0) || (conePos >= dof),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
309477c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
309577c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
309677c88f5bSMatthew G Knepley }
309777c88f5bSMatthew G Knepley 
3098552f7358SJed Brown /*@
3099eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
3100552f7358SJed Brown 
3101552f7358SJed Brown   Not collective
3102552f7358SJed Brown 
3103552f7358SJed Brown   Input Parameters:
3104552f7358SJed Brown + mesh - The DMPlex
3105eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
3106552f7358SJed Brown 
3107552f7358SJed Brown   Output Parameter:
3108552f7358SJed Brown . size - The support size for point p
3109552f7358SJed Brown 
3110552f7358SJed Brown   Level: beginner
3111552f7358SJed Brown 
3112552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
3113552f7358SJed Brown @*/
3114552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
3115552f7358SJed Brown {
3116552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3117552f7358SJed Brown   PetscErrorCode ierr;
3118552f7358SJed Brown 
3119552f7358SJed Brown   PetscFunctionBegin;
3120552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3121552f7358SJed Brown   PetscValidPointer(size, 3);
3122552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
3123552f7358SJed Brown   PetscFunctionReturn(0);
3124552f7358SJed Brown }
3125552f7358SJed Brown 
3126552f7358SJed Brown /*@
3127eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
3128552f7358SJed Brown 
3129552f7358SJed Brown   Not collective
3130552f7358SJed Brown 
3131552f7358SJed Brown   Input Parameters:
3132552f7358SJed Brown + mesh - The DMPlex
3133eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
3134552f7358SJed Brown - size - The support size for point p
3135552f7358SJed Brown 
3136552f7358SJed Brown   Output Parameter:
3137552f7358SJed Brown 
3138552f7358SJed Brown   Note:
3139552f7358SJed Brown   This should be called after DMPlexSetChart().
3140552f7358SJed Brown 
3141552f7358SJed Brown   Level: beginner
3142552f7358SJed Brown 
3143552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
3144552f7358SJed Brown @*/
3145552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
3146552f7358SJed Brown {
3147552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3148552f7358SJed Brown   PetscErrorCode ierr;
3149552f7358SJed Brown 
3150552f7358SJed Brown   PetscFunctionBegin;
3151552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3152552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
31530d644c17SKarl Rupp 
3154552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
3155552f7358SJed Brown   PetscFunctionReturn(0);
3156552f7358SJed Brown }
3157552f7358SJed Brown 
3158552f7358SJed Brown /*@C
3159eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
3160552f7358SJed Brown 
3161552f7358SJed Brown   Not collective
3162552f7358SJed Brown 
3163552f7358SJed Brown   Input Parameters:
3164552f7358SJed Brown + mesh - The DMPlex
3165eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
3166552f7358SJed Brown 
3167552f7358SJed Brown   Output Parameter:
3168552f7358SJed Brown . support - An array of points which are on the out-edges for point p
3169552f7358SJed Brown 
3170552f7358SJed Brown   Level: beginner
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.
31753b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
3176922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
31773813dfbdSMatthew G Knepley 
3178e45f02b0SMatthew G. Knepley .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
3179552f7358SJed Brown @*/
3180552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
3181552f7358SJed Brown {
3182552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3183552f7358SJed Brown   PetscInt       off;
3184552f7358SJed Brown   PetscErrorCode ierr;
3185552f7358SJed Brown 
3186552f7358SJed Brown   PetscFunctionBegin;
3187552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3188552f7358SJed Brown   PetscValidPointer(support, 3);
3189552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
3190552f7358SJed Brown   *support = &mesh->supports[off];
3191552f7358SJed Brown   PetscFunctionReturn(0);
3192552f7358SJed Brown }
3193552f7358SJed Brown 
3194552f7358SJed Brown /*@
319592371b87SBarry 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
3196552f7358SJed Brown 
3197552f7358SJed Brown   Not collective
3198552f7358SJed Brown 
3199552f7358SJed Brown   Input Parameters:
3200552f7358SJed Brown + mesh - The DMPlex
3201eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
320292371b87SBarry Smith - support - An array of points which are on the out-edges for point p
3203552f7358SJed Brown 
3204552f7358SJed Brown   Output Parameter:
3205552f7358SJed Brown 
3206552f7358SJed Brown   Note:
3207552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
3208552f7358SJed Brown 
3209552f7358SJed Brown   Level: beginner
3210552f7358SJed Brown 
321192371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
3212552f7358SJed Brown @*/
3213552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
3214552f7358SJed Brown {
3215552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3216552f7358SJed Brown   PetscInt       pStart, pEnd;
3217552f7358SJed Brown   PetscInt       dof, off, c;
3218552f7358SJed Brown   PetscErrorCode ierr;
3219552f7358SJed Brown 
3220552f7358SJed Brown   PetscFunctionBegin;
3221552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3222552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
3223552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
3224552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
3225552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
32262c71b3e2SJacob Faibussowitsch   PetscCheckFalse((p < pStart) || (p >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
3227552f7358SJed Brown   for (c = 0; c < dof; ++c) {
32282c71b3e2SJacob Faibussowitsch     PetscCheckFalse((support[c] < pStart) || (support[c] >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", support[c], pStart, pEnd);
3229552f7358SJed Brown     mesh->supports[off+c] = support[c];
3230552f7358SJed Brown   }
3231552f7358SJed Brown   PetscFunctionReturn(0);
3232552f7358SJed Brown }
3233552f7358SJed Brown 
32347cd05799SMatthew G. Knepley /*@
3235eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
32367cd05799SMatthew G. Knepley 
32377cd05799SMatthew G. Knepley   Not collective
32387cd05799SMatthew G. Knepley 
32397cd05799SMatthew G. Knepley   Input Parameters:
32407cd05799SMatthew G. Knepley + mesh - The DMPlex
3241eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
32427cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
32437cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
32447cd05799SMatthew G. Knepley 
32457cd05799SMatthew G. Knepley   Level: beginner
32467cd05799SMatthew G. Knepley 
32477cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
32487cd05799SMatthew G. Knepley @*/
3249552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
3250552f7358SJed Brown {
3251552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3252552f7358SJed Brown   PetscInt       pStart, pEnd;
3253552f7358SJed Brown   PetscInt       dof, off;
3254552f7358SJed Brown   PetscErrorCode ierr;
3255552f7358SJed Brown 
3256552f7358SJed Brown   PetscFunctionBegin;
3257552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3258552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
3259552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
3260552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
32612c71b3e2SJacob Faibussowitsch   PetscCheckFalse((p < pStart) || (p >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
32622c71b3e2SJacob Faibussowitsch   PetscCheckFalse((supportPoint < pStart) || (supportPoint >= pEnd),PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", supportPoint, pStart, pEnd);
32632c71b3e2SJacob Faibussowitsch   PetscCheckFalse(supportPos >= dof,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %D of point %D is not in the valid range [0, %D)", supportPos, p, dof);
3264552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
3265552f7358SJed Brown   PetscFunctionReturn(0);
3266552f7358SJed Brown }
3267552f7358SJed Brown 
3268b5a892a1SMatthew G. Knepley /* Converts an orientation o in the current numbering to the previous scheme used in Plex */
3269b5a892a1SMatthew G. Knepley PetscInt DMPolytopeConvertNewOrientation_Internal(DMPolytopeType ct, PetscInt o)
3270b5a892a1SMatthew G. Knepley {
3271b5a892a1SMatthew G. Knepley   switch (ct) {
3272b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
3273b5a892a1SMatthew G. Knepley       if (o == -1) return -2;
3274b5a892a1SMatthew G. Knepley       break;
3275b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
3276b5a892a1SMatthew G. Knepley       if (o == -3) return -1;
3277b5a892a1SMatthew G. Knepley       if (o == -2) return -3;
3278b5a892a1SMatthew G. Knepley       if (o == -1) return -2;
3279b5a892a1SMatthew G. Knepley       break;
3280b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
3281b5a892a1SMatthew G. Knepley       if (o == -4) return -2;
3282b5a892a1SMatthew G. Knepley       if (o == -3) return -1;
3283b5a892a1SMatthew G. Knepley       if (o == -2) return -4;
3284b5a892a1SMatthew G. Knepley       if (o == -1) return -3;
3285b5a892a1SMatthew G. Knepley       break;
3286b5a892a1SMatthew G. Knepley     default: return o;
3287b5a892a1SMatthew G. Knepley   }
3288b5a892a1SMatthew G. Knepley   return o;
3289b5a892a1SMatthew G. Knepley }
3290b5a892a1SMatthew G. Knepley 
3291b5a892a1SMatthew G. Knepley /* Converts an orientation o in the previous scheme used in Plex to the current numbering */
3292b5a892a1SMatthew G. Knepley PetscInt DMPolytopeConvertOldOrientation_Internal(DMPolytopeType ct, PetscInt o)
3293b5a892a1SMatthew G. Knepley {
3294b5a892a1SMatthew G. Knepley   switch (ct) {
3295b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
3296b5a892a1SMatthew G. Knepley       if ((o == -2) || (o == 1)) return -1;
3297b5a892a1SMatthew G. Knepley       if (o == -1) return 0;
3298b5a892a1SMatthew G. Knepley       break;
3299b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
3300b5a892a1SMatthew G. Knepley       if (o == -3) return -2;
3301b5a892a1SMatthew G. Knepley       if (o == -2) return -1;
3302b5a892a1SMatthew G. Knepley       if (o == -1) return -3;
3303b5a892a1SMatthew G. Knepley       break;
3304b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
3305b5a892a1SMatthew G. Knepley       if (o == -4) return -2;
3306b5a892a1SMatthew G. Knepley       if (o == -3) return -1;
3307b5a892a1SMatthew G. Knepley       if (o == -2) return -4;
3308b5a892a1SMatthew G. Knepley       if (o == -1) return -3;
3309b5a892a1SMatthew G. Knepley       break;
3310b5a892a1SMatthew G. Knepley     default: return o;
3311b5a892a1SMatthew G. Knepley   }
3312b5a892a1SMatthew G. Knepley   return o;
3313b5a892a1SMatthew G. Knepley }
3314b5a892a1SMatthew G. Knepley 
3315b5a892a1SMatthew G. Knepley /* Takes in a mesh whose orientations are in the previous scheme and converts them all to the current numbering */
3316b5a892a1SMatthew G. Knepley PetscErrorCode DMPlexConvertOldOrientations_Internal(DM dm)
3317b5a892a1SMatthew G. Knepley {
3318b5a892a1SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
3319b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
3320b5a892a1SMatthew G. Knepley 
3321b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
3322b5a892a1SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3323b5a892a1SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3324b5a892a1SMatthew G. Knepley     const PetscInt *cone, *ornt;
3325b5a892a1SMatthew G. Knepley     PetscInt        coneSize, c;
3326b5a892a1SMatthew G. Knepley 
3327b5a892a1SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3328b5a892a1SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
3329b5a892a1SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &ornt);CHKERRQ(ierr);
3330b5a892a1SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
3331b5a892a1SMatthew G. Knepley       DMPolytopeType ct;
3332b5a892a1SMatthew G. Knepley       const PetscInt o = ornt[c];
3333b5a892a1SMatthew G. Knepley 
3334b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cone[c], &ct);CHKERRQ(ierr);
3335b5a892a1SMatthew G. Knepley       switch (ct) {
3336b5a892a1SMatthew G. Knepley         case DM_POLYTOPE_SEGMENT:
3337b5a892a1SMatthew G. Knepley           if ((o == -2) || (o == 1)) {ierr = DMPlexInsertConeOrientation(dm, p, c, -1);CHKERRQ(ierr);}
3338b5a892a1SMatthew G. Knepley           if (o == -1) {ierr = DMPlexInsertConeOrientation(dm, p, c, 0);CHKERRQ(ierr);}
3339b5a892a1SMatthew G. Knepley           break;
3340b5a892a1SMatthew G. Knepley         case DM_POLYTOPE_TRIANGLE:
3341b5a892a1SMatthew G. Knepley           if (o == -3) {ierr = DMPlexInsertConeOrientation(dm, p, c, -2);CHKERRQ(ierr);}
3342b5a892a1SMatthew G. Knepley           if (o == -2) {ierr = DMPlexInsertConeOrientation(dm, p, c, -1);CHKERRQ(ierr);}
3343b5a892a1SMatthew G. Knepley           if (o == -1) {ierr = DMPlexInsertConeOrientation(dm, p, c, -3);CHKERRQ(ierr);}
3344b5a892a1SMatthew G. Knepley           break;
3345b5a892a1SMatthew G. Knepley         case DM_POLYTOPE_QUADRILATERAL:
3346b5a892a1SMatthew G. Knepley           if (o == -4) {ierr = DMPlexInsertConeOrientation(dm, p, c, -2);CHKERRQ(ierr);}
3347b5a892a1SMatthew G. Knepley           if (o == -3) {ierr = DMPlexInsertConeOrientation(dm, p, c, -1);CHKERRQ(ierr);}
3348b5a892a1SMatthew G. Knepley           if (o == -2) {ierr = DMPlexInsertConeOrientation(dm, p, c, -4);CHKERRQ(ierr);}
3349b5a892a1SMatthew G. Knepley           if (o == -1) {ierr = DMPlexInsertConeOrientation(dm, p, c, -3);CHKERRQ(ierr);}
3350b5a892a1SMatthew G. Knepley           break;
3351b5a892a1SMatthew G. Knepley         default: break;
3352b5a892a1SMatthew G. Knepley       }
3353b5a892a1SMatthew G. Knepley     }
3354b5a892a1SMatthew G. Knepley   }
3355b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
3356b5a892a1SMatthew G. Knepley }
3357b5a892a1SMatthew G. Knepley 
3358012bc364SMatthew G. Knepley static PetscErrorCode DMPlexGetTransitiveClosure_Depth1_Private(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3359b5a892a1SMatthew G. Knepley {
3360b5a892a1SMatthew G. Knepley   DMPolytopeType  ct = DM_POLYTOPE_UNKNOWN;
3361b5a892a1SMatthew G. Knepley   PetscInt       *closure;
3362b5a892a1SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
3363b5a892a1SMatthew G. Knepley   PetscInt        off = 0, tmpSize, t;
3364b5a892a1SMatthew G. Knepley   PetscErrorCode  ierr;
3365b5a892a1SMatthew G. Knepley 
3366b5a892a1SMatthew G. Knepley   PetscFunctionBeginHot;
3367b5a892a1SMatthew G. Knepley   if (ornt) {
3368b5a892a1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
3369b5a892a1SMatthew G. Knepley     if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3370b5a892a1SMatthew G. Knepley   }
3371b5a892a1SMatthew G. Knepley   if (*points) {
3372b5a892a1SMatthew G. Knepley     closure = *points;
3373b5a892a1SMatthew G. Knepley   } else {
3374b5a892a1SMatthew G. Knepley     PetscInt maxConeSize, maxSupportSize;
3375b5a892a1SMatthew G. Knepley     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
3376b5a892a1SMatthew G. Knepley     ierr = DMGetWorkArray(dm, 2*(PetscMax(maxConeSize, maxSupportSize)+1), MPIU_INT, &closure);CHKERRQ(ierr);
3377b5a892a1SMatthew G. Knepley   }
3378b5a892a1SMatthew G. Knepley   if (useCone) {
3379b5a892a1SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
3380b5a892a1SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
3381b5a892a1SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
3382b5a892a1SMatthew G. Knepley   } else {
3383b5a892a1SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
3384b5a892a1SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
3385b5a892a1SMatthew G. Knepley   }
3386b5a892a1SMatthew G. Knepley   if (ct == DM_POLYTOPE_UNKNOWN) {
3387b5a892a1SMatthew G. Knepley     closure[off++] = p;
3388b5a892a1SMatthew G. Knepley     closure[off++] = 0;
3389b5a892a1SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
3390b5a892a1SMatthew G. Knepley       closure[off++] = tmp[t];
3391b5a892a1SMatthew G. Knepley       closure[off++] = tmpO ? tmpO[t] : 0;
3392b5a892a1SMatthew G. Knepley     }
3393b5a892a1SMatthew G. Knepley   } else {
3394b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, ornt);CHKERRQ(ierr);
3395b5a892a1SMatthew G. Knepley 
3396b5a892a1SMatthew G. Knepley     /* We assume that cells with a valid type have faces with a valid type */
3397b5a892a1SMatthew G. Knepley     closure[off++] = p;
3398b5a892a1SMatthew G. Knepley     closure[off++] = ornt;
3399b5a892a1SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
3400b5a892a1SMatthew G. Knepley       DMPolytopeType ft;
3401b5a892a1SMatthew G. Knepley 
3402b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, tmp[t], &ft);CHKERRQ(ierr);
3403b5a892a1SMatthew G. Knepley       closure[off++] = tmp[arr[t]];
3404b5a892a1SMatthew G. Knepley       closure[off++] = tmpO ? DMPolytopeTypeComposeOrientation(ft, ornt, tmpO[t]) : 0;
3405b5a892a1SMatthew G. Knepley     }
3406b5a892a1SMatthew G. Knepley   }
3407b5a892a1SMatthew G. Knepley   if (numPoints) *numPoints = tmpSize+1;
3408b5a892a1SMatthew G. Knepley   if (points)    *points    = closure;
3409b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
3410b5a892a1SMatthew G. Knepley }
3411b5a892a1SMatthew G. Knepley 
3412b5a892a1SMatthew G. Knepley /* We need a special tensor verison becasue we want to allow duplicate points in the endcaps for hybrid cells */
3413b5a892a1SMatthew G. Knepley static PetscErrorCode DMPlexTransitiveClosure_Tensor_Internal(DM dm, PetscInt point, DMPolytopeType ct, PetscInt o, PetscBool useCone, PetscInt *numPoints, PetscInt **points)
3414b5a892a1SMatthew G. Knepley {
3415b5a892a1SMatthew G. Knepley   const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
3416b5a892a1SMatthew G. Knepley   const PetscInt *cone, *ornt;
3417b5a892a1SMatthew G. Knepley   PetscInt       *pts,  *closure = NULL;
3418b5a892a1SMatthew G. Knepley   DMPolytopeType  ft;
3419b5a892a1SMatthew G. Knepley   PetscInt        maxConeSize, maxSupportSize, coneSeries, supportSeries, maxSize;
3420b5a892a1SMatthew G. Knepley   PetscInt        dim, coneSize, c, d, clSize, cl;
3421b5a892a1SMatthew G. Knepley   PetscErrorCode  ierr;
3422b5a892a1SMatthew G. Knepley 
3423b5a892a1SMatthew G. Knepley   PetscFunctionBeginHot;
3424b5a892a1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3425b5a892a1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3426b5a892a1SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3427b5a892a1SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3428b5a892a1SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
3429b5a892a1SMatthew G. Knepley   coneSeries    = (maxConeSize    > 1) ? ((PetscPowInt(maxConeSize,    dim+1)-1)/(maxConeSize-1))    : dim+1;
3430b5a892a1SMatthew G. Knepley   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, dim+1)-1)/(maxSupportSize-1)) : dim+1;
3431b5a892a1SMatthew G. Knepley   maxSize       = PetscMax(coneSeries, supportSeries);
3432b5a892a1SMatthew G. Knepley   if (*points) {pts  = *points;}
3433b5a892a1SMatthew G. Knepley   else         {ierr = DMGetWorkArray(dm, 2*maxSize, MPIU_INT, &pts);CHKERRQ(ierr);}
3434b5a892a1SMatthew G. Knepley   c    = 0;
3435b5a892a1SMatthew G. Knepley   pts[c++] = point;
3436b5a892a1SMatthew G. Knepley   pts[c++] = o;
3437b5a892a1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cone[arr[0*2+0]], &ft);CHKERRQ(ierr);
3438b5a892a1SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[arr[0*2+0]], DMPolytopeTypeComposeOrientation(ft, arr[0*2+1], ornt[0]), useCone, &clSize, &closure);CHKERRQ(ierr);
3439b5a892a1SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2) {pts[c++] = closure[cl]; pts[c++] = closure[cl+1];}
3440b5a892a1SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[arr[1*2+0]], DMPolytopeTypeComposeOrientation(ft, arr[1*2+1], ornt[1]), useCone, &clSize, &closure);CHKERRQ(ierr);
3441b5a892a1SMatthew G. Knepley   for (cl = 0; cl < clSize*2; cl += 2) {pts[c++] = closure[cl]; pts[c++] = closure[cl+1];}
3442b5a892a1SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], useCone, &clSize, &closure);CHKERRQ(ierr);
3443b5a892a1SMatthew G. Knepley   for (d = 2; d < coneSize; ++d) {
3444b5a892a1SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cone[arr[d*2+0]], &ft);CHKERRQ(ierr);
3445b5a892a1SMatthew G. Knepley     pts[c++] = cone[arr[d*2+0]];
3446b5a892a1SMatthew G. Knepley     pts[c++] = DMPolytopeTypeComposeOrientation(ft, arr[d*2+1], ornt[d]);
3447b5a892a1SMatthew G. Knepley   }
3448b5a892a1SMatthew G. Knepley   if (dim >= 3) {
3449b5a892a1SMatthew G. Knepley     for (d = 2; d < coneSize; ++d) {
3450b5a892a1SMatthew G. Knepley       const PetscInt  fpoint = cone[arr[d*2+0]];
3451b5a892a1SMatthew G. Knepley       const PetscInt *fcone, *fornt;
3452b5a892a1SMatthew G. Knepley       PetscInt        fconeSize, fc, i;
3453b5a892a1SMatthew G. Knepley 
3454b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, fpoint, &ft);CHKERRQ(ierr);
3455b5a892a1SMatthew G. Knepley       const PetscInt *farr = DMPolytopeTypeGetArrangment(ft, DMPolytopeTypeComposeOrientation(ft, arr[d*2+1], ornt[d]));
3456b5a892a1SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, fpoint, &fconeSize);CHKERRQ(ierr);
3457b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCone(dm, fpoint, &fcone);CHKERRQ(ierr);
3458b5a892a1SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, fpoint, &fornt);CHKERRQ(ierr);
3459b5a892a1SMatthew G. Knepley       for (fc = 0; fc < fconeSize; ++fc) {
3460b5a892a1SMatthew G. Knepley         const PetscInt cp = fcone[farr[fc*2+0]];
3461b5a892a1SMatthew G. Knepley         const PetscInt co = farr[fc*2+1];
3462b5a892a1SMatthew G. Knepley 
3463b5a892a1SMatthew G. Knepley         for (i = 0; i < c; i += 2) if (pts[i] == cp) break;
3464b5a892a1SMatthew G. Knepley         if (i == c) {
3465b5a892a1SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, cp, &ft);CHKERRQ(ierr);
3466b5a892a1SMatthew G. Knepley           pts[c++] = cp;
3467b5a892a1SMatthew G. Knepley           pts[c++] = DMPolytopeTypeComposeOrientation(ft, co, fornt[farr[fc*2+0]]);
3468b5a892a1SMatthew G. Knepley         }
3469b5a892a1SMatthew G. Knepley       }
3470b5a892a1SMatthew G. Knepley     }
3471b5a892a1SMatthew G. Knepley   }
3472b5a892a1SMatthew G. Knepley   *numPoints = c/2;
3473b5a892a1SMatthew G. Knepley   *points    = pts;
3474b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
3475b5a892a1SMatthew G. Knepley }
3476b5a892a1SMatthew G. Knepley 
3477b5a892a1SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3478b5a892a1SMatthew G. Knepley {
3479b5a892a1SMatthew G. Knepley   DMPolytopeType ct;
3480b5a892a1SMatthew G. Knepley   PetscInt      *closure, *fifo;
3481b5a892a1SMatthew G. Knepley   PetscInt       closureSize = 0, fifoStart = 0, fifoSize = 0;
3482b5a892a1SMatthew G. Knepley   PetscInt       maxConeSize, maxSupportSize, coneSeries, supportSeries;
3483b5a892a1SMatthew G. Knepley   PetscInt       depth, maxSize;
3484b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
3485b5a892a1SMatthew G. Knepley 
3486b5a892a1SMatthew G. Knepley   PetscFunctionBeginHot;
3487b5a892a1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3488b5a892a1SMatthew G. Knepley   if (depth == 1) {
3489012bc364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure_Depth1_Private(dm, p, ornt, useCone, numPoints, points);CHKERRQ(ierr);
3490b5a892a1SMatthew G. Knepley     PetscFunctionReturn(0);
3491b5a892a1SMatthew G. Knepley   }
3492b5a892a1SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
3493b5a892a1SMatthew G. Knepley   if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3494b5a892a1SMatthew G. Knepley   if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
3495b5a892a1SMatthew G. Knepley     ierr = DMPlexTransitiveClosure_Tensor_Internal(dm, p, ct, ornt, useCone, numPoints, points);CHKERRQ(ierr);
3496b5a892a1SMatthew G. Knepley     PetscFunctionReturn(0);
3497b5a892a1SMatthew G. Knepley   }
3498b5a892a1SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
3499b5a892a1SMatthew G. Knepley   coneSeries    = (maxConeSize    > 1) ? ((PetscPowInt(maxConeSize,    depth+1)-1)/(maxConeSize-1))    : depth+1;
3500b5a892a1SMatthew G. Knepley   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, depth+1)-1)/(maxSupportSize-1)) : depth+1;
3501b5a892a1SMatthew G. Knepley   maxSize       = PetscMax(coneSeries, supportSeries);
3502b5a892a1SMatthew G. Knepley   ierr = DMGetWorkArray(dm, 3*maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
3503b5a892a1SMatthew G. Knepley   if (*points) {closure = *points;}
3504b5a892a1SMatthew G. Knepley   else         {ierr = DMGetWorkArray(dm, 2*maxSize, MPIU_INT, &closure);CHKERRQ(ierr);}
3505b5a892a1SMatthew G. Knepley   closure[closureSize++] = p;
3506b5a892a1SMatthew G. Knepley   closure[closureSize++] = ornt;
3507b5a892a1SMatthew G. Knepley   fifo[fifoSize++]       = p;
3508b5a892a1SMatthew G. Knepley   fifo[fifoSize++]       = ornt;
3509b5a892a1SMatthew G. Knepley   fifo[fifoSize++]       = ct;
3510b5a892a1SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
3511b5a892a1SMatthew G. Knepley   while (fifoSize - fifoStart) {
3512b5a892a1SMatthew G. Knepley     const PetscInt       q    = fifo[fifoStart++];
3513b5a892a1SMatthew G. Knepley     const PetscInt       o    = fifo[fifoStart++];
3514b5a892a1SMatthew G. Knepley     const DMPolytopeType qt   = (DMPolytopeType) fifo[fifoStart++];
3515b5a892a1SMatthew G. Knepley     const PetscInt      *qarr = DMPolytopeTypeGetArrangment(qt, o);
3516b5a892a1SMatthew G. Knepley     const PetscInt      *tmp, *tmpO;
3517b5a892a1SMatthew G. Knepley     PetscInt             tmpSize, t;
3518b5a892a1SMatthew G. Knepley 
3519b5a892a1SMatthew G. Knepley     if (PetscDefined(USE_DEBUG)) {
3520b5a892a1SMatthew G. Knepley       PetscInt nO = DMPolytopeTypeGetNumArrangments(qt)/2;
35212c71b3e2SJacob Faibussowitsch       PetscCheckFalse(o && (o >= nO || o < -nO),PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid orientation %D not in [%D,%D) for %s %D", o, -nO, nO, DMPolytopeTypes[qt], q);
3522b5a892a1SMatthew G. Knepley     }
3523b5a892a1SMatthew G. Knepley     if (useCone) {
3524b5a892a1SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
3525b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
3526b5a892a1SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
3527b5a892a1SMatthew G. Knepley     } else {
3528b5a892a1SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
3529b5a892a1SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
3530b5a892a1SMatthew G. Knepley       tmpO = NULL;
3531b5a892a1SMatthew G. Knepley     }
3532b5a892a1SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
3533b5a892a1SMatthew G. Knepley       const PetscInt ip = useCone && qarr ? qarr[t*2]   : t;
3534b5a892a1SMatthew G. Knepley       const PetscInt io = useCone && qarr ? qarr[t*2+1] : 0;
3535b5a892a1SMatthew G. Knepley       const PetscInt cp = tmp[ip];
3536b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cp, &ct);CHKERRQ(ierr);
3537b5a892a1SMatthew G. Knepley       const PetscInt co = tmpO ? DMPolytopeTypeComposeOrientation(ct, io, tmpO[ip]) : 0;
3538b5a892a1SMatthew G. Knepley       PetscInt       c;
3539b5a892a1SMatthew G. Knepley 
3540b5a892a1SMatthew G. Knepley       /* Check for duplicate */
3541b5a892a1SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
3542b5a892a1SMatthew G. Knepley         if (closure[c] == cp) break;
3543b5a892a1SMatthew G. Knepley       }
3544b5a892a1SMatthew G. Knepley       if (c == closureSize) {
3545b5a892a1SMatthew G. Knepley         closure[closureSize++] = cp;
3546b5a892a1SMatthew G. Knepley         closure[closureSize++] = co;
3547b5a892a1SMatthew G. Knepley         fifo[fifoSize++]       = cp;
3548b5a892a1SMatthew G. Knepley         fifo[fifoSize++]       = co;
3549b5a892a1SMatthew G. Knepley         fifo[fifoSize++]       = ct;
3550b5a892a1SMatthew G. Knepley       }
3551b5a892a1SMatthew G. Knepley     }
3552b5a892a1SMatthew G. Knepley   }
3553b5a892a1SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 3*maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
3554b5a892a1SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
3555b5a892a1SMatthew G. Knepley   if (points)    *points    = closure;
3556b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
3557b5a892a1SMatthew G. Knepley }
3558b5a892a1SMatthew G. Knepley 
3559552f7358SJed Brown /*@C
3560eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
3561552f7358SJed Brown 
3562552f7358SJed Brown   Not collective
3563552f7358SJed Brown 
3564552f7358SJed Brown   Input Parameters:
3565b5a892a1SMatthew G. Knepley + dm      - The DMPlex
3566b5a892a1SMatthew G. Knepley . p       - The mesh point
35676b867d5aSJose E. Roman - useCone - PETSC_TRUE for the closure, otherwise return the star
3568552f7358SJed Brown 
35696b867d5aSJose E. Roman   Input/Output Parameter:
35706b867d5aSJose E. Roman . points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...];
35716b867d5aSJose E. Roman            if NULL on input, internal storage will be returned, otherwise the provided array is used
35726b867d5aSJose E. Roman 
35736b867d5aSJose E. Roman   Output Parameter:
35746b867d5aSJose E. Roman . numPoints - The number of points in the closure, so points[] is of size 2*numPoints
3575552f7358SJed Brown 
3576552f7358SJed Brown   Note:
35770298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
3578552f7358SJed Brown 
35793813dfbdSMatthew G Knepley   Fortran Notes:
3580b5a892a1SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must include petsc.h90 in your code.
35813813dfbdSMatthew G Knepley 
35823813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
35833813dfbdSMatthew G Knepley 
3584552f7358SJed Brown   Level: beginner
3585552f7358SJed Brown 
3586552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
3587552f7358SJed Brown @*/
3588552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3589552f7358SJed Brown {
3590552f7358SJed Brown   PetscErrorCode ierr;
3591552f7358SJed Brown 
3592b5a892a1SMatthew G. Knepley   PetscFunctionBeginHot;
3593552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3594b5a892a1SMatthew G. Knepley   if (numPoints) PetscValidIntPointer(numPoints, 4);
3595b5a892a1SMatthew G. Knepley   if (points)    PetscValidPointer(points, 5);
3596b5a892a1SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure_Internal(dm, p, 0, useCone, numPoints, points);CHKERRQ(ierr);
35979bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
35989bf0dad6SMatthew G. Knepley }
35999bf0dad6SMatthew G. Knepley 
3600552f7358SJed Brown /*@C
3601eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
3602552f7358SJed Brown 
3603552f7358SJed Brown   Not collective
3604552f7358SJed Brown 
3605552f7358SJed Brown   Input Parameters:
3606b5a892a1SMatthew G. Knepley + dm        - The DMPlex
3607b5a892a1SMatthew G. Knepley . p         - The mesh point
3608b5a892a1SMatthew G. Knepley . useCone   - PETSC_TRUE for the closure, otherwise return the star
3609b5a892a1SMatthew G. Knepley . numPoints - The number of points in the closure, so points[] is of size 2*numPoints
3610b5a892a1SMatthew G. Knepley - points    - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
3611552f7358SJed Brown 
3612552f7358SJed Brown   Note:
36130298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
3614552f7358SJed Brown 
36153813dfbdSMatthew G Knepley   Fortran Notes:
3616b5a892a1SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must include petsc.h90 in your code.
36173813dfbdSMatthew G Knepley 
36183813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
36193813dfbdSMatthew G Knepley 
3620552f7358SJed Brown   Level: beginner
3621552f7358SJed Brown 
3622552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
3623552f7358SJed Brown @*/
3624552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3625552f7358SJed Brown {
3626552f7358SJed Brown   PetscErrorCode ierr;
3627552f7358SJed Brown 
3628b5a892a1SMatthew G. Knepley   PetscFunctionBeginHot;
3629552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36304ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
3631b5a892a1SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
3632552f7358SJed Brown   PetscFunctionReturn(0);
3633552f7358SJed Brown }
3634552f7358SJed Brown 
3635552f7358SJed Brown /*@
3636eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
3637552f7358SJed Brown 
3638552f7358SJed Brown   Not collective
3639552f7358SJed Brown 
3640552f7358SJed Brown   Input Parameter:
3641552f7358SJed Brown . mesh - The DMPlex
3642552f7358SJed Brown 
3643552f7358SJed Brown   Output Parameters:
3644552f7358SJed Brown + maxConeSize - The maximum number of in-edges
3645552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
3646552f7358SJed Brown 
3647552f7358SJed Brown   Level: beginner
3648552f7358SJed Brown 
3649552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
3650552f7358SJed Brown @*/
3651552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
3652552f7358SJed Brown {
3653552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3654552f7358SJed Brown 
3655552f7358SJed Brown   PetscFunctionBegin;
3656552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3657552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
3658552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
3659552f7358SJed Brown   PetscFunctionReturn(0);
3660552f7358SJed Brown }
3661552f7358SJed Brown 
3662552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
3663552f7358SJed Brown {
3664552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3665552f7358SJed Brown   PetscInt       size;
3666552f7358SJed Brown   PetscErrorCode ierr;
3667552f7358SJed Brown 
3668552f7358SJed Brown   PetscFunctionBegin;
3669552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3670552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
3671552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
36721795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
36731795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
3674ef1259faSMatthew G. Knepley   ierr = PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));CHKERRQ(ierr);
3675552f7358SJed Brown   if (mesh->maxSupportSize) {
3676552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
3677552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
36781795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
3679ef1259faSMatthew G. Knepley     ierr = PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));CHKERRQ(ierr);
3680552f7358SJed Brown   }
3681552f7358SJed Brown   PetscFunctionReturn(0);
3682552f7358SJed Brown }
3683552f7358SJed Brown 
3684276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3685552f7358SJed Brown {
3686552f7358SJed Brown   PetscErrorCode ierr;
3687552f7358SJed Brown 
3688552f7358SJed Brown   PetscFunctionBegin;
36894d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
3690792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
3691c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
3692736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
3693f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
3694f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
3695f94b4a02SBlaise Bourdin 
36963dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
36973dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
3698c3b366b1Sprj-     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);
3699f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
3700f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
3701f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
3702f94b4a02SBlaise Bourdin 
3703f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3704c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
3705f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
3706f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
3707f94b4a02SBlaise Bourdin   }
3708552f7358SJed Brown   PetscFunctionReturn(0);
3709552f7358SJed Brown }
3710552f7358SJed Brown 
37112adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
37122adcc780SMatthew G. Knepley {
37132adcc780SMatthew G. Knepley   PetscErrorCode ierr;
37143dcd263cSBlaise Bourdin   PetscInt       i = 0;
37152adcc780SMatthew G. Knepley 
37162adcc780SMatthew G. Knepley   PetscFunctionBegin;
3717435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
3718792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
3719c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
37203dcd263cSBlaise Bourdin   for (i = 0; i < len; i++) {
37213dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
37223dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
37233dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
37243dcd263cSBlaise Bourdin 
37253dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
37263dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
3727c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
372892fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
37293dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
37303dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
37313dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
37323dcd263cSBlaise Bourdin 
37333dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
3734c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
37353dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
37363dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
37373dcd263cSBlaise Bourdin       break;
37383dcd263cSBlaise Bourdin     }
37393dcd263cSBlaise Bourdin   }
37402adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
37412adcc780SMatthew G. Knepley }
37422adcc780SMatthew G. Knepley 
3743552f7358SJed Brown /*@
3744eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
3745552f7358SJed Brown 
3746552f7358SJed Brown   Not collective
3747552f7358SJed Brown 
3748552f7358SJed Brown   Input Parameter:
3749552f7358SJed Brown . mesh - The DMPlex
3750552f7358SJed Brown 
3751552f7358SJed Brown   Output Parameter:
3752552f7358SJed Brown 
3753552f7358SJed Brown   Note:
3754552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
3755552f7358SJed Brown 
3756552f7358SJed Brown   Level: beginner
3757552f7358SJed Brown 
3758552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
3759552f7358SJed Brown @*/
3760552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
3761552f7358SJed Brown {
3762552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3763552f7358SJed Brown   PetscInt      *offsets;
3764552f7358SJed Brown   PetscInt       supportSize;
3765552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3766552f7358SJed Brown   PetscErrorCode ierr;
3767552f7358SJed Brown 
3768552f7358SJed Brown   PetscFunctionBegin;
3769552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37702c71b3e2SJacob Faibussowitsch   PetscCheckFalse(mesh->supports,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
377130b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3772552f7358SJed Brown   /* Calculate support sizes */
3773552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3774552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3775552f7358SJed Brown     PetscInt dof, off, c;
3776552f7358SJed Brown 
3777552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3778552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3779552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3780552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
3781552f7358SJed Brown     }
3782552f7358SJed Brown   }
3783552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3784552f7358SJed Brown     PetscInt dof;
3785552f7358SJed Brown 
3786552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
37870d644c17SKarl Rupp 
3788552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
3789552f7358SJed Brown   }
3790552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
3791552f7358SJed Brown   /* Calculate supports */
3792552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
37931795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
37941795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
3795552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3796552f7358SJed Brown     PetscInt dof, off, c;
3797552f7358SJed Brown 
3798552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
3799552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
3800552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
3801552f7358SJed Brown       const PetscInt q = mesh->cones[c];
3802552f7358SJed Brown       PetscInt       offS;
3803552f7358SJed Brown 
3804552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
38050d644c17SKarl Rupp 
3806552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
3807552f7358SJed Brown       ++offsets[q];
3808552f7358SJed Brown     }
3809552f7358SJed Brown   }
3810552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
381130b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
3812552f7358SJed Brown   PetscFunctionReturn(0);
3813552f7358SJed Brown }
3814552f7358SJed Brown 
3815277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3816277ea44aSLisandro Dalcin {
3817277ea44aSLisandro Dalcin   IS             stratumIS;
3818277ea44aSLisandro Dalcin   PetscErrorCode ierr;
3819277ea44aSLisandro Dalcin 
3820277ea44aSLisandro Dalcin   PetscFunctionBegin;
3821277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
382276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
3823277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
3824277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
3825277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
3826277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
3827277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3828277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
3829277ea44aSLisandro Dalcin     }
38302c71b3e2SJacob Faibussowitsch     PetscCheckFalse(overlap,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);
3831277ea44aSLisandro Dalcin   }
3832277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
3833277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
3834277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
3835277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
3836277ea44aSLisandro Dalcin }
3837277ea44aSLisandro Dalcin 
3838552f7358SJed Brown /*@
3839a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
38406dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
3841552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
3842552f7358SJed Brown   the DAG.
3843552f7358SJed Brown 
3844bf4602e4SToby Isaac   Collective on dm
3845552f7358SJed Brown 
3846552f7358SJed Brown   Input Parameter:
3847552f7358SJed Brown . mesh - The DMPlex
3848552f7358SJed Brown 
3849552f7358SJed Brown   Output Parameter:
3850552f7358SJed Brown 
3851552f7358SJed Brown   Notes:
3852b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3853b1bb481bSMatthew 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
3854b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
3855c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
3856150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
3857552f7358SJed Brown 
3858b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3859b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3860b1bb481bSMatthew 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
3861b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
3862b1bb481bSMatthew Knepley $  cone(c0) = {e0, v2}
3863b1bb481bSMatthew Knepley $  cone(e0) = {v0, v1}
3864b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
3865b1bb481bSMatthew Knepley $  depth 0 = {v0, v1, v2}
3866b1bb481bSMatthew Knepley $  depth 1 = {e0, c0}
3867b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
3868b1bb481bSMatthew Knepley 
3869150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
3870552f7358SJed Brown 
3871552f7358SJed Brown   Level: beginner
3872552f7358SJed Brown 
3873ba2698f1SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
3874552f7358SJed Brown @*/
3875552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
3876552f7358SJed Brown {
3877df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3878aa50250dSMatthew G. Knepley   DMLabel        label;
3879552f7358SJed Brown   PetscInt       pStart, pEnd, p;
3880552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
3881552f7358SJed Brown   PetscErrorCode ierr;
3882552f7358SJed Brown 
3883552f7358SJed Brown   PetscFunctionBegin;
3884552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3885552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3886277ea44aSLisandro Dalcin 
3887277ea44aSLisandro Dalcin   /* Create depth label */
3888aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3889c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
3890aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3891277ea44aSLisandro Dalcin 
3892277ea44aSLisandro Dalcin   {
3893552f7358SJed Brown     /* Initialize roots and count leaves */
3894277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3895277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3896552f7358SJed Brown     PetscInt coneSize, supportSize;
3897552f7358SJed Brown 
3898277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3899552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3900552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3901552f7358SJed Brown       if (!coneSize && supportSize) {
3902277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3903277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3904552f7358SJed Brown         ++numRoots;
3905552f7358SJed Brown       } else if (!supportSize && coneSize) {
3906552f7358SJed Brown         ++numLeaves;
3907552f7358SJed Brown       } else if (!supportSize && !coneSize) {
3908552f7358SJed Brown         /* Isolated points */
3909277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3910277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3911552f7358SJed Brown       }
3912552f7358SJed Brown     }
3913277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
3914277ea44aSLisandro Dalcin   }
3915277ea44aSLisandro Dalcin 
3916552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
3917277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
3918277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
3919552f7358SJed Brown     PetscInt coneSize, supportSize;
3920552f7358SJed Brown 
3921277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
3922552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3923552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
3924552f7358SJed Brown       if (!supportSize && coneSize) {
3925277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
3926277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
3927552f7358SJed Brown       }
3928552f7358SJed Brown     }
3929277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
3930552f7358SJed Brown   } else {
3931277ea44aSLisandro Dalcin     PetscInt level = 0;
3932277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
3933552f7358SJed Brown 
3934277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
3935277ea44aSLisandro Dalcin     while (qEnd > qStart) {
3936277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
3937277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
393874ef644bSMatthew G. Knepley 
3939277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
394074ef644bSMatthew G. Knepley         const PetscInt *support;
394174ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
394274ef644bSMatthew G. Knepley 
3943277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
3944277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
394574ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3946277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
3947277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
3948552f7358SJed Brown         }
3949552f7358SJed Brown       }
3950277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
3951277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
3952277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
395374ef644bSMatthew G. Knepley     }
395474ef644bSMatthew G. Knepley   }
3955bf4602e4SToby Isaac   { /* just in case there is an empty process */
3956bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
3957bf4602e4SToby Isaac 
3958bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
3959ffc4695bSBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
3960bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
3961367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
3962bf4602e4SToby Isaac     }
3963bf4602e4SToby Isaac   }
3964d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
3965552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
3966552f7358SJed Brown   PetscFunctionReturn(0);
3967552f7358SJed Brown }
3968552f7358SJed Brown 
3969412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
3970ba2698f1SMatthew G. Knepley {
3971412e9a14SMatthew G. Knepley   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3972412e9a14SMatthew G. Knepley   PetscInt       dim, depth, pheight, coneSize;
3973ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
3974ba2698f1SMatthew G. Knepley 
3975412e9a14SMatthew G. Knepley   PetscFunctionBeginHot;
3976ba2698f1SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3977ba2698f1SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3978ba2698f1SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
3979ba2698f1SMatthew G. Knepley   pheight = depth - pdepth;
3980ba2698f1SMatthew G. Knepley   if (depth <= 1) {
3981ba2698f1SMatthew G. Knepley     switch (pdepth) {
3982ba2698f1SMatthew G. Knepley       case 0: ct = DM_POLYTOPE_POINT;break;
3983ba2698f1SMatthew G. Knepley       case 1:
3984ba2698f1SMatthew G. Knepley         switch (coneSize) {
3985ba2698f1SMatthew G. Knepley           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3986ba2698f1SMatthew G. Knepley           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3987ba2698f1SMatthew G. Knepley           case 4:
3988ba2698f1SMatthew G. Knepley           switch (dim) {
3989ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3990ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3991ba2698f1SMatthew G. Knepley             default: break;
3992ba2698f1SMatthew G. Knepley           }
3993ba2698f1SMatthew G. Knepley           break;
3994da9060c4SMatthew G. Knepley         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3995ba2698f1SMatthew G. Knepley         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3996ba2698f1SMatthew G. Knepley         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3997ba2698f1SMatthew G. Knepley         default: break;
3998ba2698f1SMatthew G. Knepley       }
3999ba2698f1SMatthew G. Knepley     }
4000ba2698f1SMatthew G. Knepley   } else {
4001ba2698f1SMatthew G. Knepley     if (pdepth == 0) {
4002ba2698f1SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
4003ba2698f1SMatthew G. Knepley     } else if (pheight == 0) {
4004ba2698f1SMatthew G. Knepley       switch (dim) {
4005ba2698f1SMatthew G. Knepley         case 1:
4006ba2698f1SMatthew G. Knepley           switch (coneSize) {
4007ba2698f1SMatthew G. Knepley             case 2: ct = DM_POLYTOPE_SEGMENT;break;
4008ba2698f1SMatthew G. Knepley             default: break;
4009ba2698f1SMatthew G. Knepley           }
4010ba2698f1SMatthew G. Knepley           break;
4011ba2698f1SMatthew G. Knepley         case 2:
4012ba2698f1SMatthew G. Knepley           switch (coneSize) {
4013ba2698f1SMatthew G. Knepley             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
4014ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
4015ba2698f1SMatthew G. Knepley             default: break;
4016ba2698f1SMatthew G. Knepley           }
4017ba2698f1SMatthew G. Knepley           break;
4018ba2698f1SMatthew G. Knepley         case 3:
4019ba2698f1SMatthew G. Knepley           switch (coneSize) {
4020ba2698f1SMatthew G. Knepley             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
4021da9060c4SMatthew G. Knepley             case 5:
4022da9060c4SMatthew G. Knepley             {
4023da9060c4SMatthew G. Knepley               const PetscInt *cone;
4024da9060c4SMatthew G. Knepley               PetscInt        faceConeSize;
4025da9060c4SMatthew G. Knepley 
4026da9060c4SMatthew G. Knepley               ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
4027da9060c4SMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[0], &faceConeSize);CHKERRQ(ierr);
4028da9060c4SMatthew G. Knepley               switch (faceConeSize) {
4029da9060c4SMatthew G. Knepley                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
4030da9060c4SMatthew G. Knepley                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
4031da9060c4SMatthew G. Knepley               }
4032da9060c4SMatthew G. Knepley             }
4033da9060c4SMatthew G. Knepley             break;
4034ba2698f1SMatthew G. Knepley             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
4035ba2698f1SMatthew G. Knepley             default: break;
4036ba2698f1SMatthew G. Knepley           }
4037ba2698f1SMatthew G. Knepley           break;
4038ba2698f1SMatthew G. Knepley         default: break;
4039ba2698f1SMatthew G. Knepley       }
4040ba2698f1SMatthew G. Knepley     } else if (pheight > 0) {
4041ba2698f1SMatthew G. Knepley       switch (coneSize) {
4042ba2698f1SMatthew G. Knepley         case 2: ct = DM_POLYTOPE_SEGMENT;break;
4043ba2698f1SMatthew G. Knepley         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
4044ba2698f1SMatthew G. Knepley         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
4045ba2698f1SMatthew G. Knepley         default: break;
4046ba2698f1SMatthew G. Knepley       }
4047ba2698f1SMatthew G. Knepley     }
4048ba2698f1SMatthew G. Knepley   }
4049412e9a14SMatthew G. Knepley   *pt = ct;
4050412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
4051ba2698f1SMatthew G. Knepley }
4052412e9a14SMatthew G. Knepley 
4053412e9a14SMatthew G. Knepley /*@
4054412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
4055412e9a14SMatthew G. Knepley 
4056412e9a14SMatthew G. Knepley   Collective on dm
4057412e9a14SMatthew G. Knepley 
4058412e9a14SMatthew G. Knepley   Input Parameter:
4059412e9a14SMatthew G. Knepley . mesh - The DMPlex
4060412e9a14SMatthew G. Knepley 
4061412e9a14SMatthew G. Knepley   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()
4062412e9a14SMatthew G. Knepley 
4063412e9a14SMatthew G. Knepley   Level: developer
4064412e9a14SMatthew G. Knepley 
4065412e9a14SMatthew G. Knepley   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
4066412e9a14SMatthew G. Knepley   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
4067412e9a14SMatthew G. Knepley   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").
4068412e9a14SMatthew G. Knepley 
4069412e9a14SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
4070412e9a14SMatthew G. Knepley @*/
4071412e9a14SMatthew G. Knepley PetscErrorCode DMPlexComputeCellTypes(DM dm)
4072412e9a14SMatthew G. Knepley {
4073412e9a14SMatthew G. Knepley   DM_Plex       *mesh;
4074412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
4075412e9a14SMatthew G. Knepley   PetscInt       pStart, pEnd, p;
4076412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
4077412e9a14SMatthew G. Knepley 
4078412e9a14SMatthew G. Knepley   PetscFunctionBegin;
4079412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4080412e9a14SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
4081412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
4082412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
4083412e9a14SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
4084412e9a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4085327c2912SStefano Zampini     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4086412e9a14SMatthew G. Knepley     PetscInt       pdepth;
4087412e9a14SMatthew G. Knepley 
4088412e9a14SMatthew G. Knepley     ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
4089412e9a14SMatthew G. Knepley     ierr = DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);CHKERRQ(ierr);
40902c71b3e2SJacob Faibussowitsch     PetscCheckFalse(ct == DM_POLYTOPE_UNKNOWN,PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
4091412e9a14SMatthew G. Knepley     ierr = DMLabelSetValue(ctLabel, p, ct);CHKERRQ(ierr);
4092412e9a14SMatthew G. Knepley   }
4093412e9a14SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);CHKERRQ(ierr);
4094412e9a14SMatthew G. Knepley   ierr = PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");CHKERRQ(ierr);
4095ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4096ba2698f1SMatthew G. Knepley }
4097ba2698f1SMatthew G. Knepley 
4098552f7358SJed Brown /*@C
4099552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
4100552f7358SJed Brown 
4101552f7358SJed Brown   Not Collective
4102552f7358SJed Brown 
4103552f7358SJed Brown   Input Parameters:
4104552f7358SJed Brown + dm - The DMPlex object
4105552f7358SJed Brown . numPoints - The number of input points for the join
4106552f7358SJed Brown - points - The input points
4107552f7358SJed Brown 
4108552f7358SJed Brown   Output Parameters:
4109552f7358SJed Brown + numCoveredPoints - The number of points in the join
4110552f7358SJed Brown - coveredPoints - The points in the join
4111552f7358SJed Brown 
4112552f7358SJed Brown   Level: intermediate
4113552f7358SJed Brown 
4114552f7358SJed Brown   Note: Currently, this is restricted to a single level join
4115552f7358SJed Brown 
41163813dfbdSMatthew G Knepley   Fortran Notes:
41173813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41183813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41193813dfbdSMatthew G Knepley 
41203813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
41213813dfbdSMatthew G Knepley 
4122552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
4123552f7358SJed Brown @*/
4124552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4125552f7358SJed Brown {
4126552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4127552f7358SJed Brown   PetscInt      *join[2];
4128552f7358SJed Brown   PetscInt       joinSize, i = 0;
4129552f7358SJed Brown   PetscInt       dof, off, p, c, m;
4130552f7358SJed Brown   PetscErrorCode ierr;
4131552f7358SJed Brown 
4132552f7358SJed Brown   PetscFunctionBegin;
4133552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
413548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
413648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
413769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
413869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
4139552f7358SJed Brown   /* Copy in support of first point */
4140552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
4141552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
4142552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
4143552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
4144552f7358SJed Brown   }
4145552f7358SJed Brown   /* Check each successive support */
4146552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
4147552f7358SJed Brown     PetscInt newJoinSize = 0;
4148552f7358SJed Brown 
4149552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
4150552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
4151552f7358SJed Brown     for (c = 0; c < dof; ++c) {
4152552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
4153552f7358SJed Brown 
4154552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
4155552f7358SJed Brown         if (point == join[i][m]) {
4156552f7358SJed Brown           join[1-i][newJoinSize++] = point;
4157552f7358SJed Brown           break;
4158552f7358SJed Brown         }
4159552f7358SJed Brown       }
4160552f7358SJed Brown     }
4161552f7358SJed Brown     joinSize = newJoinSize;
4162552f7358SJed Brown     i        = 1-i;
4163552f7358SJed Brown   }
4164552f7358SJed Brown   *numCoveredPoints = joinSize;
4165552f7358SJed Brown   *coveredPoints    = join[i];
416669291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
4167552f7358SJed Brown   PetscFunctionReturn(0);
4168552f7358SJed Brown }
4169552f7358SJed Brown 
4170552f7358SJed Brown /*@C
4171552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
4172552f7358SJed Brown 
4173552f7358SJed Brown   Not Collective
4174552f7358SJed Brown 
4175552f7358SJed Brown   Input Parameters:
4176552f7358SJed Brown + dm - The DMPlex object
4177552f7358SJed Brown . numPoints - The number of input points for the join
4178552f7358SJed Brown - points - The input points
4179552f7358SJed Brown 
4180552f7358SJed Brown   Output Parameters:
4181552f7358SJed Brown + numCoveredPoints - The number of points in the join
4182552f7358SJed Brown - coveredPoints - The points in the join
4183552f7358SJed Brown 
41843813dfbdSMatthew G Knepley   Fortran Notes:
41853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41873813dfbdSMatthew G Knepley 
41883813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
41893813dfbdSMatthew G Knepley 
4190552f7358SJed Brown   Level: intermediate
4191552f7358SJed Brown 
4192552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
4193552f7358SJed Brown @*/
4194552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4195552f7358SJed Brown {
4196552f7358SJed Brown   PetscErrorCode ierr;
4197552f7358SJed Brown 
4198552f7358SJed Brown   PetscFunctionBegin;
4199552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4200d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
4201d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
4202d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
420369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
4204d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
4205552f7358SJed Brown   PetscFunctionReturn(0);
4206552f7358SJed Brown }
4207552f7358SJed Brown 
4208552f7358SJed Brown /*@C
4209552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
4210552f7358SJed Brown 
4211552f7358SJed Brown   Not Collective
4212552f7358SJed Brown 
4213552f7358SJed Brown   Input Parameters:
4214552f7358SJed Brown + dm - The DMPlex object
4215552f7358SJed Brown . numPoints - The number of input points for the join
4216552f7358SJed Brown - points - The input points
4217552f7358SJed Brown 
4218552f7358SJed Brown   Output Parameters:
4219552f7358SJed Brown + numCoveredPoints - The number of points in the join
4220552f7358SJed Brown - coveredPoints - The points in the join
4221552f7358SJed Brown 
42223813dfbdSMatthew G Knepley   Fortran Notes:
42233813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
42243813dfbdSMatthew G Knepley   include petsc.h90 in your code.
42253813dfbdSMatthew G Knepley 
42263813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
42273813dfbdSMatthew G Knepley 
4228552f7358SJed Brown   Level: intermediate
4229552f7358SJed Brown 
4230552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
4231552f7358SJed Brown @*/
4232552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4233552f7358SJed Brown {
4234552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4235552f7358SJed Brown   PetscInt      *offsets, **closures;
4236552f7358SJed Brown   PetscInt      *join[2];
4237552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
423824c766afSToby Isaac   PetscInt       p, d, c, m, ms;
4239552f7358SJed Brown   PetscErrorCode ierr;
4240552f7358SJed Brown 
4241552f7358SJed Brown   PetscFunctionBegin;
4242552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424348bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
424448bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
424548bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
4246552f7358SJed Brown 
4247552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
42481795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
424969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
425024c766afSToby Isaac   ms      = mesh->maxSupportSize;
425124c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
425269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
425369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
4254552f7358SJed Brown 
4255552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
4256552f7358SJed Brown     PetscInt closureSize;
4257552f7358SJed Brown 
4258552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
42590d644c17SKarl Rupp 
4260552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
4261552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
4262552f7358SJed Brown       PetscInt pStart, pEnd, i;
4263552f7358SJed Brown 
4264552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
4265552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
4266552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
4267552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
4268552f7358SJed Brown           break;
4269552f7358SJed Brown         }
4270552f7358SJed Brown       }
4271552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
4272552f7358SJed Brown     }
42732c71b3e2SJacob Faibussowitsch     PetscCheckFalse(offsets[p*(depth+2)+depth+1] != closureSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(depth+2)+depth+1], closureSize);
4274552f7358SJed Brown   }
4275552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
4276552f7358SJed Brown     PetscInt dof;
4277552f7358SJed Brown 
4278552f7358SJed Brown     /* Copy in support of first point */
4279552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
4280552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
4281552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
4282552f7358SJed Brown     }
4283552f7358SJed Brown     /* Check each successive cone */
4284552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
4285552f7358SJed Brown       PetscInt newJoinSize = 0;
4286552f7358SJed Brown 
4287552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
4288552f7358SJed Brown       for (c = 0; c < dof; ++c) {
4289552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
4290552f7358SJed Brown 
4291552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
4292552f7358SJed Brown           if (point == join[i][m]) {
4293552f7358SJed Brown             join[1-i][newJoinSize++] = point;
4294552f7358SJed Brown             break;
4295552f7358SJed Brown           }
4296552f7358SJed Brown         }
4297552f7358SJed Brown       }
4298552f7358SJed Brown       joinSize = newJoinSize;
4299552f7358SJed Brown       i        = 1-i;
4300552f7358SJed Brown     }
4301552f7358SJed Brown     if (joinSize) break;
4302552f7358SJed Brown   }
4303552f7358SJed Brown   *numCoveredPoints = joinSize;
4304552f7358SJed Brown   *coveredPoints    = join[i];
4305552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
43060298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
4307552f7358SJed Brown   }
4308552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
430969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
431069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
4311552f7358SJed Brown   PetscFunctionReturn(0);
4312552f7358SJed Brown }
4313552f7358SJed Brown 
4314552f7358SJed Brown /*@C
4315552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
4316552f7358SJed Brown 
4317552f7358SJed Brown   Not Collective
4318552f7358SJed Brown 
4319552f7358SJed Brown   Input Parameters:
4320552f7358SJed Brown + dm - The DMPlex object
4321552f7358SJed Brown . numPoints - The number of input points for the meet
4322552f7358SJed Brown - points - The input points
4323552f7358SJed Brown 
4324552f7358SJed Brown   Output Parameters:
4325552f7358SJed Brown + numCoveredPoints - The number of points in the meet
4326552f7358SJed Brown - coveredPoints - The points in the meet
4327552f7358SJed Brown 
4328552f7358SJed Brown   Level: intermediate
4329552f7358SJed Brown 
4330552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
4331552f7358SJed Brown 
43323813dfbdSMatthew G Knepley   Fortran Notes:
43333813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
43343813dfbdSMatthew G Knepley   include petsc.h90 in your code.
43353813dfbdSMatthew G Knepley 
43363813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
43373813dfbdSMatthew G Knepley 
4338552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
4339552f7358SJed Brown @*/
4340552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
4341552f7358SJed Brown {
4342552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4343552f7358SJed Brown   PetscInt      *meet[2];
4344552f7358SJed Brown   PetscInt       meetSize, i = 0;
4345552f7358SJed Brown   PetscInt       dof, off, p, c, m;
4346552f7358SJed Brown   PetscErrorCode ierr;
4347552f7358SJed Brown 
4348552f7358SJed Brown   PetscFunctionBegin;
4349552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4350064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
4351064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveringPoints, 4);
4352064a246eSJacob Faibussowitsch   PetscValidPointer(coveringPoints, 5);
435369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
435469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
4355552f7358SJed Brown   /* Copy in cone of first point */
4356552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
4357552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
4358552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
4359552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
4360552f7358SJed Brown   }
4361552f7358SJed Brown   /* Check each successive cone */
4362552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
4363552f7358SJed Brown     PetscInt newMeetSize = 0;
4364552f7358SJed Brown 
4365552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
4366552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
4367552f7358SJed Brown     for (c = 0; c < dof; ++c) {
4368552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
4369552f7358SJed Brown 
4370552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
4371552f7358SJed Brown         if (point == meet[i][m]) {
4372552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
4373552f7358SJed Brown           break;
4374552f7358SJed Brown         }
4375552f7358SJed Brown       }
4376552f7358SJed Brown     }
4377552f7358SJed Brown     meetSize = newMeetSize;
4378552f7358SJed Brown     i        = 1-i;
4379552f7358SJed Brown   }
4380552f7358SJed Brown   *numCoveringPoints = meetSize;
4381552f7358SJed Brown   *coveringPoints    = meet[i];
438269291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
4383552f7358SJed Brown   PetscFunctionReturn(0);
4384552f7358SJed Brown }
4385552f7358SJed Brown 
4386552f7358SJed Brown /*@C
4387552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
4388552f7358SJed Brown 
4389552f7358SJed Brown   Not Collective
4390552f7358SJed Brown 
4391552f7358SJed Brown   Input Parameters:
4392552f7358SJed Brown + dm - The DMPlex object
4393552f7358SJed Brown . numPoints - The number of input points for the meet
4394552f7358SJed Brown - points - The input points
4395552f7358SJed Brown 
4396552f7358SJed Brown   Output Parameters:
4397552f7358SJed Brown + numCoveredPoints - The number of points in the meet
4398552f7358SJed Brown - coveredPoints - The points in the meet
4399552f7358SJed Brown 
4400552f7358SJed Brown   Level: intermediate
4401552f7358SJed Brown 
44023813dfbdSMatthew G Knepley   Fortran Notes:
44033813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
44043813dfbdSMatthew G Knepley   include petsc.h90 in your code.
44053813dfbdSMatthew G Knepley 
44063813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
44073813dfbdSMatthew G Knepley 
4408552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
4409552f7358SJed Brown @*/
4410552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4411552f7358SJed Brown {
4412552f7358SJed Brown   PetscErrorCode ierr;
4413552f7358SJed Brown 
4414552f7358SJed Brown   PetscFunctionBegin;
4415552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4416d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
4417d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
4418d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
441969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
4420d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
4421552f7358SJed Brown   PetscFunctionReturn(0);
4422552f7358SJed Brown }
4423552f7358SJed Brown 
4424552f7358SJed Brown /*@C
4425552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
4426552f7358SJed Brown 
4427552f7358SJed Brown   Not Collective
4428552f7358SJed Brown 
4429552f7358SJed Brown   Input Parameters:
4430552f7358SJed Brown + dm - The DMPlex object
4431552f7358SJed Brown . numPoints - The number of input points for the meet
4432552f7358SJed Brown - points - The input points
4433552f7358SJed Brown 
4434552f7358SJed Brown   Output Parameters:
4435552f7358SJed Brown + numCoveredPoints - The number of points in the meet
4436552f7358SJed Brown - coveredPoints - The points in the meet
4437552f7358SJed Brown 
4438552f7358SJed Brown   Level: intermediate
4439552f7358SJed Brown 
44403813dfbdSMatthew G Knepley   Fortran Notes:
44413813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
44423813dfbdSMatthew G Knepley   include petsc.h90 in your code.
44433813dfbdSMatthew G Knepley 
44443813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
44453813dfbdSMatthew G Knepley 
4446552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
4447552f7358SJed Brown @*/
4448552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4449552f7358SJed Brown {
4450552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4451552f7358SJed Brown   PetscInt      *offsets, **closures;
4452552f7358SJed Brown   PetscInt      *meet[2];
4453552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
445424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
4455552f7358SJed Brown   PetscErrorCode ierr;
4456552f7358SJed Brown 
4457552f7358SJed Brown   PetscFunctionBegin;
4458552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4459064a246eSJacob Faibussowitsch   PetscValidPointer(points, 3);
4460064a246eSJacob Faibussowitsch   PetscValidPointer(numCoveredPoints, 4);
4461064a246eSJacob Faibussowitsch   PetscValidPointer(coveredPoints, 5);
4462552f7358SJed Brown 
4463552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
4464785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
446569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
446624c766afSToby Isaac   mc      = mesh->maxConeSize;
446724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
446869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
446969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
4470552f7358SJed Brown 
4471552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
4472552f7358SJed Brown     PetscInt closureSize;
4473552f7358SJed Brown 
4474552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
44750d644c17SKarl Rupp 
4476552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
4477552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
4478552f7358SJed Brown       PetscInt pStart, pEnd, i;
4479552f7358SJed Brown 
4480552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
4481552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
4482552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
4483552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
4484552f7358SJed Brown           break;
4485552f7358SJed Brown         }
4486552f7358SJed Brown       }
4487552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
4488552f7358SJed Brown     }
44892c71b3e2SJacob Faibussowitsch     PetscCheckFalse(offsets[p*(height+2)+height+1] != closureSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(height+2)+height+1], closureSize);
4490552f7358SJed Brown   }
4491552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
4492552f7358SJed Brown     PetscInt dof;
4493552f7358SJed Brown 
4494552f7358SJed Brown     /* Copy in cone of first point */
4495552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
4496552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
4497552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
4498552f7358SJed Brown     }
4499552f7358SJed Brown     /* Check each successive cone */
4500552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
4501552f7358SJed Brown       PetscInt newMeetSize = 0;
4502552f7358SJed Brown 
4503552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
4504552f7358SJed Brown       for (c = 0; c < dof; ++c) {
4505552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
4506552f7358SJed Brown 
4507552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
4508552f7358SJed Brown           if (point == meet[i][m]) {
4509552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
4510552f7358SJed Brown             break;
4511552f7358SJed Brown           }
4512552f7358SJed Brown         }
4513552f7358SJed Brown       }
4514552f7358SJed Brown       meetSize = newMeetSize;
4515552f7358SJed Brown       i        = 1-i;
4516552f7358SJed Brown     }
4517552f7358SJed Brown     if (meetSize) break;
4518552f7358SJed Brown   }
4519552f7358SJed Brown   *numCoveredPoints = meetSize;
4520552f7358SJed Brown   *coveredPoints    = meet[i];
4521552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
45220298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
4523552f7358SJed Brown   }
4524552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
452569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
452669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
4527552f7358SJed Brown   PetscFunctionReturn(0);
4528552f7358SJed Brown }
4529552f7358SJed Brown 
45304e3744c5SMatthew G. Knepley /*@C
45314e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
45324e3744c5SMatthew G. Knepley 
45334e3744c5SMatthew G. Knepley   Not Collective
45344e3744c5SMatthew G. Knepley 
45354e3744c5SMatthew G. Knepley   Input Parameters:
45364e3744c5SMatthew G. Knepley + dmA - A DMPlex object
45374e3744c5SMatthew G. Knepley - dmB - A DMPlex object
45384e3744c5SMatthew G. Knepley 
45394e3744c5SMatthew G. Knepley   Output Parameters:
45404e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
45414e3744c5SMatthew G. Knepley 
45424e3744c5SMatthew G. Knepley   Level: intermediate
45434e3744c5SMatthew G. Knepley 
45444e3744c5SMatthew G. Knepley   Notes:
45454e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
45464e3744c5SMatthew G. Knepley 
45474e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
45484e3744c5SMatthew G. Knepley @*/
45494e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
45504e3744c5SMatthew G. Knepley {
45514e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
45524e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
45534e3744c5SMatthew G. Knepley 
45544e3744c5SMatthew G. Knepley   PetscFunctionBegin;
45554e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
45564e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
45574e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
45584e3744c5SMatthew G. Knepley 
45594e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
45604e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
45614e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
45624e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
45634e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
45644e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
45654e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
45664e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
45674e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
45684e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
45694e3744c5SMatthew G. Knepley 
45704e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
45714e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
45724e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
45734e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
45744e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
45754e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
45764e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
45774e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
45784e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
45794e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
45804e3744c5SMatthew G. Knepley     }
45814e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
45824e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
45834e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
45844e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
45854e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
45864e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
45874e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
45884e3744c5SMatthew G. Knepley     }
45894e3744c5SMatthew G. Knepley   }
45904e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
45914e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
45924e3744c5SMatthew G. Knepley }
45934e3744c5SMatthew G. Knepley 
45947cd05799SMatthew G. Knepley /*@C
45957cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
45967cd05799SMatthew G. Knepley 
45977cd05799SMatthew G. Knepley   Not Collective
45987cd05799SMatthew G. Knepley 
45997cd05799SMatthew G. Knepley   Input Parameters:
46007cd05799SMatthew G. Knepley + dm         - The DMPlex
46017cd05799SMatthew G. Knepley . cellDim    - The cell dimension
46027cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
46037cd05799SMatthew G. Knepley 
46047cd05799SMatthew G. Knepley   Output Parameters:
46057cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
46067cd05799SMatthew G. Knepley 
46077cd05799SMatthew G. Knepley   Level: developer
46087cd05799SMatthew G. Knepley 
46097cd05799SMatthew G. Knepley   Notes:
46107cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
46117cd05799SMatthew G. Knepley 
46127cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
46137cd05799SMatthew G. Knepley @*/
461418ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
4615a6dfd86eSKarl Rupp {
461682f516ccSBarry Smith   MPI_Comm       comm;
4617552f7358SJed Brown   PetscErrorCode ierr;
4618552f7358SJed Brown 
4619552f7358SJed Brown   PetscFunctionBegin;
462082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4621064a246eSJacob Faibussowitsch   PetscValidPointer(numFaceVertices,4);
4622552f7358SJed Brown   switch (cellDim) {
4623552f7358SJed Brown   case 0:
4624552f7358SJed Brown     *numFaceVertices = 0;
4625552f7358SJed Brown     break;
4626552f7358SJed Brown   case 1:
4627552f7358SJed Brown     *numFaceVertices = 1;
4628552f7358SJed Brown     break;
4629552f7358SJed Brown   case 2:
4630552f7358SJed Brown     switch (numCorners) {
463119436ca2SJed Brown     case 3: /* triangle */
463219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
4633552f7358SJed Brown       break;
463419436ca2SJed Brown     case 4: /* quadrilateral */
463519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
4636552f7358SJed Brown       break;
463719436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
463819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
4639552f7358SJed Brown       break;
464019436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
464119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
4642552f7358SJed Brown       break;
4643552f7358SJed Brown     default:
464498921bdaSJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
4645552f7358SJed Brown     }
4646552f7358SJed Brown     break;
4647552f7358SJed Brown   case 3:
4648552f7358SJed Brown     switch (numCorners) {
464919436ca2SJed Brown     case 4: /* tetradehdron */
465019436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
4651552f7358SJed Brown       break;
465219436ca2SJed Brown     case 6: /* tet cohesive cells */
465319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
4654552f7358SJed Brown       break;
465519436ca2SJed Brown     case 8: /* hexahedron */
465619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
4657552f7358SJed Brown       break;
465819436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
465919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4660552f7358SJed Brown       break;
466119436ca2SJed Brown     case 10: /* quadratic tetrahedron */
466219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4663552f7358SJed Brown       break;
466419436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
466519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4666552f7358SJed Brown       break;
466719436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
466819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
4669552f7358SJed Brown       break;
467019436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
467119436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
4672552f7358SJed Brown       break;
4673552f7358SJed Brown     default:
467498921bdaSJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
4675552f7358SJed Brown     }
4676552f7358SJed Brown     break;
4677552f7358SJed Brown   default:
467898921bdaSJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
4679552f7358SJed Brown   }
4680552f7358SJed Brown   PetscFunctionReturn(0);
4681552f7358SJed Brown }
4682552f7358SJed Brown 
4683552f7358SJed Brown /*@
4684aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
4685552f7358SJed Brown 
4686552f7358SJed Brown   Not Collective
4687552f7358SJed Brown 
4688aa50250dSMatthew G. Knepley   Input Parameter:
4689552f7358SJed Brown . dm    - The DMPlex object
4690552f7358SJed Brown 
4691aa50250dSMatthew G. Knepley   Output Parameter:
4692aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
4693552f7358SJed Brown 
4694552f7358SJed Brown   Level: developer
4695552f7358SJed Brown 
4696dc287ab2SVaclav Hapla .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
4697aa50250dSMatthew G. Knepley @*/
4698aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4699aa50250dSMatthew G. Knepley {
4700aa50250dSMatthew G. Knepley   PetscFunctionBegin;
4701aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4702aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
4703c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
4704aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
4705aa50250dSMatthew G. Knepley }
4706aa50250dSMatthew G. Knepley 
4707aa50250dSMatthew G. Knepley /*@
4708aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
4709aa50250dSMatthew G. Knepley 
4710aa50250dSMatthew G. Knepley   Not Collective
4711aa50250dSMatthew G. Knepley 
4712aa50250dSMatthew G. Knepley   Input Parameter:
4713aa50250dSMatthew G. Knepley . dm    - The DMPlex object
4714aa50250dSMatthew G. Knepley 
4715aa50250dSMatthew G. Knepley   Output Parameter:
4716aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
4717aa50250dSMatthew G. Knepley 
4718aa50250dSMatthew G. Knepley   Level: developer
4719552f7358SJed Brown 
4720b1bb481bSMatthew Knepley   Notes:
4721b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
4722dc287ab2SVaclav Hapla   The point depth is described more in detail in DMPlexGetDepthStratum().
4723dc287ab2SVaclav Hapla   An empty mesh gives -1.
4724b1bb481bSMatthew Knepley 
4725dc287ab2SVaclav Hapla .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
4726552f7358SJed Brown @*/
4727552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4728552f7358SJed Brown {
4729aa50250dSMatthew G. Knepley   DMLabel        label;
4730aa50250dSMatthew G. Knepley   PetscInt       d = 0;
4731552f7358SJed Brown   PetscErrorCode ierr;
4732552f7358SJed Brown 
4733552f7358SJed Brown   PetscFunctionBegin;
4734552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4735552f7358SJed Brown   PetscValidPointer(depth, 2);
4736aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4737aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
4738552f7358SJed Brown   *depth = d-1;
4739552f7358SJed Brown   PetscFunctionReturn(0);
4740552f7358SJed Brown }
4741552f7358SJed Brown 
4742552f7358SJed Brown /*@
4743552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
4744552f7358SJed Brown 
4745552f7358SJed Brown   Not Collective
4746552f7358SJed Brown 
4747552f7358SJed Brown   Input Parameters:
4748552f7358SJed Brown + dm           - The DMPlex object
4749552f7358SJed Brown - stratumValue - The requested depth
4750552f7358SJed Brown 
4751552f7358SJed Brown   Output Parameters:
4752552f7358SJed Brown + start - The first point at this depth
4753552f7358SJed Brown - end   - One beyond the last point at this depth
4754552f7358SJed Brown 
4755647867b2SJed Brown   Notes:
4756647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
4757647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
4758647867b2SJed Brown   higher dimension, e.g., "edges".
4759647867b2SJed Brown 
4760552f7358SJed Brown   Level: developer
4761552f7358SJed Brown 
4762dc287ab2SVaclav Hapla .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
4763552f7358SJed Brown @*/
47640adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
47650adebc6cSBarry Smith {
4766aa50250dSMatthew G. Knepley   DMLabel        label;
476763d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
4768552f7358SJed Brown   PetscErrorCode ierr;
4769552f7358SJed Brown 
4770552f7358SJed Brown   PetscFunctionBegin;
4771552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477263d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
477363d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4774552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
47750d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
477663d1a920SMatthew G. Knepley   if (stratumValue < 0) {
477763d1a920SMatthew G. Knepley     if (start) *start = pStart;
477863d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
477963d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4780552f7358SJed Brown   }
4781aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
47822c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!label,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
478363d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
4784552f7358SJed Brown   PetscFunctionReturn(0);
4785552f7358SJed Brown }
4786552f7358SJed Brown 
4787552f7358SJed Brown /*@
4788552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
4789552f7358SJed Brown 
4790552f7358SJed Brown   Not Collective
4791552f7358SJed Brown 
4792552f7358SJed Brown   Input Parameters:
4793552f7358SJed Brown + dm           - The DMPlex object
4794552f7358SJed Brown - stratumValue - The requested height
4795552f7358SJed Brown 
4796552f7358SJed Brown   Output Parameters:
4797552f7358SJed Brown + start - The first point at this height
4798552f7358SJed Brown - end   - One beyond the last point at this height
4799552f7358SJed Brown 
4800647867b2SJed Brown   Notes:
4801647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
4802647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
4803647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
4804647867b2SJed Brown 
4805552f7358SJed Brown   Level: developer
4806552f7358SJed Brown 
48073dc9a465SVaclav Hapla .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
4808552f7358SJed Brown @*/
48090adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
48100adebc6cSBarry Smith {
4811aa50250dSMatthew G. Knepley   DMLabel        label;
481263d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
4813552f7358SJed Brown   PetscErrorCode ierr;
4814552f7358SJed Brown 
4815552f7358SJed Brown   PetscFunctionBegin;
4816552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
481763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
481863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4819552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
48200d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
482163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
482263d1a920SMatthew G. Knepley     if (start) *start = pStart;
482363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
482463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4825552f7358SJed Brown   }
4826aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
48272c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!label,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
482863d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
482963d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
4830552f7358SJed Brown   PetscFunctionReturn(0);
4831552f7358SJed Brown }
4832552f7358SJed Brown 
4833ba2698f1SMatthew G. Knepley /*@
4834ba2698f1SMatthew G. Knepley   DMPlexGetPointDepth - Get the depth of a given point
4835ba2698f1SMatthew G. Knepley 
4836ba2698f1SMatthew G. Knepley   Not Collective
4837ba2698f1SMatthew G. Knepley 
4838d8d19677SJose E. Roman   Input Parameters:
4839ba2698f1SMatthew G. Knepley + dm    - The DMPlex object
4840ba2698f1SMatthew G. Knepley - point - The point
4841ba2698f1SMatthew G. Knepley 
4842ba2698f1SMatthew G. Knepley   Output Parameter:
4843ba2698f1SMatthew G. Knepley . depth - The depth of the point
4844ba2698f1SMatthew G. Knepley 
4845ba2698f1SMatthew G. Knepley   Level: intermediate
4846ba2698f1SMatthew G. Knepley 
48473dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
4848ba2698f1SMatthew G. Knepley @*/
4849ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4850ba2698f1SMatthew G. Knepley {
4851ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4852ba2698f1SMatthew G. Knepley 
4853ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4854ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
485540a2aa30SMatthew G. Knepley   PetscValidIntPointer(depth, 3);
4856ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(dm->depthLabel, point, depth);CHKERRQ(ierr);
4857ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4858ba2698f1SMatthew G. Knepley }
4859ba2698f1SMatthew G. Knepley 
4860ba2698f1SMatthew G. Knepley /*@
48610c0a32dcSVaclav Hapla   DMPlexGetPointHeight - Get the height of a given point
48620c0a32dcSVaclav Hapla 
48630c0a32dcSVaclav Hapla   Not Collective
48640c0a32dcSVaclav Hapla 
4865d8d19677SJose E. Roman   Input Parameters:
48660c0a32dcSVaclav Hapla + dm    - The DMPlex object
48670c0a32dcSVaclav Hapla - point - The point
48680c0a32dcSVaclav Hapla 
48690c0a32dcSVaclav Hapla   Output Parameter:
48700c0a32dcSVaclav Hapla . height - The height of the point
48710c0a32dcSVaclav Hapla 
48720c0a32dcSVaclav Hapla   Level: intermediate
48730c0a32dcSVaclav Hapla 
48743dc9a465SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
48750c0a32dcSVaclav Hapla @*/
48760c0a32dcSVaclav Hapla PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
48770c0a32dcSVaclav Hapla {
48780c0a32dcSVaclav Hapla   PetscInt       n, pDepth;
48790c0a32dcSVaclav Hapla   PetscErrorCode ierr;
48800c0a32dcSVaclav Hapla 
48810c0a32dcSVaclav Hapla   PetscFunctionBegin;
48820c0a32dcSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48830c0a32dcSVaclav Hapla   PetscValidIntPointer(height, 3);
48840c0a32dcSVaclav Hapla   ierr = DMLabelGetNumValues(dm->depthLabel, &n);CHKERRQ(ierr);
48850c0a32dcSVaclav Hapla   ierr = DMLabelGetValue(dm->depthLabel, point, &pDepth);CHKERRQ(ierr);
48860c0a32dcSVaclav Hapla   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
48870c0a32dcSVaclav Hapla   PetscFunctionReturn(0);
48880c0a32dcSVaclav Hapla }
48890c0a32dcSVaclav Hapla 
48900c0a32dcSVaclav Hapla /*@
4891ba2698f1SMatthew G. Knepley   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell
4892ba2698f1SMatthew G. Knepley 
4893ba2698f1SMatthew G. Knepley   Not Collective
4894ba2698f1SMatthew G. Knepley 
4895ba2698f1SMatthew G. Knepley   Input Parameter:
4896ba2698f1SMatthew G. Knepley . dm - The DMPlex object
4897ba2698f1SMatthew G. Knepley 
4898ba2698f1SMatthew G. Knepley   Output Parameter:
4899ba2698f1SMatthew G. Knepley . celltypeLabel - The DMLabel recording cell polytope type
4900ba2698f1SMatthew G. Knepley 
4901412e9a14SMatthew G. Knepley   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
4902412e9a14SMatthew G. Knepley   DMCreateLabel(dm, "celltype") beforehand.
4903412e9a14SMatthew G. Knepley 
4904ba2698f1SMatthew G. Knepley   Level: developer
4905ba2698f1SMatthew G. Knepley 
4906dc287ab2SVaclav Hapla .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
4907ba2698f1SMatthew G. Knepley @*/
4908ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4909ba2698f1SMatthew G. Knepley {
4910ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4911ba2698f1SMatthew G. Knepley 
4912ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4913ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4914ba2698f1SMatthew G. Knepley   PetscValidPointer(celltypeLabel, 2);
4915ba2698f1SMatthew G. Knepley   if (!dm->celltypeLabel) {ierr = DMPlexComputeCellTypes(dm);CHKERRQ(ierr);}
4916ba2698f1SMatthew G. Knepley   *celltypeLabel = dm->celltypeLabel;
4917ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4918ba2698f1SMatthew G. Knepley }
4919ba2698f1SMatthew G. Knepley 
4920ba2698f1SMatthew G. Knepley /*@
4921ba2698f1SMatthew G. Knepley   DMPlexGetCellType - Get the polytope type of a given cell
4922ba2698f1SMatthew G. Knepley 
4923ba2698f1SMatthew G. Knepley   Not Collective
4924ba2698f1SMatthew G. Knepley 
4925d8d19677SJose E. Roman   Input Parameters:
4926ba2698f1SMatthew G. Knepley + dm   - The DMPlex object
4927ba2698f1SMatthew G. Knepley - cell - The cell
4928ba2698f1SMatthew G. Knepley 
4929ba2698f1SMatthew G. Knepley   Output Parameter:
4930ba2698f1SMatthew G. Knepley . celltype - The polytope type of the cell
4931ba2698f1SMatthew G. Knepley 
4932ba2698f1SMatthew G. Knepley   Level: intermediate
4933ba2698f1SMatthew G. Knepley 
4934ba2698f1SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
4935ba2698f1SMatthew G. Knepley @*/
4936ba2698f1SMatthew G. Knepley PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4937ba2698f1SMatthew G. Knepley {
4938ba2698f1SMatthew G. Knepley   DMLabel        label;
4939ba2698f1SMatthew G. Knepley   PetscInt       ct;
4940ba2698f1SMatthew G. Knepley   PetscErrorCode ierr;
4941ba2698f1SMatthew G. Knepley 
4942ba2698f1SMatthew G. Knepley   PetscFunctionBegin;
4943ba2698f1SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4944ba2698f1SMatthew G. Knepley   PetscValidPointer(celltype, 3);
4945ba2698f1SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4946ba2698f1SMatthew G. Knepley   ierr = DMLabelGetValue(label, cell, &ct);CHKERRQ(ierr);
49472c71b3e2SJacob Faibussowitsch   PetscCheckFalse(ct < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %D has not been assigned a cell type", cell);
4948ba2698f1SMatthew G. Knepley   *celltype = (DMPolytopeType) ct;
4949ba2698f1SMatthew G. Knepley   PetscFunctionReturn(0);
4950ba2698f1SMatthew G. Knepley }
4951ba2698f1SMatthew G. Knepley 
4952412e9a14SMatthew G. Knepley /*@
4953412e9a14SMatthew G. Knepley   DMPlexSetCellType - Set the polytope type of a given cell
4954412e9a14SMatthew G. Knepley 
4955412e9a14SMatthew G. Knepley   Not Collective
4956412e9a14SMatthew G. Knepley 
4957412e9a14SMatthew G. Knepley   Input Parameters:
4958412e9a14SMatthew G. Knepley + dm   - The DMPlex object
4959412e9a14SMatthew G. Knepley . cell - The cell
4960412e9a14SMatthew G. Knepley - celltype - The polytope type of the cell
4961412e9a14SMatthew G. Knepley 
4962412e9a14SMatthew G. Knepley   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
4963412e9a14SMatthew G. Knepley   is executed. This function will override the computed type. However, if automatic classification will not succeed
4964412e9a14SMatthew G. Knepley   and a user wants to manually specify all types, the classification must be disabled by calling
4965412e9a14SMatthew G. Knepley   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
4966412e9a14SMatthew G. Knepley 
4967412e9a14SMatthew G. Knepley   Level: advanced
4968412e9a14SMatthew G. Knepley 
4969412e9a14SMatthew G. Knepley .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
4970412e9a14SMatthew G. Knepley @*/
4971412e9a14SMatthew G. Knepley PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4972412e9a14SMatthew G. Knepley {
4973412e9a14SMatthew G. Knepley   DMLabel        label;
4974412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
4975412e9a14SMatthew G. Knepley 
4976412e9a14SMatthew G. Knepley   PetscFunctionBegin;
4977412e9a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4978412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &label);CHKERRQ(ierr);
4979412e9a14SMatthew G. Knepley   ierr = DMLabelSetValue(label, cell, celltype);CHKERRQ(ierr);
4980412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
4981412e9a14SMatthew G. Knepley }
4982412e9a14SMatthew G. Knepley 
49830adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
49840adebc6cSBarry Smith {
4985efe440bfSMatthew G. Knepley   PetscSection   section, s;
4986efe440bfSMatthew G. Knepley   Mat            m;
49873e922f36SToby Isaac   PetscInt       maxHeight;
4988552f7358SJed Brown   PetscErrorCode ierr;
4989552f7358SJed Brown 
4990552f7358SJed Brown   PetscFunctionBegin;
499138221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
49923e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
49933e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
499482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
499592fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
49961d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4997efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
4998efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
4999efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
5000efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
5001efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
50028f4c458bSMatthew G. Knepley 
50038f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
50048f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
5005552f7358SJed Brown   PetscFunctionReturn(0);
5006552f7358SJed Brown }
5007552f7358SJed Brown 
5008f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
5009f19dbd58SToby Isaac {
5010f19dbd58SToby Isaac   Vec            coordsLocal;
5011f19dbd58SToby Isaac   DM             coordsDM;
5012f19dbd58SToby Isaac   PetscErrorCode ierr;
5013f19dbd58SToby Isaac 
5014f19dbd58SToby Isaac   PetscFunctionBegin;
5015f19dbd58SToby Isaac   *field = NULL;
5016f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
5017f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
5018f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
5019f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
5020f19dbd58SToby Isaac   }
5021f19dbd58SToby Isaac   PetscFunctionReturn(0);
5022f19dbd58SToby Isaac }
5023f19dbd58SToby Isaac 
50247cd05799SMatthew G. Knepley /*@C
50257cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
50267cd05799SMatthew G. Knepley 
50277cd05799SMatthew G. Knepley   Not Collective
50287cd05799SMatthew G. Knepley 
50297cd05799SMatthew G. Knepley   Input Parameters:
50307cd05799SMatthew G. Knepley . dm        - The DMPlex object
50317cd05799SMatthew G. Knepley 
50327cd05799SMatthew G. Knepley   Output Parameter:
50337cd05799SMatthew G. Knepley . section - The PetscSection object
50347cd05799SMatthew G. Knepley 
50357cd05799SMatthew G. Knepley   Level: developer
50367cd05799SMatthew G. Knepley 
50377cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
50387cd05799SMatthew G. Knepley @*/
50390adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
50400adebc6cSBarry Smith {
5041552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5042552f7358SJed Brown 
5043552f7358SJed Brown   PetscFunctionBegin;
5044552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5045552f7358SJed Brown   if (section) *section = mesh->coneSection;
5046552f7358SJed Brown   PetscFunctionReturn(0);
5047552f7358SJed Brown }
5048552f7358SJed Brown 
50497cd05799SMatthew G. Knepley /*@C
50507cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
50517cd05799SMatthew G. Knepley 
50527cd05799SMatthew G. Knepley   Not Collective
50537cd05799SMatthew G. Knepley 
50547cd05799SMatthew G. Knepley   Input Parameters:
50557cd05799SMatthew G. Knepley . dm        - The DMPlex object
50567cd05799SMatthew G. Knepley 
50577cd05799SMatthew G. Knepley   Output Parameter:
50587cd05799SMatthew G. Knepley . section - The PetscSection object
50597cd05799SMatthew G. Knepley 
50607cd05799SMatthew G. Knepley   Level: developer
50617cd05799SMatthew G. Knepley 
50627cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
50637cd05799SMatthew G. Knepley @*/
50648cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
50658cb4d582SMatthew G. Knepley {
50668cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
50678cb4d582SMatthew G. Knepley 
50688cb4d582SMatthew G. Knepley   PetscFunctionBegin;
50698cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50708cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
50718cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
50728cb4d582SMatthew G. Knepley }
50738cb4d582SMatthew G. Knepley 
50747cd05799SMatthew G. Knepley /*@C
50757cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
50767cd05799SMatthew G. Knepley 
50777cd05799SMatthew G. Knepley   Not Collective
50787cd05799SMatthew G. Knepley 
50797cd05799SMatthew G. Knepley   Input Parameters:
50807cd05799SMatthew G. Knepley . dm        - The DMPlex object
50817cd05799SMatthew G. Knepley 
50827cd05799SMatthew G. Knepley   Output Parameter:
50837cd05799SMatthew G. Knepley . cones - The cone for each point
50847cd05799SMatthew G. Knepley 
50857cd05799SMatthew G. Knepley   Level: developer
50867cd05799SMatthew G. Knepley 
50877cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
50887cd05799SMatthew G. Knepley @*/
5089a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
5090a6dfd86eSKarl Rupp {
5091552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5092552f7358SJed Brown 
5093552f7358SJed Brown   PetscFunctionBegin;
5094552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5095552f7358SJed Brown   if (cones) *cones = mesh->cones;
5096552f7358SJed Brown   PetscFunctionReturn(0);
5097552f7358SJed Brown }
5098552f7358SJed Brown 
50997cd05799SMatthew G. Knepley /*@C
51007cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
51017cd05799SMatthew G. Knepley 
51027cd05799SMatthew G. Knepley   Not Collective
51037cd05799SMatthew G. Knepley 
51047cd05799SMatthew G. Knepley   Input Parameters:
51057cd05799SMatthew G. Knepley . dm        - The DMPlex object
51067cd05799SMatthew G. Knepley 
51077cd05799SMatthew G. Knepley   Output Parameter:
5108b5a892a1SMatthew G. Knepley . coneOrientations - The array of cone orientations for all points
51097cd05799SMatthew G. Knepley 
51107cd05799SMatthew G. Knepley   Level: developer
51117cd05799SMatthew G. Knepley 
5112b5a892a1SMatthew G. Knepley   Notes:
5113b5a892a1SMatthew G. Knepley   The PetscSection returned by DMPlexGetConeSection() partitions coneOrientations into cone orientations of particular points as returned by DMPlexGetConeOrientation().
5114b5a892a1SMatthew G. Knepley 
5115b5a892a1SMatthew G. Knepley   The meaning of coneOrientations values is detailed in DMPlexGetConeOrientation().
5116b5a892a1SMatthew G. Knepley 
5117b5a892a1SMatthew G. Knepley .seealso: DMPlexGetConeSection(), DMPlexGetConeOrientation()
51187cd05799SMatthew G. Knepley @*/
5119a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
5120a6dfd86eSKarl Rupp {
5121552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5122552f7358SJed Brown 
5123552f7358SJed Brown   PetscFunctionBegin;
5124552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5125552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
5126552f7358SJed Brown   PetscFunctionReturn(0);
5127552f7358SJed Brown }
5128552f7358SJed Brown 
5129552f7358SJed Brown /******************************** FEM Support **********************************/
5130552f7358SJed Brown 
51319e8305c2SJed Brown /*
51329e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
51339e8305c2SJed Brown  representing a line in the section.
51349e8305c2SJed Brown */
51359e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
51369e8305c2SJed Brown {
51379e8305c2SJed Brown   PetscErrorCode ierr;
51389e8305c2SJed Brown 
51399e8305c2SJed Brown   PetscFunctionBeginHot;
51409e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
5141a433471fSStefano Zampini   if (line < 0) {
5142a433471fSStefano Zampini     *k = 0;
5143a433471fSStefano Zampini     *Nc = 0;
5144a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
51459e8305c2SJed Brown     *k = 1;
51469e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
51479e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
51489e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
51499e8305c2SJed Brown     *k = *k / *Nc + 1;
51509e8305c2SJed Brown   }
51519e8305c2SJed Brown   PetscFunctionReturn(0);
51529e8305c2SJed Brown }
51539e8305c2SJed Brown 
5154a4355906SMatthew Knepley /*@
5155bc1eb3faSJed Brown 
5156bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
5157bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
51581bb6d2a8SBarry Smith   section provided (or the section of the DM).
5159a4355906SMatthew Knepley 
5160a4355906SMatthew Knepley   Input Parameters:
5161a4355906SMatthew Knepley + dm      - The DM
5162a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
5163a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
5164a4355906SMatthew Knepley 
5165a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
5166a4355906SMatthew Knepley   degree of the basis.
5167a4355906SMatthew Knepley 
5168bc1eb3faSJed Brown   Example:
5169bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
5170bc1eb3faSJed Brown .vb
5171bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
5172bc1eb3faSJed Brown 
5173bc1eb3faSJed Brown   v4 -- e6 -- v3
5174bc1eb3faSJed Brown   |           |
5175bc1eb3faSJed Brown   e7    c0    e8
5176bc1eb3faSJed Brown   |           |
5177bc1eb3faSJed Brown   v1 -- e5 -- v2
5178bc1eb3faSJed Brown .ve
5179bc1eb3faSJed Brown 
5180bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
5181bc1eb3faSJed Brown   dofs in the order of points, e.g.,
5182bc1eb3faSJed Brown .vb
5183bc1eb3faSJed Brown     c0 -> [0,1,2,3]
5184bc1eb3faSJed Brown     v1 -> [4]
5185bc1eb3faSJed Brown     ...
5186bc1eb3faSJed Brown     e5 -> [8, 9]
5187bc1eb3faSJed Brown .ve
5188bc1eb3faSJed Brown 
5189bc1eb3faSJed Brown   which corresponds to the dofs
5190bc1eb3faSJed Brown .vb
5191bc1eb3faSJed Brown     6   10  11  7
5192bc1eb3faSJed Brown     13  2   3   15
5193bc1eb3faSJed Brown     12  0   1   14
5194bc1eb3faSJed Brown     4   8   9   5
5195bc1eb3faSJed Brown .ve
5196bc1eb3faSJed Brown 
5197bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
5198bc1eb3faSJed Brown .vb
5199bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
5200bc1eb3faSJed Brown .ve
5201bc1eb3faSJed Brown 
5202bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
5203bc1eb3faSJed Brown .vb
5204bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
5205bc1eb3faSJed Brown .ve
5206bc1eb3faSJed Brown 
5207a4355906SMatthew Knepley   Level: developer
5208a4355906SMatthew Knepley 
52099df75925SJed Brown .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
5210a4355906SMatthew Knepley @*/
5211bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
52123194fc30SMatthew G. Knepley {
52137391a63aSMatthew G. Knepley   DMLabel        label;
5214bb197d40SJed Brown   PetscInt       dim, depth = -1, eStart = -1, Nf;
52159e8305c2SJed Brown   PetscBool      vertexchart;
52163194fc30SMatthew G. Knepley   PetscErrorCode ierr;
52173194fc30SMatthew G. Knepley 
52183194fc30SMatthew G. Knepley   PetscFunctionBegin;
52193194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5220a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
5221a433471fSStefano Zampini   if (point < 0) {
5222a433471fSStefano Zampini     PetscInt sStart,sEnd;
5223a433471fSStefano Zampini 
5224a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
5225a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
5226a433471fSStefano Zampini   }
52277391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
5228a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
5229a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
52307391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
52317391a63aSMatthew G. Knepley   else if  (depth == dim) {
52327391a63aSMatthew G. Knepley     const PetscInt *cone;
52337391a63aSMatthew G. Knepley 
52347391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5235d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
5236d4e6627bSStefano Zampini     else if (dim == 3) {
5237d4e6627bSStefano Zampini       const PetscInt *cone2;
5238d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
5239d4e6627bSStefano Zampini       eStart = cone2[0];
524098921bdaSJacob Faibussowitsch     } else SETERRQ(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);
52412c71b3e2SJacob Faibussowitsch   } else PetscCheckFalse(depth >= 0,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);
52429e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
52439e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
52449e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
52459e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
52469e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
52479e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
52489e8305c2SJed Brown   }
52493194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
5250bb197d40SJed Brown   for (PetscInt d=1; d<=dim; d++) {
5251bb197d40SJed Brown     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
5252bb197d40SJed Brown     PetscInt *perm;
5253bb197d40SJed Brown 
52543194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
52559e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
5256bb197d40SJed Brown       size += PetscPowInt(k+1, d)*Nc;
52573194fc30SMatthew G. Knepley     }
52583194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
52593194fc30SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
5260bb197d40SJed Brown       switch (d) {
5261babf31e0SJed Brown       case 1:
52629e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
5263babf31e0SJed Brown         /*
5264babf31e0SJed Brown          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
5265babf31e0SJed Brown          We want              [ vtx0; edge of length k-1; vtx1 ]
5266babf31e0SJed Brown          */
5267babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
5268babf31e0SJed Brown         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
5269babf31e0SJed Brown         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
5270babf31e0SJed Brown         foffset = offset;
5271babf31e0SJed Brown         break;
527289eabcffSMatthew G. Knepley       case 2:
52733194fc30SMatthew 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} */
52749e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
52753194fc30SMatthew G. Knepley         /* The SEM order is
52763194fc30SMatthew G. Knepley 
52773194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
527889eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
52793194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
52803194fc30SMatthew G. Knepley          */
52813194fc30SMatthew G. Knepley         {
52823194fc30SMatthew G. Knepley           const PetscInt of   = 0;
52833194fc30SMatthew G. Knepley           const PetscInt oeb  = of   + PetscSqr(k-1);
52843194fc30SMatthew G. Knepley           const PetscInt oer  = oeb  + (k-1);
52853194fc30SMatthew G. Knepley           const PetscInt oet  = oer  + (k-1);
52863194fc30SMatthew G. Knepley           const PetscInt oel  = oet  + (k-1);
52873194fc30SMatthew G. Knepley           const PetscInt ovlb = oel  + (k-1);
52883194fc30SMatthew G. Knepley           const PetscInt ovrb = ovlb + 1;
52893194fc30SMatthew G. Knepley           const PetscInt ovrt = ovrb + 1;
52903194fc30SMatthew G. Knepley           const PetscInt ovlt = ovrt + 1;
52913194fc30SMatthew G. Knepley           PetscInt       o;
52923194fc30SMatthew G. Knepley 
52933194fc30SMatthew G. Knepley           /* bottom */
52943194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
52953194fc30SMatthew G. Knepley           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
52963194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
52973194fc30SMatthew G. Knepley           /* middle */
52983194fc30SMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
52993194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
53003194fc30SMatthew 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;
53013194fc30SMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
53023194fc30SMatthew G. Knepley           }
53033194fc30SMatthew G. Knepley           /* top */
53043194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
53053194fc30SMatthew G. Knepley           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
53063194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
53073194fc30SMatthew G. Knepley           foffset = offset;
53083194fc30SMatthew G. Knepley         }
530989eabcffSMatthew G. Knepley         break;
531089eabcffSMatthew G. Knepley       case 3:
531189eabcffSMatthew G. Knepley         /* The original hex closure is
531289eabcffSMatthew G. Knepley 
531389eabcffSMatthew G. Knepley          {c,
531489eabcffSMatthew G. Knepley          f_b, f_t, f_f, f_b, f_r, f_l,
531589eabcffSMatthew 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,
531689eabcffSMatthew G. Knepley          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
531789eabcffSMatthew G. Knepley          */
53189e8305c2SJed Brown         ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
531989eabcffSMatthew G. Knepley         /* The SEM order is
532089eabcffSMatthew G. Knepley          Bottom Slice
532189eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
532289eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
532389eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
532489eabcffSMatthew G. Knepley 
532589eabcffSMatthew G. Knepley          Middle Slice (j)
532689eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
532789eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
532889eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
532989eabcffSMatthew G. Knepley 
533089eabcffSMatthew G. Knepley          Top Slice
533189eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
533289eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
533389eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
533489eabcffSMatthew G. Knepley          */
533589eabcffSMatthew G. Knepley         {
533689eabcffSMatthew G. Knepley           const PetscInt oc    = 0;
533789eabcffSMatthew G. Knepley           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
533889eabcffSMatthew G. Knepley           const PetscInt oft   = ofb   + PetscSqr(k-1);
533989eabcffSMatthew G. Knepley           const PetscInt off   = oft   + PetscSqr(k-1);
534089eabcffSMatthew G. Knepley           const PetscInt ofk   = off   + PetscSqr(k-1);
534189eabcffSMatthew G. Knepley           const PetscInt ofr   = ofk   + PetscSqr(k-1);
534289eabcffSMatthew G. Knepley           const PetscInt ofl   = ofr   + PetscSqr(k-1);
534389eabcffSMatthew G. Knepley           const PetscInt oebl  = ofl   + PetscSqr(k-1);
534489eabcffSMatthew G. Knepley           const PetscInt oebb  = oebl  + (k-1);
534589eabcffSMatthew G. Knepley           const PetscInt oebr  = oebb  + (k-1);
534689eabcffSMatthew G. Knepley           const PetscInt oebf  = oebr  + (k-1);
534789eabcffSMatthew G. Knepley           const PetscInt oetf  = oebf  + (k-1);
534889eabcffSMatthew G. Knepley           const PetscInt oetr  = oetf  + (k-1);
534989eabcffSMatthew G. Knepley           const PetscInt oetb  = oetr  + (k-1);
535089eabcffSMatthew G. Knepley           const PetscInt oetl  = oetb  + (k-1);
535189eabcffSMatthew G. Knepley           const PetscInt oerf  = oetl  + (k-1);
535289eabcffSMatthew G. Knepley           const PetscInt oelf  = oerf  + (k-1);
535389eabcffSMatthew G. Knepley           const PetscInt oelb  = oelf  + (k-1);
535489eabcffSMatthew G. Knepley           const PetscInt oerb  = oelb  + (k-1);
535589eabcffSMatthew G. Knepley           const PetscInt ovblf = oerb  + (k-1);
535689eabcffSMatthew G. Knepley           const PetscInt ovblb = ovblf + 1;
535789eabcffSMatthew G. Knepley           const PetscInt ovbrb = ovblb + 1;
535889eabcffSMatthew G. Knepley           const PetscInt ovbrf = ovbrb + 1;
535989eabcffSMatthew G. Knepley           const PetscInt ovtlf = ovbrf + 1;
536089eabcffSMatthew G. Knepley           const PetscInt ovtrf = ovtlf + 1;
536189eabcffSMatthew G. Knepley           const PetscInt ovtrb = ovtrf + 1;
536289eabcffSMatthew G. Knepley           const PetscInt ovtlb = ovtrb + 1;
536389eabcffSMatthew G. Knepley           PetscInt       o, n;
536489eabcffSMatthew G. Knepley 
536589eabcffSMatthew G. Knepley           /* Bottom Slice */
536689eabcffSMatthew G. Knepley           /*   bottom */
536789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
536889eabcffSMatthew G. Knepley           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
536989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
537089eabcffSMatthew G. Knepley           /*   middle */
537189eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
537289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
5373316b7f87SMax 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;}
537489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
53753194fc30SMatthew G. Knepley           }
537689eabcffSMatthew G. Knepley           /*   top */
537789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
537889eabcffSMatthew G. Knepley           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
537989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
538089eabcffSMatthew G. Knepley 
538189eabcffSMatthew G. Knepley           /* Middle Slice */
538289eabcffSMatthew G. Knepley           for (j = 0; j < k-1; ++j) {
538389eabcffSMatthew G. Knepley             /*   bottom */
538489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
538589eabcffSMatthew 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;
538689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
538789eabcffSMatthew G. Knepley             /*   middle */
538889eabcffSMatthew G. Knepley             for (i = 0; i < k-1; ++i) {
538989eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
539089eabcffSMatthew 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;
539189eabcffSMatthew G. Knepley               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
539289eabcffSMatthew G. Knepley             }
539389eabcffSMatthew G. Knepley             /*   top */
539489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
539589eabcffSMatthew 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;
539689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
539789eabcffSMatthew G. Knepley           }
539889eabcffSMatthew G. Knepley 
539989eabcffSMatthew G. Knepley           /* Top Slice */
540089eabcffSMatthew G. Knepley           /*   bottom */
540189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
540289eabcffSMatthew G. Knepley           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
540389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
540489eabcffSMatthew G. Knepley           /*   middle */
540589eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
540689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
540789eabcffSMatthew 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;
540889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
540989eabcffSMatthew G. Knepley           }
541089eabcffSMatthew G. Knepley           /*   top */
541189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
541289eabcffSMatthew G. Knepley           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
541389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
541489eabcffSMatthew G. Knepley 
541589eabcffSMatthew G. Knepley           foffset = offset;
541689eabcffSMatthew G. Knepley         }
541789eabcffSMatthew G. Knepley         break;
541898921bdaSJacob Faibussowitsch       default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
541989eabcffSMatthew G. Knepley       }
542089eabcffSMatthew G. Knepley     }
54212c71b3e2SJacob Faibussowitsch     PetscCheckFalse(offset != size,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
54223194fc30SMatthew G. Knepley     /* Check permutation */
54233194fc30SMatthew G. Knepley     {
54243194fc30SMatthew G. Knepley       PetscInt *check;
54253194fc30SMatthew G. Knepley 
54263194fc30SMatthew G. Knepley       ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
54272c71b3e2SJacob Faibussowitsch       for (i = 0; i < size; ++i) {check[i] = -1; PetscCheckFalse(perm[i] < 0 || perm[i] >= size,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid permutation index p[%D] = %D", i, perm[i]);}
54283194fc30SMatthew G. Knepley       for (i = 0; i < size; ++i) check[perm[i]] = i;
54292c71b3e2SJacob Faibussowitsch       for (i = 0; i < size; ++i) {PetscCheckFalse(check[i] < 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
54303194fc30SMatthew G. Knepley       ierr = PetscFree(check);CHKERRQ(ierr);
54313194fc30SMatthew G. Knepley     }
5432bb197d40SJed Brown     ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
5433bb197d40SJed Brown   }
54343194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
54353194fc30SMatthew G. Knepley }
54363194fc30SMatthew G. Knepley 
5437e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
5438e071409bSToby Isaac {
5439e071409bSToby Isaac   PetscDS        prob;
5440e071409bSToby Isaac   PetscInt       depth, Nf, h;
5441e071409bSToby Isaac   DMLabel        label;
5442e071409bSToby Isaac   PetscErrorCode ierr;
5443e071409bSToby Isaac 
5444e071409bSToby Isaac   PetscFunctionBeginHot;
5445e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
5446e071409bSToby Isaac   Nf      = prob->Nf;
5447e071409bSToby Isaac   label   = dm->depthLabel;
5448e071409bSToby Isaac   *dspace = NULL;
5449e071409bSToby Isaac   if (field < Nf) {
5450e071409bSToby Isaac     PetscObject disc = prob->disc[field];
5451e071409bSToby Isaac 
5452e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
5453e071409bSToby Isaac       PetscDualSpace dsp;
5454e071409bSToby Isaac 
5455e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
5456e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
5457e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
5458e071409bSToby Isaac       h    = depth - 1 - h;
5459e071409bSToby Isaac       if (h) {
5460e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
5461e071409bSToby Isaac       } else {
5462e071409bSToby Isaac         *dspace = dsp;
5463e071409bSToby Isaac       }
5464e071409bSToby Isaac     }
5465e071409bSToby Isaac   }
5466e071409bSToby Isaac   PetscFunctionReturn(0);
5467e071409bSToby Isaac }
5468e071409bSToby Isaac 
54699fbee547SJacob Faibussowitsch static inline PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5470a6dfd86eSKarl Rupp {
5471552f7358SJed Brown   PetscScalar    *array, *vArray;
5472d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
54731a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
5474552f7358SJed Brown   PetscErrorCode  ierr;
5475552f7358SJed Brown 
54761b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
54772a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
54785a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
54795a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
54805a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
54813f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
54829df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
54839df71ca4SMatthew G. Knepley       PetscInt dof;
5484d9917b9dSMatthew G. Knepley 
54859df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
54869df71ca4SMatthew G. Knepley       size += dof;
54879df71ca4SMatthew G. Knepley     }
54889df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
54899df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
54902a3aaacfSMatthew G. Knepley       PetscInt       dof;
54915a1bb5cfSMatthew G. Knepley 
54925a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
54932a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
54945a1bb5cfSMatthew G. Knepley       size += dof;
54955a1bb5cfSMatthew G. Knepley     }
54963f7cbbe7SMatthew G. Knepley     if (!values) {
54973f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
54983f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
54993f7cbbe7SMatthew G. Knepley     }
550069291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
5501982e9ed1SMatthew G. Knepley   } else {
5502982e9ed1SMatthew G. Knepley     array = *values;
5503982e9ed1SMatthew G. Knepley   }
55049df71ca4SMatthew G. Knepley   size = 0;
55055a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
55069df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
55079df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
55089df71ca4SMatthew G. Knepley     PetscScalar *varr;
5509d9917b9dSMatthew G. Knepley 
55109df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
55119df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
55129df71ca4SMatthew G. Knepley     varr = &vArray[off];
55131a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
55141a271a75SMatthew G. Knepley       array[offset] = varr[d];
55159df71ca4SMatthew G. Knepley     }
55169df71ca4SMatthew G. Knepley     size += dof;
55179df71ca4SMatthew G. Knepley   }
55189df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
55199df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
55209df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
55215a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
55225a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
55235a1bb5cfSMatthew G. Knepley 
552452ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
55255a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
55265a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
55275a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
55285a1bb5cfSMatthew G. Knepley     if (o >= 0) {
55291a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
55301a271a75SMatthew G. Knepley         array[offset] = varr[d];
55315a1bb5cfSMatthew G. Knepley       }
55325a1bb5cfSMatthew G. Knepley     } else {
55331a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
55341a271a75SMatthew G. Knepley         array[offset] = varr[d];
55355a1bb5cfSMatthew G. Knepley       }
55365a1bb5cfSMatthew G. Knepley     }
55379df71ca4SMatthew G. Knepley     size += dof;
55385a1bb5cfSMatthew G. Knepley   }
55395a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
55409df71ca4SMatthew G. Knepley   if (!*values) {
55415a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
55425a1bb5cfSMatthew G. Knepley     *values = array;
55439df71ca4SMatthew G. Knepley   } else {
55442c71b3e2SJacob Faibussowitsch     PetscCheckFalse(size > *csize,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
55458c312ff3SMatthew G. Knepley     *csize = size;
55469df71ca4SMatthew G. Knepley   }
55475a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
55485a1bb5cfSMatthew G. Knepley }
5549d9917b9dSMatthew G. Knepley 
555027f02ce8SMatthew G. Knepley /* Compress out points not in the section */
55519fbee547SJacob Faibussowitsch static inline PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
555227f02ce8SMatthew G. Knepley {
555327f02ce8SMatthew G. Knepley   const PetscInt np = *numPoints;
555427f02ce8SMatthew G. Knepley   PetscInt       pStart, pEnd, p, q;
555527f02ce8SMatthew G. Knepley   PetscErrorCode ierr;
555627f02ce8SMatthew G. Knepley 
555727f02ce8SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
555827f02ce8SMatthew G. Knepley   for (p = 0, q = 0; p < np; ++p) {
555927f02ce8SMatthew G. Knepley     const PetscInt r = points[p*2];
556027f02ce8SMatthew G. Knepley     if ((r >= pStart) && (r < pEnd)) {
556127f02ce8SMatthew G. Knepley       points[q*2]   = r;
556227f02ce8SMatthew G. Knepley       points[q*2+1] = points[p*2+1];
556327f02ce8SMatthew G. Knepley       ++q;
556427f02ce8SMatthew G. Knepley     }
556527f02ce8SMatthew G. Knepley   }
556627f02ce8SMatthew G. Knepley   *numPoints = q;
556727f02ce8SMatthew G. Knepley   return 0;
556827f02ce8SMatthew G. Knepley }
556927f02ce8SMatthew G. Knepley 
557097529cf3SJed Brown /* Compressed closure does not apply closure permutation */
55711dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5572923c78e0SToby Isaac {
557327f02ce8SMatthew G. Knepley   const PetscInt *cla = NULL;
5574923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
5575923c78e0SToby Isaac   PetscErrorCode ierr;
5576923c78e0SToby Isaac 
5577923c78e0SToby Isaac   PetscFunctionBeginHot;
5578923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
557927f02ce8SMatthew G. Knepley   if (*clPoints) {
5580923c78e0SToby Isaac     PetscInt dof, off;
5581923c78e0SToby Isaac 
5582923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
5583923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
5584923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
5585923c78e0SToby Isaac     np   = dof/2;
5586923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
558727f02ce8SMatthew G. Knepley   } else {
558827f02ce8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
558927f02ce8SMatthew G. Knepley     ierr = CompressPoints_Private(section, &np, pts);CHKERRQ(ierr);
5590923c78e0SToby Isaac   }
5591923c78e0SToby Isaac   *numPoints = np;
5592923c78e0SToby Isaac   *points    = pts;
5593923c78e0SToby Isaac   *clp       = cla;
5594923c78e0SToby Isaac   PetscFunctionReturn(0);
5595923c78e0SToby Isaac }
5596923c78e0SToby Isaac 
55971dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5598923c78e0SToby Isaac {
5599923c78e0SToby Isaac   PetscErrorCode ierr;
5600923c78e0SToby Isaac 
5601923c78e0SToby Isaac   PetscFunctionBeginHot;
5602923c78e0SToby Isaac   if (!*clPoints) {
5603923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
5604923c78e0SToby Isaac   } else {
5605923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
5606923c78e0SToby Isaac   }
5607923c78e0SToby Isaac   *numPoints = 0;
5608923c78e0SToby Isaac   *points    = NULL;
5609923c78e0SToby Isaac   *clSec     = NULL;
5610923c78e0SToby Isaac   *clPoints  = NULL;
5611923c78e0SToby Isaac   *clp       = NULL;
5612923c78e0SToby Isaac   PetscFunctionReturn(0);
5613923c78e0SToby Isaac }
5614923c78e0SToby Isaac 
56159fbee547SJacob Faibussowitsch static inline PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
56161a271a75SMatthew G. Knepley {
56171a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
561897e99dd9SToby Isaac   const PetscInt    **perms = NULL;
561997e99dd9SToby Isaac   const PetscScalar **flips = NULL;
56201a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
56211a271a75SMatthew G. Knepley 
56221a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5623fe02ba77SJed Brown   *size = 0;
562497e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
562597e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
562697e99dd9SToby Isaac     const PetscInt    point = points[2*p];
562797e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
562897e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
56291a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
56301a271a75SMatthew G. Knepley     const PetscScalar *varr;
56311a271a75SMatthew G. Knepley 
56321a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
56331a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
56341a271a75SMatthew G. Knepley     varr = &vArray[off];
563597e99dd9SToby Isaac     if (clperm) {
563697e99dd9SToby Isaac       if (perm) {
563797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
56381a271a75SMatthew G. Knepley       } else {
563997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
564097e99dd9SToby Isaac       }
564197e99dd9SToby Isaac       if (flip) {
564297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
564397e99dd9SToby Isaac       }
564497e99dd9SToby Isaac     } else {
564597e99dd9SToby Isaac       if (perm) {
564697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
564797e99dd9SToby Isaac       } else {
564897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
564997e99dd9SToby Isaac       }
565097e99dd9SToby Isaac       if (flip) {
565197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
56521a271a75SMatthew G. Knepley       }
56531a271a75SMatthew G. Knepley     }
565497e99dd9SToby Isaac     offset += dof;
565597e99dd9SToby Isaac   }
565697e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
56571a271a75SMatthew G. Knepley   *size = offset;
56581a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
56591a271a75SMatthew G. Knepley }
56601a271a75SMatthew G. Knepley 
56619fbee547SJacob Faibussowitsch 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[])
56621a271a75SMatthew G. Knepley {
56631a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
56641a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
56651a271a75SMatthew G. Knepley 
56661a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
5667fe02ba77SJed Brown   *size = 0;
56681a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
566997e99dd9SToby Isaac     PetscInt          p;
567097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
567197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
56721a271a75SMatthew G. Knepley 
567397e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
567497e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
567597e99dd9SToby Isaac       const PetscInt    point = points[2*p];
567697e99dd9SToby Isaac       PetscInt          fdof, foff, b;
56771a271a75SMatthew G. Knepley       const PetscScalar *varr;
567897e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
567997e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
56801a271a75SMatthew G. Knepley 
56811a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
56821a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
56831a271a75SMatthew G. Knepley       varr = &vArray[foff];
568497e99dd9SToby Isaac       if (clperm) {
568597e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
568697e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
568797e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
56881a271a75SMatthew G. Knepley       } else {
568997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
569097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
569197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
56921a271a75SMatthew G. Knepley       }
569397e99dd9SToby Isaac       offset += fdof;
56941a271a75SMatthew G. Knepley     }
569597e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
56961a271a75SMatthew G. Knepley   }
56971a271a75SMatthew G. Knepley   *size = offset;
56981a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
56991a271a75SMatthew G. Knepley }
57001a271a75SMatthew G. Knepley 
5701552f7358SJed Brown /*@C
5702552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
5703552f7358SJed Brown 
5704552f7358SJed Brown   Not collective
5705552f7358SJed Brown 
5706552f7358SJed Brown   Input Parameters:
5707552f7358SJed Brown + dm - The DM
5708552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5709552f7358SJed Brown . v - The local vector
57106b867d5aSJose E. Roman - point - The point in the DM
5711552f7358SJed Brown 
57126b867d5aSJose E. Roman   Input/Output Parameters:
57136b867d5aSJose E. Roman + csize  - The size of the input values array, or NULL; on output the number of values in the closure
57146b867d5aSJose E. Roman - values - An array to use for the values, or NULL to have it allocated automatically;
57156b867d5aSJose E. Roman            if the user provided NULL, it is a borrowed array and should not be freed
571622c1ee49SMatthew G. Knepley 
571722c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
571822c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
571922c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
572022c1ee49SMatthew G. Knepley $
572122c1ee49SMatthew G. Knepley $ A typical use could be
572222c1ee49SMatthew G. Knepley $
572322c1ee49SMatthew G. Knepley $  values = NULL;
572422c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
572522c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
572622c1ee49SMatthew G. Knepley $    <Compute on closure>
572722c1ee49SMatthew G. Knepley $  }
572822c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
572922c1ee49SMatthew G. Knepley $
573022c1ee49SMatthew G. Knepley $ or
573122c1ee49SMatthew G. Knepley $
573222c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
573322c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
573422c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
573522c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
573622c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
573722c1ee49SMatthew G. Knepley $      <Compute on closure>
573822c1ee49SMatthew G. Knepley $    }
573922c1ee49SMatthew G. Knepley $  }
574022c1ee49SMatthew G. Knepley $  PetscFree(values);
5741552f7358SJed Brown 
5742552f7358SJed Brown   Fortran Notes:
5743552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
5744552f7358SJed Brown   include petsc.h90 in your code.
5745552f7358SJed Brown 
5746552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
5747552f7358SJed Brown 
5748552f7358SJed Brown   Level: intermediate
5749552f7358SJed Brown 
5750552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5751552f7358SJed Brown @*/
5752552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5753552f7358SJed Brown {
5754552f7358SJed Brown   PetscSection       clSection;
5755d9917b9dSMatthew G. Knepley   IS                 clPoints;
5756552f7358SJed Brown   PetscInt          *points = NULL;
5757c459fbc1SJed Brown   const PetscInt    *clp, *perm;
5758c459fbc1SJed Brown   PetscInt           depth, numFields, numPoints, asize;
5759552f7358SJed Brown   PetscErrorCode     ierr;
5760552f7358SJed Brown 
5761d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
5762552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
576392fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
57641a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
57651a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5766552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5767552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5768552f7358SJed Brown   if (depth == 1 && numFields < 2) {
57691a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5770552f7358SJed Brown     PetscFunctionReturn(0);
5771552f7358SJed Brown   }
57721a271a75SMatthew G. Knepley   /* Get points */
5773923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5774c459fbc1SJed Brown   /* Get sizes */
5775c459fbc1SJed Brown   asize = 0;
5776c459fbc1SJed Brown   for (PetscInt p = 0; p < numPoints*2; p += 2) {
5777c459fbc1SJed Brown     PetscInt dof;
5778552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
57791a271a75SMatthew G. Knepley     asize += dof;
5780552f7358SJed Brown   }
5781c459fbc1SJed Brown   if (values) {
5782c459fbc1SJed Brown     const PetscScalar *vArray;
5783c459fbc1SJed Brown     PetscInt          size;
5784c459fbc1SJed Brown 
5785c459fbc1SJed Brown     if (*values) {
57862c71b3e2SJacob Faibussowitsch       PetscCheckFalse(*csize < asize,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Provided array size %D not sufficient to hold closure size %D", *csize, asize);
5787c459fbc1SJed Brown     } else {ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, values);CHKERRQ(ierr);}
5788c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);CHKERRQ(ierr);
57898a84db2dSMatthew G. Knepley     ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
57901a271a75SMatthew G. Knepley     /* Get values */
5791c459fbc1SJed Brown     if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);CHKERRQ(ierr);}
5792c459fbc1SJed Brown     else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);CHKERRQ(ierr);}
57932c71b3e2SJacob Faibussowitsch     PetscCheckFalse(asize != size,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
57941a271a75SMatthew G. Knepley     /* Cleanup array */
57958a84db2dSMatthew G. Knepley     ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5796d0f6b257SMatthew G. Knepley   }
5797c459fbc1SJed Brown   if (csize) *csize = asize;
5798c459fbc1SJed Brown   /* Cleanup points */
5799c459fbc1SJed Brown   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5800552f7358SJed Brown   PetscFunctionReturn(0);
5801552f7358SJed Brown }
5802552f7358SJed Brown 
5803e5c487bfSMatthew G. Knepley PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5804e5c487bfSMatthew G. Knepley {
5805e5c487bfSMatthew G. Knepley   DMLabel            depthLabel;
5806e5c487bfSMatthew G. Knepley   PetscSection       clSection;
5807e5c487bfSMatthew G. Knepley   IS                 clPoints;
5808e5c487bfSMatthew G. Knepley   PetscScalar       *array;
5809e5c487bfSMatthew G. Knepley   const PetscScalar *vArray;
5810e5c487bfSMatthew G. Knepley   PetscInt          *points = NULL;
5811c459fbc1SJed Brown   const PetscInt    *clp, *perm = NULL;
5812c459fbc1SJed Brown   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
5813e5c487bfSMatthew G. Knepley   PetscErrorCode     ierr;
5814e5c487bfSMatthew G. Knepley 
5815e5c487bfSMatthew G. Knepley   PetscFunctionBeginHot;
5816e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5817e5c487bfSMatthew G. Knepley   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5818e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5819e5c487bfSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5820e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &mdepth);CHKERRQ(ierr);
5821e5c487bfSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
5822e5c487bfSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5823e5c487bfSMatthew G. Knepley   if (mdepth == 1 && numFields < 2) {
5824e5c487bfSMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
5825e5c487bfSMatthew G. Knepley     PetscFunctionReturn(0);
5826e5c487bfSMatthew G. Knepley   }
5827e5c487bfSMatthew G. Knepley   /* Get points */
5828e5c487bfSMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5829c459fbc1SJed Brown   for (clsize=0,p=0; p<Np; p++) {
5830c459fbc1SJed Brown     PetscInt dof;
5831c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
5832c459fbc1SJed Brown     clsize += dof;
5833c459fbc1SJed Brown   }
5834c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);CHKERRQ(ierr);
5835e5c487bfSMatthew G. Knepley   /* Filter points */
5836e5c487bfSMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
5837e5c487bfSMatthew G. Knepley     PetscInt dep;
5838e5c487bfSMatthew G. Knepley 
5839e5c487bfSMatthew G. Knepley     ierr = DMLabelGetValue(depthLabel, points[p], &dep);CHKERRQ(ierr);
5840e5c487bfSMatthew G. Knepley     if (dep != depth) continue;
5841e5c487bfSMatthew G. Knepley     points[Np*2+0] = points[p];
5842e5c487bfSMatthew G. Knepley     points[Np*2+1] = points[p+1];
5843e5c487bfSMatthew G. Knepley     ++Np;
5844e5c487bfSMatthew G. Knepley   }
5845e5c487bfSMatthew G. Knepley   /* Get array */
5846e5c487bfSMatthew G. Knepley   if (!values || !*values) {
5847e5c487bfSMatthew G. Knepley     PetscInt asize = 0, dof;
5848e5c487bfSMatthew G. Knepley 
5849e5c487bfSMatthew G. Knepley     for (p = 0; p < Np*2; p += 2) {
5850e5c487bfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5851e5c487bfSMatthew G. Knepley       asize += dof;
5852e5c487bfSMatthew G. Knepley     }
5853e5c487bfSMatthew G. Knepley     if (!values) {
5854e5c487bfSMatthew G. Knepley       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5855e5c487bfSMatthew G. Knepley       if (csize) *csize = asize;
5856e5c487bfSMatthew G. Knepley       PetscFunctionReturn(0);
5857e5c487bfSMatthew G. Knepley     }
5858e5c487bfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
5859e5c487bfSMatthew G. Knepley   } else {
5860e5c487bfSMatthew G. Knepley     array = *values;
5861e5c487bfSMatthew G. Knepley   }
5862e5c487bfSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
5863e5c487bfSMatthew G. Knepley   /* Get values */
5864e5c487bfSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
5865e5c487bfSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);CHKERRQ(ierr);}
5866e5c487bfSMatthew G. Knepley   /* Cleanup points */
5867e5c487bfSMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5868e5c487bfSMatthew G. Knepley   /* Cleanup array */
5869e5c487bfSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
5870e5c487bfSMatthew G. Knepley   if (!*values) {
5871e5c487bfSMatthew G. Knepley     if (csize) *csize = size;
5872e5c487bfSMatthew G. Knepley     *values = array;
5873e5c487bfSMatthew G. Knepley   } else {
58742c71b3e2SJacob Faibussowitsch     PetscCheckFalse(size > *csize,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
5875e5c487bfSMatthew G. Knepley     *csize = size;
5876e5c487bfSMatthew G. Knepley   }
5877e5c487bfSMatthew G. Knepley   PetscFunctionReturn(0);
5878e5c487bfSMatthew G. Knepley }
5879e5c487bfSMatthew G. Knepley 
5880552f7358SJed Brown /*@C
5881552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5882552f7358SJed Brown 
5883552f7358SJed Brown   Not collective
5884552f7358SJed Brown 
5885552f7358SJed Brown   Input Parameters:
5886552f7358SJed Brown + dm - The DM
58870298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
5888552f7358SJed Brown . v - The local vector
5889eaf898f9SPatrick Sanan . point - The point in the DM
58900298fd71SBarry Smith . csize - The number of values in the closure, or NULL
5891552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
5892552f7358SJed Brown 
589322c1ee49SMatthew 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()
589422c1ee49SMatthew G. Knepley 
58953813dfbdSMatthew G Knepley   Fortran Notes:
58963813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
58973813dfbdSMatthew G Knepley   include petsc.h90 in your code.
58983813dfbdSMatthew G Knepley 
58993813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
59003813dfbdSMatthew G Knepley 
5901552f7358SJed Brown   Level: intermediate
5902552f7358SJed Brown 
5903552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5904552f7358SJed Brown @*/
59057c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5906a6dfd86eSKarl Rupp {
5907552f7358SJed Brown   PetscInt       size = 0;
5908552f7358SJed Brown   PetscErrorCode ierr;
5909552f7358SJed Brown 
5910552f7358SJed Brown   PetscFunctionBegin;
5911552f7358SJed Brown   /* Should work without recalculating size */
591269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
5913c9fdaa05SMatthew G. Knepley   *values = NULL;
5914552f7358SJed Brown   PetscFunctionReturn(0);
5915552f7358SJed Brown }
5916552f7358SJed Brown 
59179fbee547SJacob Faibussowitsch static inline void add   (PetscScalar *x, PetscScalar y) {*x += y;}
59189fbee547SJacob Faibussowitsch static inline void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
5919552f7358SJed Brown 
59209fbee547SJacob Faibussowitsch 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[])
5921552f7358SJed Brown {
5922552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5923552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5924552f7358SJed Brown   PetscScalar    *a;
5925552f7358SJed Brown   PetscInt        off, cind = 0, k;
5926552f7358SJed Brown   PetscErrorCode  ierr;
5927552f7358SJed Brown 
5928552f7358SJed Brown   PetscFunctionBegin;
5929552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5930552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5931552f7358SJed Brown   a    = &array[off];
5932552f7358SJed Brown   if (!cdof || setBC) {
593397e99dd9SToby Isaac     if (clperm) {
593497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
593597e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5936552f7358SJed Brown     } else {
593797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
593897e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5939552f7358SJed Brown     }
5940552f7358SJed Brown   } else {
5941552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
594297e99dd9SToby Isaac     if (clperm) {
594397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
5944552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
594597e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5946552f7358SJed Brown         }
5947552f7358SJed Brown       } else {
5948552f7358SJed Brown         for (k = 0; k < dof; ++k) {
5949552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
595097e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
595197e99dd9SToby Isaac         }
595297e99dd9SToby Isaac       }
595397e99dd9SToby Isaac     } else {
595497e99dd9SToby Isaac       if (perm) {
595597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
595697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
595797e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
595897e99dd9SToby Isaac         }
595997e99dd9SToby Isaac       } else {
596097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
596197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
596297e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
596397e99dd9SToby Isaac         }
5964552f7358SJed Brown       }
5965552f7358SJed Brown     }
5966552f7358SJed Brown   }
5967552f7358SJed Brown   PetscFunctionReturn(0);
5968552f7358SJed Brown }
5969552f7358SJed Brown 
59709fbee547SJacob Faibussowitsch 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[])
5971a5e93ea8SMatthew G. Knepley {
5972a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5973a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5974a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5975a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5976a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5977a5e93ea8SMatthew G. Knepley 
5978a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5979a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5980a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5981a5e93ea8SMatthew G. Knepley   a    = &array[off];
5982a5e93ea8SMatthew G. Knepley   if (cdof) {
5983a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
598497e99dd9SToby Isaac     if (clperm) {
598597e99dd9SToby Isaac       if (perm) {
5986a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5987a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
598897e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
598997e99dd9SToby Isaac             cind++;
5990a5e93ea8SMatthew G. Knepley           }
5991a5e93ea8SMatthew G. Knepley         }
5992a5e93ea8SMatthew G. Knepley       } else {
5993a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
5994a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
599597e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
599697e99dd9SToby Isaac             cind++;
599797e99dd9SToby Isaac           }
599897e99dd9SToby Isaac         }
599997e99dd9SToby Isaac       }
600097e99dd9SToby Isaac     } else {
600197e99dd9SToby Isaac       if (perm) {
600297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
600397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
600497e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
600597e99dd9SToby Isaac             cind++;
600697e99dd9SToby Isaac           }
600797e99dd9SToby Isaac         }
600897e99dd9SToby Isaac       } else {
600997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
601097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
601197e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
601297e99dd9SToby Isaac             cind++;
601397e99dd9SToby Isaac           }
6014a5e93ea8SMatthew G. Knepley         }
6015a5e93ea8SMatthew G. Knepley       }
6016a5e93ea8SMatthew G. Knepley     }
6017a5e93ea8SMatthew G. Knepley   }
6018a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6019a5e93ea8SMatthew G. Knepley }
6020a5e93ea8SMatthew G. Knepley 
60219fbee547SJacob Faibussowitsch 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[])
6022a6dfd86eSKarl Rupp {
6023552f7358SJed Brown   PetscScalar    *a;
60241a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
60251a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
602697e99dd9SToby Isaac   PetscInt        cind = 0, b;
6027552f7358SJed Brown   PetscErrorCode  ierr;
6028552f7358SJed Brown 
6029552f7358SJed Brown   PetscFunctionBegin;
6030552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
6031552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
60321a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
60331a271a75SMatthew G. Knepley   a    = &array[foff];
6034552f7358SJed Brown   if (!fcdof || setBC) {
603597e99dd9SToby Isaac     if (clperm) {
603697e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
603797e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
6038552f7358SJed Brown     } else {
603997e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
604097e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
6041552f7358SJed Brown     }
6042552f7358SJed Brown   } else {
6043552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
604497e99dd9SToby Isaac     if (clperm) {
604597e99dd9SToby Isaac       if (perm) {
604697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
604797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
604897e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
6049552f7358SJed Brown         }
6050552f7358SJed Brown       } else {
605197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
605297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
605397e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
605497e99dd9SToby Isaac         }
605597e99dd9SToby Isaac       }
605697e99dd9SToby Isaac     } else {
605797e99dd9SToby Isaac       if (perm) {
605897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
605997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
606097e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
606197e99dd9SToby Isaac         }
606297e99dd9SToby Isaac       } else {
606397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
606497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
606597e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
6066552f7358SJed Brown         }
6067552f7358SJed Brown       }
6068552f7358SJed Brown     }
6069552f7358SJed Brown   }
60701a271a75SMatthew G. Knepley   *offset += fdof;
6071552f7358SJed Brown   PetscFunctionReturn(0);
6072552f7358SJed Brown }
6073552f7358SJed Brown 
60749fbee547SJacob Faibussowitsch 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[])
6075a5e93ea8SMatthew G. Knepley {
6076a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
60771a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
60781a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
60795da9d227SMatthew G. Knepley   PetscInt        Nc, cind = 0, ncind = 0, b;
6080ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
6081a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
6082a5e93ea8SMatthew G. Knepley 
6083a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
60845da9d227SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
6085a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
6086a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
60871a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
60881a271a75SMatthew G. Knepley   a    = &array[foff];
6089a5e93ea8SMatthew G. Knepley   if (fcdof) {
6090ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
6091a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
609297e99dd9SToby Isaac     if (clperm) {
609397e99dd9SToby Isaac       if (perm) {
6094ba322698SMatthew G. Knepley         if (comps) {
6095ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
6096ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
60975da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6098ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6099ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
6100ba322698SMatthew G. Knepley           }
6101ba322698SMatthew G. Knepley         } else {
610297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
610397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
610497e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
6105a5e93ea8SMatthew G. Knepley               ++cind;
6106a5e93ea8SMatthew G. Knepley             }
6107a5e93ea8SMatthew G. Knepley           }
6108ba322698SMatthew G. Knepley         }
6109ba322698SMatthew G. Knepley       } else {
6110ba322698SMatthew G. Knepley         if (comps) {
6111ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
6112ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
61135da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6114ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6115ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
6116ba322698SMatthew G. Knepley           }
6117a5e93ea8SMatthew G. Knepley         } else {
611897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
611997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
612097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
612197e99dd9SToby Isaac               ++cind;
612297e99dd9SToby Isaac             }
612397e99dd9SToby Isaac           }
612497e99dd9SToby Isaac         }
6125ba322698SMatthew G. Knepley       }
612697e99dd9SToby Isaac     } else {
612797e99dd9SToby Isaac       if (perm) {
6128ba322698SMatthew G. Knepley         if (comps) {
6129ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
6130ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
61315da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6132ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6133ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
6134ba322698SMatthew G. Knepley           }
6135ba322698SMatthew G. Knepley         } else {
613697e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
613797e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
613897e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
613997e99dd9SToby Isaac               ++cind;
614097e99dd9SToby Isaac             }
614197e99dd9SToby Isaac           }
6142ba322698SMatthew G. Knepley         }
6143ba322698SMatthew G. Knepley       } else {
6144ba322698SMatthew G. Knepley         if (comps) {
6145ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
6146ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
61475da9d227SMatthew G. Knepley             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6148ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6149ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
6150ba322698SMatthew G. Knepley           }
615197e99dd9SToby Isaac         } else {
615297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
615397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
615497e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
6155a5e93ea8SMatthew G. Knepley               ++cind;
6156a5e93ea8SMatthew G. Knepley             }
6157a5e93ea8SMatthew G. Knepley           }
6158a5e93ea8SMatthew G. Knepley         }
6159a5e93ea8SMatthew G. Knepley       }
6160a5e93ea8SMatthew G. Knepley     }
6161ba322698SMatthew G. Knepley   }
61621a271a75SMatthew G. Knepley   *offset += fdof;
6163a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6164a5e93ea8SMatthew G. Knepley }
6165a5e93ea8SMatthew G. Knepley 
61669fbee547SJacob Faibussowitsch static inline PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6167a6dfd86eSKarl Rupp {
6168552f7358SJed Brown   PetscScalar    *array;
61691b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
61701b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
6171552f7358SJed Brown   PetscErrorCode  ierr;
6172552f7358SJed Brown 
61731b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
6174b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
6175b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
6176b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
6177b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
6178b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
6179b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
6180b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
6181b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
6182b6ebb6e6SMatthew G. Knepley 
6183b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
6184b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
6185b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
6186b6ebb6e6SMatthew G. Knepley     {
6187b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6188b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
6189b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
6190b6ebb6e6SMatthew G. Knepley 
6191b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
6192b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
6193b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
6194b6ebb6e6SMatthew G. Knepley       if (!cdof) {
6195b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
6196b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
6197b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
6198b6ebb6e6SMatthew G. Knepley           }
6199b6ebb6e6SMatthew G. Knepley         } else {
6200b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
6201b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
6202b6ebb6e6SMatthew G. Knepley           }
6203b6ebb6e6SMatthew G. Knepley         }
6204b6ebb6e6SMatthew G. Knepley       } else {
6205b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
6206b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
6207b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
6208b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
6209b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
6210b6ebb6e6SMatthew G. Knepley           }
6211b6ebb6e6SMatthew G. Knepley         } else {
6212b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
6213b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
6214b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
6215b6ebb6e6SMatthew G. Knepley           }
6216b6ebb6e6SMatthew G. Knepley         }
6217b6ebb6e6SMatthew G. Knepley       }
6218b6ebb6e6SMatthew G. Knepley     }
6219b6ebb6e6SMatthew G. Knepley   }
6220b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
6221b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
6222b6ebb6e6SMatthew G. Knepley }
62231b406b76SMatthew G. Knepley 
62241b406b76SMatthew G. Knepley /*@C
62251b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
62261b406b76SMatthew G. Knepley 
62271b406b76SMatthew G. Knepley   Not collective
62281b406b76SMatthew G. Knepley 
62291b406b76SMatthew G. Knepley   Input Parameters:
62301b406b76SMatthew G. Knepley + dm - The DM
62311b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
62321b406b76SMatthew G. Knepley . v - The local vector
6233eaf898f9SPatrick Sanan . point - The point in the DM
62341b406b76SMatthew G. Knepley . values - The array of values
623522c1ee49SMatthew 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,
623622c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
62371b406b76SMatthew G. Knepley 
62381b406b76SMatthew G. Knepley   Fortran Notes:
62391b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
62401b406b76SMatthew G. Knepley 
62411b406b76SMatthew G. Knepley   Level: intermediate
62421b406b76SMatthew G. Knepley 
62431b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
62441b406b76SMatthew G. Knepley @*/
62451b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
62461b406b76SMatthew G. Knepley {
62471b406b76SMatthew G. Knepley   PetscSection    clSection;
62481b406b76SMatthew G. Knepley   IS              clPoints;
62491b406b76SMatthew G. Knepley   PetscScalar    *array;
62501b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
625127f02ce8SMatthew G. Knepley   const PetscInt *clp, *clperm = NULL;
6252c459fbc1SJed Brown   PetscInt        depth, numFields, numPoints, p, clsize;
62531b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
62541b406b76SMatthew G. Knepley 
62551a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
62561b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
625792fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
62581a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
62591a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
62601b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
62611b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
62621b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
62638f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
62641b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
62651b406b76SMatthew G. Knepley   }
62661a271a75SMatthew G. Knepley   /* Get points */
6267923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6268c459fbc1SJed Brown   for (clsize=0,p=0; p<numPoints; p++) {
6269c459fbc1SJed Brown     PetscInt dof;
6270c459fbc1SJed Brown     ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
6271c459fbc1SJed Brown     clsize += dof;
6272c459fbc1SJed Brown   }
6273c459fbc1SJed Brown   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
62741a271a75SMatthew G. Knepley   /* Get array */
6275552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
62761a271a75SMatthew G. Knepley   /* Get values */
6277ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
627897e99dd9SToby Isaac     PetscInt offset = 0, f;
6279552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
628097e99dd9SToby Isaac       const PetscInt    **perms = NULL;
628197e99dd9SToby Isaac       const PetscScalar **flips = NULL;
628297e99dd9SToby Isaac 
628397e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
6284552f7358SJed Brown       switch (mode) {
6285552f7358SJed Brown       case INSERT_VALUES:
628697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
628797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
628897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
628997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
629097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
6291552f7358SJed Brown         } break;
6292552f7358SJed Brown       case INSERT_ALL_VALUES:
629397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
629497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
629597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
629697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
629797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
6298552f7358SJed Brown         } break;
6299a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
630097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
630197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
630297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
630397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
6304ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
6305a5e93ea8SMatthew G. Knepley         } break;
6306552f7358SJed Brown       case ADD_VALUES:
630797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
630897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
630997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
631097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
631197e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
6312552f7358SJed Brown         } break;
6313552f7358SJed Brown       case ADD_ALL_VALUES:
631497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
631597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
631697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
631797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
631897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
6319552f7358SJed Brown         } break;
6320304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
632197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
632297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
632397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
632497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
6325ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
6326304ab55fSMatthew G. Knepley         } break;
6327552f7358SJed Brown       default:
632898921bdaSJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6329552f7358SJed Brown       }
633097e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
63311a271a75SMatthew G. Knepley     }
6332552f7358SJed Brown   } else {
63331a271a75SMatthew G. Knepley     PetscInt dof, off;
633497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
633597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
63361a271a75SMatthew G. Knepley 
633797e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
6338552f7358SJed Brown     switch (mode) {
6339552f7358SJed Brown     case INSERT_VALUES:
634097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
634197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
634297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
634397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
634497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
634597e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
6346552f7358SJed Brown       } break;
6347552f7358SJed Brown     case INSERT_ALL_VALUES:
634897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
634997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
635097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
635197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
635297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
635397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
6354552f7358SJed Brown       } break;
6355a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
635697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
635797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
635897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
635997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
636097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
636197e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
6362a5e93ea8SMatthew G. Knepley       } break;
6363552f7358SJed Brown     case ADD_VALUES:
636497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
636597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
636697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
636797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
636897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
636997e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
6370552f7358SJed Brown       } break;
6371552f7358SJed Brown     case ADD_ALL_VALUES:
637297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
637397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
637497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
637597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
637697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
637797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
6378552f7358SJed Brown       } break;
6379304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
638097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
638197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
638297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
638397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
638497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
638597e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
6386304ab55fSMatthew G. Knepley       } break;
6387552f7358SJed Brown     default:
638898921bdaSJacob Faibussowitsch       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6389552f7358SJed Brown     }
639097e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
6391552f7358SJed Brown   }
63921a271a75SMatthew G. Knepley   /* Cleanup points */
6393923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
63941a271a75SMatthew G. Knepley   /* Cleanup array */
6395552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
6396552f7358SJed Brown   PetscFunctionReturn(0);
6397552f7358SJed Brown }
6398552f7358SJed Brown 
63995f790a90SMatthew G. Knepley /* Check whether the given point is in the label. If not, update the offset to skip this point */
64009fbee547SJacob Faibussowitsch static inline PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
64015f790a90SMatthew G. Knepley {
64025f790a90SMatthew G. Knepley   PetscFunctionBegin;
64035f790a90SMatthew G. Knepley   if (label) {
64045f790a90SMatthew G. Knepley     PetscInt       val, fdof;
64055f790a90SMatthew G. Knepley     PetscErrorCode ierr;
64065f790a90SMatthew G. Knepley 
64075f790a90SMatthew G. Knepley     /* There is a problem with this:
64085f790a90SMatthew G. Knepley          Suppose we have two label values, defining surfaces, interecting along a line in 3D. When we add cells to the label, the cells that
64095f790a90SMatthew G. Knepley        touch both surfaces must pick a label value. Thus we miss setting values for the surface with that other value intersecting that cell.
64105f790a90SMatthew G. Knepley        Thus I am only going to check val != -1, not val != labelId
64115f790a90SMatthew G. Knepley     */
64125f790a90SMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
64135f790a90SMatthew G. Knepley     if (val < 0) {
64145f790a90SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
64155f790a90SMatthew G. Knepley       *offset += fdof;
64165f790a90SMatthew G. Knepley       PetscFunctionReturn(1);
64175f790a90SMatthew G. Knepley     }
64185f790a90SMatthew G. Knepley   }
64195f790a90SMatthew G. Knepley   PetscFunctionReturn(0);
64205f790a90SMatthew G. Knepley }
64215f790a90SMatthew G. Knepley 
642297529cf3SJed Brown /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
64235f790a90SMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], DMLabel label, PetscInt labelId, const PetscScalar values[], InsertMode mode)
6424e07394fbSMatthew G. Knepley {
6425e07394fbSMatthew G. Knepley   PetscSection      clSection;
6426e07394fbSMatthew G. Knepley   IS                clPoints;
6427e07394fbSMatthew G. Knepley   PetscScalar       *array;
6428e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
642997529cf3SJed Brown   const PetscInt    *clp;
6430e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
643197e99dd9SToby Isaac   PetscInt          offset = 0, f;
6432e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
6433e07394fbSMatthew G. Knepley 
6434e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
6435e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
643692fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
6437e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6438e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
6439e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6440e07394fbSMatthew G. Knepley   /* Get points */
6441923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6442e07394fbSMatthew G. Knepley   /* Get array */
6443e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
6444e07394fbSMatthew G. Knepley   /* Get values */
6445e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
644697e99dd9SToby Isaac     const PetscInt    **perms = NULL;
644797e99dd9SToby Isaac     const PetscScalar **flips = NULL;
644897e99dd9SToby Isaac 
6449e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
6450e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
6451e07394fbSMatthew G. Knepley         PetscInt fdof;
6452e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
6453e07394fbSMatthew G. Knepley         offset += fdof;
6454e07394fbSMatthew G. Knepley       }
6455e07394fbSMatthew G. Knepley       continue;
6456e07394fbSMatthew G. Knepley     }
645797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
6458e07394fbSMatthew G. Knepley     switch (mode) {
6459e07394fbSMatthew G. Knepley     case INSERT_VALUES:
646097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
646197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
646297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
646397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
64645f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
646597529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
6466e07394fbSMatthew G. Knepley       } break;
6467e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
646897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
646997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
647097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
647197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
64725f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
647397529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
6474e07394fbSMatthew G. Knepley       } break;
6475e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
647697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
647797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
647897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
647997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
64805f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
648197529cf3SJed Brown         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
6482e07394fbSMatthew G. Knepley       } break;
6483e07394fbSMatthew G. Knepley     case ADD_VALUES:
648497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
648597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
648697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
648797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
64885f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
648997529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
6490e07394fbSMatthew G. Knepley       } break;
6491e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
649297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
649397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
649497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
649597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
64965f790a90SMatthew G. Knepley         ierr = CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
649797529cf3SJed Brown         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
6498e07394fbSMatthew G. Knepley       } break;
6499e07394fbSMatthew G. Knepley     default:
650098921bdaSJacob Faibussowitsch       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6501e07394fbSMatthew G. Knepley     }
650297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
6503e07394fbSMatthew G. Knepley   }
6504e07394fbSMatthew G. Knepley   /* Cleanup points */
6505923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6506e07394fbSMatthew G. Knepley   /* Cleanup array */
6507e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
6508e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
6509e07394fbSMatthew G. Knepley }
6510e07394fbSMatthew G. Knepley 
65117cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
6512552f7358SJed Brown {
6513552f7358SJed Brown   PetscMPIInt    rank;
6514552f7358SJed Brown   PetscInt       i, j;
6515552f7358SJed Brown   PetscErrorCode ierr;
6516552f7358SJed Brown 
6517552f7358SJed Brown   PetscFunctionBegin;
6518ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr);
6519eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
6520e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
6521e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
6522b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
6523557cf195SMatthew G. Knepley   if (!values) PetscFunctionReturn(0);
6524b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
6525e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
6526b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
6527519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
65287eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
6529552f7358SJed Brown #else
6530b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
6531552f7358SJed Brown #endif
6532552f7358SJed Brown     }
653377a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
6534552f7358SJed Brown   }
6535552f7358SJed Brown   PetscFunctionReturn(0);
6536552f7358SJed Brown }
6537552f7358SJed Brown 
653805586334SMatthew G. Knepley /*
653905586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
654005586334SMatthew G. Knepley 
654105586334SMatthew G. Knepley   Input Parameters:
654205586334SMatthew G. Knepley + section - The section for this data layout
654336fa2b79SJed Brown . islocal - Is the section (and thus indices being requested) local or global?
654405586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
654505586334SMatthew G. Knepley . off     - The global offset of this point
654605586334SMatthew G. Knepley . loff    - The local offset of each field
6547a5b23f4aSJose E. Roman . setBC   - The flag determining whether to include indices of boundary values
654805586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
654905586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
655005586334SMatthew G. Knepley 
655105586334SMatthew G. Knepley   Output Parameter:
655205586334SMatthew G. Knepley . indices - Indices for dofs on this point
655305586334SMatthew G. Knepley 
655405586334SMatthew G. Knepley   Level: developer
655505586334SMatthew G. Knepley 
655605586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
655705586334SMatthew G. Knepley */
655836fa2b79SJed Brown PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
6559a6dfd86eSKarl Rupp {
6560e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
6561552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
6562552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6563552f7358SJed Brown   PetscInt        cind = 0, k;
6564552f7358SJed Brown   PetscErrorCode  ierr;
6565552f7358SJed Brown 
6566552f7358SJed Brown   PetscFunctionBegin;
65672c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!islocal && setBC,PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
6568552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
6569552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
6570552f7358SJed Brown   if (!cdof || setBC) {
657105586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
657205586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
657305586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
657405586334SMatthew G. Knepley 
657505586334SMatthew G. Knepley       indices[ind] = off + k;
6576552f7358SJed Brown     }
6577552f7358SJed Brown   } else {
6578552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
65794acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
658005586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
658105586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
658205586334SMatthew G. Knepley 
65834acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
65844acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
658505586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
65864acb8e1eSToby Isaac         ++cind;
65874acb8e1eSToby Isaac       } else {
658836fa2b79SJed Brown         indices[ind] = off + k - (islocal ? 0 : cind);
6589552f7358SJed Brown       }
6590552f7358SJed Brown     }
6591552f7358SJed Brown   }
6592e6ccafaeSMatthew G Knepley   *loff += dof;
6593552f7358SJed Brown   PetscFunctionReturn(0);
6594552f7358SJed Brown }
6595552f7358SJed Brown 
65967e29afd2SMatthew G. Knepley /*
659736fa2b79SJed Brown  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
65987e29afd2SMatthew G. Knepley 
659936fa2b79SJed Brown  Input Parameters:
660036fa2b79SJed Brown + section - a section (global or local)
660136fa2b79SJed Brown - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
660236fa2b79SJed Brown . point - point within section
660336fa2b79SJed Brown . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
660436fa2b79SJed Brown . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
660536fa2b79SJed Brown . setBC - identify constrained (boundary condition) points via involution.
660636fa2b79SJed Brown . perms - perms[f][permsoff][:] is a permutation of dofs within each field
660736fa2b79SJed Brown . permsoff - offset
660836fa2b79SJed Brown - indperm - index permutation
660936fa2b79SJed Brown 
661036fa2b79SJed Brown  Output Parameter:
661136fa2b79SJed Brown . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
661236fa2b79SJed Brown . indices - array to hold indices (as defined by section) of each dof associated with point
661336fa2b79SJed Brown 
661436fa2b79SJed Brown  Notes:
661536fa2b79SJed Brown  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
661636fa2b79SJed Brown  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
661736fa2b79SJed Brown  in the local vector.
661836fa2b79SJed Brown 
661936fa2b79SJed Brown  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
662036fa2b79SJed Brown  significant).  It is invalid to call with a global section and setBC=true.
662136fa2b79SJed Brown 
662236fa2b79SJed Brown  Developer Note:
662336fa2b79SJed Brown  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
662436fa2b79SJed Brown  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
662536fa2b79SJed Brown  offset could be obtained from the section instead of passing it explicitly as we do now.
662636fa2b79SJed Brown 
662736fa2b79SJed Brown  Example:
662836fa2b79SJed Brown  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
662936fa2b79SJed Brown  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
663036fa2b79SJed Brown  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
663136fa2b79SJed 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.
663236fa2b79SJed Brown 
663336fa2b79SJed Brown  Level: developer
66347e29afd2SMatthew G. Knepley */
663536fa2b79SJed 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[])
6636a6dfd86eSKarl Rupp {
6637552f7358SJed Brown   PetscInt       numFields, foff, f;
6638552f7358SJed Brown   PetscErrorCode ierr;
6639552f7358SJed Brown 
6640552f7358SJed Brown   PetscFunctionBegin;
66412c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!islocal && setBC,PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
6642552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6643552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
66444acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
6645552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
66464acb8e1eSToby Isaac     PetscInt        cind = 0, b;
66474acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
6648552f7358SJed Brown 
6649552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
6650552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
6651552f7358SJed Brown     if (!cfdof || setBC) {
665205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
665305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
665405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
665505586334SMatthew G. Knepley 
665605586334SMatthew G. Knepley         indices[ind] = off+foff+b;
665705586334SMatthew G. Knepley       }
6658552f7358SJed Brown     } else {
6659552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
666005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
666105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
666205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
666305586334SMatthew G. Knepley 
66644acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
666505586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
6666552f7358SJed Brown           ++cind;
6667552f7358SJed Brown         } else {
666836fa2b79SJed Brown           indices[ind] = off + foff + b - (islocal ? 0 : cind);
6669552f7358SJed Brown         }
6670552f7358SJed Brown       }
6671552f7358SJed Brown     }
667236fa2b79SJed Brown     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
6673552f7358SJed Brown     foffs[f] += fdof;
6674552f7358SJed Brown   }
6675552f7358SJed Brown   PetscFunctionReturn(0);
6676552f7358SJed Brown }
6677552f7358SJed Brown 
66787e29afd2SMatthew G. Knepley /*
66797e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
66807e29afd2SMatthew G. Knepley 
66817e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
6682645102dcSJed Brown 
6683645102dcSJed Brown  Notes:
6684645102dcSJed Brown  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6685645102dcSJed Brown  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
66867e29afd2SMatthew G. Knepley */
6687645102dcSJed Brown static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
66887e29afd2SMatthew G. Knepley {
66897e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
66907e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
66917e29afd2SMatthew G. Knepley 
66927e29afd2SMatthew G. Knepley   PetscFunctionBegin;
66937e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
66947e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
66957e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
66967e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
66977e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
66987e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
66997e29afd2SMatthew G. Knepley 
67007e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
67017e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
67027e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
6703645102dcSJed Brown     if (!cfdof) {
670405586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
670505586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
670605586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
670705586334SMatthew G. Knepley 
670805586334SMatthew G. Knepley         indices[ind] = foff+b;
670905586334SMatthew G. Knepley       }
67107e29afd2SMatthew G. Knepley     } else {
67117e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
671205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
671305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
671405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
671505586334SMatthew G. Knepley 
67167e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
671705586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
67187e29afd2SMatthew G. Knepley           ++cind;
67197e29afd2SMatthew G. Knepley         } else {
672005586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
67217e29afd2SMatthew G. Knepley         }
67227e29afd2SMatthew G. Knepley       }
67237e29afd2SMatthew G. Knepley     }
67247e29afd2SMatthew G. Knepley     foffs[f] += fdof;
67257e29afd2SMatthew G. Knepley   }
67267e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
67277e29afd2SMatthew G. Knepley }
67287e29afd2SMatthew G. Knepley 
67294acb8e1eSToby 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)
6730d3d1a6afSToby Isaac {
6731d3d1a6afSToby Isaac   Mat             cMat;
6732d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
6733d3d1a6afSToby Isaac   IS              aIS;
6734d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
6735d3d1a6afSToby Isaac   const PetscInt  *anchors;
6736e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
6737d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
6738d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
6739d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
6740d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
6741d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
6742d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
6743d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
67446ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
6745d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
6746d3d1a6afSToby Isaac   PetscErrorCode  ierr;
6747d3d1a6afSToby Isaac 
6748d3d1a6afSToby Isaac   PetscFunctionBegin;
6749d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6750d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
6751d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
6752d3d1a6afSToby Isaac 
6753a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
6754d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
6755d3d1a6afSToby Isaac   if (aSec) {
6756580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
6757d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
6758d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
6759d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
6760d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
6761d3d1a6afSToby Isaac      * into the global matrix anyway) */
6762d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
6763d3d1a6afSToby Isaac       PetscInt b    = points[p];
67644b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6765d3d1a6afSToby Isaac 
67664b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
67674b2f2278SToby Isaac       if (!bSecDof) {
67684b2f2278SToby Isaac         continue;
67694b2f2278SToby Isaac       }
6770d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6771d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
6772d3d1a6afSToby Isaac       }
6773d3d1a6afSToby Isaac       if (bDof) {
6774d3d1a6afSToby Isaac         /* this point is constrained */
6775d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
6776d3d1a6afSToby Isaac         PetscInt bOff, q;
6777d3d1a6afSToby Isaac 
6778d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
6779d3d1a6afSToby Isaac         newNumPoints  += bDof;
6780d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
6781d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6782d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
6783d3d1a6afSToby Isaac           PetscInt aDof;
6784d3d1a6afSToby Isaac 
6785d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
6786d3d1a6afSToby Isaac           newNumIndices += aDof;
6787d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
6788d3d1a6afSToby Isaac             PetscInt fDof;
6789d3d1a6afSToby Isaac 
6790d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
6791d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
6792d3d1a6afSToby Isaac           }
6793d3d1a6afSToby Isaac         }
6794d3d1a6afSToby Isaac       }
6795d3d1a6afSToby Isaac       else {
6796d3d1a6afSToby Isaac         /* this point is not constrained */
6797d3d1a6afSToby Isaac         newNumPoints++;
67984b2f2278SToby Isaac         newNumIndices += bSecDof;
6799d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
6800d3d1a6afSToby Isaac           PetscInt fDof;
6801d3d1a6afSToby Isaac 
6802d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6803d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
6804d3d1a6afSToby Isaac         }
6805d3d1a6afSToby Isaac       }
6806d3d1a6afSToby Isaac     }
6807d3d1a6afSToby Isaac   }
6808d3d1a6afSToby Isaac   if (!anyConstrained) {
680972b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
681072b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
681172b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
681272b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
681372b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
6814d3d1a6afSToby Isaac     PetscFunctionReturn(0);
6815d3d1a6afSToby Isaac   }
6816d3d1a6afSToby Isaac 
68176ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
68186ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
68196ecaa68aSToby Isaac 
6820f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
6821d3d1a6afSToby Isaac 
68226ecaa68aSToby Isaac   if (!outPoints && !outValues) {
68236ecaa68aSToby Isaac     if (offsets) {
68246ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
68256ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
68266ecaa68aSToby Isaac       }
68276ecaa68aSToby Isaac     }
68286ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
68296ecaa68aSToby Isaac     PetscFunctionReturn(0);
68306ecaa68aSToby Isaac   }
68316ecaa68aSToby Isaac 
68322c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields && newOffsets[numFields] != newNumIndices,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
6833d3d1a6afSToby Isaac 
6834f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
6835d3d1a6afSToby Isaac 
6836d3d1a6afSToby Isaac   /* workspaces */
6837d3d1a6afSToby Isaac   if (numFields) {
6838d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
683969291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
684069291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
6841d3d1a6afSToby Isaac     }
6842d3d1a6afSToby Isaac   }
6843d3d1a6afSToby Isaac   else {
684469291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
684569291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
6846d3d1a6afSToby Isaac   }
6847d3d1a6afSToby Isaac 
6848d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
6849d3d1a6afSToby Isaac   if (numFields) {
68504b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
68514b2f2278SToby Isaac 
6852d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6853d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
68544b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6855d3d1a6afSToby Isaac 
68564b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
68574b2f2278SToby Isaac       if (!bSecDof) {
68584b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
68594b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
68604b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
68614b2f2278SToby Isaac         }
68624b2f2278SToby Isaac         continue;
68634b2f2278SToby Isaac       }
6864d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6865d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6866d3d1a6afSToby Isaac       }
6867d3d1a6afSToby Isaac       if (bDof) {
6868d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6869d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
6870d3d1a6afSToby Isaac 
6871d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6872d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6873d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
6874d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
6875d3d1a6afSToby Isaac             PetscInt aFDof;
6876d3d1a6afSToby Isaac 
6877d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
6878d3d1a6afSToby Isaac             allFDof += aFDof;
6879d3d1a6afSToby Isaac           }
6880d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
6881d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
6882d3d1a6afSToby Isaac         }
6883d3d1a6afSToby Isaac       }
6884d3d1a6afSToby Isaac       else {
6885d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6886d3d1a6afSToby Isaac           PetscInt fDof;
6887d3d1a6afSToby Isaac 
6888d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
6889d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
6890d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
6891d3d1a6afSToby Isaac         }
6892d3d1a6afSToby Isaac       }
6893d3d1a6afSToby Isaac     }
68944b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
68954b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
68964b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
6897d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
6898d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
6899d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
6900d3d1a6afSToby Isaac       }
690119f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
690219f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
690369291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
6904d3d1a6afSToby Isaac     }
6905d3d1a6afSToby Isaac   }
6906d3d1a6afSToby Isaac   else {
6907d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6908d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
69094b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6910d3d1a6afSToby Isaac 
69114b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
69124b2f2278SToby Isaac       if (!bSecDof) {
69134b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
69144b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
69154b2f2278SToby Isaac         continue;
69164b2f2278SToby Isaac       }
6917d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6918d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6919d3d1a6afSToby Isaac       }
6920d3d1a6afSToby Isaac       if (bDof) {
69214b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
6922d3d1a6afSToby Isaac 
6923d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6924d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6925d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
6926d3d1a6afSToby Isaac 
6927d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
6928d3d1a6afSToby Isaac           allDof += aDof;
6929d3d1a6afSToby Isaac         }
6930d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
69314b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
6932d3d1a6afSToby Isaac       }
6933d3d1a6afSToby Isaac       else {
69344b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
6935d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
6936d3d1a6afSToby Isaac       }
6937d3d1a6afSToby Isaac     }
6938d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
6939d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
6940d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
6941d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6942d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6943d3d1a6afSToby Isaac     }
694469291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
6945d3d1a6afSToby Isaac   }
6946d3d1a6afSToby Isaac 
69476ecaa68aSToby Isaac   /* output arrays */
694869291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
69496ecaa68aSToby Isaac 
6950d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
6951d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
6952d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
695369291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
695469291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
6955d3d1a6afSToby Isaac   if (numFields) {
6956d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
6957d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
6958d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
69594b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
6960d3d1a6afSToby Isaac 
69614b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
69624b2f2278SToby Isaac       if (!bSecDof) {
69634b2f2278SToby Isaac         continue;
69644b2f2278SToby Isaac       }
6965d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
6966d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
6967d3d1a6afSToby Isaac       }
6968d3d1a6afSToby Isaac       if (bDof) {
6969d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
6970d3d1a6afSToby Isaac 
6971d3d1a6afSToby Isaac         fStart[0] = 0;
6972d3d1a6afSToby Isaac         fEnd[0]   = 0;
6973d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6974d3d1a6afSToby Isaac           PetscInt fDof;
6975d3d1a6afSToby Isaac 
6976d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
6977d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
6978d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
6979d3d1a6afSToby Isaac         }
6980d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
698136fa2b79SJed Brown         ierr = DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
6982d3d1a6afSToby Isaac 
6983d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
6984d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
6985d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
6986d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
6987d3d1a6afSToby Isaac 
6988d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6989d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6990d3d1a6afSToby Isaac         }
6991d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
6992d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
6993d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
6994d3d1a6afSToby Isaac 
6995d3d1a6afSToby 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 */
6996d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
6997d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
6998302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
699936fa2b79SJed Brown           ierr = DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
7000d3d1a6afSToby Isaac         }
7001d3d1a6afSToby Isaac         newP += bDof;
7002d3d1a6afSToby Isaac 
70036ecaa68aSToby Isaac         if (outValues) {
7004d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
7005d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
7006d3d1a6afSToby 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);
7007d3d1a6afSToby Isaac           }
7008d3d1a6afSToby Isaac         }
70096ecaa68aSToby Isaac       }
7010d3d1a6afSToby Isaac       else {
7011d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
7012d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
7013d3d1a6afSToby Isaac         newP++;
7014d3d1a6afSToby Isaac       }
7015d3d1a6afSToby Isaac     }
7016d3d1a6afSToby Isaac   } else {
7017d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
7018d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
7019d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
70204b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
7021d3d1a6afSToby Isaac 
70224b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
70234b2f2278SToby Isaac       if (!bSecDof) {
70244b2f2278SToby Isaac         continue;
70254b2f2278SToby Isaac       }
7026d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
7027d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
7028d3d1a6afSToby Isaac       }
7029d3d1a6afSToby Isaac       if (bDof) {
7030d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
7031d3d1a6afSToby Isaac 
7032d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
703336fa2b79SJed Brown         ierr = DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
7034d3d1a6afSToby Isaac 
7035d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
7036d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
7037d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
7038d3d1a6afSToby Isaac 
7039d3d1a6afSToby 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 */
7040d3d1a6afSToby Isaac 
7041d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
7042d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
7043302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
704436fa2b79SJed Brown           ierr = DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
7045d3d1a6afSToby Isaac         }
7046d3d1a6afSToby Isaac         newP += bDof;
7047d3d1a6afSToby Isaac 
7048d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
70496ecaa68aSToby Isaac         if (outValues) {
7050d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
7051d3d1a6afSToby Isaac         }
70526ecaa68aSToby Isaac       }
7053d3d1a6afSToby Isaac       else {
7054d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
7055d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
7056d3d1a6afSToby Isaac         newP++;
7057d3d1a6afSToby Isaac       }
7058d3d1a6afSToby Isaac     }
7059d3d1a6afSToby Isaac   }
7060d3d1a6afSToby Isaac 
70616ecaa68aSToby Isaac   if (outValues) {
706269291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
7063580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
7064d3d1a6afSToby Isaac     /* multiply constraints on the right */
7065d3d1a6afSToby Isaac     if (numFields) {
7066d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
7067d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
7068d3d1a6afSToby Isaac 
7069d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
7070d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
7071d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
7072d3d1a6afSToby Isaac           PetscInt c, r, k;
7073d3d1a6afSToby Isaac           PetscInt dof;
7074d3d1a6afSToby Isaac 
7075d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
70764b2f2278SToby Isaac           if (!dof) {
70774b2f2278SToby Isaac             continue;
70784b2f2278SToby Isaac           }
7079d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
7080d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
7081d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
7082d3d1a6afSToby Isaac 
7083d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
7084d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
7085d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
70864acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
7087d3d1a6afSToby Isaac                 }
7088d3d1a6afSToby Isaac               }
7089d3d1a6afSToby Isaac             }
7090d3d1a6afSToby Isaac           }
7091d3d1a6afSToby Isaac           else {
7092d3d1a6afSToby Isaac             /* copy this column as is */
7093d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
7094d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
7095d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7096d3d1a6afSToby Isaac               }
7097d3d1a6afSToby Isaac             }
7098d3d1a6afSToby Isaac           }
7099d3d1a6afSToby Isaac           oldOff += dof;
7100d3d1a6afSToby Isaac         }
7101d3d1a6afSToby Isaac       }
7102d3d1a6afSToby Isaac     }
7103d3d1a6afSToby Isaac     else {
7104d3d1a6afSToby Isaac       PetscInt oldOff = 0;
7105d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
7106d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
7107d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
7108d3d1a6afSToby Isaac         PetscInt c, r, k;
7109d3d1a6afSToby Isaac         PetscInt dof;
7110d3d1a6afSToby Isaac 
7111d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
71124b2f2278SToby Isaac         if (!dof) {
71134b2f2278SToby Isaac           continue;
71144b2f2278SToby Isaac         }
7115d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7116d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
7117d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
7118d3d1a6afSToby Isaac 
7119d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
7120d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
7121d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
7122d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
7123d3d1a6afSToby Isaac               }
7124d3d1a6afSToby Isaac             }
7125d3d1a6afSToby Isaac           }
7126d3d1a6afSToby Isaac         }
7127d3d1a6afSToby Isaac         else {
7128d3d1a6afSToby Isaac           /* copy this column as is */
7129d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
7130d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
7131d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7132d3d1a6afSToby Isaac             }
7133d3d1a6afSToby Isaac           }
7134d3d1a6afSToby Isaac         }
7135d3d1a6afSToby Isaac         oldOff += dof;
7136d3d1a6afSToby Isaac       }
7137d3d1a6afSToby Isaac     }
7138d3d1a6afSToby Isaac 
71396ecaa68aSToby Isaac     if (multiplyLeft) {
714069291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
7141580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
7142d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
7143d3d1a6afSToby Isaac       if (numFields) {
7144d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
7145d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
7146d3d1a6afSToby Isaac 
7147d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
7148d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
7149d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
7150d3d1a6afSToby Isaac             PetscInt c, r, k;
7151d3d1a6afSToby Isaac             PetscInt dof;
7152d3d1a6afSToby Isaac 
7153d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
7154d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
7155d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
7156d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
7157d3d1a6afSToby Isaac 
7158d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
7159d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
7160d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
7161d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7162d3d1a6afSToby Isaac                   }
7163d3d1a6afSToby Isaac                 }
7164d3d1a6afSToby Isaac               }
7165d3d1a6afSToby Isaac             }
7166d3d1a6afSToby Isaac             else {
7167d3d1a6afSToby Isaac               /* copy this row as is */
7168d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
7169d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
7170d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7171d3d1a6afSToby Isaac                 }
7172d3d1a6afSToby Isaac               }
7173d3d1a6afSToby Isaac             }
7174d3d1a6afSToby Isaac             oldOff += dof;
7175d3d1a6afSToby Isaac           }
7176d3d1a6afSToby Isaac         }
7177d3d1a6afSToby Isaac       }
7178d3d1a6afSToby Isaac       else {
7179d3d1a6afSToby Isaac         PetscInt oldOff = 0;
7180d3d1a6afSToby Isaac 
7181d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
7182d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
7183d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
7184d3d1a6afSToby Isaac           PetscInt c, r, k;
7185d3d1a6afSToby Isaac           PetscInt dof;
7186d3d1a6afSToby Isaac 
7187d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
7188d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7189d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
7190d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
7191d3d1a6afSToby Isaac 
7192d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
7193d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
7194d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
7195d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7196d3d1a6afSToby Isaac                 }
7197d3d1a6afSToby Isaac               }
7198d3d1a6afSToby Isaac             }
7199d3d1a6afSToby Isaac           }
7200d3d1a6afSToby Isaac           else {
7201d3d1a6afSToby Isaac             /* copy this row as is */
72029fc93327SToby Isaac             for (r = 0; r < dof; r++) {
7203d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
7204d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7205d3d1a6afSToby Isaac               }
7206d3d1a6afSToby Isaac             }
7207d3d1a6afSToby Isaac           }
7208d3d1a6afSToby Isaac           oldOff += dof;
7209d3d1a6afSToby Isaac         }
7210d3d1a6afSToby Isaac       }
7211d3d1a6afSToby Isaac 
721269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
72136ecaa68aSToby Isaac     }
72146ecaa68aSToby Isaac     else {
72156ecaa68aSToby Isaac       newValues = tmpValues;
72166ecaa68aSToby Isaac     }
72176ecaa68aSToby Isaac   }
72186ecaa68aSToby Isaac 
7219d3d1a6afSToby Isaac   /* clean up */
722069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
722169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
72226ecaa68aSToby Isaac 
7223d3d1a6afSToby Isaac   if (numFields) {
7224d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
722569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
722669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
722769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
7228d3d1a6afSToby Isaac     }
7229d3d1a6afSToby Isaac   }
7230d3d1a6afSToby Isaac   else {
723169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
723269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
723369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
7234d3d1a6afSToby Isaac   }
7235d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
7236d3d1a6afSToby Isaac 
7237d3d1a6afSToby Isaac   /* output */
72386ecaa68aSToby Isaac   if (outPoints) {
7239d3d1a6afSToby Isaac     *outPoints = newPoints;
72406ecaa68aSToby Isaac   }
72416ecaa68aSToby Isaac   else {
724269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
72436ecaa68aSToby Isaac   }
724431620726SToby Isaac   if (outValues) {
7245d3d1a6afSToby Isaac     *outValues = newValues;
72466ecaa68aSToby Isaac   }
72476ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
7248d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
7249d3d1a6afSToby Isaac   }
7250d3d1a6afSToby Isaac   PetscFunctionReturn(0);
7251d3d1a6afSToby Isaac }
7252d3d1a6afSToby Isaac 
72534a1e0b3eSMatthew G. Knepley /*@C
725471f0bbf9SMatthew G. Knepley   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
72557cd05799SMatthew G. Knepley 
72567cd05799SMatthew G. Knepley   Not collective
72577cd05799SMatthew G. Knepley 
72587cd05799SMatthew G. Knepley   Input Parameters:
72597cd05799SMatthew G. Knepley + dm         - The DM
726071f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
726171f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
726271f0bbf9SMatthew G. Knepley . point      - The point defining the closure
726371f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
72647cd05799SMatthew G. Knepley 
726571f0bbf9SMatthew G. Knepley   Output Parameters:
726671f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
726771f0bbf9SMatthew G. Knepley . indices    - The dof indices
726871f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
726971f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
72707cd05799SMatthew G. Knepley 
727136fa2b79SJed Brown   Notes:
727236fa2b79SJed Brown   Must call DMPlexRestoreClosureIndices() to free allocated memory
727336fa2b79SJed Brown 
727436fa2b79SJed Brown   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
727536fa2b79SJed Brown   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
727636fa2b79SJed Brown   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
727736fa2b79SJed Brown   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
727836fa2b79SJed Brown   indices (with the above semantics) are implied.
72797cd05799SMatthew G. Knepley 
72807cd05799SMatthew G. Knepley   Level: advanced
72817cd05799SMatthew G. Knepley 
728236fa2b79SJed Brown .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
72834a1e0b3eSMatthew G. Knepley @*/
728471f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
728571f0bbf9SMatthew G. Knepley                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
72867773e69fSMatthew G. Knepley {
728771f0bbf9SMatthew G. Knepley   /* Closure ordering */
72887773e69fSMatthew G. Knepley   PetscSection        clSection;
72897773e69fSMatthew G. Knepley   IS                  clPoints;
729071f0bbf9SMatthew G. Knepley   const PetscInt     *clp;
729171f0bbf9SMatthew G. Knepley   PetscInt           *points;
729271f0bbf9SMatthew G. Knepley   const PetscInt     *clperm = NULL;
729371f0bbf9SMatthew G. Knepley   /* Dof permutation and sign flips */
72944acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
729571f0bbf9SMatthew G. Knepley   const PetscScalar **flips[32] = {NULL};
729671f0bbf9SMatthew G. Knepley   PetscScalar        *valCopy   = NULL;
729771f0bbf9SMatthew G. Knepley   /* Hanging node constraints */
729871f0bbf9SMatthew G. Knepley   PetscInt           *pointsC = NULL;
729971f0bbf9SMatthew G. Knepley   PetscScalar        *valuesC = NULL;
730071f0bbf9SMatthew G. Knepley   PetscInt            NclC, NiC;
730171f0bbf9SMatthew G. Knepley 
730271f0bbf9SMatthew G. Knepley   PetscInt           *idx;
730371f0bbf9SMatthew G. Knepley   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
730471f0bbf9SMatthew G. Knepley   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
73057773e69fSMatthew G. Knepley   PetscErrorCode      ierr;
73067773e69fSMatthew G. Knepley 
730771f0bbf9SMatthew G. Knepley   PetscFunctionBeginHot;
73087773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73097773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
731036fa2b79SJed Brown   PetscValidHeaderSpecific(idxSection, PETSC_SECTION_CLASSID, 3);
731171f0bbf9SMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 6);
731271f0bbf9SMatthew G. Knepley   if (indices)    PetscValidPointer(indices, 7);
731371f0bbf9SMatthew G. Knepley   if (outOffsets) PetscValidPointer(outOffsets, 8);
731471f0bbf9SMatthew G. Knepley   if (values)     PetscValidPointer(values, 9);
73157773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
73162c71b3e2SJacob Faibussowitsch   PetscCheckFalse(Nf > 31,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
7317580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
731871f0bbf9SMatthew G. Knepley   /* 1) Get points in closure */
731971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
7320c459fbc1SJed Brown   if (useClPerm) {
7321c459fbc1SJed Brown     PetscInt depth, clsize;
7322c459fbc1SJed Brown     ierr = DMPlexGetPointDepth(dm, point, &depth);CHKERRQ(ierr);
7323c459fbc1SJed Brown     for (clsize=0,p=0; p<Ncl; p++) {
7324c459fbc1SJed Brown       PetscInt dof;
7325c459fbc1SJed Brown       ierr = PetscSectionGetDof(section, points[2*p], &dof);CHKERRQ(ierr);
7326c459fbc1SJed Brown       clsize += dof;
7327c459fbc1SJed Brown     }
7328c459fbc1SJed Brown     ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);CHKERRQ(ierr);
7329c459fbc1SJed Brown   }
733071f0bbf9SMatthew G. Knepley   /* 2) Get number of indices on these points and field offsets from section */
733171f0bbf9SMatthew G. Knepley   for (p = 0; p < Ncl*2; p += 2) {
73327773e69fSMatthew G. Knepley     PetscInt dof, fdof;
73337773e69fSMatthew G. Knepley 
73347773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
73357773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
73367773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
73377773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
73387773e69fSMatthew G. Knepley     }
733971f0bbf9SMatthew G. Knepley     Ni += dof;
73407773e69fSMatthew G. Knepley   }
73417773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
73422c71b3e2SJacob Faibussowitsch   PetscCheckFalse(Nf && offsets[Nf] != Ni,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Ni);
734371f0bbf9SMatthew G. Knepley   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
734471f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
734571f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
734671f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
734771f0bbf9SMatthew G. Knepley     /* may need to apply sign changes to the element matrix */
734871f0bbf9SMatthew G. Knepley     if (values && flips[f]) {
734971f0bbf9SMatthew G. Knepley       PetscInt foffset = offsets[f];
73506ecaa68aSToby Isaac 
735171f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
735271f0bbf9SMatthew G. Knepley         PetscInt           pnt  = points[2*p], fdof;
735371f0bbf9SMatthew G. Knepley         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
735471f0bbf9SMatthew G. Knepley 
735571f0bbf9SMatthew G. Knepley         if (!Nf) {ierr = PetscSectionGetDof(section, pnt, &fdof);CHKERRQ(ierr);}
735671f0bbf9SMatthew G. Knepley         else     {ierr = PetscSectionGetFieldDof(section, pnt, f, &fdof);CHKERRQ(ierr);}
735771f0bbf9SMatthew G. Knepley         if (flip) {
735871f0bbf9SMatthew G. Knepley           PetscInt i, j, k;
735971f0bbf9SMatthew G. Knepley 
736071f0bbf9SMatthew G. Knepley           if (!valCopy) {
736171f0bbf9SMatthew G. Knepley             ierr = DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);
736271f0bbf9SMatthew G. Knepley             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
736371f0bbf9SMatthew G. Knepley             *values = valCopy;
736471f0bbf9SMatthew G. Knepley           }
736571f0bbf9SMatthew G. Knepley           for (i = 0; i < fdof; ++i) {
736671f0bbf9SMatthew G. Knepley             PetscScalar fval = flip[i];
736771f0bbf9SMatthew G. Knepley 
736871f0bbf9SMatthew G. Knepley             for (k = 0; k < Ni; ++k) {
736971f0bbf9SMatthew G. Knepley               valCopy[Ni * (foffset + i) + k] *= fval;
737071f0bbf9SMatthew G. Knepley               valCopy[Ni * k + (foffset + i)] *= fval;
73716ecaa68aSToby Isaac             }
73726ecaa68aSToby Isaac           }
737371f0bbf9SMatthew G. Knepley         }
737471f0bbf9SMatthew G. Knepley         foffset += fdof;
737571f0bbf9SMatthew G. Knepley       }
737671f0bbf9SMatthew G. Knepley     }
737771f0bbf9SMatthew G. Knepley   }
737871f0bbf9SMatthew G. Knepley   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
737971f0bbf9SMatthew G. Knepley   ierr = DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
738071f0bbf9SMatthew G. Knepley   if (NclC) {
738171f0bbf9SMatthew G. Knepley     if (valCopy) {ierr = DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);CHKERRQ(ierr);}
738271f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
738371f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
738471f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
738571f0bbf9SMatthew G. Knepley     }
738671f0bbf9SMatthew G. Knepley     for (f = 0; f < PetscMax(1, Nf); ++f) {
738771f0bbf9SMatthew G. Knepley       if (Nf) {ierr = PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
738871f0bbf9SMatthew G. Knepley       else    {ierr = PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);CHKERRQ(ierr);}
738971f0bbf9SMatthew G. Knepley     }
739071f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
739171f0bbf9SMatthew G. Knepley     Ncl     = NclC;
739271f0bbf9SMatthew G. Knepley     Ni      = NiC;
739371f0bbf9SMatthew G. Knepley     points  = pointsC;
739471f0bbf9SMatthew G. Knepley     if (values) *values = valuesC;
739571f0bbf9SMatthew G. Knepley   }
739671f0bbf9SMatthew G. Knepley   /* 5) Calculate indices */
739771f0bbf9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, Ni, MPIU_INT, &idx);CHKERRQ(ierr);
739871f0bbf9SMatthew G. Knepley   if (Nf) {
739971f0bbf9SMatthew G. Knepley     PetscInt  idxOff;
740071f0bbf9SMatthew G. Knepley     PetscBool useFieldOffsets;
740171f0bbf9SMatthew G. Knepley 
740271f0bbf9SMatthew G. Knepley     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
740371f0bbf9SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);CHKERRQ(ierr);
740471f0bbf9SMatthew G. Knepley     if (useFieldOffsets) {
740571f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
740671f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
740771f0bbf9SMatthew G. Knepley 
740871f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);CHKERRQ(ierr);
74097773e69fSMatthew G. Knepley       }
74107773e69fSMatthew G. Knepley     } else {
741171f0bbf9SMatthew G. Knepley       for (p = 0; p < Ncl; ++p) {
741271f0bbf9SMatthew G. Knepley         const PetscInt pnt = points[p*2];
741371f0bbf9SMatthew G. Knepley 
741471f0bbf9SMatthew G. Knepley         ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
741571f0bbf9SMatthew G. Knepley         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
741671f0bbf9SMatthew G. Knepley          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
741771f0bbf9SMatthew G. Knepley          * global section. */
741871f0bbf9SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);CHKERRQ(ierr);
741971f0bbf9SMatthew G. Knepley       }
742071f0bbf9SMatthew G. Knepley     }
742171f0bbf9SMatthew G. Knepley   } else {
742271f0bbf9SMatthew G. Knepley     PetscInt off = 0, idxOff;
742371f0bbf9SMatthew G. Knepley 
742471f0bbf9SMatthew G. Knepley     for (p = 0; p < Ncl; ++p) {
742571f0bbf9SMatthew G. Knepley       const PetscInt  pnt  = points[p*2];
74264acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
74274acb8e1eSToby Isaac 
742871f0bbf9SMatthew G. Knepley       ierr = PetscSectionGetOffset(idxSection, pnt, &idxOff);CHKERRQ(ierr);
742971f0bbf9SMatthew G. Knepley       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
743071f0bbf9SMatthew G. Knepley        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
743171f0bbf9SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);CHKERRQ(ierr);
74327773e69fSMatthew G. Knepley     }
74337773e69fSMatthew G. Knepley   }
743471f0bbf9SMatthew G. Knepley   /* 6) Cleanup */
743571f0bbf9SMatthew G. Knepley   for (f = 0; f < PetscMax(1, Nf); ++f) {
743671f0bbf9SMatthew G. Knepley     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
743771f0bbf9SMatthew G. Knepley     else    {ierr = PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);CHKERRQ(ierr);}
74384acb8e1eSToby Isaac   }
743971f0bbf9SMatthew G. Knepley   if (NclC) {
744071f0bbf9SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);CHKERRQ(ierr);
74417773e69fSMatthew G. Knepley   } else {
744271f0bbf9SMatthew G. Knepley     ierr = DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
74437773e69fSMatthew G. Knepley   }
744471f0bbf9SMatthew G. Knepley 
744571f0bbf9SMatthew G. Knepley   if (numIndices) *numIndices = Ni;
744671f0bbf9SMatthew G. Knepley   if (indices)    *indices    = idx;
74477773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
74487773e69fSMatthew G. Knepley }
74497773e69fSMatthew G. Knepley 
74507cd05799SMatthew G. Knepley /*@C
745171f0bbf9SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
74527cd05799SMatthew G. Knepley 
74537cd05799SMatthew G. Knepley   Not collective
74547cd05799SMatthew G. Knepley 
74557cd05799SMatthew G. Knepley   Input Parameters:
74567cd05799SMatthew G. Knepley + dm         - The DM
745771f0bbf9SMatthew G. Knepley . section    - The PetscSection describing the points (a local section)
745871f0bbf9SMatthew G. Knepley . idxSection - The PetscSection from which to obtain indices (may be local or global)
745971f0bbf9SMatthew G. Knepley . point      - The point defining the closure
746071f0bbf9SMatthew G. Knepley - useClPerm  - Use the closure point permutation if available
746171f0bbf9SMatthew G. Knepley 
746271f0bbf9SMatthew G. Knepley   Output Parameters:
746371f0bbf9SMatthew G. Knepley + numIndices - The number of dof indices in the closure of point with the input sections
746471f0bbf9SMatthew G. Knepley . indices    - The dof indices
746571f0bbf9SMatthew G. Knepley . outOffsets - Array to write the field offsets into, or NULL
746671f0bbf9SMatthew G. Knepley - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
746771f0bbf9SMatthew G. Knepley 
746871f0bbf9SMatthew G. Knepley   Notes:
746971f0bbf9SMatthew G. Knepley   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).
747071f0bbf9SMatthew G. Knepley 
747171f0bbf9SMatthew G. Knepley   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
747271f0bbf9SMatthew G. Knepley   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
747371f0bbf9SMatthew G. Knepley   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
747471f0bbf9SMatthew G. Knepley   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
747571f0bbf9SMatthew G. Knepley   indices (with the above semantics) are implied.
74767cd05799SMatthew G. Knepley 
74777cd05799SMatthew G. Knepley   Level: advanced
74787cd05799SMatthew G. Knepley 
747971f0bbf9SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
74807cd05799SMatthew G. Knepley @*/
748171f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
748271f0bbf9SMatthew G. Knepley                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
74837773e69fSMatthew G. Knepley {
74847773e69fSMatthew G. Knepley   PetscErrorCode ierr;
74857773e69fSMatthew G. Knepley 
74867773e69fSMatthew G. Knepley   PetscFunctionBegin;
74877773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7488064a246eSJacob Faibussowitsch   PetscValidPointer(indices, 7);
748969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
74907773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
74917773e69fSMatthew G. Knepley }
74927773e69fSMatthew G. Knepley 
74937f5d1fdeSMatthew G. Knepley /*@C
74947f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
74957f5d1fdeSMatthew G. Knepley 
74967f5d1fdeSMatthew G. Knepley   Not collective
74977f5d1fdeSMatthew G. Knepley 
74987f5d1fdeSMatthew G. Knepley   Input Parameters:
74997f5d1fdeSMatthew G. Knepley + dm - The DM
7500ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
7501ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
75027f5d1fdeSMatthew G. Knepley . A - The matrix
7503eaf898f9SPatrick Sanan . point - The point in the DM
75047f5d1fdeSMatthew G. Knepley . values - The array of values
75057f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
75067f5d1fdeSMatthew G. Knepley 
75077f5d1fdeSMatthew G. Knepley   Fortran Notes:
75087f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
75097f5d1fdeSMatthew G. Knepley 
75107f5d1fdeSMatthew G. Knepley   Level: intermediate
75117f5d1fdeSMatthew G. Knepley 
75124a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
75137f5d1fdeSMatthew G. Knepley @*/
75147c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7515552f7358SJed Brown {
7516552f7358SJed Brown   DM_Plex           *mesh = (DM_Plex*) dm->data;
7517552f7358SJed Brown   PetscInt          *indices;
751871f0bbf9SMatthew G. Knepley   PetscInt           numIndices;
751971f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
7520552f7358SJed Brown   PetscErrorCode     ierr;
7521552f7358SJed Brown 
7522552f7358SJed Brown   PetscFunctionBegin;
7523552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
752492fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
75253dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
7526e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
75273dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
75283dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
7529552f7358SJed Brown 
753071f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
75310d644c17SKarl Rupp 
7532b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
75334a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
7534552f7358SJed Brown   if (ierr) {
7535552f7358SJed Brown     PetscMPIInt    rank;
7536552f7358SJed Brown     PetscErrorCode ierr2;
7537552f7358SJed Brown 
753855b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
7539e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
7540b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
754171f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
754271f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
7543c3e24edfSBarry Smith     SETERRQ(PetscObjectComm((PetscObject)dm),ierr,"Not possible to set matrix values");
7544552f7358SJed Brown   }
75454a1e0b3eSMatthew G. Knepley   if (mesh->printFEM > 1) {
75464a1e0b3eSMatthew G. Knepley     PetscInt i;
75474a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
75484a1e0b3eSMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
75494a1e0b3eSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
75504a1e0b3eSMatthew G. Knepley   }
755171f0bbf9SMatthew G. Knepley 
755271f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
755371f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
755471f0bbf9SMatthew G. Knepley   PetscFunctionReturn(0);
75554acb8e1eSToby Isaac }
755671f0bbf9SMatthew G. Knepley 
75574a1e0b3eSMatthew G. Knepley /*@C
75584a1e0b3eSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
75594a1e0b3eSMatthew G. Knepley 
75604a1e0b3eSMatthew G. Knepley   Not collective
75614a1e0b3eSMatthew G. Knepley 
75624a1e0b3eSMatthew G. Knepley   Input Parameters:
75634a1e0b3eSMatthew G. Knepley + dmRow - The DM for the row fields
75644a1e0b3eSMatthew G. Knepley . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
75654a1e0b3eSMatthew G. Knepley . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
75664a1e0b3eSMatthew G. Knepley . dmCol - The DM for the column fields
75674a1e0b3eSMatthew G. Knepley . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
75684a1e0b3eSMatthew G. Knepley . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
75694a1e0b3eSMatthew G. Knepley . A - The matrix
75704a1e0b3eSMatthew G. Knepley . point - The point in the DMs
75714a1e0b3eSMatthew G. Knepley . values - The array of values
75724a1e0b3eSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
75734a1e0b3eSMatthew G. Knepley 
75744a1e0b3eSMatthew G. Knepley   Level: intermediate
75754a1e0b3eSMatthew G. Knepley 
75764a1e0b3eSMatthew G. Knepley .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
75774a1e0b3eSMatthew G. Knepley @*/
757871f0bbf9SMatthew G. Knepley PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
757971f0bbf9SMatthew G. Knepley {
758071f0bbf9SMatthew G. Knepley   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
758171f0bbf9SMatthew G. Knepley   PetscInt          *indicesRow, *indicesCol;
758271f0bbf9SMatthew G. Knepley   PetscInt           numIndicesRow, numIndicesCol;
758371f0bbf9SMatthew G. Knepley   const PetscScalar *valuesOrig = values;
758471f0bbf9SMatthew G. Knepley   PetscErrorCode     ierr;
758571f0bbf9SMatthew G. Knepley 
758671f0bbf9SMatthew G. Knepley   PetscFunctionBegin;
758771f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmRow, DM_CLASSID, 1);
758871f0bbf9SMatthew G. Knepley   if (!sectionRow) {ierr = DMGetLocalSection(dmRow, &sectionRow);CHKERRQ(ierr);}
758971f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionRow, PETSC_SECTION_CLASSID, 2);
759071f0bbf9SMatthew G. Knepley   if (!globalSectionRow) {ierr = DMGetGlobalSection(dmRow, &globalSectionRow);CHKERRQ(ierr);}
759171f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionRow, PETSC_SECTION_CLASSID, 3);
759271f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(dmCol, DM_CLASSID, 4);
759371f0bbf9SMatthew G. Knepley   if (!sectionCol) {ierr = DMGetLocalSection(dmCol, &sectionCol);CHKERRQ(ierr);}
759471f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(sectionCol, PETSC_SECTION_CLASSID, 5);
759571f0bbf9SMatthew G. Knepley   if (!globalSectionCol) {ierr = DMGetGlobalSection(dmCol, &globalSectionCol);CHKERRQ(ierr);}
759671f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(globalSectionCol, PETSC_SECTION_CLASSID, 6);
759771f0bbf9SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
759871f0bbf9SMatthew G. Knepley 
759971f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
760071f0bbf9SMatthew G. Knepley   ierr = DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
760171f0bbf9SMatthew G. Knepley 
760271f0bbf9SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr);}
76034a1e0b3eSMatthew G. Knepley   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
760471f0bbf9SMatthew G. Knepley   if (ierr) {
760571f0bbf9SMatthew G. Knepley     PetscMPIInt    rank;
760671f0bbf9SMatthew G. Knepley     PetscErrorCode ierr2;
760771f0bbf9SMatthew G. Knepley 
760855b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
760971f0bbf9SMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
761071f0bbf9SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
761171f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
761271f0bbf9SMatthew G. Knepley     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
761371f0bbf9SMatthew G. Knepley     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
761471f0bbf9SMatthew G. Knepley     CHKERRQ(ierr);
7615d3d1a6afSToby Isaac   }
761671f0bbf9SMatthew G. Knepley 
761771f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
761871f0bbf9SMatthew G. Knepley   ierr = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);CHKERRQ(ierr);
761971f0bbf9SMatthew G. Knepley   if (values != valuesOrig) {ierr = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr);}
7620552f7358SJed Brown   PetscFunctionReturn(0);
7621552f7358SJed Brown }
7622552f7358SJed Brown 
7623de41b84cSMatthew 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)
7624de41b84cSMatthew G. Knepley {
7625de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
7626de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
7627de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
7628de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
762917c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7630de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
7631412e9a14SMatthew G. Knepley   DMPolytopeType  ct;
76324ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7633de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
7634de41b84cSMatthew G. Knepley 
7635de41b84cSMatthew G. Knepley   PetscFunctionBegin;
7636de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
7637de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
763892fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
7639de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
764092fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
7641de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7642e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
7643de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7644e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
7645de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
7646de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
7647de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
76482c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields > 31,PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7649580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7650580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
7651de41b84cSMatthew G. Knepley   /* Column indices */
7652de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
76534ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
7654de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
7655de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
7656de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
7657de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
7658de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7659de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
7660de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
7661de41b84cSMatthew G. Knepley       ++q;
7662de41b84cSMatthew G. Knepley     }
7663de41b84cSMatthew G. Knepley   }
7664de41b84cSMatthew G. Knepley   numCPoints = q;
7665de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
7666de41b84cSMatthew G. Knepley     PetscInt fdof;
7667de41b84cSMatthew G. Knepley 
7668de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
76694ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7670de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7671de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
7672de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
7673de41b84cSMatthew G. Knepley     }
7674de41b84cSMatthew G. Knepley     numCIndices += dof;
7675de41b84cSMatthew G. Knepley   }
7676de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
7677de41b84cSMatthew G. Knepley   /* Row indices */
7678412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7679412e9a14SMatthew G. Knepley   {
7680012bc364SMatthew G. Knepley     DMPlexTransform tr;
7681012bc364SMatthew G. Knepley     DMPolytopeType *rct;
7682012bc364SMatthew G. Knepley     PetscInt       *rsize, *rcone, *rornt, Nt;
7683012bc364SMatthew G. Knepley 
7684012bc364SMatthew G. Knepley     ierr = DMPlexTransformCreate(PETSC_COMM_SELF, &tr);CHKERRQ(ierr);
7685012bc364SMatthew G. Knepley     ierr = DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);CHKERRQ(ierr);
7686012bc364SMatthew G. Knepley     ierr = DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);CHKERRQ(ierr);
7687012bc364SMatthew G. Knepley     numSubcells = rsize[Nt-1];
7688012bc364SMatthew G. Knepley     ierr = DMPlexTransformDestroy(&tr);CHKERRQ(ierr);
7689412e9a14SMatthew G. Knepley   }
769069291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7691de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
7692de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
7693de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7694de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
7695de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
7696de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
7697de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
76984ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
76994ca5e9f5SMatthew G. Knepley         if (!dof) continue;
77004ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
77014ca5e9f5SMatthew G. Knepley         if (s < q) continue;
7702de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
7703de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
7704de41b84cSMatthew G. Knepley         ++q;
7705de41b84cSMatthew G. Knepley       }
7706de41b84cSMatthew G. Knepley     }
7707de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
7708de41b84cSMatthew G. Knepley   }
7709de41b84cSMatthew G. Knepley   numFPoints = q;
7710de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
7711de41b84cSMatthew G. Knepley     PetscInt fdof;
7712de41b84cSMatthew G. Knepley 
7713de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
77144ca5e9f5SMatthew G. Knepley     if (!dof) continue;
7715de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
7716de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
7717de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
7718de41b84cSMatthew G. Knepley     }
7719de41b84cSMatthew G. Knepley     numFIndices += dof;
7720de41b84cSMatthew G. Knepley   }
7721de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
7722de41b84cSMatthew G. Knepley 
77232c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields && foffsets[numFields] != numFIndices,PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
77242c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields && coffsets[numFields] != numCIndices,PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
772569291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
772669291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7727de41b84cSMatthew G. Knepley   if (numFields) {
77284acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
77294acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
77304acb8e1eSToby Isaac 
77314acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
77324acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
77334acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7734de41b84cSMatthew G. Knepley     }
77354acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
77364acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
773736fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
77384acb8e1eSToby Isaac     }
77394acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
77404acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
774136fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
77424acb8e1eSToby Isaac     }
77434acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
77444acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
77454acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
7746de41b84cSMatthew G. Knepley     }
7747de41b84cSMatthew G. Knepley   } else {
77484acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
77494acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
77504acb8e1eSToby Isaac 
77514acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
77524acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
77534acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
77544acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
77554acb8e1eSToby Isaac 
77564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
775736fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
7758de41b84cSMatthew G. Knepley     }
77594acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
77604acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
77614acb8e1eSToby Isaac 
77624acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
776336fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
7764de41b84cSMatthew G. Knepley     }
77654acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
77664acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
7767de41b84cSMatthew G. Knepley   }
7768de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
77694acb8e1eSToby Isaac   /* TODO: flips */
7770de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7771de41b84cSMatthew G. Knepley   if (ierr) {
7772de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
7773de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
7774de41b84cSMatthew G. Knepley 
777555b25c41SPierre Jolivet     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
7776e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
7777de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
777869291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
777969291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
7780de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
7781de41b84cSMatthew G. Knepley   }
778269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
7783de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
778469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
778569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
7786de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
7787de41b84cSMatthew G. Knepley }
7788de41b84cSMatthew G. Knepley 
77897c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
77907c927364SMatthew G. Knepley {
77917c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
77927c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
77937c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
779417c0192bSMatthew G. Knepley   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7795412e9a14SMatthew G. Knepley   DMPolytopeType ct;
77967c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
77977c927364SMatthew G. Knepley   PetscErrorCode ierr;
77987c927364SMatthew G. Knepley 
77997c927364SMatthew G. Knepley   PetscFunctionBegin;
78007c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
78017c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
780292fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
78037c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
780492fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
78057c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
7806e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
78077c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
7808e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
78097c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
78107c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
78112c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields > 31,PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
7812580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
7813580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
78147c927364SMatthew G. Knepley   /* Column indices */
78157c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
78167c927364SMatthew G. Knepley   maxFPoints = numCPoints;
78177c927364SMatthew G. Knepley   /* Compress out points not in the section */
78187c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
78197c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
78207c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
78217c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
78227c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
78237c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
78247c927364SMatthew G. Knepley       ++q;
78257c927364SMatthew G. Knepley     }
78267c927364SMatthew G. Knepley   }
78277c927364SMatthew G. Knepley   numCPoints = q;
78287c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
78297c927364SMatthew G. Knepley     PetscInt fdof;
78307c927364SMatthew G. Knepley 
78317c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
78327c927364SMatthew G. Knepley     if (!dof) continue;
78337c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
78347c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
78357c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
78367c927364SMatthew G. Knepley     }
78377c927364SMatthew G. Knepley     numCIndices += dof;
78387c927364SMatthew G. Knepley   }
78397c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
78407c927364SMatthew G. Knepley   /* Row indices */
7841412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dmc, point, &ct);CHKERRQ(ierr);
7842412e9a14SMatthew G. Knepley   {
7843012bc364SMatthew G. Knepley     DMPlexTransform tr;
7844012bc364SMatthew G. Knepley     DMPolytopeType *rct;
7845012bc364SMatthew G. Knepley     PetscInt       *rsize, *rcone, *rornt, Nt;
7846012bc364SMatthew G. Knepley 
7847012bc364SMatthew G. Knepley     ierr = DMPlexTransformCreate(PETSC_COMM_SELF, &tr);CHKERRQ(ierr);
7848012bc364SMatthew G. Knepley     ierr = DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);CHKERRQ(ierr);
7849012bc364SMatthew G. Knepley     ierr = DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);CHKERRQ(ierr);
7850012bc364SMatthew G. Knepley     numSubcells = rsize[Nt-1];
7851012bc364SMatthew G. Knepley     ierr = DMPlexTransformDestroy(&tr);CHKERRQ(ierr);
7852412e9a14SMatthew G. Knepley   }
785369291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
78547c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
78557c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
78567c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
78577c927364SMatthew G. Knepley     /* Compress out points not in the section */
78587c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
78597c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
78607c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
78617c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
78627c927364SMatthew G. Knepley         if (!dof) continue;
78637c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
78647c927364SMatthew G. Knepley         if (s < q) continue;
78657c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
78667c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
78677c927364SMatthew G. Knepley         ++q;
78687c927364SMatthew G. Knepley       }
78697c927364SMatthew G. Knepley     }
78707c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
78717c927364SMatthew G. Knepley   }
78727c927364SMatthew G. Knepley   numFPoints = q;
78737c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
78747c927364SMatthew G. Knepley     PetscInt fdof;
78757c927364SMatthew G. Knepley 
78767c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
78777c927364SMatthew G. Knepley     if (!dof) continue;
78787c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
78797c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
78807c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
78817c927364SMatthew G. Knepley     }
78827c927364SMatthew G. Knepley     numFIndices += dof;
78837c927364SMatthew G. Knepley   }
78847c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
78857c927364SMatthew G. Knepley 
78862c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields && foffsets[numFields] != numFIndices,PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
78872c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields && coffsets[numFields] != numCIndices,PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
78887c927364SMatthew G. Knepley   if (numFields) {
78894acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
78904acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
78914acb8e1eSToby Isaac 
78924acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
78934acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
78944acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
78957c927364SMatthew G. Knepley     }
78964acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
78974acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
789836fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
78994acb8e1eSToby Isaac     }
79004acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
79014acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
790236fa2b79SJed Brown       ierr = DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
79034acb8e1eSToby Isaac     }
79044acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
79054acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
79064acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
79077c927364SMatthew G. Knepley     }
79087c927364SMatthew G. Knepley   } else {
79094acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
79104acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
79114acb8e1eSToby Isaac 
79124acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
79134acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
79144acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
79154acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
79164acb8e1eSToby Isaac 
79174acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
791836fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
79197c927364SMatthew G. Knepley     }
79204acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
79214acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
79224acb8e1eSToby Isaac 
79234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
792436fa2b79SJed Brown       ierr = DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
79257c927364SMatthew G. Knepley     }
79264acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
79274acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
79287c927364SMatthew G. Knepley   }
792969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
79307c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
79317c927364SMatthew G. Knepley   PetscFunctionReturn(0);
79327c927364SMatthew G. Knepley }
79337c927364SMatthew G. Knepley 
79347cd05799SMatthew G. Knepley /*@C
79357cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
79367cd05799SMatthew G. Knepley 
79377cd05799SMatthew G. Knepley   Input Parameter:
79387cd05799SMatthew G. Knepley . dm   - The DMPlex object
79397cd05799SMatthew G. Knepley 
79407cd05799SMatthew G. Knepley   Output Parameter:
79417cd05799SMatthew G. Knepley . cellHeight - The height of a cell
79427cd05799SMatthew G. Knepley 
79437cd05799SMatthew G. Knepley   Level: developer
79447cd05799SMatthew G. Knepley 
79457cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
79467cd05799SMatthew G. Knepley @*/
7947552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7948552f7358SJed Brown {
7949552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7950552f7358SJed Brown 
7951552f7358SJed Brown   PetscFunctionBegin;
7952552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7953552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
7954552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
7955552f7358SJed Brown   PetscFunctionReturn(0);
7956552f7358SJed Brown }
7957552f7358SJed Brown 
79587cd05799SMatthew G. Knepley /*@C
79597cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
79607cd05799SMatthew G. Knepley 
79617cd05799SMatthew G. Knepley   Input Parameters:
79627cd05799SMatthew G. Knepley + dm   - The DMPlex object
79637cd05799SMatthew G. Knepley - cellHeight - The height of a cell
79647cd05799SMatthew G. Knepley 
79657cd05799SMatthew G. Knepley   Level: developer
79667cd05799SMatthew G. Knepley 
79677cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
79687cd05799SMatthew G. Knepley @*/
7969552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7970552f7358SJed Brown {
7971552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
7972552f7358SJed Brown 
7973552f7358SJed Brown   PetscFunctionBegin;
7974552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7975552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
7976552f7358SJed Brown   PetscFunctionReturn(0);
7977552f7358SJed Brown }
7978552f7358SJed Brown 
7979e6139122SMatthew G. Knepley /*@
7980e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
7981e6139122SMatthew G. Knepley 
7982e6139122SMatthew G. Knepley   Input Parameter:
7983e6139122SMatthew G. Knepley . dm - The DMPlex object
7984e6139122SMatthew G. Knepley 
7985e6139122SMatthew G. Knepley   Output Parameters:
79862a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
79872a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
7988e6139122SMatthew G. Knepley 
79892a9f31c0SMatthew G. Knepley   Level: advanced
7990e6139122SMatthew G. Knepley 
79918065b584SMatthew Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7992e6139122SMatthew G. Knepley @*/
7993e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7994e6139122SMatthew G. Knepley {
7995412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
7996e6139122SMatthew G. Knepley   PetscErrorCode ierr;
7997e6139122SMatthew G. Knepley 
7998e6139122SMatthew G. Knepley   PetscFunctionBegin;
7999e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8000412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
8001412e9a14SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);CHKERRQ(ierr);
8002e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
8003e6139122SMatthew G. Knepley }
8004e6139122SMatthew G. Knepley 
8005552f7358SJed Brown /* We can easily have a form that takes an IS instead */
80069886b8cfSStefano Zampini PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
8007552f7358SJed Brown {
8008552f7358SJed Brown   PetscSection   section, globalSection;
8009552f7358SJed Brown   PetscInt      *numbers, p;
8010552f7358SJed Brown   PetscErrorCode ierr;
8011552f7358SJed Brown 
8012552f7358SJed Brown   PetscFunctionBegin;
801382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
8014552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
8015552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
8016552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
8017552f7358SJed Brown   }
8018552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
801915b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
8020854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
8021552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
8022552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
8023ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
8024ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
8025552f7358SJed Brown   }
802682f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
8027ef48cebcSMatthew G. Knepley   if (globalSize) {
8028ef48cebcSMatthew G. Knepley     PetscLayout layout;
8029ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
8030ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
8031ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
8032ef48cebcSMatthew G. Knepley   }
8033552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
8034552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
8035552f7358SJed Brown   PetscFunctionReturn(0);
8036552f7358SJed Brown }
8037552f7358SJed Brown 
803881ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
8039552f7358SJed Brown {
8040412e9a14SMatthew G. Knepley   PetscInt       cellHeight, cStart, cEnd;
8041552f7358SJed Brown   PetscErrorCode ierr;
8042552f7358SJed Brown 
8043552f7358SJed Brown   PetscFunctionBegin;
8044552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8045412e9a14SMatthew G. Knepley   if (includeHybrid) {ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
8046412e9a14SMatthew G. Knepley   else               {ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);}
80479886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
804881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
8049552f7358SJed Brown }
805081ed3555SMatthew G. Knepley 
80518dab3259SMatthew G. Knepley /*@
80527cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
80537cd05799SMatthew G. Knepley 
80547cd05799SMatthew G. Knepley   Input Parameter:
80557cd05799SMatthew G. Knepley . dm   - The DMPlex object
80567cd05799SMatthew G. Knepley 
80577cd05799SMatthew G. Knepley   Output Parameter:
80587cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
80597cd05799SMatthew G. Knepley 
80607cd05799SMatthew G. Knepley   Level: developer
80617cd05799SMatthew G. Knepley 
80627cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
80637cd05799SMatthew G. Knepley @*/
806481ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
806581ed3555SMatthew G. Knepley {
806681ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
806781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
806881ed3555SMatthew G. Knepley 
806981ed3555SMatthew G. Knepley   PetscFunctionBegin;
807081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
807181ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
8072552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
8073552f7358SJed Brown   PetscFunctionReturn(0);
8074552f7358SJed Brown }
8075552f7358SJed Brown 
807681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
807781ed3555SMatthew G. Knepley {
8078412e9a14SMatthew G. Knepley   PetscInt       vStart, vEnd;
807981ed3555SMatthew G. Knepley   PetscErrorCode ierr;
808081ed3555SMatthew G. Knepley 
808181ed3555SMatthew G. Knepley   PetscFunctionBegin;
808281ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
808381ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
80849886b8cfSStefano Zampini   ierr = DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
808581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
808681ed3555SMatthew G. Knepley }
808781ed3555SMatthew G. Knepley 
80888dab3259SMatthew G. Knepley /*@
80896aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
80907cd05799SMatthew G. Knepley 
80917cd05799SMatthew G. Knepley   Input Parameter:
80927cd05799SMatthew G. Knepley . dm   - The DMPlex object
80937cd05799SMatthew G. Knepley 
80947cd05799SMatthew G. Knepley   Output Parameter:
80957cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
80967cd05799SMatthew G. Knepley 
80977cd05799SMatthew G. Knepley   Level: developer
80987cd05799SMatthew G. Knepley 
80997cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
81007cd05799SMatthew G. Knepley @*/
8101552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
8102552f7358SJed Brown {
8103552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
8104552f7358SJed Brown   PetscErrorCode ierr;
8105552f7358SJed Brown 
8106552f7358SJed Brown   PetscFunctionBegin;
8107552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
810881ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
8109552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
8110552f7358SJed Brown   PetscFunctionReturn(0);
8111552f7358SJed Brown }
8112552f7358SJed Brown 
81138dab3259SMatthew G. Knepley /*@
81147cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
81157cd05799SMatthew G. Knepley 
81167cd05799SMatthew G. Knepley   Input Parameter:
81177cd05799SMatthew G. Knepley . dm   - The DMPlex object
81187cd05799SMatthew G. Knepley 
81197cd05799SMatthew G. Knepley   Output Parameter:
81207cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
81217cd05799SMatthew G. Knepley 
81227cd05799SMatthew G. Knepley   Level: developer
81237cd05799SMatthew G. Knepley 
81247cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
81257cd05799SMatthew G. Knepley @*/
8126ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
8127ef48cebcSMatthew G. Knepley {
8128ef48cebcSMatthew G. Knepley   IS             nums[4];
8129862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
8130ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
8131ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
8132ef48cebcSMatthew G. Knepley 
8133ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
8134ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8135ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
81368abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
81378abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
8138862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
8139862913ffSStefano Zampini     PetscInt end;
8140862913ffSStefano Zampini 
8141862913ffSStefano Zampini     depths[d] = depth-d;
8142862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
8143862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
8144862913ffSStefano Zampini   }
8145862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
8146820f2d46SBarry Smith   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
8147862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
81482c71b3e2SJacob Faibussowitsch     PetscCheckFalse(starts[d] >= 0 && depths[d] != gdepths[d],PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected depth %D, found %D",depths[d],gdepths[d]);
8149862913ffSStefano Zampini   }
8150ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
8151ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
8152ef48cebcSMatthew G. Knepley 
8153862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
81549886b8cfSStefano Zampini     ierr = DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
8155ef48cebcSMatthew G. Knepley     shift += gsize;
8156ef48cebcSMatthew G. Knepley   }
8157302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
8158ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
8159ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
8160ef48cebcSMatthew G. Knepley }
8161ef48cebcSMatthew G. Knepley 
816208a22f4bSMatthew G. Knepley /*@
816308a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
816408a22f4bSMatthew G. Knepley 
816508a22f4bSMatthew G. Knepley   Input Parameter:
816608a22f4bSMatthew G. Knepley . dm - The DMPlex object
816708a22f4bSMatthew G. Knepley 
816808a22f4bSMatthew G. Knepley   Output Parameter:
816908a22f4bSMatthew G. Knepley . ranks - The rank field
817008a22f4bSMatthew G. Knepley 
817108a22f4bSMatthew G. Knepley   Options Database Keys:
817208a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
817308a22f4bSMatthew G. Knepley 
817408a22f4bSMatthew G. Knepley   Level: intermediate
817508a22f4bSMatthew G. Knepley 
817608a22f4bSMatthew G. Knepley .seealso: DMView()
817708a22f4bSMatthew G. Knepley @*/
817808a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
817908a22f4bSMatthew G. Knepley {
818008a22f4bSMatthew G. Knepley   DM             rdm;
818108a22f4bSMatthew G. Knepley   PetscFE        fe;
818208a22f4bSMatthew G. Knepley   PetscScalar   *r;
818308a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
8184a55f9a55SMatthew G. Knepley   DMPolytopeType ct;
818508a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
8186a55f9a55SMatthew G. Knepley   PetscBool      simplex;
818708a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
818808a22f4bSMatthew G. Knepley 
818908a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
8190f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8191f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
8192ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
819308a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
819408a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
8195a55f9a55SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8196a55f9a55SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
8197a55f9a55SMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
8198a55f9a55SMatthew G. Knepley   ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
819908a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
8200e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
820108a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
8202e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
820308a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
820408a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
820508a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
820608a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
820708a22f4bSMatthew G. Knepley     PetscScalar *lr;
820808a22f4bSMatthew G. Knepley 
820908a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
821071f09efeSPierre Jolivet     if (lr) *lr = rank;
821108a22f4bSMatthew G. Knepley   }
821208a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
821308a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
821408a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
821508a22f4bSMatthew G. Knepley }
821608a22f4bSMatthew G. Knepley 
8217ca8062c8SMatthew G. Knepley /*@
821818e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
821918e14f0cSMatthew G. Knepley 
822018e14f0cSMatthew G. Knepley   Input Parameters:
822118e14f0cSMatthew G. Knepley + dm    - The DMPlex
822218e14f0cSMatthew G. Knepley - label - The DMLabel
822318e14f0cSMatthew G. Knepley 
822418e14f0cSMatthew G. Knepley   Output Parameter:
822518e14f0cSMatthew G. Knepley . val - The label value field
822618e14f0cSMatthew G. Knepley 
822718e14f0cSMatthew G. Knepley   Options Database Keys:
822818e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
822918e14f0cSMatthew G. Knepley 
823018e14f0cSMatthew G. Knepley   Level: intermediate
823118e14f0cSMatthew G. Knepley 
823218e14f0cSMatthew G. Knepley .seealso: DMView()
823318e14f0cSMatthew G. Knepley @*/
823418e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
823518e14f0cSMatthew G. Knepley {
823618e14f0cSMatthew G. Knepley   DM             rdm;
823718e14f0cSMatthew G. Knepley   PetscFE        fe;
823818e14f0cSMatthew G. Knepley   PetscScalar   *v;
823918e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
824018e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
824118e14f0cSMatthew G. Knepley 
824218e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
824318e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
824418e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
824518e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
824618e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
824718e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
824818e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
824918e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
8250e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
825118e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
8252e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
825318e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
825418e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
8255effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
825618e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
825718e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
825818e14f0cSMatthew G. Knepley     PetscScalar *lv;
825918e14f0cSMatthew G. Knepley     PetscInt     cval;
826018e14f0cSMatthew G. Knepley 
826118e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
826218e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
826318e14f0cSMatthew G. Knepley     *lv = cval;
826418e14f0cSMatthew G. Knepley   }
826518e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
826618e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
826718e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
826818e14f0cSMatthew G. Knepley }
826918e14f0cSMatthew G. Knepley 
827018e14f0cSMatthew G. Knepley /*@
8271ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
8272ca8062c8SMatthew G. Knepley 
827369916449SMatthew G. Knepley   Input Parameter:
827469916449SMatthew G. Knepley . dm - The DMPlex object
8275ca8062c8SMatthew G. Knepley 
827695eb5ee5SVaclav Hapla   Notes:
827795eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
827895eb5ee5SVaclav Hapla 
827995eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8280ca8062c8SMatthew G. Knepley 
8281ca8062c8SMatthew G. Knepley   Level: developer
8282ca8062c8SMatthew G. Knepley 
828395eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
8284ca8062c8SMatthew G. Knepley @*/
8285ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
8286ca8062c8SMatthew G. Knepley {
8287ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
8288ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
8289ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
829057beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
829157beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
8292ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
8293ca8062c8SMatthew G. Knepley 
8294ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
8295ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8296412e9a14SMatthew G. Knepley   ierr = DMViewFromOptions(dm, NULL, "-sym_dm_view");CHKERRQ(ierr);
8297ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
8298ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
8299ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
8300ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
8301ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
8302ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
8303ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
8304ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
830542e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
830642e66dfaSMatthew G. Knepley       PetscInt  d;
830742e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
830842e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
830942e66dfaSMatthew G. Knepley       }
8310ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
8311ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
8312ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
8313ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
8314ca8062c8SMatthew G. Knepley       }
831542e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
83168ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
8317ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
83188ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
8319ca8062c8SMatthew G. Knepley         }
8320302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
83218ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
8322ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
83238ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
8324ca8062c8SMatthew G. Knepley         }
8325302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
83262c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dup,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not repeatedly found in support of repeated cone point %D", p, cone[c]);
832798921bdaSJacob Faibussowitsch         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
8328ca8062c8SMatthew G. Knepley       }
832942e66dfaSMatthew G. Knepley     }
833057beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
833157beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
8332ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
8333ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
8334ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
8335ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
8336ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
8337ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
833857beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
833957beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
8340ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
8341ca8062c8SMatthew G. Knepley       }
8342ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
83438ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
8344ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
83458ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
8346ca8062c8SMatthew G. Knepley         }
8347302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
83488ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
8349ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
83508ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
8351ca8062c8SMatthew G. Knepley         }
8352302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
835398921bdaSJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
8354ca8062c8SMatthew G. Knepley       }
8355ca8062c8SMatthew G. Knepley     }
8356ca8062c8SMatthew G. Knepley   }
835757beb4faSStefano Zampini   if (storagecheck) {
8358ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
8359ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
83602c71b3e2SJacob Faibussowitsch     PetscCheckFalse(csize != ssize,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
836157beb4faSStefano Zampini   }
8362ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
8363ca8062c8SMatthew G. Knepley }
8364ca8062c8SMatthew G. Knepley 
8365412e9a14SMatthew G. Knepley /*
8366412e9a14SMatthew 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.
8367412e9a14SMatthew G. Knepley */
8368412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
8369412e9a14SMatthew G. Knepley {
8370412e9a14SMatthew G. Knepley   DMPolytopeType  cct;
8371412e9a14SMatthew G. Knepley   PetscInt        ptpoints[4];
8372412e9a14SMatthew G. Knepley   const PetscInt *cone, *ccone, *ptcone;
8373412e9a14SMatthew G. Knepley   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
8374412e9a14SMatthew G. Knepley   PetscErrorCode  ierr;
8375412e9a14SMatthew G. Knepley 
8376412e9a14SMatthew G. Knepley   PetscFunctionBegin;
8377412e9a14SMatthew G. Knepley   *unsplit = 0;
8378412e9a14SMatthew G. Knepley   switch (ct) {
8379b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
8380b5a892a1SMatthew G. Knepley       ptpoints[npt++] = c;
8381b5a892a1SMatthew G. Knepley       break;
8382412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
8383412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
8384412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
8385412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
8386412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, cone[cp], &cct);CHKERRQ(ierr);
8387412e9a14SMatthew G. Knepley         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
8388412e9a14SMatthew G. Knepley       }
8389412e9a14SMatthew G. Knepley       break;
8390412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
8391412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8392412e9a14SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
8393412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
8394412e9a14SMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
8395412e9a14SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cone[cp], &ccone);CHKERRQ(ierr);
8396412e9a14SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cone[cp], &cconeSize);CHKERRQ(ierr);
8397412e9a14SMatthew G. Knepley         for (ccp = 0; ccp < cconeSize; ++ccp) {
8398412e9a14SMatthew G. Knepley           ierr = DMPlexGetCellType(dm, ccone[ccp], &cct);CHKERRQ(ierr);
8399412e9a14SMatthew G. Knepley           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
8400412e9a14SMatthew G. Knepley             PetscInt p;
8401412e9a14SMatthew G. Knepley             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
8402412e9a14SMatthew G. Knepley             if (p == npt) ptpoints[npt++] = ccone[ccp];
8403412e9a14SMatthew G. Knepley           }
8404412e9a14SMatthew G. Knepley         }
8405412e9a14SMatthew G. Knepley       }
8406412e9a14SMatthew G. Knepley       break;
8407412e9a14SMatthew G. Knepley     default: break;
8408412e9a14SMatthew G. Knepley   }
8409412e9a14SMatthew G. Knepley   for (pt = 0; pt < npt; ++pt) {
8410412e9a14SMatthew G. Knepley     ierr = DMPlexGetCone(dm, ptpoints[pt], &ptcone);CHKERRQ(ierr);
8411412e9a14SMatthew G. Knepley     if (ptcone[0] == ptcone[1]) ++(*unsplit);
8412412e9a14SMatthew G. Knepley   }
8413412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
8414412e9a14SMatthew G. Knepley }
8415412e9a14SMatthew G. Knepley 
8416ca8062c8SMatthew G. Knepley /*@
8417ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
8418ca8062c8SMatthew G. Knepley 
8419ca8062c8SMatthew G. Knepley   Input Parameters:
8420ca8062c8SMatthew G. Knepley + dm - The DMPlex object
842158723a97SMatthew G. Knepley - cellHeight - Normally 0
8422ca8062c8SMatthew G. Knepley 
842395eb5ee5SVaclav Hapla   Notes:
842495eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
842525c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
8426ca8062c8SMatthew G. Knepley 
842795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
842895eb5ee5SVaclav Hapla 
8429ca8062c8SMatthew G. Knepley   Level: developer
8430ca8062c8SMatthew G. Knepley 
843195eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
8432ca8062c8SMatthew G. Knepley @*/
843325c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
8434ca8062c8SMatthew G. Knepley {
8435412e9a14SMatthew G. Knepley   DMPlexInterpolatedFlag interp;
8436412e9a14SMatthew G. Knepley   DMPolytopeType         ct;
8437412e9a14SMatthew G. Knepley   PetscInt               vStart, vEnd, cStart, cEnd, c;
8438ca8062c8SMatthew G. Knepley   PetscErrorCode         ierr;
8439ca8062c8SMatthew G. Knepley 
8440ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
8441ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8442412e9a14SMatthew G. Knepley   ierr = DMPlexIsInterpolated(dm, &interp);CHKERRQ(ierr);
844325c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
844458723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
8445412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
8446412e9a14SMatthew G. Knepley     PetscInt *closure = NULL;
8447412e9a14SMatthew G. Knepley     PetscInt  coneSize, closureSize, cl, Nv = 0;
844858723a97SMatthew G. Knepley 
8449412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
84502c71b3e2SJacob Faibussowitsch     PetscCheckFalse((PetscInt) ct < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
8451412e9a14SMatthew G. Knepley     if (ct == DM_POLYTOPE_UNKNOWN) continue;
8452412e9a14SMatthew G. Knepley     if (interp == DMPLEX_INTERPOLATED_FULL) {
8453412e9a14SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
84542c71b3e2SJacob Faibussowitsch       PetscCheckFalse(coneSize != DMPolytopeTypeGetConeSize(ct),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has cone size %D != %D", c, DMPolytopeTypes[ct], coneSize, DMPolytopeTypeGetConeSize(ct));
8455412e9a14SMatthew G. Knepley     }
845658723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
845758723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
845858723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
8459412e9a14SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++Nv;
846058723a97SMatthew G. Knepley     }
846158723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
8462412e9a14SMatthew G. Knepley     /* Special Case: Tensor faces with identified vertices */
8463412e9a14SMatthew G. Knepley     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
8464412e9a14SMatthew G. Knepley       PetscInt unsplit;
846542363296SMatthew G. Knepley 
8466412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
8467412e9a14SMatthew G. Knepley       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
846842363296SMatthew G. Knepley     }
84692c71b3e2SJacob Faibussowitsch     PetscCheckFalse(Nv != DMPolytopeTypeGetNumVertices(ct),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has %D vertices != %D", c, DMPolytopeTypes[ct], Nv, DMPolytopeTypeGetNumVertices(ct));
847042363296SMatthew G. Knepley   }
8471ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
8472ca8062c8SMatthew G. Knepley }
84739bf0dad6SMatthew G. Knepley 
84749bf0dad6SMatthew G. Knepley /*@
84759bf0dad6SMatthew 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
84769bf0dad6SMatthew G. Knepley 
8477899ea2b8SJacob Faibussowitsch   Not Collective
8478899ea2b8SJacob Faibussowitsch 
84799bf0dad6SMatthew G. Knepley   Input Parameters:
84809bf0dad6SMatthew G. Knepley + dm - The DMPlex object
84819bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
84829bf0dad6SMatthew G. Knepley 
848345da879fSVaclav Hapla   Notes:
848445da879fSVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
848545da879fSVaclav Hapla   This routine is only relevant for meshes that are fully interpolated across all ranks.
848645da879fSVaclav Hapla   It will error out if a partially interpolated mesh is given on some rank.
848745da879fSVaclav Hapla   It will do nothing for locally uninterpolated mesh (as there is nothing to check).
84889bf0dad6SMatthew G. Knepley 
848995eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
849095eb5ee5SVaclav Hapla 
84919bf0dad6SMatthew G. Knepley   Level: developer
84929bf0dad6SMatthew G. Knepley 
849395eb5ee5SVaclav Hapla .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
84949bf0dad6SMatthew G. Knepley @*/
849525c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
84969bf0dad6SMatthew G. Knepley {
8497ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
84989bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
8499899ea2b8SJacob Faibussowitsch   DMPlexInterpolatedFlag interpEnum;
85009bf0dad6SMatthew G. Knepley 
85019bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
85029bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8503899ea2b8SJacob Faibussowitsch   ierr = DMPlexIsInterpolated(dm, &interpEnum);CHKERRQ(ierr);
850445da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(0);
850545da879fSVaclav Hapla   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
8506899ea2b8SJacob Faibussowitsch     PetscMPIInt rank;
8507899ea2b8SJacob Faibussowitsch     MPI_Comm    comm;
8508899ea2b8SJacob Faibussowitsch 
8509899ea2b8SJacob Faibussowitsch     ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
8510ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
851198921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
8512899ea2b8SJacob Faibussowitsch   }
8513899ea2b8SJacob Faibussowitsch 
8514c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8515ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
85169bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
8517ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
85183554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
85193554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
8520412e9a14SMatthew G. Knepley       const PetscInt      *cone, *ornt, *faceSizes, *faces;
8521412e9a14SMatthew G. Knepley       const DMPolytopeType *faceTypes;
8522ba2698f1SMatthew G. Knepley       DMPolytopeType        ct;
8523412e9a14SMatthew G. Knepley       PetscInt              numFaces, coneSize, f;
8524412e9a14SMatthew G. Knepley       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
85259bf0dad6SMatthew G. Knepley 
8526ba2698f1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
8527412e9a14SMatthew G. Knepley       ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
8528412e9a14SMatthew G. Knepley       if (unsplit) continue;
85299bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
85309bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
85319bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
85329bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
85339bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
85349bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
85359bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
85369bf0dad6SMatthew G. Knepley       }
8537412e9a14SMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
85382c71b3e2SJacob Faibussowitsch       PetscCheckFalse(coneSize != numFaces,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has %D faces but should have %D", c, DMPolytopeTypes[ct], coneSize, numFaces);
85399bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
8540d4961f80SStefano Zampini         DMPolytopeType fct;
85419bf0dad6SMatthew G. Knepley         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
85429bf0dad6SMatthew G. Knepley 
8543d4961f80SStefano Zampini         ierr = DMPlexGetCellType(dm, cone[f], &fct);CHKERRQ(ierr);
85449bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
85459bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
85469bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
85479bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
85489bf0dad6SMatthew G. Knepley         }
85492c71b3e2SJacob Faibussowitsch         PetscCheckFalse(fnumCorners != faceSizes[f],PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %D) of cell %D of type %s has %D vertices but should have %D", cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], fnumCorners, faceSizes[f]);
85509bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
8551b5a892a1SMatthew G. Knepley           if (fclosure[v] != faces[fOff+v]) {
8552b5a892a1SMatthew G. Knepley             PetscInt v1;
8553b5a892a1SMatthew G. Knepley 
8554b5a892a1SMatthew G. Knepley             ierr = PetscPrintf(PETSC_COMM_SELF, "face closure:");CHKERRQ(ierr);
8555b5a892a1SMatthew G. Knepley             for (v1 = 0; v1 < fnumCorners; ++v1) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", fclosure[v1]);CHKERRQ(ierr);}
8556b5a892a1SMatthew G. Knepley             ierr = PetscPrintf(PETSC_COMM_SELF, "\ncell face:");CHKERRQ(ierr);
8557b5a892a1SMatthew G. Knepley             for (v1 = 0; v1 < fnumCorners; ++v1) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", faces[fOff+v1]);CHKERRQ(ierr);}
8558b5a892a1SMatthew G. Knepley             ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
855998921bdaSJacob Faibussowitsch             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %d, ornt %D) of cell %D of type %s vertex %D, %D != %D", cone[f], DMPolytopeTypes[fct], f, ornt[f], c, DMPolytopeTypes[ct], v, fclosure[v], faces[fOff+v]);
8560b5a892a1SMatthew G. Knepley           }
85619bf0dad6SMatthew G. Knepley         }
85629bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
8563412e9a14SMatthew G. Knepley         fOff += faceSizes[f];
85649bf0dad6SMatthew G. Knepley       }
8565412e9a14SMatthew G. Knepley       ierr = DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);CHKERRQ(ierr);
85669bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
85679bf0dad6SMatthew G. Knepley     }
85683554e41dSMatthew G. Knepley   }
8569552f7358SJed Brown   PetscFunctionReturn(0);
8570552f7358SJed Brown }
85713913d7c8SMatthew G. Knepley 
8572bb6a34a8SMatthew G. Knepley /*@
8573bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
8574bb6a34a8SMatthew G. Knepley 
8575bb6a34a8SMatthew G. Knepley   Input Parameter:
8576bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
8577bb6a34a8SMatthew G. Knepley 
857895eb5ee5SVaclav Hapla   Notes:
857995eb5ee5SVaclav Hapla   This is a useful diagnostic when creating meshes programmatically.
858095eb5ee5SVaclav Hapla 
858195eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8582bb6a34a8SMatthew G. Knepley 
8583bb6a34a8SMatthew G. Knepley   Level: developer
8584bb6a34a8SMatthew G. Knepley 
858595eb5ee5SVaclav Hapla .seealso: DMCreate(), DMSetFromOptions()
8586bb6a34a8SMatthew G. Knepley @*/
8587bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
8588bb6a34a8SMatthew G. Knepley {
8589a2a9e04cSMatthew G. Knepley   Vec            coordinates;
8590bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
8591bb6a34a8SMatthew G. Knepley   PetscReal      vol;
8592412e9a14SMatthew G. Knepley   PetscBool      periodic;
859351a74b61SMatthew G. Knepley   PetscInt       dim, depth, dE, d, cStart, cEnd, c;
8594bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
8595bb6a34a8SMatthew G. Knepley 
8596bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
8597bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
859851a74b61SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
859951a74b61SMatthew G. Knepley   if (dim != dE) PetscFunctionReturn(0);
8600bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
8601412e9a14SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
8602bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
8603bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8604a2a9e04cSMatthew G. Knepley   /* Make sure local coordinates are created, because that step is collective */
8605a2a9e04cSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
8606412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
8607412e9a14SMatthew G. Knepley     DMPolytopeType ct;
8608412e9a14SMatthew G. Knepley     PetscInt       unsplit;
8609412e9a14SMatthew G. Knepley     PetscBool      ignoreZeroVol = PETSC_FALSE;
8610412e9a14SMatthew G. Knepley 
8611412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(dm, c, &ct);CHKERRQ(ierr);
8612412e9a14SMatthew G. Knepley     switch (ct) {
8613412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEG_PRISM_TENSOR:
8614412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
8615412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8616412e9a14SMatthew G. Knepley         ignoreZeroVol = PETSC_TRUE; break;
8617412e9a14SMatthew G. Knepley       default: break;
8618412e9a14SMatthew G. Knepley     }
8619412e9a14SMatthew G. Knepley     switch (ct) {
8620412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM:
8621412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
8622412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8623a2a9e04cSMatthew G. Knepley       case DM_POLYTOPE_PYRAMID:
8624412e9a14SMatthew G. Knepley         continue;
8625412e9a14SMatthew G. Knepley       default: break;
8626412e9a14SMatthew G. Knepley     }
8627412e9a14SMatthew G. Knepley     ierr = DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);CHKERRQ(ierr);
8628412e9a14SMatthew G. Knepley     if (unsplit) continue;
8629bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
86302c71b3e2SJacob Faibussowitsch     PetscCheckFalse(detJ < -PETSC_SMALL || (detJ <= 0.0 && !ignoreZeroVol),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D of type %s is inverted, |J| = %g", c, DMPolytopeTypes[ct], (double) detJ);
86317d3de750SJacob Faibussowitsch     ierr = PetscInfo(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
8632412e9a14SMatthew G. Knepley     if (depth > 1 && !periodic) {
8633bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
86342c71b3e2SJacob Faibussowitsch       PetscCheckFalse(vol < -PETSC_SMALL || (vol <= 0.0 && !ignoreZeroVol),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D of type %s is inverted, vol = %g", c, DMPolytopeTypes[ct], (double) vol);
86357d3de750SJacob Faibussowitsch       ierr = PetscInfo(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
8636bb6a34a8SMatthew G. Knepley     }
8637bb6a34a8SMatthew G. Knepley   }
8638bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
8639bb6a34a8SMatthew G. Knepley }
8640bb6a34a8SMatthew G. Knepley 
864103da9461SVaclav Hapla /*@
8642e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
864303da9461SVaclav Hapla 
864403da9461SVaclav Hapla   Input Parameters:
864503da9461SVaclav Hapla . dm - The DMPlex object
864603da9461SVaclav Hapla 
8647e83a0d2dSVaclav Hapla   Notes:
8648e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
86498918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
865003da9461SVaclav Hapla 
865195eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
865295eb5ee5SVaclav Hapla 
865303da9461SVaclav Hapla   Level: developer
865403da9461SVaclav Hapla 
865595eb5ee5SVaclav Hapla .seealso: DMGetPointSF(), DMSetFromOptions()
865603da9461SVaclav Hapla @*/
865703da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
865803da9461SVaclav Hapla {
8659f0cfc026SVaclav Hapla   PetscSF         pointSF;
8660f5869d18SMatthew G. Knepley   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
8661f5869d18SMatthew G. Knepley   const PetscInt *locals, *rootdegree;
8662f0cfc026SVaclav Hapla   PetscBool       distributed;
866303da9461SVaclav Hapla   PetscErrorCode  ierr;
866403da9461SVaclav Hapla 
866503da9461SVaclav Hapla   PetscFunctionBegin;
866603da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8667f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
8668f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
8669f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
8670f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
8671f0cfc026SVaclav Hapla   if (overlap) {
86721e1ea65dSPierre Jolivet     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");CHKERRQ(ierr);
86738918e3e2SVaclav Hapla     PetscFunctionReturn(0);
86748918e3e2SVaclav Hapla   }
86752c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!pointSF,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
8676f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
86772c71b3e2SJacob Faibussowitsch   PetscCheckFalse(nroots < 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
8678f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeBegin(pointSF, &rootdegree);CHKERRQ(ierr);
8679f5869d18SMatthew G. Knepley   ierr = PetscSFComputeDegreeEnd(pointSF, &rootdegree);CHKERRQ(ierr);
868003da9461SVaclav Hapla 
8681ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
8682f5869d18SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8683f5869d18SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8684f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8685f5869d18SMatthew G. Knepley     const PetscInt point = locals[l];
8686f5869d18SMatthew G. Knepley 
86872c71b3e2SJacob Faibussowitsch     PetscCheckFalse(point >= cStart && point < cEnd,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
868803da9461SVaclav Hapla   }
8689ece87651SVaclav Hapla 
8690f5869d18SMatthew G. Knepley   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8691f5869d18SMatthew G. Knepley   for (l = 0; l < nleaves; ++l) {
8692f5869d18SMatthew G. Knepley     const PetscInt  point = locals[l];
8693f5869d18SMatthew G. Knepley     const PetscInt *cone;
8694f5869d18SMatthew G. Knepley     PetscInt        coneSize, c, idx;
8695f5869d18SMatthew G. Knepley 
8696f5869d18SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
8697f5869d18SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
8698f5869d18SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
8699f5869d18SMatthew G. Knepley       if (!rootdegree[cone[c]]) {
8700f5869d18SMatthew G. Knepley         ierr = PetscFindInt(cone[c], nleaves, locals, &idx);CHKERRQ(ierr);
87012c71b3e2SJacob Faibussowitsch         PetscCheckFalse(idx < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D but not %D from its cone", point, cone[c]);
8702f5869d18SMatthew G. Knepley       }
8703f5869d18SMatthew G. Knepley     }
8704ece87651SVaclav Hapla   }
870503da9461SVaclav Hapla   PetscFunctionReturn(0);
870603da9461SVaclav Hapla }
870703da9461SVaclav Hapla 
8708b5a892a1SMatthew G. Knepley PetscErrorCode DMPlexCheckAll_Internal(DM dm, PetscInt cellHeight)
8709b5a892a1SMatthew G. Knepley {
8710b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
8711b5a892a1SMatthew G. Knepley 
8712b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
8713b5a892a1SMatthew G. Knepley   ierr = DMPlexCheckSymmetry(dm);CHKERRQ(ierr);
8714b5a892a1SMatthew G. Knepley   ierr = DMPlexCheckSkeleton(dm, cellHeight);CHKERRQ(ierr);
8715b5a892a1SMatthew G. Knepley   ierr = DMPlexCheckFaces(dm, cellHeight);CHKERRQ(ierr);
8716b5a892a1SMatthew G. Knepley   ierr = DMPlexCheckGeometry(dm);CHKERRQ(ierr);
8717b5a892a1SMatthew G. Knepley   ierr = DMPlexCheckPointSF(dm);CHKERRQ(ierr);
8718b5a892a1SMatthew G. Knepley   ierr = DMPlexCheckInterfaceCones(dm);CHKERRQ(ierr);
8719b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8720b5a892a1SMatthew G. Knepley }
8721b5a892a1SMatthew G. Knepley 
8722068a5610SStefano Zampini typedef struct cell_stats
8723068a5610SStefano Zampini {
8724068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
8725068a5610SStefano Zampini   PetscInt  count;
8726068a5610SStefano Zampini } cell_stats_t;
8727068a5610SStefano Zampini 
872825befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
8729068a5610SStefano Zampini {
8730068a5610SStefano Zampini   PetscInt i, N = *len;
8731068a5610SStefano Zampini 
8732068a5610SStefano Zampini   for (i = 0; i < N; i++) {
8733068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
8734068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
8735068a5610SStefano Zampini 
8736068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
8737068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
8738068a5610SStefano Zampini     B->sum += A->sum;
8739068a5610SStefano Zampini     B->squaresum += A->squaresum;
8740068a5610SStefano Zampini     B->count += A->count;
8741068a5610SStefano Zampini   }
8742068a5610SStefano Zampini }
8743068a5610SStefano Zampini 
8744068a5610SStefano Zampini /*@
874543fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
8746068a5610SStefano Zampini 
87478261a58bSMatthew G. Knepley   Collective on dm
87488261a58bSMatthew G. Knepley 
8749068a5610SStefano Zampini   Input Parameters:
8750068a5610SStefano Zampini + dm        - The DMPlex object
875143fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
875243fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
8753068a5610SStefano Zampini 
875495eb5ee5SVaclav Hapla   Notes:
875595eb5ee5SVaclav Hapla   This is mainly intended for debugging/testing purposes.
875695eb5ee5SVaclav Hapla 
875795eb5ee5SVaclav Hapla   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8758068a5610SStefano Zampini 
8759068a5610SStefano Zampini   Level: developer
8760068a5610SStefano Zampini 
8761f108dbd7SJacob Faibussowitsch .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
8762068a5610SStefano Zampini @*/
876343fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8764068a5610SStefano Zampini {
8765068a5610SStefano Zampini   DM             dmCoarse;
876643fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
876743fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
876843fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
876943fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8770412e9a14SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
877143fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
8772068a5610SStefano Zampini   PetscErrorCode ierr;
8773068a5610SStefano Zampini 
8774068a5610SStefano Zampini   PetscFunctionBegin;
8775068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8776068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
8777068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
8778068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
8779068a5610SStefano Zampini   stats.count = 0;
8780068a5610SStefano Zampini 
8781ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
8782ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
878343fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
878443fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
8785412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
878643fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
8787412e9a14SMatthew G. Knepley   for (c = cStart; c < cEnd; c++) {
8788068a5610SStefano Zampini     PetscInt  i;
8789068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
8790068a5610SStefano Zampini 
8791068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
87922c71b3e2SJacob Faibussowitsch     PetscCheckFalse(detJ < 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
879343fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
8794068a5610SStefano Zampini       frobJ    += J[i] * J[i];
8795068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
8796068a5610SStefano Zampini     }
8797068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
8798068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
8799068a5610SStefano Zampini 
8800068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
8801068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
8802068a5610SStefano Zampini     stats.sum       += cond;
8803068a5610SStefano Zampini     stats.squaresum += cond2;
8804068a5610SStefano Zampini     stats.count++;
88058261a58bSMatthew G. Knepley     if (output && cond > limit) {
880643fa8764SMatthew G. Knepley       PetscSection coordSection;
880743fa8764SMatthew G. Knepley       Vec          coordsLocal;
880843fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
880943fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
881043fa8764SMatthew G. Knepley 
881143fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
881243fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
881343fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
8814087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
881543fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
881643fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
881743fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
881843fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
881948afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
882043fa8764SMatthew G. Knepley         }
882143fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
882243fa8764SMatthew G. Knepley       }
882343fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
882443fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
882543fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
882643fa8764SMatthew G. Knepley 
882743fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
882843fa8764SMatthew G. Knepley           PetscReal len;
882943fa8764SMatthew G. Knepley 
883043fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
8831087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
883243fa8764SMatthew G. Knepley         }
883343fa8764SMatthew G. Knepley       }
883443fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
883543fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
883643fa8764SMatthew G. Knepley     }
8837068a5610SStefano Zampini   }
88388261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
8839068a5610SStefano Zampini 
8840068a5610SStefano Zampini   if (size > 1) {
8841068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
8842068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
8843068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
8844068a5610SStefano Zampini     MPI_Op        statReduce;
8845068a5610SStefano Zampini 
8846ffc4695bSBarry Smith     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRMPI(ierr);
8847ffc4695bSBarry Smith     ierr = MPI_Type_commit(&statType);CHKERRMPI(ierr);
8848ffc4695bSBarry Smith     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRMPI(ierr);
8849ffc4695bSBarry Smith     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRMPI(ierr);
8850ffc4695bSBarry Smith     ierr = MPI_Op_free(&statReduce);CHKERRMPI(ierr);
8851ffc4695bSBarry Smith     ierr = MPI_Type_free(&statType);CHKERRMPI(ierr);
8852068a5610SStefano Zampini   } else {
8853580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
8854068a5610SStefano Zampini   }
8855dd400576SPatrick Sanan   if (rank == 0) {
8856068a5610SStefano Zampini     count = globalStats.count;
8857068a5610SStefano Zampini     min   = globalStats.min;
8858068a5610SStefano Zampini     max   = globalStats.max;
8859068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
8860068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
8861068a5610SStefano Zampini   }
8862068a5610SStefano Zampini 
8863068a5610SStefano Zampini   if (output) {
8864068a5610SStefano 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);
8865068a5610SStefano Zampini   }
8866068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
8867068a5610SStefano Zampini 
8868068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
8869068a5610SStefano Zampini   if (dmCoarse) {
8870068a5610SStefano Zampini     PetscBool isplex;
8871068a5610SStefano Zampini 
8872068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
8873068a5610SStefano Zampini     if (isplex) {
887443fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
8875068a5610SStefano Zampini     }
8876068a5610SStefano Zampini   }
8877068a5610SStefano Zampini   PetscFunctionReturn(0);
8878068a5610SStefano Zampini }
8879068a5610SStefano Zampini 
8880f108dbd7SJacob Faibussowitsch /*@
8881f108dbd7SJacob Faibussowitsch   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8882f108dbd7SJacob Faibussowitsch   orthogonal quality below given tolerance.
8883f108dbd7SJacob Faibussowitsch 
88846ed19f2fSJacob Faibussowitsch   Collective on dm
8885f108dbd7SJacob Faibussowitsch 
8886f108dbd7SJacob Faibussowitsch   Input Parameters:
8887f108dbd7SJacob Faibussowitsch + dm   - The DMPlex object
8888f108dbd7SJacob Faibussowitsch . fv   - Optional PetscFV object for pre-computed cell/face centroid information
8889f108dbd7SJacob Faibussowitsch - atol - [0, 1] Absolute tolerance for tagging cells.
8890f108dbd7SJacob Faibussowitsch 
8891f108dbd7SJacob Faibussowitsch   Output Parameters:
8892f108dbd7SJacob Faibussowitsch + OrthQual      - Vec containing orthogonal quality per cell
8893f108dbd7SJacob Faibussowitsch - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE
8894f108dbd7SJacob Faibussowitsch 
8895f108dbd7SJacob Faibussowitsch   Options Database Keys:
8896f108dbd7SJacob Faibussowitsch + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
8897f108dbd7SJacob Faibussowitsch supported.
8898f108dbd7SJacob Faibussowitsch - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
8899f108dbd7SJacob Faibussowitsch 
8900f108dbd7SJacob Faibussowitsch   Notes:
8901f108dbd7SJacob Faibussowitsch   Orthogonal quality is given by the following formula:
8902f108dbd7SJacob Faibussowitsch 
8903f108dbd7SJacob Faibussowitsch   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]
8904f108dbd7SJacob Faibussowitsch 
8905f108dbd7SJacob Faibussowitsch   Where A_i is the i'th face-normal vector, f_i is the vector from the cell centroid to the i'th face centroid, and c_i
8906f108dbd7SJacob Faibussowitsch   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8907f108dbd7SJacob Faibussowitsch   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8908f108dbd7SJacob Faibussowitsch   calculating the cosine of the angle between these vectors.
8909f108dbd7SJacob Faibussowitsch 
8910f108dbd7SJacob Faibussowitsch   Orthogonal quality ranges from 1 (best) to 0 (worst).
8911f108dbd7SJacob Faibussowitsch 
8912f108dbd7SJacob Faibussowitsch   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
8913f108dbd7SJacob Faibussowitsch   pre-computed FVM cell data, but if it is not passed in then this data will be computed.
8914f108dbd7SJacob Faibussowitsch 
8915f108dbd7SJacob Faibussowitsch   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
8916f108dbd7SJacob Faibussowitsch 
8917f108dbd7SJacob Faibussowitsch   Level: intermediate
8918f108dbd7SJacob Faibussowitsch 
8919f108dbd7SJacob Faibussowitsch .seealso: DMPlexCheckCellShape(), DMCreateLabel()
8920f108dbd7SJacob Faibussowitsch @*/
8921f108dbd7SJacob Faibussowitsch PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
8922f108dbd7SJacob Faibussowitsch {
89236ed19f2fSJacob Faibussowitsch   PetscInt                nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
89246ed19f2fSJacob Faibussowitsch   PetscInt                *idx;
89256ed19f2fSJacob Faibussowitsch   PetscScalar             *oqVals;
8926f108dbd7SJacob Faibussowitsch   const PetscScalar       *cellGeomArr, *faceGeomArr;
89276ed19f2fSJacob Faibussowitsch   PetscReal               *ci, *fi, *Ai;
8928f108dbd7SJacob Faibussowitsch   MPI_Comm                comm;
8929f108dbd7SJacob Faibussowitsch   Vec                     cellgeom, facegeom;
8930f108dbd7SJacob Faibussowitsch   DM                      dmFace, dmCell;
8931f108dbd7SJacob Faibussowitsch   IS                      glob;
8932f108dbd7SJacob Faibussowitsch   ISLocalToGlobalMapping  ltog;
8933f108dbd7SJacob Faibussowitsch   PetscViewer             vwr;
8934f108dbd7SJacob Faibussowitsch   PetscErrorCode          ierr;
8935f108dbd7SJacob Faibussowitsch 
8936f108dbd7SJacob Faibussowitsch   PetscFunctionBegin;
8937f108dbd7SJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89386ed19f2fSJacob Faibussowitsch   if (fv) {PetscValidHeaderSpecific(fv, PETSCFV_CLASSID, 2);}
8939f108dbd7SJacob Faibussowitsch   PetscValidPointer(OrthQual, 4);
89406bdcaf15SBarry Smith   PetscCheck(atol >= 0.0 && atol <= 1.0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %g not in [0,1]",(double)atol);
8941f108dbd7SJacob Faibussowitsch   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
8942f108dbd7SJacob Faibussowitsch   ierr = DMGetDimension(dm, &nc);CHKERRQ(ierr);
89432c71b3e2SJacob Faibussowitsch   PetscCheckFalse(nc < 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
89446ed19f2fSJacob Faibussowitsch   {
89456ed19f2fSJacob Faibussowitsch     DMPlexInterpolatedFlag interpFlag;
89466ed19f2fSJacob Faibussowitsch 
8947f108dbd7SJacob Faibussowitsch     ierr = DMPlexIsInterpolated(dm, &interpFlag);CHKERRQ(ierr);
8948f108dbd7SJacob Faibussowitsch     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
8949f108dbd7SJacob Faibussowitsch       PetscMPIInt rank;
8950f108dbd7SJacob Faibussowitsch 
8951ffc4695bSBarry Smith       ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
895298921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
8953f108dbd7SJacob Faibussowitsch     }
89546ed19f2fSJacob Faibussowitsch   }
8955f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
8956f108dbd7SJacob Faibussowitsch     PetscValidPointer(OrthQualLabel, 5);
8957f108dbd7SJacob Faibussowitsch     ierr = DMCreateLabel(dm, "Orthogonal_Quality");CHKERRQ(ierr);
8958f108dbd7SJacob Faibussowitsch     ierr = DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);CHKERRQ(ierr);
89596ed19f2fSJacob Faibussowitsch   } else {*OrthQualLabel = NULL;}
8960f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8961f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
8962f108dbd7SJacob Faibussowitsch   ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);CHKERRQ(ierr);
8963f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingCreateIS(glob, &ltog);CHKERRQ(ierr);
8964f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
8965f108dbd7SJacob Faibussowitsch   ierr = VecCreate(comm, OrthQual);CHKERRQ(ierr);
8966f108dbd7SJacob Faibussowitsch   ierr = VecSetType(*OrthQual, VECSTANDARD);CHKERRQ(ierr);
8967f108dbd7SJacob Faibussowitsch   ierr = VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);CHKERRQ(ierr);
8968f108dbd7SJacob Faibussowitsch   ierr = VecSetLocalToGlobalMapping(*OrthQual, ltog);CHKERRQ(ierr);
8969f108dbd7SJacob Faibussowitsch   ierr = VecSetUp(*OrthQual);CHKERRQ(ierr);
8970f108dbd7SJacob Faibussowitsch   ierr = ISDestroy(&glob);CHKERRQ(ierr);
8971f108dbd7SJacob Faibussowitsch   ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
8972f108dbd7SJacob Faibussowitsch   ierr = DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);CHKERRQ(ierr);
8973f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
8974f108dbd7SJacob Faibussowitsch   ierr = VecGetArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
8975f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(cellgeom, &dmCell);CHKERRQ(ierr);
8976f108dbd7SJacob Faibussowitsch   ierr = VecGetDM(facegeom, &dmFace);CHKERRQ(ierr);
89776ed19f2fSJacob Faibussowitsch   ierr = PetscMalloc5(cEnd-cStart, &idx, cEnd-cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);CHKERRQ(ierr);
89786ed19f2fSJacob Faibussowitsch   for (cell = cStart; cell < cEnd; cellIter++,cell++) {
89796ed19f2fSJacob Faibussowitsch     PetscInt           cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
8980f108dbd7SJacob Faibussowitsch     PetscInt           cellarr[2], *adj = NULL;
8981f108dbd7SJacob Faibussowitsch     PetscScalar        *cArr, *fArr;
8982898cd552SSatish Balay     PetscReal          minvalc = 1.0, minvalf = 1.0;
8983f108dbd7SJacob Faibussowitsch     PetscFVCellGeom    *cg;
8984f108dbd7SJacob Faibussowitsch 
89856ed19f2fSJacob Faibussowitsch     idx[cellIter] = cell-cStart;
8986f108dbd7SJacob Faibussowitsch     cellarr[0] = cell;
8987f108dbd7SJacob Faibussowitsch     /* Make indexing into cellGeom easier */
8988f108dbd7SJacob Faibussowitsch     ierr = DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);CHKERRQ(ierr);
8989f108dbd7SJacob Faibussowitsch     ierr = DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);CHKERRQ(ierr);
8990f108dbd7SJacob Faibussowitsch     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8991f108dbd7SJacob Faibussowitsch     ierr = PetscCalloc2(adjSize, &cArr, adjSize, &fArr);CHKERRQ(ierr);
89926ed19f2fSJacob Faibussowitsch     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++,cellneigh++) {
89936ed19f2fSJacob Faibussowitsch       PetscInt         i;
89946ed19f2fSJacob Faibussowitsch       const PetscInt   neigh = adj[cellneigh];
8995f108dbd7SJacob Faibussowitsch       PetscReal        normci = 0, normfi = 0, normai = 0;
8996f108dbd7SJacob Faibussowitsch       PetscFVCellGeom  *cgneigh;
8997f108dbd7SJacob Faibussowitsch       PetscFVFaceGeom  *fg;
8998f108dbd7SJacob Faibussowitsch 
8999f108dbd7SJacob Faibussowitsch       /* Don't count ourselves in the neighbor list */
9000f108dbd7SJacob Faibussowitsch       if (neigh == cell) continue;
9001f108dbd7SJacob Faibussowitsch       ierr = DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);CHKERRQ(ierr);
9002f108dbd7SJacob Faibussowitsch       cellarr[1] = neigh;
90036ed19f2fSJacob Faibussowitsch       {
90046ed19f2fSJacob Faibussowitsch         PetscInt       numcovpts;
90056ed19f2fSJacob Faibussowitsch         const PetscInt *covpts;
90066ed19f2fSJacob Faibussowitsch 
9007f108dbd7SJacob Faibussowitsch         ierr = DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
9008f108dbd7SJacob Faibussowitsch         ierr = DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);CHKERRQ(ierr);
9009f108dbd7SJacob Faibussowitsch         ierr = DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);CHKERRQ(ierr);
90106ed19f2fSJacob Faibussowitsch       }
9011f108dbd7SJacob Faibussowitsch 
9012f108dbd7SJacob Faibussowitsch       /* Compute c_i, f_i and their norms */
9013f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
9014f108dbd7SJacob Faibussowitsch         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
9015f108dbd7SJacob Faibussowitsch         fi[i] = fg->centroid[i] - cg->centroid[i];
9016f108dbd7SJacob Faibussowitsch         Ai[i] = fg->normal[i];
9017addd1e01SJunchao Zhang         normci += PetscPowReal(ci[i], 2);
9018addd1e01SJunchao Zhang         normfi += PetscPowReal(fi[i], 2);
9019addd1e01SJunchao Zhang         normai += PetscPowReal(Ai[i], 2);
9020f108dbd7SJacob Faibussowitsch       }
9021addd1e01SJunchao Zhang       normci = PetscSqrtReal(normci);
9022addd1e01SJunchao Zhang       normfi = PetscSqrtReal(normfi);
9023addd1e01SJunchao Zhang       normai = PetscSqrtReal(normai);
9024f108dbd7SJacob Faibussowitsch 
9025f108dbd7SJacob Faibussowitsch       /* Normalize and compute for each face-cell-normal pair */
9026f108dbd7SJacob Faibussowitsch       for (i = 0; i < nc; i++) {
9027f108dbd7SJacob Faibussowitsch         ci[i] = ci[i]/normci;
9028f108dbd7SJacob Faibussowitsch         fi[i] = fi[i]/normfi;
9029f108dbd7SJacob Faibussowitsch         Ai[i] = Ai[i]/normai;
9030f108dbd7SJacob Faibussowitsch         /* PetscAbs because I don't know if normals are guaranteed to point out */
9031f108dbd7SJacob Faibussowitsch         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
9032f108dbd7SJacob Faibussowitsch         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
9033f108dbd7SJacob Faibussowitsch       }
9034f108dbd7SJacob Faibussowitsch       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
9035f108dbd7SJacob Faibussowitsch         minvalc = PetscRealPart(cArr[cellneighiter]);
9036f108dbd7SJacob Faibussowitsch       }
9037f108dbd7SJacob Faibussowitsch       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
9038f108dbd7SJacob Faibussowitsch         minvalf = PetscRealPart(fArr[cellneighiter]);
9039f108dbd7SJacob Faibussowitsch       }
9040f108dbd7SJacob Faibussowitsch     }
9041f108dbd7SJacob Faibussowitsch     ierr = PetscFree(adj);CHKERRQ(ierr);
9042f108dbd7SJacob Faibussowitsch     ierr = PetscFree2(cArr, fArr);CHKERRQ(ierr);
9043f108dbd7SJacob Faibussowitsch     /* Defer to cell if they're equal */
90446ed19f2fSJacob Faibussowitsch     oqVals[cellIter] = PetscMin(minvalf, minvalc);
9045f108dbd7SJacob Faibussowitsch     if (OrthQualLabel) {
90466ed19f2fSJacob Faibussowitsch       if (PetscRealPart(oqVals[cellIter]) <= atol) {ierr = DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);CHKERRQ(ierr);}
9047f108dbd7SJacob Faibussowitsch     }
9048f108dbd7SJacob Faibussowitsch   }
90496ed19f2fSJacob Faibussowitsch   ierr = VecSetValuesLocal(*OrthQual, cEnd-cStart, idx, oqVals, INSERT_VALUES);CHKERRQ(ierr);
9050f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyBegin(*OrthQual);CHKERRQ(ierr);
9051f108dbd7SJacob Faibussowitsch   ierr = VecAssemblyEnd(*OrthQual);CHKERRQ(ierr);
9052f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(cellgeom, &cellGeomArr);CHKERRQ(ierr);
9053f108dbd7SJacob Faibussowitsch   ierr = VecRestoreArrayRead(facegeom, &faceGeomArr);CHKERRQ(ierr);
9054f108dbd7SJacob Faibussowitsch   ierr = PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);CHKERRQ(ierr);
9055f108dbd7SJacob Faibussowitsch   if (OrthQualLabel) {
90566ed19f2fSJacob Faibussowitsch     if (vwr) {ierr = DMLabelView(*OrthQualLabel, vwr);CHKERRQ(ierr);}
9057f108dbd7SJacob Faibussowitsch   }
90586ed19f2fSJacob Faibussowitsch   ierr = PetscFree5(idx, oqVals, ci, fi, Ai);CHKERRQ(ierr);
9059f108dbd7SJacob Faibussowitsch   ierr = PetscViewerDestroy(&vwr);CHKERRQ(ierr);
9060f108dbd7SJacob Faibussowitsch   ierr = VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");CHKERRQ(ierr);
9061f108dbd7SJacob Faibussowitsch   PetscFunctionReturn(0);
9062f108dbd7SJacob Faibussowitsch }
9063f108dbd7SJacob Faibussowitsch 
90641eb70e55SToby Isaac /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
90651eb70e55SToby Isaac  * interpolator construction */
90661eb70e55SToby Isaac static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
90671eb70e55SToby Isaac {
90681eb70e55SToby Isaac   PetscSection   section, newSection, gsection;
90691eb70e55SToby Isaac   PetscSF        sf;
90701eb70e55SToby Isaac   PetscBool      hasConstraints, ghasConstraints;
90711eb70e55SToby Isaac   PetscErrorCode ierr;
90721eb70e55SToby Isaac 
90731eb70e55SToby Isaac   PetscFunctionBegin;
90741eb70e55SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90751eb70e55SToby Isaac   PetscValidPointer(odm,2);
90761eb70e55SToby Isaac   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
90771eb70e55SToby Isaac   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
90781eb70e55SToby Isaac   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
90791eb70e55SToby Isaac   if (!ghasConstraints) {
90801eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
90811eb70e55SToby Isaac     *odm = dm;
90821eb70e55SToby Isaac     PetscFunctionReturn(0);
90831eb70e55SToby Isaac   }
90841eb70e55SToby Isaac   ierr = DMClone(dm, odm);CHKERRQ(ierr);
90851eb70e55SToby Isaac   ierr = DMCopyFields(dm, *odm);CHKERRQ(ierr);
90861eb70e55SToby Isaac   ierr = DMGetLocalSection(*odm, &newSection);CHKERRQ(ierr);
90871eb70e55SToby Isaac   ierr = DMGetPointSF(*odm, &sf);CHKERRQ(ierr);
90881eb70e55SToby Isaac   ierr = PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
90891eb70e55SToby Isaac   ierr = DMSetGlobalSection(*odm, gsection);CHKERRQ(ierr);
90901eb70e55SToby Isaac   ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
90911eb70e55SToby Isaac   PetscFunctionReturn(0);
90921eb70e55SToby Isaac }
90931eb70e55SToby Isaac 
90941eb70e55SToby Isaac static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
90951eb70e55SToby Isaac {
90961eb70e55SToby Isaac   DM             dmco, dmfo;
90971eb70e55SToby Isaac   Mat            interpo;
90981eb70e55SToby Isaac   Vec            rscale;
90991eb70e55SToby Isaac   Vec            cglobalo, clocal;
91001eb70e55SToby Isaac   Vec            fglobal, fglobalo, flocal;
91011eb70e55SToby Isaac   PetscBool      regular;
91021eb70e55SToby Isaac   PetscErrorCode ierr;
91031eb70e55SToby Isaac 
91041eb70e55SToby Isaac   PetscFunctionBegin;
91051eb70e55SToby Isaac   ierr = DMGetFullDM(dmc, &dmco);CHKERRQ(ierr);
91061eb70e55SToby Isaac   ierr = DMGetFullDM(dmf, &dmfo);CHKERRQ(ierr);
91071eb70e55SToby Isaac   ierr = DMSetCoarseDM(dmfo, dmco);CHKERRQ(ierr);
91081eb70e55SToby Isaac   ierr = DMPlexGetRegularRefinement(dmf, &regular);CHKERRQ(ierr);
91091eb70e55SToby Isaac   ierr = DMPlexSetRegularRefinement(dmfo, regular);CHKERRQ(ierr);
91101eb70e55SToby Isaac   ierr = DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);CHKERRQ(ierr);
91111eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmco, &cglobalo);CHKERRQ(ierr);
91121eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmc, &clocal);CHKERRQ(ierr);
91131eb70e55SToby Isaac   ierr = VecSet(cglobalo, 0.);CHKERRQ(ierr);
91141eb70e55SToby Isaac   ierr = VecSet(clocal, 0.);CHKERRQ(ierr);
91151eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmf, &fglobal);CHKERRQ(ierr);
91161eb70e55SToby Isaac   ierr = DMCreateGlobalVector(dmfo, &fglobalo);CHKERRQ(ierr);
91171eb70e55SToby Isaac   ierr = DMCreateLocalVector(dmf, &flocal);CHKERRQ(ierr);
91181eb70e55SToby Isaac   ierr = VecSet(fglobal, 0.);CHKERRQ(ierr);
91191eb70e55SToby Isaac   ierr = VecSet(fglobalo, 0.);CHKERRQ(ierr);
91201eb70e55SToby Isaac   ierr = VecSet(flocal, 0.);CHKERRQ(ierr);
91211eb70e55SToby Isaac   ierr = DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);CHKERRQ(ierr);
91221eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
91231eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);CHKERRQ(ierr);
91241eb70e55SToby Isaac   ierr = MatMult(interpo, cglobalo, fglobalo);CHKERRQ(ierr);
91251eb70e55SToby Isaac   ierr = DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
91261eb70e55SToby Isaac   ierr = DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);CHKERRQ(ierr);
91271eb70e55SToby Isaac   ierr = DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
91281eb70e55SToby Isaac   ierr = DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);CHKERRQ(ierr);
91291eb70e55SToby Isaac   *shift = fglobal;
91301eb70e55SToby Isaac   ierr = VecDestroy(&flocal);CHKERRQ(ierr);
91311eb70e55SToby Isaac   ierr = VecDestroy(&fglobalo);CHKERRQ(ierr);
91321eb70e55SToby Isaac   ierr = VecDestroy(&clocal);CHKERRQ(ierr);
91331eb70e55SToby Isaac   ierr = VecDestroy(&cglobalo);CHKERRQ(ierr);
91341eb70e55SToby Isaac   ierr = VecDestroy(&rscale);CHKERRQ(ierr);
91351eb70e55SToby Isaac   ierr = MatDestroy(&interpo);CHKERRQ(ierr);
91361eb70e55SToby Isaac   ierr = DMDestroy(&dmfo);CHKERRQ(ierr);
91371eb70e55SToby Isaac   ierr = DMDestroy(&dmco);CHKERRQ(ierr);
91381eb70e55SToby Isaac   PetscFunctionReturn(0);
91391eb70e55SToby Isaac }
91401eb70e55SToby Isaac 
91411eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
91421eb70e55SToby Isaac {
91431eb70e55SToby Isaac   PetscObject    shifto;
91441eb70e55SToby Isaac   Vec            shift;
91451eb70e55SToby Isaac 
91461eb70e55SToby Isaac   PetscErrorCode ierr;
91471eb70e55SToby Isaac 
91481eb70e55SToby Isaac   PetscFunctionBegin;
91491eb70e55SToby Isaac   if (!interp) {
91501eb70e55SToby Isaac     Vec rscale;
91511eb70e55SToby Isaac 
91521eb70e55SToby Isaac     ierr = DMCreateInterpolation(coarse, fine, &interp, &rscale);CHKERRQ(ierr);
91531eb70e55SToby Isaac     ierr = VecDestroy(&rscale);CHKERRQ(ierr);
91541eb70e55SToby Isaac   } else {
91551eb70e55SToby Isaac     ierr = PetscObjectReference((PetscObject)interp);CHKERRQ(ierr);
91561eb70e55SToby Isaac   }
91571eb70e55SToby Isaac   ierr = PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);CHKERRQ(ierr);
91581eb70e55SToby Isaac   if (!shifto) {
91591eb70e55SToby Isaac     ierr = DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);CHKERRQ(ierr);
91601eb70e55SToby Isaac     ierr = PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);CHKERRQ(ierr);
91611eb70e55SToby Isaac     shifto = (PetscObject) shift;
91621eb70e55SToby Isaac     ierr = VecDestroy(&shift);CHKERRQ(ierr);
91631eb70e55SToby Isaac   }
91641eb70e55SToby Isaac   shift = (Vec) shifto;
91651eb70e55SToby Isaac   ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
91661eb70e55SToby Isaac   ierr = VecAXPY(fineSol, 1.0, shift);CHKERRQ(ierr);
91671eb70e55SToby Isaac   ierr = MatDestroy(&interp);CHKERRQ(ierr);
91681eb70e55SToby Isaac   PetscFunctionReturn(0);
91691eb70e55SToby Isaac }
91701eb70e55SToby Isaac 
9171bceba477SMatthew G. Knepley /* Pointwise interpolation
9172bceba477SMatthew G. Knepley      Just code FEM for now
9173bceba477SMatthew G. Knepley      u^f = I u^c
91744ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
91754ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
91764ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
9177bceba477SMatthew G. Knepley */
9178bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
9179bceba477SMatthew G. Knepley {
9180bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
9181bceba477SMatthew G. Knepley   PetscInt       m, n;
9182a063dac3SMatthew G. Knepley   void          *ctx;
918368132eb9SMatthew G. Knepley   DM             cdm;
9184cf51de39SMatthew G. Knepley   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
9185bceba477SMatthew G. Knepley   PetscErrorCode ierr;
9186bceba477SMatthew G. Knepley 
9187bceba477SMatthew G. Knepley   PetscFunctionBegin;
9188e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
9189bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
9190e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
9191bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
919268132eb9SMatthew G. Knepley 
9193fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
9194bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
9195bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
9196fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
9197a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
919868132eb9SMatthew G. Knepley 
9199a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
920068132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
9201cf51de39SMatthew G. Knepley   if (!isRefined || (regular && cdm == dmCoarse)) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);CHKERRQ(ierr);}
920268132eb9SMatthew G. Knepley   else                                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
920368132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
92044db47ee9SStefano Zampini   if (scaling) {
92055d1c2e58SMatthew G. Knepley     /* Use naive scaling */
92065d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
92074db47ee9SStefano Zampini   }
9208a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
9209a063dac3SMatthew G. Knepley }
9210bceba477SMatthew G. Knepley 
92116dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
9212a063dac3SMatthew G. Knepley {
921390748bafSMatthew G. Knepley   PetscErrorCode ierr;
92146dbf9973SLawrence Mitchell   VecScatter     ctx;
921590748bafSMatthew G. Knepley 
9216a063dac3SMatthew G. Knepley   PetscFunctionBegin;
92176dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
92186dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
92196dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
9220bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
9221bceba477SMatthew G. Knepley }
9222bceba477SMatthew G. Knepley 
92233e9753d6SMatthew G. Knepley static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
92243e9753d6SMatthew G. Knepley                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
92253e9753d6SMatthew G. Knepley                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
92263e9753d6SMatthew G. Knepley                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
92273e9753d6SMatthew G. Knepley {
922800635df3SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
922900635df3SMatthew G. Knepley   PetscInt       c;
923000635df3SMatthew G. Knepley   for (c = 0; c < Nc; ++c) g0[c*Nc+c] = 1.0;
92313e9753d6SMatthew G. Knepley }
92323e9753d6SMatthew G. Knepley 
9233b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped_Plex(DM dm, Vec *mass)
9234b4937a87SMatthew G. Knepley {
9235b4937a87SMatthew G. Knepley   DM             dmc;
9236b4937a87SMatthew G. Knepley   PetscDS        ds;
9237b4937a87SMatthew G. Knepley   Vec            ones, locmass;
9238b4937a87SMatthew G. Knepley   IS             cellIS;
9239b4937a87SMatthew G. Knepley   PetscFormKey   key;
9240b4937a87SMatthew G. Knepley   PetscInt       depth;
9241b4937a87SMatthew G. Knepley   PetscErrorCode ierr;
9242b4937a87SMatthew G. Knepley 
9243b4937a87SMatthew G. Knepley   PetscFunctionBegin;
9244b4937a87SMatthew G. Knepley   ierr = DMClone(dm, &dmc);CHKERRQ(ierr);
9245b4937a87SMatthew G. Knepley   ierr = DMCopyDisc(dm, dmc);CHKERRQ(ierr);
9246b4937a87SMatthew G. Knepley   ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
9247b4937a87SMatthew G. Knepley   ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
9248b4937a87SMatthew G. Knepley   ierr = DMCreateGlobalVector(dmc, mass);CHKERRQ(ierr);
9249b4937a87SMatthew G. Knepley   ierr = DMGetLocalVector(dmc, &ones);CHKERRQ(ierr);
9250b4937a87SMatthew G. Knepley   ierr = DMGetLocalVector(dmc, &locmass);CHKERRQ(ierr);
9251b4937a87SMatthew G. Knepley   ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
9252b4937a87SMatthew G. Knepley   ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
9253b4937a87SMatthew G. Knepley   ierr = VecSet(locmass, 0.0);CHKERRQ(ierr);
9254b4937a87SMatthew G. Knepley   ierr = VecSet(ones, 1.0);CHKERRQ(ierr);
9255b4937a87SMatthew G. Knepley   key.label = NULL;
9256b4937a87SMatthew G. Knepley   key.value = 0;
9257b4937a87SMatthew G. Knepley   key.field = 0;
9258b4937a87SMatthew G. Knepley   key.part  = 0;
9259b4937a87SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Action_Internal(dmc, key, cellIS, 0.0, 0.0, ones, NULL, ones, locmass, NULL);CHKERRQ(ierr);
9260b4937a87SMatthew G. Knepley   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
9261b4937a87SMatthew G. Knepley   ierr = VecSet(*mass, 0.0);CHKERRQ(ierr);
9262b4937a87SMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dmc, locmass, ADD_VALUES, *mass);CHKERRQ(ierr);
9263b4937a87SMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dmc, locmass, ADD_VALUES, *mass);CHKERRQ(ierr);
9264b4937a87SMatthew G. Knepley   ierr = DMRestoreLocalVector(dmc, &ones);CHKERRQ(ierr);
9265b4937a87SMatthew G. Knepley   ierr = DMRestoreLocalVector(dmc, &locmass);CHKERRQ(ierr);
9266b4937a87SMatthew G. Knepley   ierr = DMDestroy(&dmc);CHKERRQ(ierr);
9267b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
9268b4937a87SMatthew G. Knepley }
9269b4937a87SMatthew G. Knepley 
9270bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
9271bd041c0cSMatthew G. Knepley {
9272bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
9273bd041c0cSMatthew G. Knepley   PetscInt       m, n;
9274bd041c0cSMatthew G. Knepley   void          *ctx;
9275bd041c0cSMatthew G. Knepley   DM             cdm;
9276bd041c0cSMatthew G. Knepley   PetscBool      regular;
9277bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
9278bd041c0cSMatthew G. Knepley 
9279bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
92803e9753d6SMatthew G. Knepley   if (dmFine == dmCoarse) {
92813e9753d6SMatthew G. Knepley     DM            dmc;
92823e9753d6SMatthew G. Knepley     PetscDS       ds;
9283b4937a87SMatthew G. Knepley     PetscWeakForm wf;
92843e9753d6SMatthew G. Knepley     Vec           u;
92853e9753d6SMatthew G. Knepley     IS            cellIS;
928606ad1575SMatthew G. Knepley     PetscFormKey  key;
92873e9753d6SMatthew G. Knepley     PetscInt      depth;
92883e9753d6SMatthew G. Knepley 
92893e9753d6SMatthew G. Knepley     ierr = DMClone(dmFine, &dmc);CHKERRQ(ierr);
92903e9753d6SMatthew G. Knepley     ierr = DMCopyDisc(dmFine, dmc);CHKERRQ(ierr);
92913e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmc, &ds);CHKERRQ(ierr);
9292b4937a87SMatthew G. Knepley     ierr = PetscDSGetWeakForm(ds, &wf);CHKERRQ(ierr);
9293b4937a87SMatthew G. Knepley     ierr = PetscWeakFormClear(wf);CHKERRQ(ierr);
92943e9753d6SMatthew G. Knepley     ierr = PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);CHKERRQ(ierr);
92953e9753d6SMatthew G. Knepley     ierr = DMCreateMatrix(dmc, mass);CHKERRQ(ierr);
92963e9753d6SMatthew G. Knepley     ierr = DMGetGlobalVector(dmc, &u);CHKERRQ(ierr);
92973e9753d6SMatthew G. Knepley     ierr = DMPlexGetDepth(dmc, &depth);CHKERRQ(ierr);
92983e9753d6SMatthew G. Knepley     ierr = DMGetStratumIS(dmc, "depth", depth, &cellIS);CHKERRQ(ierr);
92993e9753d6SMatthew G. Knepley     ierr = MatZeroEntries(*mass);CHKERRQ(ierr);
93006528b96dSMatthew G. Knepley     key.label = NULL;
93016528b96dSMatthew G. Knepley     key.value = 0;
93026528b96dSMatthew G. Knepley     key.field = 0;
930306ad1575SMatthew G. Knepley     key.part  = 0;
93046528b96dSMatthew G. Knepley     ierr = DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);CHKERRQ(ierr);
93053e9753d6SMatthew G. Knepley     ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
93063e9753d6SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmc, &u);CHKERRQ(ierr);
93073e9753d6SMatthew G. Knepley     ierr = DMDestroy(&dmc);CHKERRQ(ierr);
93083e9753d6SMatthew G. Knepley   } else {
9309e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
9310bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
9311e87a4003SBarry Smith     ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
9312bd041c0cSMatthew G. Knepley     ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
9313bd041c0cSMatthew G. Knepley 
9314bd041c0cSMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
9315bd041c0cSMatthew G. Knepley     ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
9316bd041c0cSMatthew G. Knepley     ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
9317bd041c0cSMatthew G. Knepley     ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
9318bd041c0cSMatthew G. Knepley 
9319bd041c0cSMatthew G. Knepley     ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
9320bd041c0cSMatthew G. Knepley     ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
9321bd041c0cSMatthew G. Knepley     if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
9322bd041c0cSMatthew G. Knepley     else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
93233e9753d6SMatthew G. Knepley   }
9324bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
9325bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
9326bd041c0cSMatthew G. Knepley }
9327bd041c0cSMatthew G. Knepley 
93280aef6b92SMatthew G. Knepley /*@
93290aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
93300aef6b92SMatthew G. Knepley 
93310aef6b92SMatthew G. Knepley   Input Parameter:
93320aef6b92SMatthew G. Knepley . dm - The DMPlex object
93330aef6b92SMatthew G. Knepley 
93340aef6b92SMatthew G. Knepley   Output Parameter:
93350aef6b92SMatthew G. Knepley . regular - The flag
93360aef6b92SMatthew G. Knepley 
93370aef6b92SMatthew G. Knepley   Level: intermediate
93380aef6b92SMatthew G. Knepley 
93390aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
93400aef6b92SMatthew G. Knepley @*/
93410aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
93420aef6b92SMatthew G. Knepley {
93430aef6b92SMatthew G. Knepley   PetscFunctionBegin;
93440aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93450aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
93460aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
93470aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
93480aef6b92SMatthew G. Knepley }
93490aef6b92SMatthew G. Knepley 
93500aef6b92SMatthew G. Knepley /*@
93510aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
93520aef6b92SMatthew G. Knepley 
93530aef6b92SMatthew G. Knepley   Input Parameters:
93540aef6b92SMatthew G. Knepley + dm - The DMPlex object
93550aef6b92SMatthew G. Knepley - regular - The flag
93560aef6b92SMatthew G. Knepley 
93570aef6b92SMatthew G. Knepley   Level: intermediate
93580aef6b92SMatthew G. Knepley 
93590aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
93600aef6b92SMatthew G. Knepley @*/
93610aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
93620aef6b92SMatthew G. Knepley {
93630aef6b92SMatthew G. Knepley   PetscFunctionBegin;
93640aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93650aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
93660aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
93670aef6b92SMatthew G. Knepley }
93680aef6b92SMatthew G. Knepley 
9369f7c74593SToby Isaac /* anchors */
9370a68b90caSToby Isaac /*@
9371f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
9372f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
9373a68b90caSToby Isaac 
9374e228b242SToby Isaac   not collective
9375a68b90caSToby Isaac 
9376f899ff85SJose E. Roman   Input Parameter:
9377a68b90caSToby Isaac . dm - The DMPlex object
9378a68b90caSToby Isaac 
9379a68b90caSToby Isaac   Output Parameters:
9380a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
9381a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
9382a68b90caSToby Isaac 
9383a68b90caSToby Isaac   Level: intermediate
9384a68b90caSToby Isaac 
9385f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
9386a68b90caSToby Isaac @*/
9387a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
9388a68b90caSToby Isaac {
9389a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
939041e6d900SToby Isaac   PetscErrorCode ierr;
9391a68b90caSToby Isaac 
9392a68b90caSToby Isaac   PetscFunctionBegin;
9393a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
939441e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
9395a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
9396a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
9397a68b90caSToby Isaac   PetscFunctionReturn(0);
9398a68b90caSToby Isaac }
9399a68b90caSToby Isaac 
9400a68b90caSToby Isaac /*@
9401f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
9402f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
9403a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
9404a68b90caSToby Isaac 
9405a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
9406f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
9407a68b90caSToby Isaac 
9408e228b242SToby Isaac   collective on dm
9409a68b90caSToby Isaac 
9410a68b90caSToby Isaac   Input Parameters:
9411a68b90caSToby Isaac + dm - The DMPlex object
9412e228b242SToby 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).
9413e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
9414a68b90caSToby Isaac 
9415a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
9416a68b90caSToby Isaac 
9417a68b90caSToby Isaac   Level: intermediate
9418a68b90caSToby Isaac 
9419f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
9420a68b90caSToby Isaac @*/
9421a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
9422a68b90caSToby Isaac {
9423a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
9424e228b242SToby Isaac   PetscMPIInt    result;
9425a68b90caSToby Isaac   PetscErrorCode ierr;
9426a68b90caSToby Isaac 
9427a68b90caSToby Isaac   PetscFunctionBegin;
9428a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9429e228b242SToby Isaac   if (anchorSection) {
9430e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
9431ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRMPI(ierr);
94322c71b3e2SJacob Faibussowitsch     PetscCheckFalse(result != MPI_CONGRUENT && result != MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
9433e228b242SToby Isaac   }
9434e228b242SToby Isaac   if (anchorIS) {
9435e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
9436ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRMPI(ierr);
94372c71b3e2SJacob Faibussowitsch     PetscCheckFalse(result != MPI_CONGRUENT && result != MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
9438e228b242SToby Isaac   }
9439a68b90caSToby Isaac 
9440a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
9441a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
9442a68b90caSToby Isaac   plex->anchorSection = anchorSection;
9443a68b90caSToby Isaac 
9444a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
9445a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
9446a68b90caSToby Isaac   plex->anchorIS = anchorIS;
9447a68b90caSToby Isaac 
9448cf9c20a2SJed Brown   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
9449a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
9450a68b90caSToby Isaac     const PetscInt *anchors;
9451a68b90caSToby Isaac 
9452a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
9453a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
9454a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
9455a68b90caSToby Isaac     for (a = 0; a < size; a++) {
9456a68b90caSToby Isaac       PetscInt p;
9457a68b90caSToby Isaac 
9458a68b90caSToby Isaac       p = anchors[a];
9459a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
9460a68b90caSToby Isaac         PetscInt dof;
9461a68b90caSToby Isaac 
9462a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
9463a68b90caSToby Isaac         if (dof) {
9464a68b90caSToby Isaac           PetscErrorCode ierr2;
9465a68b90caSToby Isaac 
9466a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
946798921bdaSJacob Faibussowitsch           SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
9468a68b90caSToby Isaac         }
9469a68b90caSToby Isaac       }
9470a68b90caSToby Isaac     }
9471a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
9472a68b90caSToby Isaac   }
9473f7c74593SToby Isaac   /* reset the generic constraints */
9474f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
9475a68b90caSToby Isaac   PetscFunctionReturn(0);
9476a68b90caSToby Isaac }
9477a68b90caSToby Isaac 
9478f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
9479a68b90caSToby Isaac {
9480f7c74593SToby Isaac   PetscSection anchorSection;
94816995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
9482a68b90caSToby Isaac   PetscErrorCode ierr;
9483a68b90caSToby Isaac 
9484a68b90caSToby Isaac   PetscFunctionBegin;
9485a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9486a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
9487e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
9488a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
94896995de1eSToby Isaac   if (numFields) {
9490719ab38cSToby Isaac     PetscInt f;
9491a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
9492719ab38cSToby Isaac 
9493719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
9494719ab38cSToby Isaac       PetscInt numComp;
9495719ab38cSToby Isaac 
9496719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
9497719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
9498719ab38cSToby Isaac     }
94996995de1eSToby Isaac   }
9500a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
95016995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
95026995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
95036995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
95046995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
9505a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
9506a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
9507a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
9508a68b90caSToby Isaac     if (dof) {
9509a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
9510a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
9511a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
9512a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
9513a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
9514a68b90caSToby Isaac       }
9515a68b90caSToby Isaac     }
9516a68b90caSToby Isaac   }
9517a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
9518ae65431dSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *cSec, "Constraint Section");CHKERRQ(ierr);
9519a68b90caSToby Isaac   PetscFunctionReturn(0);
9520a68b90caSToby Isaac }
9521a68b90caSToby Isaac 
9522f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
9523a68b90caSToby Isaac {
9524f7c74593SToby Isaac   PetscSection   aSec;
9525ae65431dSMatthew G. Knepley   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
95260ac89760SToby Isaac   const PetscInt *anchors;
95270ac89760SToby Isaac   PetscInt       numFields, f;
952866ad2231SToby Isaac   IS             aIS;
95290ac89760SToby Isaac   PetscErrorCode ierr;
9530e19f7ee6SMark Adams   MatType        mtype;
9531e19f7ee6SMark Adams   PetscBool      iscuda,iskokkos;
95320ac89760SToby Isaac 
95330ac89760SToby Isaac   PetscFunctionBegin;
95340ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95350ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
95360ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
95370ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
95380ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
9539e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);CHKERRQ(ierr);
9540e19f7ee6SMark Adams   if (!iscuda) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);CHKERRQ(ierr); }
9541e19f7ee6SMark Adams   ierr = PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);CHKERRQ(ierr);
9542e19f7ee6SMark Adams   if (!iskokkos) { ierr = PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);CHKERRQ(ierr); }
9543e19f7ee6SMark Adams   if (iscuda) mtype = MATSEQAIJCUSPARSE;
9544e19f7ee6SMark Adams   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
9545e19f7ee6SMark Adams   else mtype = MATSEQAIJ;
9546e19f7ee6SMark Adams   ierr = MatSetType(*cMat,mtype);CHKERRQ(ierr);
9547a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
954866ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
95496995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
95506995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
9551ae65431dSMatthew G. Knepley   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
95520ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
95530ac89760SToby Isaac   i[0] = 0;
95540ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
95550ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
9556f19733c5SToby Isaac     PetscInt rDof, rOff, r;
9557f19733c5SToby Isaac 
9558f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
9559f19733c5SToby Isaac     if (!rDof) continue;
9560f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
95610ac89760SToby Isaac     if (numFields) {
95620ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
95630ac89760SToby Isaac         annz = 0;
9564f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
9565f19733c5SToby Isaac           a = anchors[rOff + r];
9566ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
95670ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
95680ac89760SToby Isaac           annz += aDof;
95690ac89760SToby Isaac         }
95700ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
95710ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
95720ac89760SToby Isaac         for (q = 0; q < dof; q++) {
95730ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
95740ac89760SToby Isaac         }
95750ac89760SToby Isaac       }
95762f7452b8SBarry Smith     } else {
95770ac89760SToby Isaac       annz = 0;
9578326b8f31SMatthew G. Knepley       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
95790ac89760SToby Isaac       for (q = 0; q < dof; q++) {
9580ae65431dSMatthew G. Knepley         a = anchors[rOff + q];
9581ae65431dSMatthew G. Knepley         if (a < sStart || a >= sEnd) continue;
95820ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
95830ac89760SToby Isaac         annz += aDof;
95840ac89760SToby Isaac       }
95850ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
95860ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
95870ac89760SToby Isaac       for (q = 0; q < dof; q++) {
95880ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
95890ac89760SToby Isaac       }
95900ac89760SToby Isaac     }
95910ac89760SToby Isaac   }
95920ac89760SToby Isaac   nnz = i[m];
95930ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
95940ac89760SToby Isaac   offset = 0;
95950ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
95960ac89760SToby Isaac     if (numFields) {
95970ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
95980ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
95990ac89760SToby Isaac         for (q = 0; q < dof; q++) {
96000ac89760SToby Isaac           PetscInt rDof, rOff, r;
96010ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
96020ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
96030ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
96040ac89760SToby Isaac             PetscInt s;
96050ac89760SToby Isaac 
96060ac89760SToby Isaac             a = anchors[rOff + r];
9607ae65431dSMatthew G. Knepley             if (a < sStart || a >= sEnd) continue;
96080ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
96090ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
96100ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
96110ac89760SToby Isaac               j[offset++] = aOff + s;
96120ac89760SToby Isaac             }
96130ac89760SToby Isaac           }
96140ac89760SToby Isaac         }
96150ac89760SToby Isaac       }
96162f7452b8SBarry Smith     } else {
96170ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
96180ac89760SToby Isaac       for (q = 0; q < dof; q++) {
96190ac89760SToby Isaac         PetscInt rDof, rOff, r;
96200ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
96210ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
96220ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
96230ac89760SToby Isaac           PetscInt s;
96240ac89760SToby Isaac 
96250ac89760SToby Isaac           a = anchors[rOff + r];
9626ae65431dSMatthew G. Knepley           if (a < sStart || a >= sEnd) continue;
96270ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
96280ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
96290ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
96300ac89760SToby Isaac             j[offset++] = aOff + s;
96310ac89760SToby Isaac           }
96320ac89760SToby Isaac         }
96330ac89760SToby Isaac       }
96340ac89760SToby Isaac     }
96350ac89760SToby Isaac   }
96360ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
963725570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
963825570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
963966ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
96400ac89760SToby Isaac   PetscFunctionReturn(0);
96410ac89760SToby Isaac }
96420ac89760SToby Isaac 
964366ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
964466ad2231SToby Isaac {
9645f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
9646f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
964766ad2231SToby Isaac   Mat            cMat;
964866ad2231SToby Isaac   PetscErrorCode ierr;
964966ad2231SToby Isaac 
965066ad2231SToby Isaac   PetscFunctionBegin;
965166ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9652a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
965366ad2231SToby Isaac   if (anchorSection) {
965444a7f3ddSMatthew G. Knepley     PetscInt Nf;
9655e228b242SToby Isaac 
965692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
9657f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
9658f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
965944a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
966044a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
966166ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
966266ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
966366ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
966466ad2231SToby Isaac   }
966566ad2231SToby Isaac   PetscFunctionReturn(0);
966666ad2231SToby Isaac }
9667a93c429eSMatthew G. Knepley 
9668a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
9669a93c429eSMatthew G. Knepley {
9670a93c429eSMatthew G. Knepley   IS             subis;
9671a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
9672a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
9673a93c429eSMatthew G. Knepley 
9674a93c429eSMatthew G. Knepley   PetscFunctionBegin;
967592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
96762c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!section,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
96772c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!subdm,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
9678a93c429eSMatthew G. Knepley   /* Create subdomain */
9679a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
9680a93c429eSMatthew G. Knepley   /* Create submodel */
968197d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(*subdm, &subis);CHKERRQ(ierr);
9682a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
968392fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
9684a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
9685e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
9686a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
9687a93c429eSMatthew G. Knepley   if (is) {
9688a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
9689a93c429eSMatthew G. Knepley     IS              spIS;
9690a93c429eSMatthew G. Knepley     const PetscInt *spmap;
9691a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
9692a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
9693a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
9694a93c429eSMatthew G. Knepley 
969597d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
9696a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
9697a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
96986f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
96996f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
9700a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
9701a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9702a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
9703a93c429eSMatthew G. Knepley 
9704a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
9705a93c429eSMatthew G. Knepley       if (gdof > 0) {
9706a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9707a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
9708a93c429eSMatthew G. Knepley 
9709a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
9710a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
9711a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
9712a93c429eSMatthew G. Knepley         }
9713a93c429eSMatthew G. Knepley         subSize += pSubSize;
9714a93c429eSMatthew G. Knepley         if (pSubSize) {
9715a93c429eSMatthew G. Knepley           if (bs < 0) {
9716a93c429eSMatthew G. Knepley             bs = pSubSize;
9717a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
9718a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
9719a93c429eSMatthew G. Knepley             bs = 1;
9720a93c429eSMatthew G. Knepley           }
9721a93c429eSMatthew G. Knepley         }
9722a93c429eSMatthew G. Knepley       }
9723a93c429eSMatthew G. Knepley     }
9724a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
9725a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9726a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9727a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9728a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
9729a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
9730a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
9731a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
9732a93c429eSMatthew G. Knepley 
9733a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
9734a93c429eSMatthew G. Knepley       if (gdof > 0) {
9735a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
9736a93c429eSMatthew G. Knepley 
9737a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
9738a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
9739a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
9740a93c429eSMatthew G. Knepley 
9741a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
9742a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
9743a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
9744a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
9745a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
9746a93c429eSMatthew G. Knepley           }
9747a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
9748a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
9749a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
9750a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
9751a93c429eSMatthew G. Knepley           }
9752a93c429eSMatthew G. Knepley         }
9753a93c429eSMatthew G. Knepley       }
9754a93c429eSMatthew G. Knepley     }
9755a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
9756a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
9757a93c429eSMatthew G. Knepley     if (bs > 1) {
9758a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
9759a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
9760a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
9761a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
9762a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
9763a93c429eSMatthew G. Knepley         }
9764a93c429eSMatthew G. Knepley       }
9765a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
9766a93c429eSMatthew G. Knepley     }
9767a93c429eSMatthew G. Knepley     /* Attach nullspace */
9768a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
9769a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
9770a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
9771a93c429eSMatthew G. Knepley     }
9772a93c429eSMatthew G. Knepley     if (f < Nf) {
9773a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
97748cda7954SMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);CHKERRQ(ierr);
97756823f3c5SBlaise Bourdin 
9776a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
9777a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
9778a93c429eSMatthew G. Knepley     }
9779a93c429eSMatthew G. Knepley   }
9780a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
9781a93c429eSMatthew G. Knepley }
9782c0f0dcc3SMatthew G. Knepley 
9783c0f0dcc3SMatthew G. Knepley /*@
9784c0f0dcc3SMatthew G. Knepley   DMPlexMonitorThroughput - Report the cell throughput of FE integration
9785c0f0dcc3SMatthew G. Knepley 
9786c0f0dcc3SMatthew G. Knepley   Input Parameter:
9787c0f0dcc3SMatthew G. Knepley - dm - The DM
9788c0f0dcc3SMatthew G. Knepley 
9789c0f0dcc3SMatthew G. Knepley   Level: developer
9790c0f0dcc3SMatthew G. Knepley 
9791c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9792c0f0dcc3SMatthew G. Knepley . -dm_plex_monitor_throughput - Activate the monitor
9793c0f0dcc3SMatthew G. Knepley 
9794c0f0dcc3SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreate()
9795c0f0dcc3SMatthew G. Knepley @*/
9796c0f0dcc3SMatthew G. Knepley PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
9797c0f0dcc3SMatthew G. Knepley {
9798e5ed2c37SJose E. Roman #if defined(PETSC_USE_LOG)
9799c0f0dcc3SMatthew G. Knepley   PetscStageLog      stageLog;
9800c0f0dcc3SMatthew G. Knepley   PetscLogEvent      event;
9801c0f0dcc3SMatthew G. Knepley   PetscLogStage      stage;
9802c0f0dcc3SMatthew G. Knepley   PetscEventPerfInfo eventInfo;
9803c0f0dcc3SMatthew G. Knepley   PetscReal          cellRate, flopRate;
9804c0f0dcc3SMatthew G. Knepley   PetscInt           cStart, cEnd, Nf, N;
9805c0f0dcc3SMatthew G. Knepley   const char        *name;
9806c0f0dcc3SMatthew G. Knepley   PetscErrorCode     ierr;
9807e5ed2c37SJose E. Roman #endif
9808c0f0dcc3SMatthew G. Knepley 
9809c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9810c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9811c0f0dcc3SMatthew G. Knepley #if defined(PETSC_USE_LOG)
9812c0f0dcc3SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
9813c0f0dcc3SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9814c0f0dcc3SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
9815c0f0dcc3SMatthew G. Knepley   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
9816c0f0dcc3SMatthew G. Knepley   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
9817c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetId("DMPlexResidualFE", &event);CHKERRQ(ierr);
9818c0f0dcc3SMatthew G. Knepley   ierr = PetscLogEventGetPerfInfo(stage, event, &eventInfo);CHKERRQ(ierr);
9819c0f0dcc3SMatthew G. Knepley   N        = (cEnd - cStart)*Nf*eventInfo.count;
9820c0f0dcc3SMatthew G. Knepley   flopRate = eventInfo.flops/eventInfo.time;
9821c0f0dcc3SMatthew G. Knepley   cellRate = N/eventInfo.time;
9822fae64647SBarry 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);
9823c0f0dcc3SMatthew G. Knepley #else
9824c0f0dcc3SMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
9825c0f0dcc3SMatthew G. Knepley #endif
9826c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9827c0f0dcc3SMatthew G. Knepley }
9828