xref: /petsc/src/dm/impls/plex/plex.c (revision e8ecbf3f510e255170ff9ba7f7358c857bd32179)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
3e5c6e791SKarl Rupp #include <petsc/private/vecimpl.h>
48135c375SStefano Zampini #include <petsc/private/glvisvecimpl.h>
50c312b8eSJed Brown #include <petscsf.h>
6e228b242SToby Isaac #include <petscds.h>
7e412dcbdSMatthew G. Knepley #include <petscdraw.h>
8f19dbd58SToby Isaac #include <petscdmfield.h>
9552f7358SJed Brown 
10552f7358SJed Brown /* Logging support */
1167eb269bSLisandro Dalcin 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_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
12552f7358SJed Brown 
135a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
14552f7358SJed Brown 
15e5337592SStefano Zampini /*@
16e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
17e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
18e5337592SStefano Zampini 
19e5337592SStefano Zampini   Collective
20e5337592SStefano Zampini 
21e5337592SStefano Zampini   Input Parameters:
22e5337592SStefano Zampini . dm - The DMPlex object
23e5337592SStefano Zampini 
24e5337592SStefano Zampini   Output Parameters:
25e5337592SStefano Zampini . dmRefined - The refined DMPlex object
26e5337592SStefano Zampini 
27e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
28e5337592SStefano Zampini 
29e5337592SStefano Zampini   Level: intermediate
30e5337592SStefano Zampini 
31e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
32e5337592SStefano Zampini @*/
33e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
34e5337592SStefano Zampini {
35e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
36e5337592SStefano Zampini   CellRefiner      cellRefiner;
37e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
38e5337592SStefano Zampini   PetscErrorCode   ierr;
39e5337592SStefano Zampini 
40e5337592SStefano Zampini   PetscFunctionBegin;
41e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424330a3fcSStefano Zampini   PetscValidPointer(dmRefined, 1);
43e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
45e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
46e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
47e5337592SStefano Zampini   else {
48e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
49e5337592SStefano Zampini     switch (dim) {
50e5337592SStefano Zampini     case 1:
51e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
52e5337592SStefano Zampini     break;
53e5337592SStefano Zampini     case 2:
54e5337592SStefano Zampini       switch (coneSize) {
55e5337592SStefano Zampini       case 3:
564330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
574330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
58e5337592SStefano Zampini       break;
59e5337592SStefano Zampini       case 4:
604330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
614330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
62e5337592SStefano Zampini       break;
63e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
64e5337592SStefano Zampini       }
65e5337592SStefano Zampini     break;
66e5337592SStefano Zampini     case 3:
67e5337592SStefano Zampini       switch (coneSize) {
68e5337592SStefano Zampini       case 4:
694330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
704330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
714330a3fcSStefano Zampini       break;
724330a3fcSStefano Zampini       case 5:
734330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
744330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
75e5337592SStefano Zampini       break;
76e5337592SStefano Zampini       case 6:
774330a3fcSStefano Zampini         if (cMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Simplex2Tensor in 3D with Hybrid mesh not yet done");
78e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
79e5337592SStefano Zampini       break;
80e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
81e5337592SStefano Zampini       }
82e5337592SStefano Zampini     break;
83e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
84e5337592SStefano Zampini     }
85e5337592SStefano Zampini   }
86e5337592SStefano Zampini   /* return if we don't need to refine */
87e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
88e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
89e5337592SStefano Zampini   if (allnoop) {
90e5337592SStefano Zampini     *dmRefined = NULL;
91e5337592SStefano Zampini     PetscFunctionReturn(0);
92e5337592SStefano Zampini   }
93e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
94e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
95e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
96e5337592SStefano Zampini   if (localized) {
97e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
98e5337592SStefano Zampini   }
99e5337592SStefano Zampini   PetscFunctionReturn(0);
100e5337592SStefano Zampini }
101e5337592SStefano Zampini 
1027afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
1037e42fee7SMatthew G. Knepley {
104a99a26bcSAdrian Croucher   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
105a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
1067e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
1077e42fee7SMatthew G. Knepley 
1087e42fee7SMatthew G. Knepley   PetscFunctionBegin;
1097e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
110c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1117e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1127e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11380d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11480d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1157e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1167e42fee7SMatthew G. Knepley   if (field >= 0) {
117a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
118a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1197e42fee7SMatthew G. Knepley   } else {
120a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
121a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1227e42fee7SMatthew G. Knepley   }
1235483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
124a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1257e42fee7SMatthew G. Knepley     *sStart = vStart;
1267e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
127a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1287e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
129a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1307e42fee7SMatthew G. Knepley     *sStart = cStart;
1317e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
132a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1337e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
1347e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1357e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1367e42fee7SMatthew G. Knepley }
1377e42fee7SMatthew G. Knepley 
1387cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
139e412dcbdSMatthew G. Knepley {
140e412dcbdSMatthew G. Knepley   DM                 dm;
141d1df6f1dSMatthew G. Knepley   PetscSection       s;
142e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
143e412dcbdSMatthew G. Knepley   DM                 cdm;
144e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
145e412dcbdSMatthew G. Knepley   Vec                coordinates;
146e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
147e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
148339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
149339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
150d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
151e412dcbdSMatthew G. Knepley   const char        *name;
152339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
153e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
154e412dcbdSMatthew G. Knepley 
155e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
156d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
157d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
158d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
159d1df6f1dSMatthew G. Knepley 
160e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1628135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
163e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
164d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
167e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley 
172e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
173339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley 
175e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
176e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
177e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1780c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1790c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
180e412dcbdSMatthew G. Knepley   }
181e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
182e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
183e412dcbdSMatthew G. Knepley 
184d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
185d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
186d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
187d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
188d1df6f1dSMatthew G. Knepley     IS   fis;
189d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
190d1df6f1dSMatthew G. Knepley     const char *fname;
191d1df6f1dSMatthew G. Knepley 
192d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
193d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
194d1df6f1dSMatthew G. Knepley 
195a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
196d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
197d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
198d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
199d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
200a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
201a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
202d1df6f1dSMatthew G. Knepley     }
203d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
204d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
205d1df6f1dSMatthew G. Knepley 
206d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
207d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
208d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
209d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
210d1df6f1dSMatthew G. Knepley 
211d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
212d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
213339e3443SMatthew G. Knepley       if (!flg) {
214d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
215d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
216d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
217339e3443SMatthew G. Knepley       }
218e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
219339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
220d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
221e412dcbdSMatthew G. Knepley 
222d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
223e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22499a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
225e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
226e412dcbdSMatthew G. Knepley 
227d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
228339e3443SMatthew G. Knepley         if (a) {
229d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
230339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
231339e3443SMatthew G. Knepley         } else {
232339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
233339e3443SMatthew G. Knepley           PetscInt     numVals, va;
234339e3443SMatthew G. Knepley 
235d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
236d1df6f1dSMatthew G. Knepley           if (numVals % Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %D does not divide the number of values in the closure %D", Nc, numVals);
237d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
238d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
239d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
240d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
241339e3443SMatthew G. Knepley             break;
242d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
243d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
244d1df6f1dSMatthew 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]);
245d1df6f1dSMatthew G. Knepley             break;
246d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
247339e3443SMatthew G. Knepley           }
248d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
249339e3443SMatthew G. Knepley         }
250e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
251e412dcbdSMatthew G. Knepley         switch (numCoords) {
252e412dcbdSMatthew G. Knepley         case 6:
253339e3443SMatthew 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);
254e412dcbdSMatthew G. Knepley           break;
255e412dcbdSMatthew G. Knepley         case 8:
256339e3443SMatthew 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);
257339e3443SMatthew 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);
258e412dcbdSMatthew G. Knepley           break;
259e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
260e412dcbdSMatthew G. Knepley         }
261e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
262e412dcbdSMatthew G. Knepley       }
263d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
264e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
265e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
266e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
267d1df6f1dSMatthew G. Knepley     }
268d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
269d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
270d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
271d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
272d1df6f1dSMatthew G. Knepley     }
273d1df6f1dSMatthew G. Knepley   }
274e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
275e412dcbdSMatthew G. Knepley }
276e412dcbdSMatthew G. Knepley 
277684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
278684b87d9SLisandro Dalcin {
279684b87d9SLisandro Dalcin   DM                      dm;
280684b87d9SLisandro Dalcin   Vec                     locv;
281684b87d9SLisandro Dalcin   const char              *name;
282684b87d9SLisandro Dalcin   PetscSection            section;
283684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
284684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
285684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
286684b87d9SLisandro Dalcin 
287684b87d9SLisandro Dalcin   PetscFunctionBegin;
288684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
289684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
290684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
291684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
292684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
293e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
294684b87d9SLisandro Dalcin   ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
295a8f87f1dSPatrick Sanan   ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
296684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
297684b87d9SLisandro Dalcin }
298684b87d9SLisandro Dalcin 
299552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
300552f7358SJed Brown {
301552f7358SJed Brown   DM             dm;
302684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
303552f7358SJed Brown   PetscErrorCode ierr;
304552f7358SJed Brown 
305552f7358SJed Brown   PetscFunctionBegin;
306552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
30782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
308552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
309b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
310f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3118135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
312684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
313684b87d9SLisandro Dalcin     PetscInt    i,numFields;
314684b87d9SLisandro Dalcin     PetscObject fe;
315ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
316684b87d9SLisandro Dalcin     Vec         locv = v;
317684b87d9SLisandro Dalcin     const char  *name;
318684b87d9SLisandro Dalcin     PetscInt    step;
319684b87d9SLisandro Dalcin     PetscReal   time;
320ef31f671SMatthew G. Knepley 
321ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
323684b87d9SLisandro Dalcin       ierr = DMGetField(dm, i, &fe);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
325ef31f671SMatthew G. Knepley     }
326684b87d9SLisandro Dalcin     if (fem) {
327684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
328684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
329684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
330684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
331684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
332684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
333ef31f671SMatthew G. Knepley     }
334552f7358SJed Brown     if (isvtk) {
335684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
336b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
337b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
338684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
339b136c2c9SMatthew G. Knepley #else
340b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
341b136c2c9SMatthew G. Knepley #endif
342f13a32a3SMatthew G. Knepley     } else if (isdraw) {
343684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
344684b87d9SLisandro Dalcin     } else if (isglvis) {
345684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
346684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
347684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
348684b87d9SLisandro Dalcin     }
349684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
350552f7358SJed Brown   } else {
351684b87d9SLisandro Dalcin     PetscBool isseq;
352684b87d9SLisandro Dalcin 
353684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
35455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
35555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
356552f7358SJed Brown   }
357552f7358SJed Brown   PetscFunctionReturn(0);
358552f7358SJed Brown }
359552f7358SJed Brown 
360552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
361552f7358SJed Brown {
362552f7358SJed Brown   DM             dm;
363684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
364552f7358SJed Brown   PetscErrorCode ierr;
365552f7358SJed Brown 
366552f7358SJed Brown   PetscFunctionBegin;
367552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
36882f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
369552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
37033c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
371e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3728135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
373684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
374552f7358SJed Brown     Vec         locv;
375552f7358SJed Brown     const char *name;
376552f7358SJed Brown 
377c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
378552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
379552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
380552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
381552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
382552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
383c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
384b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
385b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
387b136c2c9SMatthew G. Knepley #else
388b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
389b136c2c9SMatthew G. Knepley #endif
390552f7358SJed Brown   } else {
391684b87d9SLisandro Dalcin     PetscBool isseq;
392684b87d9SLisandro Dalcin 
393684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
39455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
39555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
396552f7358SJed Brown   }
397552f7358SJed Brown   PetscFunctionReturn(0);
398552f7358SJed Brown }
399552f7358SJed Brown 
400d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
401d930f514SMatthew G. Knepley {
402d930f514SMatthew G. Knepley   DM                dm;
403d930f514SMatthew G. Knepley   MPI_Comm          comm;
404d930f514SMatthew G. Knepley   PetscViewerFormat format;
405d930f514SMatthew G. Knepley   Vec               v;
406d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
407d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
408d930f514SMatthew G. Knepley 
409d930f514SMatthew G. Knepley   PetscFunctionBegin;
410d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
411d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4122c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
413d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
417d930f514SMatthew G. Knepley     const char *vecname;
418d930f514SMatthew G. Knepley     PetscInt    n, nroots;
419d930f514SMatthew G. Knepley 
420d930f514SMatthew G. Knepley     if (dm->sfNatural) {
421ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
422d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
423d930f514SMatthew G. Knepley       if (n == nroots) {
424d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
430d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
431d930f514SMatthew G. Knepley   } else {
432d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
433d930f514SMatthew G. Knepley     v = originalv;
434d930f514SMatthew G. Knepley   }
435d930f514SMatthew G. Knepley   if (ishdf5) {
436d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley #else
439d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
440d930f514SMatthew G. Knepley #endif
441d930f514SMatthew G. Knepley   } else if (isvtk) {
442d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
443d930f514SMatthew G. Knepley   } else {
444d930f514SMatthew G. Knepley     PetscBool isseq;
445d930f514SMatthew G. Knepley 
446d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
447d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
448d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
449d930f514SMatthew G. Knepley   }
450d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
451d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
452d930f514SMatthew G. Knepley }
453d930f514SMatthew G. Knepley 
4542c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4552c40f234SMatthew G. Knepley {
4562c40f234SMatthew G. Knepley   DM             dm;
4572c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4582c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4592c40f234SMatthew G. Knepley 
4602c40f234SMatthew G. Knepley   PetscFunctionBegin;
4612c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4632c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (ishdf5) {
4652c40f234SMatthew G. Knepley     DM          dmBC;
4662c40f234SMatthew G. Knepley     Vec         gv;
4672c40f234SMatthew G. Knepley     const char *name;
4682c40f234SMatthew G. Knepley 
4692c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4702c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley   } else {
4782c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley   }
4802c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4812c40f234SMatthew G. Knepley }
4822c40f234SMatthew G. Knepley 
4832c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4842c40f234SMatthew G. Knepley {
4852c40f234SMatthew G. Knepley   DM             dm;
4862c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4872c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4882c40f234SMatthew G. Knepley 
4892c40f234SMatthew G. Knepley   PetscFunctionBegin;
4902c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4912c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4922c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   if (ishdf5) {
494878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
496b136c2c9SMatthew G. Knepley #else
497b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
498878b459fSMatthew G. Knepley #endif
4992c40f234SMatthew G. Knepley   } else {
5002c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
501552f7358SJed Brown   }
502552f7358SJed Brown   PetscFunctionReturn(0);
503552f7358SJed Brown }
504552f7358SJed Brown 
505d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
506d930f514SMatthew G. Knepley {
507d930f514SMatthew G. Knepley   DM                dm;
508d930f514SMatthew G. Knepley   PetscViewerFormat format;
509d930f514SMatthew G. Knepley   PetscBool         ishdf5;
510d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
511d930f514SMatthew G. Knepley 
512d930f514SMatthew G. Knepley   PetscFunctionBegin;
513d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
514d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
515d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
518d930f514SMatthew G. Knepley     if (dm->sfNatural) {
519d930f514SMatthew G. Knepley       if (ishdf5) {
520d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
521d930f514SMatthew G. Knepley         Vec         v;
522d930f514SMatthew G. Knepley         const char *vecname;
523d930f514SMatthew G. Knepley 
524d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
525d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
526d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
52739d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
530d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley #else
532d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
533d930f514SMatthew G. Knepley #endif
534d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
535d930f514SMatthew G. Knepley     }
536d930f514SMatthew G. Knepley   }
537d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
538d930f514SMatthew G. Knepley }
539d930f514SMatthew G. Knepley 
5407cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
541731e8ddeSMatthew G. Knepley {
542731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
543731e8ddeSMatthew G. Knepley   Vec                coordinates;
544731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
545731e8ddeSMatthew G. Knepley   const char        *name[4];
546731e8ddeSMatthew G. Knepley   const PetscScalar *a;
547731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
548731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
549731e8ddeSMatthew G. Knepley 
550731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
551731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
554731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
555731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
556731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
557731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
559731e8ddeSMatthew G. Knepley   name[1]     = "edge";
560731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
561731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
562731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
563731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
564731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
565731e8ddeSMatthew G. Knepley 
566f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
567731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
568731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
569731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
570731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
571731e8ddeSMatthew G. Knepley 
572731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
573731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley       if (!dof) continue;
575731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
577f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
578731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
579731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
581731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
58241477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley         }
584731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley       }
586731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
587731e8ddeSMatthew G. Knepley     }
588731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
590731e8ddeSMatthew G. Knepley   }
591731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
592731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
593731e8ddeSMatthew G. Knepley }
594731e8ddeSMatthew G. Knepley 
5957cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
596552f7358SJed Brown {
597552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
598552f7358SJed Brown   DM                cdm;
599552f7358SJed Brown   DMLabel           markers;
600552f7358SJed Brown   PetscSection      coordSection;
601552f7358SJed Brown   Vec               coordinates;
602552f7358SJed Brown   PetscViewerFormat format;
603552f7358SJed Brown   PetscErrorCode    ierr;
604552f7358SJed Brown 
605552f7358SJed Brown   PetscFunctionBegin;
606552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
607e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
608552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
609552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
610552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
611552f7358SJed Brown     const char *name;
612f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
613552f7358SJed Brown     PetscInt    pStart, pEnd, p;
614552f7358SJed Brown     PetscMPIInt rank, size;
615552f7358SJed Brown 
61682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
61782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
618552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
619552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
620552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
621f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
622f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
623f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
624f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
625f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
626e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6274d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
628e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
629552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
630552f7358SJed Brown       PetscInt dof, off, s;
631552f7358SJed Brown 
632552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
633552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
634552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
635e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
636552f7358SJed Brown       }
637552f7358SJed Brown     }
638552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
639e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
640e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
641552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
642552f7358SJed Brown       PetscInt dof, off, c;
643552f7358SJed Brown 
644552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
645552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
646552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
647e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
648552f7358SJed Brown       }
649552f7358SJed Brown     }
650552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6514d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
65280180ce3SStefano Zampini     ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
653c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
654552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
655552f7358SJed Brown     if (size > 1) {
656552f7358SJed Brown       PetscSF sf;
657552f7358SJed Brown 
658552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
659552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
660552f7358SJed Brown     }
661552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
662552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6630588280cSMatthew G. Knepley     const char  *name, *color;
6640588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6650588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
666552f7358SJed Brown     PetscReal    scale         = 2.0;
6670588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6680588280cSMatthew G. Knepley     double       tcoords[3];
669552f7358SJed Brown     PetscScalar *coords;
6700588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
671552f7358SJed Brown     PetscMPIInt  rank, size;
6720588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
673202fd40aSStefano Zampini     PetscBool    plotEdges, flg;
674552f7358SJed Brown 
6750588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
676552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
677c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6780588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6790588280cSMatthew G. Knepley     numColors  = 10;
6800588280cSMatthew G. Knepley     numLColors = 10;
6810588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
682c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
683c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
684c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6850588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
686c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6870588280cSMatthew G. Knepley     if (!useColors) {
6880588280cSMatthew G. Knepley       numColors = 3;
6890588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6900588280cSMatthew G. Knepley     }
691c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6920588280cSMatthew G. Knepley     if (!useColors) {
6930588280cSMatthew G. Knepley       numLColors = 4;
6940588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6950588280cSMatthew G. Knepley     }
696202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
697202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
698202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
699202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
70082f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
70182f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
702552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
703770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7040588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
705552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
706552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
707552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7080588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7090588280cSMatthew G. Knepley     if (size > 1) {
710f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
711770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
712770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
713770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
714770b213bSMatthew G Knepley         } else if (p > 0) {
715770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
716770b213bSMatthew G Knepley         }
717770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
718770b213bSMatthew G Knepley       }
7190588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7200588280cSMatthew G. Knepley     }
7210588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
722552f7358SJed Brown     /* Plot vertices */
723552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
724552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7254d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
726552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
727552f7358SJed Brown       PetscInt  off, dof, d;
7280588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
729552f7358SJed Brown 
730552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
731552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7320588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
733f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7340588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7350588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
736c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7370588280cSMatthew G. Knepley       }
7380588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7390588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
740552f7358SJed Brown       for (d = 0; d < dof; ++d) {
741552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7420588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
743552f7358SJed Brown       }
7440588280cSMatthew G. Knepley       color = colors[rank%numColors];
7450588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7460588280cSMatthew G. Knepley         PetscInt val;
747c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7480588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7490588280cSMatthew G. Knepley       }
7500588280cSMatthew G. Knepley       if (useNumbers) {
751e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7520588280cSMatthew G. Knepley       } else {
753e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7540588280cSMatthew G. Knepley       }
755552f7358SJed Brown     }
756552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
757552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
758846a3e8bSMatthew G. Knepley     /* Plot cells */
759846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
760846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
761846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
762846a3e8bSMatthew G. Knepley         const PetscInt *cone;
763846a3e8bSMatthew G. Knepley 
764846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
765846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
766846a3e8bSMatthew G. Knepley           PetscInt val;
767846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
768846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
769846a3e8bSMatthew G. Knepley         }
770846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
771846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
772846a3e8bSMatthew G. Knepley       }
773846a3e8bSMatthew G. Knepley     } else {
774846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
775846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
776846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
777846a3e8bSMatthew G. Knepley 
778846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
779846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
780846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
781846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
782846a3e8bSMatthew G. Knepley 
783846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
784846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
785846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
786846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
787846a3e8bSMatthew G. Knepley         }
788846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
789846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
790846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
791846a3e8bSMatthew G. Knepley       }
792846a3e8bSMatthew G. Knepley     }
793846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
794846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
795846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
796846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
797846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
798846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
799846a3e8bSMatthew G. Knepley 
800846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
801846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
802846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
803846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
804846a3e8bSMatthew G. Knepley         PetscInt       off;
805846a3e8bSMatthew G. Knepley 
806846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
807846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
808846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
809846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
810846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
811846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
812846a3e8bSMatthew G. Knepley         }
813846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
814846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
815846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
816846a3e8bSMatthew G. Knepley         ++n;
817846a3e8bSMatthew G. Knepley       }
818846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
819846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
820846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
821846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
822846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]);CHKERRQ(ierr);
823846a3e8bSMatthew G. Knepley       }
824846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
825846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
826846a3e8bSMatthew G. Knepley         PetscInt val;
827846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
828846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
829846a3e8bSMatthew G. Knepley       }
830846a3e8bSMatthew G. Knepley       if (useNumbers) {
831846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
832846a3e8bSMatthew G. Knepley       } else {
833846a3e8bSMatthew 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);
834846a3e8bSMatthew G. Knepley       }
835846a3e8bSMatthew G. Knepley     }
836846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
837552f7358SJed Brown     /* Plot edges */
838202fd40aSStefano Zampini     if (depth > 1 || plotEdges) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
839202fd40aSStefano Zampini     if (plotEdges) {
840552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
841552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
842552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
843552f7358SJed Brown         const PetscInt *cone;
844552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
845552f7358SJed Brown 
846552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
847f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
848552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
849552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
850552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
851552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
852552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
853552f7358SJed Brown         for (d = 0; d < dof; ++d) {
854202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
855c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
856552f7358SJed Brown         }
8570588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
8580588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
8590588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
8600588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
861f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
8620588280cSMatthew G. Knepley         }
8630588280cSMatthew G. Knepley         color = colors[rank%numColors];
8640588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
8650588280cSMatthew G. Knepley           PetscInt val;
866c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8670750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
8680588280cSMatthew G. Knepley         }
869e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
870552f7358SJed Brown       }
871552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
872552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
873552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
8740588280cSMatthew G. Knepley     }
875552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8764d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8770588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
878770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8790588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8800588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8810588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8820588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
883552f7358SJed Brown   } else {
88482f516ccSBarry Smith     MPI_Comm    comm;
885834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
886f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
887552f7358SJed Brown     PetscInt    pStart, pEnd, p;
888a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8891143a3c0SMatthew G. Knepley     const char *name;
890552f7358SJed Brown     PetscMPIInt size;
891552f7358SJed Brown 
89282f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
893552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
894c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
895f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8965f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
897f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
898f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
899f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
900552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
901b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
902a04427b4SStefano Zampini     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);CHKERRQ(ierr);
903b0138d5dSStefano Zampini     ierr = PetscCalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
904552f7358SJed Brown     if (depth == 1) {
905552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
906552f7358SJed Brown       pEnd = pEnd - pStart;
907a04427b4SStefano Zampini       pMax[0] -= pStart;
908552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
909a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
910f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
911a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
912a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
913a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
914a04427b4SStefano Zampini       }
915552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
916552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
917552f7358SJed Brown       pEnd = pEnd - pStart;
918a04427b4SStefano Zampini       pMax[depth] -= pStart;
919552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
920a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
921552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
922a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
923a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
924a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
925a04427b4SStefano Zampini       }
926552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
927552f7358SJed Brown     } else {
928cbb7f117SMark Adams       PetscMPIInt rank;
929cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
930552f7358SJed Brown       for (d = 0; d <= dim; d++) {
931552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
932834065abSMatthew G. Knepley         pEnd    -= pStart;
933834065abSMatthew G. Knepley         pMax[d] -= pStart;
934552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
935834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
936552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
937834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
938cbb7f117SMark Adams           if (!rank) {
939834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
940834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
941834065abSMatthew G. Knepley           }
942cbb7f117SMark Adams         }
943552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
944552f7358SJed Brown       }
945552f7358SJed Brown     }
946834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
947c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
948a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
949a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
950a57dd577SMatthew G Knepley       DMLabel         label;
951a57dd577SMatthew G Knepley       const char     *name;
952a57dd577SMatthew G Knepley       IS              valueIS;
953a57dd577SMatthew G Knepley       const PetscInt *values;
954a57dd577SMatthew G Knepley       PetscInt        numValues, v;
955a57dd577SMatthew G Knepley 
956c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
957c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
958a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
959d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
960a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
961a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
962120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
963a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
964a57dd577SMatthew G Knepley         PetscInt size;
965a57dd577SMatthew G Knepley 
966a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
967a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
968d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
969a57dd577SMatthew G Knepley       }
970a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
971120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
972a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
9734d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
974a57dd577SMatthew G Knepley     }
975a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
9768e7ff633SMatthew G. Knepley     if (cdm) {
9778e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9788e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
9798e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
9808e7ff633SMatthew G. Knepley     }
981552f7358SJed Brown   }
982552f7358SJed Brown   PetscFunctionReturn(0);
983552f7358SJed Brown }
984552f7358SJed Brown 
9857cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
986e412dcbdSMatthew G. Knepley {
987e412dcbdSMatthew G. Knepley   PetscDraw          draw;
988e412dcbdSMatthew G. Knepley   DM                 cdm;
989e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
990e412dcbdSMatthew G. Knepley   Vec                coordinates;
991e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
99229494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
993e412dcbdSMatthew G. Knepley   PetscBool          isnull;
994e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
995cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
996e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
997e412dcbdSMatthew G. Knepley 
998e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
999e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1000e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1001e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1002e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
1003e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1004e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1005e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1006e412dcbdSMatthew G. Knepley 
1007e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1008e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1009e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1010e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1011e412dcbdSMatthew G. Knepley 
1012e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1013e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1014e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
10150c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
10160c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1017e412dcbdSMatthew G. Knepley   }
1018e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
101929494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
102029494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
102129494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1022e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1023e412dcbdSMatthew G. Knepley 
1024cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1025cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1026cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1027cf3064d3SMatthew G. Knepley     PetscInt     numCoords,coneSize;
1028cf3064d3SMatthew G. Knepley 
1029cf3064d3SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1030cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1031cf3064d3SMatthew G. Knepley     switch (coneSize) {
1032cf3064d3SMatthew G. Knepley     case 3:
1033cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1034cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1035cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1036cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1037cf3064d3SMatthew G. Knepley       break;
1038cf3064d3SMatthew G. Knepley     case 4:
1039cf3064d3SMatthew G. Knepley       ierr = PetscDrawRectangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1040cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1041cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1042cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1043cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1044cf3064d3SMatthew G. Knepley       break;
1045cf3064d3SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1046cf3064d3SMatthew G. Knepley     }
1047cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1048cf3064d3SMatthew G. Knepley   }
1049e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1050e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
105129494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
1052e412dcbdSMatthew G. Knepley 
105329494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1054e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
105529494db1SLisandro Dalcin     switch (coneSize) {
105629494db1SLisandro Dalcin     case 3:
10570c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10580c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10590c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1060e412dcbdSMatthew G. Knepley       break;
106129494db1SLisandro Dalcin     case 4:
10620c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10630c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10640c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10650c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1066e412dcbdSMatthew G. Knepley       break;
106729494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1068e412dcbdSMatthew G. Knepley     }
1069e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1070e412dcbdSMatthew G. Knepley   }
1071e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1072e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1073e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1074e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1075e412dcbdSMatthew G. Knepley }
1076e412dcbdSMatthew G. Knepley 
1077552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1078552f7358SJed Brown {
1079e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1080002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1081552f7358SJed Brown   PetscErrorCode ierr;
1082552f7358SJed Brown 
1083552f7358SJed Brown   PetscFunctionBegin;
1084552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1085552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1086552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1087fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1088c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1089e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
10908135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1091552f7358SJed Brown   if (iascii) {
10928135c375SStefano Zampini     PetscViewerFormat format;
10938135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10948135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
10958135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
10968135c375SStefano Zampini     } else {
1097552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
10988135c375SStefano Zampini     }
1099c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1100c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
110139d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1102c6ccd67eSMatthew G. Knepley #else
1103c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1104552f7358SJed Brown #endif
1105e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1106fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1107e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1108e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
11098135c375SStefano Zampini   } else if (isglvis) {
11108135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
111162201deeSVaclav Hapla   } else {
1112a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1113fcf6c8fdSToby Isaac   }
1114cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1115cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1116cb3ba0daSMatthew G. Knepley   if (flg) {
1117cb3ba0daSMatthew G. Knepley     Vec ranks;
1118cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1119cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1120cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1121cb3ba0daSMatthew G. Knepley   }
1122002a2709SMatthew G. Knepley   /* Optionally view a label */
1123002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1124002a2709SMatthew G. Knepley   if (flg) {
1125002a2709SMatthew G. Knepley     DMLabel label;
1126002a2709SMatthew G. Knepley     Vec     val;
1127002a2709SMatthew G. Knepley 
1128002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1129002a2709SMatthew G. Knepley     if (!label) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
1130002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1131002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1132002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1133002a2709SMatthew G. Knepley   }
1134552f7358SJed Brown   PetscFunctionReturn(0);
1135552f7358SJed Brown }
1136552f7358SJed Brown 
11372c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
11382c40f234SMatthew G. Knepley {
1139d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
11402c40f234SMatthew G. Knepley   PetscErrorCode ierr;
11412c40f234SMatthew G. Knepley 
11422c40f234SMatthew G. Knepley   PetscFunctionBegin;
11432c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11442c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
11452c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1146d4f5a9a0SVaclav Hapla   if (ishdf5) {
11472c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
11489c48423bSVaclav Hapla     PetscViewerFormat format;
11499c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
11509c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
11519c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
11529c48423bSVaclav Hapla     } else {
115339d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
11549c48423bSVaclav Hapla     }
11552c40f234SMatthew G. Knepley #else
11562c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1157552f7358SJed Brown #endif
1158d4f5a9a0SVaclav Hapla   } else {
1159d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1160552f7358SJed Brown   }
1161552f7358SJed Brown   PetscFunctionReturn(0);
1162552f7358SJed Brown }
1163552f7358SJed Brown 
1164552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1165552f7358SJed Brown {
1166552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1167552f7358SJed Brown   PetscErrorCode ierr;
1168552f7358SJed Brown 
1169552f7358SJed Brown   PetscFunctionBegin;
11708135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
11718135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
11720d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1173552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1174552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1175552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1176552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1177be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1178552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1179552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1180d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1181d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
118277623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1183a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1184552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1185552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1186a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1187a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1188d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1189d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1190d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1191d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1192d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1193d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1194fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1195552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1196552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1197552f7358SJed Brown   PetscFunctionReturn(0);
1198552f7358SJed Brown }
1199552f7358SJed Brown 
1200b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1201552f7358SJed Brown {
12028d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1203acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1204552f7358SJed Brown   PetscInt               localSize;
1205837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1206552f7358SJed Brown   PetscErrorCode         ierr;
1207b412c318SBarry Smith   MatType                mtype;
12081428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1209552f7358SJed Brown 
1210552f7358SJed Brown   PetscFunctionBegin;
1211607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1212b412c318SBarry Smith   mtype = dm->mattype;
1213e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1214552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1215552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
121682f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1217552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1218552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1219552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1220acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1221acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1222552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1223552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1224552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1225552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1226552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1227552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1228552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1229837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1230552f7358SJed Brown   if (!isShell) {
1231be36d101SStefano Zampini     PetscSection subSection;
1232837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
12330be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1234fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1235552f7358SJed Brown 
123600140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1237be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1238be36d101SStefano Zampini       PetscSection section;
1239be36d101SStefano Zampini       PetscInt     size;
1240be36d101SStefano Zampini 
1241e87a4003SBarry Smith       ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1242be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1243be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1244be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1245be36d101SStefano Zampini     } else {
124600140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1247be36d101SStefano Zampini     }
1248552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1249be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1250a9d99c84SMatthew G. Knepley       PetscInt bdof;
1251a9d99c84SMatthew G. Knepley 
1252552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1253fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
12541d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
12551d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
12561d17a0a3SMatthew G. Knepley       if (dof) {
12571d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1258be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1259be36d101SStefano Zampini       }
1260be36d101SStefano Zampini       if (isMatIS) {
1261be36d101SStefano Zampini         PetscInt loff,c,off;
1262be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1263be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1264be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1265552f7358SJed Brown       }
12662a28c762SMatthew G Knepley     }
12672a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
12680be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
12690be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
12700be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
12710be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
12724fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1273be36d101SStefano Zampini     if (isMatIS) {
12744fa26246SMatthew G. Knepley       PetscInt l;
12754fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
12764fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
12774fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1278be36d101SStefano Zampini     }
1279be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1280be36d101SStefano Zampini     if (isMatIS) {
1281be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1282be36d101SStefano Zampini     }
12831795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
12848d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1285552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1286552f7358SJed Brown   }
1287b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1288552f7358SJed Brown   PetscFunctionReturn(0);
1289552f7358SJed Brown }
1290552f7358SJed Brown 
12917cd05799SMatthew G. Knepley /*@
1292a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1293be36d101SStefano Zampini 
1294be36d101SStefano Zampini   Not collective
1295be36d101SStefano Zampini 
1296be36d101SStefano Zampini   Input Parameter:
1297be36d101SStefano Zampini . mesh - The DMPlex
1298be36d101SStefano Zampini 
1299be36d101SStefano Zampini   Output Parameters:
1300be36d101SStefano Zampini . subsection - The subdomain section
1301be36d101SStefano Zampini 
1302be36d101SStefano Zampini   Level: developer
1303be36d101SStefano Zampini 
1304be36d101SStefano Zampini .seealso:
13057cd05799SMatthew G. Knepley @*/
1306be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1307be36d101SStefano Zampini {
1308be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1309be36d101SStefano Zampini   PetscErrorCode ierr;
1310be36d101SStefano Zampini 
1311be36d101SStefano Zampini   PetscFunctionBegin;
1312be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1313be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1314be36d101SStefano Zampini     PetscSection section;
1315be36d101SStefano Zampini     PetscSF      sf;
1316be36d101SStefano Zampini 
1317be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1318e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
1319be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1320be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1321be36d101SStefano Zampini   }
1322be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1323be36d101SStefano Zampini   PetscFunctionReturn(0);
1324be36d101SStefano Zampini }
1325be36d101SStefano Zampini 
1326552f7358SJed Brown /*@
1327552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1328552f7358SJed Brown 
1329552f7358SJed Brown   Not collective
1330552f7358SJed Brown 
1331552f7358SJed Brown   Input Parameter:
1332552f7358SJed Brown . mesh - The DMPlex
1333552f7358SJed Brown 
1334552f7358SJed Brown   Output Parameters:
1335552f7358SJed Brown + pStart - The first mesh point
1336552f7358SJed Brown - pEnd   - The upper bound for mesh points
1337552f7358SJed Brown 
1338552f7358SJed Brown   Level: beginner
1339552f7358SJed Brown 
1340552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1341552f7358SJed Brown @*/
1342552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1343552f7358SJed Brown {
1344552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1345552f7358SJed Brown   PetscErrorCode ierr;
1346552f7358SJed Brown 
1347552f7358SJed Brown   PetscFunctionBegin;
1348552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1349552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1350552f7358SJed Brown   PetscFunctionReturn(0);
1351552f7358SJed Brown }
1352552f7358SJed Brown 
1353552f7358SJed Brown /*@
1354552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1355552f7358SJed Brown 
1356552f7358SJed Brown   Not collective
1357552f7358SJed Brown 
1358552f7358SJed Brown   Input Parameters:
1359552f7358SJed Brown + mesh - The DMPlex
1360552f7358SJed Brown . pStart - The first mesh point
1361552f7358SJed Brown - pEnd   - The upper bound for mesh points
1362552f7358SJed Brown 
1363552f7358SJed Brown   Output Parameters:
1364552f7358SJed Brown 
1365552f7358SJed Brown   Level: beginner
1366552f7358SJed Brown 
1367552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1368552f7358SJed Brown @*/
1369552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1370552f7358SJed Brown {
1371552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1372552f7358SJed Brown   PetscErrorCode ierr;
1373552f7358SJed Brown 
1374552f7358SJed Brown   PetscFunctionBegin;
1375552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1376552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1377552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1378552f7358SJed Brown   PetscFunctionReturn(0);
1379552f7358SJed Brown }
1380552f7358SJed Brown 
1381552f7358SJed Brown /*@
1382eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1383552f7358SJed Brown 
1384552f7358SJed Brown   Not collective
1385552f7358SJed Brown 
1386552f7358SJed Brown   Input Parameters:
1387552f7358SJed Brown + mesh - The DMPlex
1388eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1389552f7358SJed Brown 
1390552f7358SJed Brown   Output Parameter:
1391552f7358SJed Brown . size - The cone size for point p
1392552f7358SJed Brown 
1393552f7358SJed Brown   Level: beginner
1394552f7358SJed Brown 
1395552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1396552f7358SJed Brown @*/
1397552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1398552f7358SJed Brown {
1399552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1400552f7358SJed Brown   PetscErrorCode ierr;
1401552f7358SJed Brown 
1402552f7358SJed Brown   PetscFunctionBegin;
1403552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1404552f7358SJed Brown   PetscValidPointer(size, 3);
1405552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1406552f7358SJed Brown   PetscFunctionReturn(0);
1407552f7358SJed Brown }
1408552f7358SJed Brown 
1409552f7358SJed Brown /*@
1410eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1411552f7358SJed Brown 
1412552f7358SJed Brown   Not collective
1413552f7358SJed Brown 
1414552f7358SJed Brown   Input Parameters:
1415552f7358SJed Brown + mesh - The DMPlex
1416eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1417552f7358SJed Brown - size - The cone size for point p
1418552f7358SJed Brown 
1419552f7358SJed Brown   Output Parameter:
1420552f7358SJed Brown 
1421552f7358SJed Brown   Note:
1422552f7358SJed Brown   This should be called after DMPlexSetChart().
1423552f7358SJed Brown 
1424552f7358SJed Brown   Level: beginner
1425552f7358SJed Brown 
1426552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1427552f7358SJed Brown @*/
1428552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1429552f7358SJed Brown {
1430552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1431552f7358SJed Brown   PetscErrorCode ierr;
1432552f7358SJed Brown 
1433552f7358SJed Brown   PetscFunctionBegin;
1434552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1435552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
14360d644c17SKarl Rupp 
1437552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1438552f7358SJed Brown   PetscFunctionReturn(0);
1439552f7358SJed Brown }
1440552f7358SJed Brown 
1441f5a469b9SMatthew G. Knepley /*@
1442eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1443f5a469b9SMatthew G. Knepley 
1444f5a469b9SMatthew G. Knepley   Not collective
1445f5a469b9SMatthew G. Knepley 
1446f5a469b9SMatthew G. Knepley   Input Parameters:
1447f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1448eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1449f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1450f5a469b9SMatthew G. Knepley 
1451f5a469b9SMatthew G. Knepley   Output Parameter:
1452f5a469b9SMatthew G. Knepley 
1453f5a469b9SMatthew G. Knepley   Note:
1454f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1455f5a469b9SMatthew G. Knepley 
1456f5a469b9SMatthew G. Knepley   Level: beginner
1457f5a469b9SMatthew G. Knepley 
1458f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1459f5a469b9SMatthew G. Knepley @*/
1460f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1461f5a469b9SMatthew G. Knepley {
1462f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1463f5a469b9SMatthew G. Knepley   PetscInt       csize;
1464f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1465f5a469b9SMatthew G. Knepley 
1466f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1467f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1468f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1469f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1470f5a469b9SMatthew G. Knepley 
1471f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1472f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1473f5a469b9SMatthew G. Knepley }
1474f5a469b9SMatthew G. Knepley 
1475552f7358SJed Brown /*@C
1476eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1477552f7358SJed Brown 
1478552f7358SJed Brown   Not collective
1479552f7358SJed Brown 
1480552f7358SJed Brown   Input Parameters:
1481833c876bSVaclav Hapla + dm - The DMPlex
1482eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1483552f7358SJed Brown 
1484552f7358SJed Brown   Output Parameter:
1485552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1486552f7358SJed Brown 
1487552f7358SJed Brown   Level: beginner
1488552f7358SJed Brown 
14893813dfbdSMatthew G Knepley   Fortran Notes:
14903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14923813dfbdSMatthew G Knepley 
14930ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1494552f7358SJed Brown @*/
1495552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1496552f7358SJed Brown {
1497552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1498552f7358SJed Brown   PetscInt       off;
1499552f7358SJed Brown   PetscErrorCode ierr;
1500552f7358SJed Brown 
1501552f7358SJed Brown   PetscFunctionBegin;
1502552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1503552f7358SJed Brown   PetscValidPointer(cone, 3);
1504552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1505552f7358SJed Brown   *cone = &mesh->cones[off];
1506552f7358SJed Brown   PetscFunctionReturn(0);
1507552f7358SJed Brown }
1508552f7358SJed Brown 
15090ce7577fSVaclav Hapla /*@C
15100ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
15110ce7577fSVaclav Hapla 
15120ce7577fSVaclav Hapla   Not collective
15130ce7577fSVaclav Hapla 
15140ce7577fSVaclav Hapla   Input Parameters:
15150ce7577fSVaclav Hapla + dm - The DMPlex
15160ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
15170ce7577fSVaclav Hapla 
15180ce7577fSVaclav Hapla   Output Parameter:
15190ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
15200ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
15210ce7577fSVaclav Hapla 
15220ce7577fSVaclav Hapla   Level: intermediate
15230ce7577fSVaclav Hapla 
1524d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
15250ce7577fSVaclav Hapla @*/
15260ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
15270ce7577fSVaclav Hapla {
15280ce7577fSVaclav Hapla   PetscSection        cs, newcs;
15290ce7577fSVaclav Hapla   PetscInt            *cones;
15300ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
15310ce7577fSVaclav Hapla   PetscInt            n;
15320ce7577fSVaclav Hapla   PetscErrorCode      ierr;
15330ce7577fSVaclav Hapla 
15340ce7577fSVaclav Hapla   PetscFunctionBegin;
15350ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
15360ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
15370ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
15380ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
15390ce7577fSVaclav Hapla   if (pCones) {
15400ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
15410ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
15420ce7577fSVaclav Hapla   }
15430ce7577fSVaclav Hapla   PetscFunctionReturn(0);
15440ce7577fSVaclav Hapla }
15450ce7577fSVaclav Hapla 
1546d4636a37SVaclav Hapla static PetscErrorCode DMPlexGetConeRecursive_Private(DM dm, PetscInt *n_inout, const PetscInt points[], PetscInt *offset_inout, PetscInt buf[])
1547d4636a37SVaclav Hapla {
1548d4636a37SVaclav Hapla   PetscInt p, n, cn, i;
1549d4636a37SVaclav Hapla   const PetscInt *cone;
1550d4636a37SVaclav Hapla   PetscErrorCode ierr;
1551d4636a37SVaclav Hapla 
1552d4636a37SVaclav Hapla   PetscFunctionBegin;
1553d4636a37SVaclav Hapla   n = *n_inout;
1554d4636a37SVaclav Hapla   *n_inout = 0;
1555d4636a37SVaclav Hapla   for (i=0; i<n; i++) {
1556d4636a37SVaclav Hapla     p = points[i];
1557d4636a37SVaclav Hapla     ierr = DMPlexGetConeSize(dm, p, &cn);CHKERRQ(ierr);
1558d4636a37SVaclav Hapla     if (!cn) {
1559d4636a37SVaclav Hapla       cn = 1;
1560d4636a37SVaclav Hapla       if (buf) {
1561d4636a37SVaclav Hapla         buf[*offset_inout] = p;
1562d4636a37SVaclav Hapla         ++(*offset_inout);
1563d4636a37SVaclav Hapla       }
1564d4636a37SVaclav Hapla     } else {
1565d4636a37SVaclav Hapla       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1566d4636a37SVaclav Hapla       ierr = DMPlexGetConeRecursive_Private(dm, &cn, cone, offset_inout, buf);CHKERRQ(ierr);
1567d4636a37SVaclav Hapla     }
1568d4636a37SVaclav Hapla     *n_inout += cn;
1569d4636a37SVaclav Hapla   }
1570d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1571d4636a37SVaclav Hapla }
1572d4636a37SVaclav Hapla 
1573d4636a37SVaclav Hapla /*@C
1574d4636a37SVaclav Hapla   DMPlexGetConeRecursive - Like DMPlexGetConeTuple() but recursive, i.e. each cone point is expanded into a set of its own cone points until a vertex (DAG point with no cone) is reached.
1575d4636a37SVaclav Hapla 
1576d4636a37SVaclav Hapla   Not collective
1577d4636a37SVaclav Hapla 
1578d4636a37SVaclav Hapla   Input Parameters:
1579d4636a37SVaclav Hapla + dm - The DMPlex
1580d4636a37SVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
1581d4636a37SVaclav Hapla 
1582d4636a37SVaclav Hapla   Output Parameter:
1583d4636a37SVaclav Hapla . pCones - An array of recursively expanded cones, i.e. containing only vertices, and each of them can be present multiple times
1584d4636a37SVaclav Hapla 
1585d4636a37SVaclav Hapla   Level: advanced
1586d4636a37SVaclav Hapla 
1587d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple()
1588d4636a37SVaclav Hapla @*/
1589d4636a37SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS p, IS *pCones)
1590d4636a37SVaclav Hapla {
1591d4636a37SVaclav Hapla   const PetscInt      *arr=NULL;
1592d4636a37SVaclav Hapla   PetscInt            *cpoints=NULL;
1593d4636a37SVaclav Hapla   PetscInt            n, cn;
1594d4636a37SVaclav Hapla   PetscInt            zero;
1595d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1596d4636a37SVaclav Hapla 
1597d4636a37SVaclav Hapla   PetscFunctionBegin;
1598d4636a37SVaclav Hapla   ierr = ISGetLocalSize(p, &n);CHKERRQ(ierr);
1599d4636a37SVaclav Hapla   ierr = ISGetIndices(p, &arr);CHKERRQ(ierr);
1600d4636a37SVaclav Hapla   zero = 0;
1601d4636a37SVaclav Hapla   /* first figure out the total number of returned points */
1602d4636a37SVaclav Hapla   cn = n;
1603d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &cn, arr, &zero, NULL);CHKERRQ(ierr);
1604d4636a37SVaclav Hapla   ierr = PetscMalloc1(cn, &cpoints);CHKERRQ(ierr);
1605d4636a37SVaclav Hapla   /* now get recursive cones themselves */
1606d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &n, arr, &zero, cpoints);CHKERRQ(ierr);
1607d4636a37SVaclav Hapla   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, cpoints, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
1608d4636a37SVaclav Hapla   ierr = ISRestoreIndices(p, &arr);CHKERRQ(ierr);
1609d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1610d4636a37SVaclav Hapla }
1611d4636a37SVaclav Hapla 
1612552f7358SJed Brown /*@
161392371b87SBarry 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
1614552f7358SJed Brown 
1615552f7358SJed Brown   Not collective
1616552f7358SJed Brown 
1617552f7358SJed Brown   Input Parameters:
1618552f7358SJed Brown + mesh - The DMPlex
1619eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1620552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1621552f7358SJed Brown 
1622552f7358SJed Brown   Output Parameter:
1623552f7358SJed Brown 
1624552f7358SJed Brown   Note:
1625552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1626552f7358SJed Brown 
162792371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
162892371b87SBarry Smith 
1629552f7358SJed Brown   Level: beginner
1630552f7358SJed Brown 
163192371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1632552f7358SJed Brown @*/
1633552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1634552f7358SJed Brown {
1635552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1636552f7358SJed Brown   PetscInt       pStart, pEnd;
1637552f7358SJed Brown   PetscInt       dof, off, c;
1638552f7358SJed Brown   PetscErrorCode ierr;
1639552f7358SJed Brown 
1640552f7358SJed Brown   PetscFunctionBegin;
1641552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1642552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1643552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1644552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1645552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
164682f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1647552f7358SJed Brown   for (c = 0; c < dof; ++c) {
164882f516ccSBarry Smith     if ((cone[c] < pStart) || (cone[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", cone[c], pStart, pEnd);
1649552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1650552f7358SJed Brown   }
1651552f7358SJed Brown   PetscFunctionReturn(0);
1652552f7358SJed Brown }
1653552f7358SJed Brown 
1654552f7358SJed Brown /*@C
1655eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1656552f7358SJed Brown 
1657552f7358SJed Brown   Not collective
1658552f7358SJed Brown 
1659552f7358SJed Brown   Input Parameters:
1660552f7358SJed Brown + mesh - The DMPlex
1661eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1662552f7358SJed Brown 
1663552f7358SJed Brown   Output Parameter:
1664552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1665552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1666552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1667552f7358SJed Brown                     the index of the cone point on which to start.
1668552f7358SJed Brown 
1669552f7358SJed Brown   Level: beginner
1670552f7358SJed Brown 
16713813dfbdSMatthew G Knepley   Fortran Notes:
16723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16743813dfbdSMatthew G Knepley 
16753813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1676552f7358SJed Brown 
1677552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1678552f7358SJed Brown @*/
1679552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1680552f7358SJed Brown {
1681552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1682552f7358SJed Brown   PetscInt       off;
1683552f7358SJed Brown   PetscErrorCode ierr;
1684552f7358SJed Brown 
1685552f7358SJed Brown   PetscFunctionBegin;
1686552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1687552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1688552f7358SJed Brown   {
1689552f7358SJed Brown     PetscInt dof;
1690552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1691552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1692552f7358SJed Brown   }
1693552f7358SJed Brown #endif
1694552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
16950d644c17SKarl Rupp 
1696552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1697552f7358SJed Brown   PetscFunctionReturn(0);
1698552f7358SJed Brown }
1699552f7358SJed Brown 
1700552f7358SJed Brown /*@
1701eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1702552f7358SJed Brown 
1703552f7358SJed Brown   Not collective
1704552f7358SJed Brown 
1705552f7358SJed Brown   Input Parameters:
1706552f7358SJed Brown + mesh - The DMPlex
1707eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1708552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1709552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1710552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1711552f7358SJed Brown                     the index of the cone point on which to start.
1712552f7358SJed Brown 
1713552f7358SJed Brown   Output Parameter:
1714552f7358SJed Brown 
1715552f7358SJed Brown   Note:
1716552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1717552f7358SJed Brown 
1718552f7358SJed Brown   Level: beginner
1719552f7358SJed Brown 
1720552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1721552f7358SJed Brown @*/
1722552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1723552f7358SJed Brown {
1724552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1725552f7358SJed Brown   PetscInt       pStart, pEnd;
1726552f7358SJed Brown   PetscInt       dof, off, c;
1727552f7358SJed Brown   PetscErrorCode ierr;
1728552f7358SJed Brown 
1729552f7358SJed Brown   PetscFunctionBegin;
1730552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1731552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1732552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1733552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1734552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
173582f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1736552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1737552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1738552f7358SJed Brown 
1739552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
174082f516ccSBarry Smith     if (o && ((o < -(cdof+1)) || (o >= cdof))) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %D is not in the valid range [%D. %D)", o, -(cdof+1), cdof);
1741552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1742552f7358SJed Brown   }
1743552f7358SJed Brown   PetscFunctionReturn(0);
1744552f7358SJed Brown }
1745552f7358SJed Brown 
17467cd05799SMatthew G. Knepley /*@
1747eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
17487cd05799SMatthew G. Knepley 
17497cd05799SMatthew G. Knepley   Not collective
17507cd05799SMatthew G. Knepley 
17517cd05799SMatthew G. Knepley   Input Parameters:
17527cd05799SMatthew G. Knepley + mesh - The DMPlex
1753eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17547cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
17557cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
17567cd05799SMatthew G. Knepley 
17577cd05799SMatthew G. Knepley   Level: beginner
17587cd05799SMatthew G. Knepley 
17597cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17607cd05799SMatthew G. Knepley @*/
1761552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1762552f7358SJed Brown {
1763552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1764552f7358SJed Brown   PetscInt       pStart, pEnd;
1765552f7358SJed Brown   PetscInt       dof, off;
1766552f7358SJed Brown   PetscErrorCode ierr;
1767552f7358SJed Brown 
1768552f7358SJed Brown   PetscFunctionBegin;
1769552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1770552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
177182f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
177282f516ccSBarry Smith   if ((conePoint < pStart) || (conePoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", conePoint, pStart, pEnd);
177377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
177477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
177577c88f5bSMatthew G Knepley   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
1776552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1777552f7358SJed Brown   PetscFunctionReturn(0);
1778552f7358SJed Brown }
1779552f7358SJed Brown 
17807cd05799SMatthew G. Knepley /*@
1781eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
17827cd05799SMatthew G. Knepley 
17837cd05799SMatthew G. Knepley   Not collective
17847cd05799SMatthew G. Knepley 
17857cd05799SMatthew G. Knepley   Input Parameters:
17867cd05799SMatthew G. Knepley + mesh - The DMPlex
1787eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17887cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
17897cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
17907cd05799SMatthew G. Knepley 
17917cd05799SMatthew G. Knepley   Level: beginner
17927cd05799SMatthew G. Knepley 
17937cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17947cd05799SMatthew G. Knepley @*/
179577c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
179677c88f5bSMatthew G Knepley {
179777c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
179877c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
179977c88f5bSMatthew G Knepley   PetscInt       dof, off;
180077c88f5bSMatthew G Knepley   PetscErrorCode ierr;
180177c88f5bSMatthew G Knepley 
180277c88f5bSMatthew G Knepley   PetscFunctionBegin;
180377c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
180477c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
180577c88f5bSMatthew G Knepley   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
180677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
180777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
180877c88f5bSMatthew G Knepley   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
180977c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
181077c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
181177c88f5bSMatthew G Knepley }
181277c88f5bSMatthew G Knepley 
1813552f7358SJed Brown /*@
1814eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1815552f7358SJed Brown 
1816552f7358SJed Brown   Not collective
1817552f7358SJed Brown 
1818552f7358SJed Brown   Input Parameters:
1819552f7358SJed Brown + mesh - The DMPlex
1820eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1821552f7358SJed Brown 
1822552f7358SJed Brown   Output Parameter:
1823552f7358SJed Brown . size - The support size for point p
1824552f7358SJed Brown 
1825552f7358SJed Brown   Level: beginner
1826552f7358SJed Brown 
1827552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1828552f7358SJed Brown @*/
1829552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1830552f7358SJed Brown {
1831552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1832552f7358SJed Brown   PetscErrorCode ierr;
1833552f7358SJed Brown 
1834552f7358SJed Brown   PetscFunctionBegin;
1835552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1836552f7358SJed Brown   PetscValidPointer(size, 3);
1837552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1838552f7358SJed Brown   PetscFunctionReturn(0);
1839552f7358SJed Brown }
1840552f7358SJed Brown 
1841552f7358SJed Brown /*@
1842eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1843552f7358SJed Brown 
1844552f7358SJed Brown   Not collective
1845552f7358SJed Brown 
1846552f7358SJed Brown   Input Parameters:
1847552f7358SJed Brown + mesh - The DMPlex
1848eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1849552f7358SJed Brown - size - The support size for point p
1850552f7358SJed Brown 
1851552f7358SJed Brown   Output Parameter:
1852552f7358SJed Brown 
1853552f7358SJed Brown   Note:
1854552f7358SJed Brown   This should be called after DMPlexSetChart().
1855552f7358SJed Brown 
1856552f7358SJed Brown   Level: beginner
1857552f7358SJed Brown 
1858552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1859552f7358SJed Brown @*/
1860552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1861552f7358SJed Brown {
1862552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1863552f7358SJed Brown   PetscErrorCode ierr;
1864552f7358SJed Brown 
1865552f7358SJed Brown   PetscFunctionBegin;
1866552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1867552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
18680d644c17SKarl Rupp 
1869552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1870552f7358SJed Brown   PetscFunctionReturn(0);
1871552f7358SJed Brown }
1872552f7358SJed Brown 
1873552f7358SJed Brown /*@C
1874eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1875552f7358SJed Brown 
1876552f7358SJed Brown   Not collective
1877552f7358SJed Brown 
1878552f7358SJed Brown   Input Parameters:
1879552f7358SJed Brown + mesh - The DMPlex
1880eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1881552f7358SJed Brown 
1882552f7358SJed Brown   Output Parameter:
1883552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1884552f7358SJed Brown 
1885552f7358SJed Brown   Level: beginner
1886552f7358SJed Brown 
18873813dfbdSMatthew G Knepley   Fortran Notes:
18883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18903813dfbdSMatthew G Knepley 
18913813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
18923813dfbdSMatthew G Knepley 
1893552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1894552f7358SJed Brown @*/
1895552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1896552f7358SJed Brown {
1897552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1898552f7358SJed Brown   PetscInt       off;
1899552f7358SJed Brown   PetscErrorCode ierr;
1900552f7358SJed Brown 
1901552f7358SJed Brown   PetscFunctionBegin;
1902552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1903552f7358SJed Brown   PetscValidPointer(support, 3);
1904552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1905552f7358SJed Brown   *support = &mesh->supports[off];
1906552f7358SJed Brown   PetscFunctionReturn(0);
1907552f7358SJed Brown }
1908552f7358SJed Brown 
1909552f7358SJed Brown /*@
191092371b87SBarry 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
1911552f7358SJed Brown 
1912552f7358SJed Brown   Not collective
1913552f7358SJed Brown 
1914552f7358SJed Brown   Input Parameters:
1915552f7358SJed Brown + mesh - The DMPlex
1916eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
191792371b87SBarry Smith - support - An array of points which are on the out-edges for point p
1918552f7358SJed Brown 
1919552f7358SJed Brown   Output Parameter:
1920552f7358SJed Brown 
1921552f7358SJed Brown   Note:
1922552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1923552f7358SJed Brown 
1924552f7358SJed Brown   Level: beginner
1925552f7358SJed Brown 
192692371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1927552f7358SJed Brown @*/
1928552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1929552f7358SJed Brown {
1930552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1931552f7358SJed Brown   PetscInt       pStart, pEnd;
1932552f7358SJed Brown   PetscInt       dof, off, c;
1933552f7358SJed Brown   PetscErrorCode ierr;
1934552f7358SJed Brown 
1935552f7358SJed Brown   PetscFunctionBegin;
1936552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1937552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1938552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1939552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1940552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
194182f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1942552f7358SJed Brown   for (c = 0; c < dof; ++c) {
194382f516ccSBarry Smith     if ((support[c] < pStart) || (support[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", support[c], pStart, pEnd);
1944552f7358SJed Brown     mesh->supports[off+c] = support[c];
1945552f7358SJed Brown   }
1946552f7358SJed Brown   PetscFunctionReturn(0);
1947552f7358SJed Brown }
1948552f7358SJed Brown 
19497cd05799SMatthew G. Knepley /*@
1950eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
19517cd05799SMatthew G. Knepley 
19527cd05799SMatthew G. Knepley   Not collective
19537cd05799SMatthew G. Knepley 
19547cd05799SMatthew G. Knepley   Input Parameters:
19557cd05799SMatthew G. Knepley + mesh - The DMPlex
1956eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19577cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
19587cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
19597cd05799SMatthew G. Knepley 
19607cd05799SMatthew G. Knepley   Level: beginner
19617cd05799SMatthew G. Knepley 
19627cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
19637cd05799SMatthew G. Knepley @*/
1964552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1965552f7358SJed Brown {
1966552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1967552f7358SJed Brown   PetscInt       pStart, pEnd;
1968552f7358SJed Brown   PetscInt       dof, off;
1969552f7358SJed Brown   PetscErrorCode ierr;
1970552f7358SJed Brown 
1971552f7358SJed Brown   PetscFunctionBegin;
1972552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1973552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1974552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1975552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
197682f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
197782f516ccSBarry Smith   if ((supportPoint < pStart) || (supportPoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", supportPoint, pStart, pEnd);
197882f516ccSBarry Smith   if (supportPos >= dof) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %D of point %D is not in the valid range [0, %D)", supportPos, p, dof);
1979552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1980552f7358SJed Brown   PetscFunctionReturn(0);
1981552f7358SJed Brown }
1982552f7358SJed Brown 
1983552f7358SJed Brown /*@C
1984eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1985552f7358SJed Brown 
1986552f7358SJed Brown   Not collective
1987552f7358SJed Brown 
1988552f7358SJed Brown   Input Parameters:
1989552f7358SJed Brown + mesh - The DMPlex
1990eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1991552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19920298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1993552f7358SJed Brown 
1994552f7358SJed Brown   Output Parameters:
1995552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1996552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1997552f7358SJed Brown 
1998552f7358SJed Brown   Note:
19990298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2000552f7358SJed Brown 
20013813dfbdSMatthew G Knepley   Fortran Notes:
20023813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20033813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20043813dfbdSMatthew G Knepley 
20053813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20063813dfbdSMatthew G Knepley 
2007552f7358SJed Brown   Level: beginner
2008552f7358SJed Brown 
2009552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2010552f7358SJed Brown @*/
2011552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2012552f7358SJed Brown {
2013552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2014552f7358SJed Brown   PetscInt       *closure, *fifo;
20150298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2016552f7358SJed Brown   PetscInt        tmpSize, t;
2017552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2018552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2019552f7358SJed Brown   PetscErrorCode  ierr;
2020552f7358SJed Brown 
2021552f7358SJed Brown   PetscFunctionBegin;
2022552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2023552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2024552f7358SJed Brown   /* This is only 1-level */
2025552f7358SJed Brown   if (useCone) {
2026552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2027552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2028552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2029552f7358SJed Brown   } else {
2030552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2031552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2032552f7358SJed Brown   }
2033bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2034bfbcdd7aSMatthew G. Knepley     if (*points) {
2035bfbcdd7aSMatthew G. Knepley       closure = *points;
2036bfbcdd7aSMatthew G. Knepley     } else {
2037bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
203869291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2039bfbcdd7aSMatthew G. Knepley     }
2040bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2041bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2042bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2043bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2044bfbcdd7aSMatthew G. Knepley     }
2045bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2046bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2047bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2048bfbcdd7aSMatthew G. Knepley   }
204924c766afSToby Isaac   {
205024c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
205124c766afSToby Isaac 
205224c766afSToby Isaac     c = mesh->maxConeSize;
205324c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
205424c766afSToby Isaac     s = mesh->maxSupportSize;
205524c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
205624c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
205724c766afSToby Isaac   }
205869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2059bfbcdd7aSMatthew G. Knepley   if (*points) {
2060bfbcdd7aSMatthew G. Knepley     closure = *points;
2061bfbcdd7aSMatthew G. Knepley   } else {
206269291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2063bfbcdd7aSMatthew G. Knepley   }
2064bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2065552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2066552f7358SJed Brown     const PetscInt cp = tmp[t];
2067552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2068552f7358SJed Brown 
2069552f7358SJed Brown     closure[closureSize]   = cp;
2070552f7358SJed Brown     closure[closureSize+1] = co;
2071552f7358SJed Brown     fifo[fifoSize]         = cp;
2072552f7358SJed Brown     fifo[fifoSize+1]       = co;
2073552f7358SJed Brown   }
2074bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2075552f7358SJed Brown   while (fifoSize - fifoStart) {
2076552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2077552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2078552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2079552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2080552f7358SJed Brown 
2081552f7358SJed Brown     if (useCone) {
2082552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2083552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2084552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2085552f7358SJed Brown     } else {
2086552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2087552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20880298fd71SBarry Smith       tmpO = NULL;
2089552f7358SJed Brown     }
2090552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2091552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2092552f7358SJed Brown       const PetscInt cp = tmp[i];
20932e1b13c2SMatthew G. Knepley       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
20942e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20952e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20962e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2097552f7358SJed Brown       PetscInt       c;
2098552f7358SJed Brown 
20992e1b13c2SMatthew G. Knepley       if (rev) {
21002e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
21012e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
21022e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
21032e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
21042e1b13c2SMatthew G. Knepley       }
2105552f7358SJed Brown       /* Check for duplicate */
2106552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2107552f7358SJed Brown         if (closure[c] == cp) break;
2108552f7358SJed Brown       }
2109552f7358SJed Brown       if (c == closureSize) {
2110552f7358SJed Brown         closure[closureSize]   = cp;
2111552f7358SJed Brown         closure[closureSize+1] = co;
2112552f7358SJed Brown         fifo[fifoSize]         = cp;
2113552f7358SJed Brown         fifo[fifoSize+1]       = co;
2114552f7358SJed Brown         closureSize           += 2;
2115552f7358SJed Brown         fifoSize              += 2;
2116552f7358SJed Brown       }
2117552f7358SJed Brown     }
2118552f7358SJed Brown     fifoStart += 2;
2119552f7358SJed Brown   }
2120552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2121552f7358SJed Brown   if (points)    *points    = closure;
212269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2123552f7358SJed Brown   PetscFunctionReturn(0);
2124552f7358SJed Brown }
2125552f7358SJed Brown 
21269bf0dad6SMatthew G. Knepley /*@C
2127eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure_Internal - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG with a specified initial orientation
21289bf0dad6SMatthew G. Knepley 
21299bf0dad6SMatthew G. Knepley   Not collective
21309bf0dad6SMatthew G. Knepley 
21319bf0dad6SMatthew G. Knepley   Input Parameters:
21329bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2133eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
21349bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
21359bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
21369bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
21379bf0dad6SMatthew G. Knepley 
21389bf0dad6SMatthew G. Knepley   Output Parameters:
21399bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
21409bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
21419bf0dad6SMatthew G. Knepley 
21429bf0dad6SMatthew G. Knepley   Note:
21439bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
21449bf0dad6SMatthew G. Knepley 
21459bf0dad6SMatthew G. Knepley   Fortran Notes:
21469bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21479bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
21489bf0dad6SMatthew G. Knepley 
21499bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
21509bf0dad6SMatthew G. Knepley 
21519bf0dad6SMatthew G. Knepley   Level: beginner
21529bf0dad6SMatthew G. Knepley 
21539bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
21549bf0dad6SMatthew G. Knepley @*/
21559bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
21569bf0dad6SMatthew G. Knepley {
21579bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
21589bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
21599bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
21609bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
21619bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
21629bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
21639bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
21649bf0dad6SMatthew G. Knepley 
21659bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
21669bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21679bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
21689bf0dad6SMatthew G. Knepley   /* This is only 1-level */
21699bf0dad6SMatthew G. Knepley   if (useCone) {
21709bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
21719bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
21729bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
21739bf0dad6SMatthew G. Knepley   } else {
21749bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
21759bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
21769bf0dad6SMatthew G. Knepley   }
21779bf0dad6SMatthew G. Knepley   if (depth == 1) {
21789bf0dad6SMatthew G. Knepley     if (*points) {
21799bf0dad6SMatthew G. Knepley       closure = *points;
21809bf0dad6SMatthew G. Knepley     } else {
21819bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
218269291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
21839bf0dad6SMatthew G. Knepley     }
21849bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
21859bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
21869bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
21879bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
21889bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
21899bf0dad6SMatthew G. Knepley     }
21909bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
21919bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
21929bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
21939bf0dad6SMatthew G. Knepley   }
219424c766afSToby Isaac   {
219524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
219624c766afSToby Isaac 
219724c766afSToby Isaac     c = mesh->maxConeSize;
219824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
219924c766afSToby Isaac     s = mesh->maxSupportSize;
220024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
220124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
220224c766afSToby Isaac   }
220369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
22049bf0dad6SMatthew G. Knepley   if (*points) {
22059bf0dad6SMatthew G. Knepley     closure = *points;
22069bf0dad6SMatthew G. Knepley   } else {
220769291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
22089bf0dad6SMatthew G. Knepley   }
22099bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
22109bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
22119bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
22129bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
22139bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
22149bf0dad6SMatthew G. Knepley 
22159bf0dad6SMatthew G. Knepley     if (ornt < 0) {
22169bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
22179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
221886b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
22199bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22209bf0dad6SMatthew G. Knepley     }
22219bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
22229bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
22239bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
22249bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
22259bf0dad6SMatthew G. Knepley   }
22269bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
22279bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
22289bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
22299bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
22309bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
22319bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
22329bf0dad6SMatthew G. Knepley 
22339bf0dad6SMatthew G. Knepley     if (useCone) {
22349bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
22359bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
22369bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
22379bf0dad6SMatthew G. Knepley     } else {
22389bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
22399bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
22409bf0dad6SMatthew G. Knepley       tmpO = NULL;
22419bf0dad6SMatthew G. Knepley     }
22429bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
22439bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
22449bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
22459bf0dad6SMatthew G. Knepley       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
22469bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
22479bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
22489bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
22499bf0dad6SMatthew G. Knepley       PetscInt       c;
22509bf0dad6SMatthew G. Knepley 
22519bf0dad6SMatthew G. Knepley       if (rev) {
22529bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
22539bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
22549bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
22559bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22569bf0dad6SMatthew G. Knepley       }
22579bf0dad6SMatthew G. Knepley       /* Check for duplicate */
22589bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
22599bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
22609bf0dad6SMatthew G. Knepley       }
22619bf0dad6SMatthew G. Knepley       if (c == closureSize) {
22629bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
22639bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
22649bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
22659bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
22669bf0dad6SMatthew G. Knepley         closureSize           += 2;
22679bf0dad6SMatthew G. Knepley         fifoSize              += 2;
22689bf0dad6SMatthew G. Knepley       }
22699bf0dad6SMatthew G. Knepley     }
22709bf0dad6SMatthew G. Knepley     fifoStart += 2;
22719bf0dad6SMatthew G. Knepley   }
22729bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
22739bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
227469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
22759bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
22769bf0dad6SMatthew G. Knepley }
22779bf0dad6SMatthew G. Knepley 
2278552f7358SJed Brown /*@C
2279eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2280552f7358SJed Brown 
2281552f7358SJed Brown   Not collective
2282552f7358SJed Brown 
2283552f7358SJed Brown   Input Parameters:
2284552f7358SJed Brown + mesh - The DMPlex
2285eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2286552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2287e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2288e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2289552f7358SJed Brown 
2290552f7358SJed Brown   Note:
22910298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2292552f7358SJed Brown 
22933813dfbdSMatthew G Knepley   Fortran Notes:
22943813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22953813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22963813dfbdSMatthew G Knepley 
22973813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22983813dfbdSMatthew G Knepley 
2299552f7358SJed Brown   Level: beginner
2300552f7358SJed Brown 
2301552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2302552f7358SJed Brown @*/
2303552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2304552f7358SJed Brown {
2305552f7358SJed Brown   PetscErrorCode ierr;
2306552f7358SJed Brown 
2307552f7358SJed Brown   PetscFunctionBegin;
2308552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2309e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2310e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
231169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
23124ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2313552f7358SJed Brown   PetscFunctionReturn(0);
2314552f7358SJed Brown }
2315552f7358SJed Brown 
2316552f7358SJed Brown /*@
2317eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2318552f7358SJed Brown 
2319552f7358SJed Brown   Not collective
2320552f7358SJed Brown 
2321552f7358SJed Brown   Input Parameter:
2322552f7358SJed Brown . mesh - The DMPlex
2323552f7358SJed Brown 
2324552f7358SJed Brown   Output Parameters:
2325552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2326552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2327552f7358SJed Brown 
2328552f7358SJed Brown   Level: beginner
2329552f7358SJed Brown 
2330552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2331552f7358SJed Brown @*/
2332552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2333552f7358SJed Brown {
2334552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2335552f7358SJed Brown 
2336552f7358SJed Brown   PetscFunctionBegin;
2337552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2338552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2339552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2340552f7358SJed Brown   PetscFunctionReturn(0);
2341552f7358SJed Brown }
2342552f7358SJed Brown 
2343552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2344552f7358SJed Brown {
2345552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2346552f7358SJed Brown   PetscInt       size;
2347552f7358SJed Brown   PetscErrorCode ierr;
2348552f7358SJed Brown 
2349552f7358SJed Brown   PetscFunctionBegin;
2350552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2351552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2352552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
23531795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
23541795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2355552f7358SJed Brown   if (mesh->maxSupportSize) {
2356552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2357552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
23581795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2359552f7358SJed Brown   }
2360552f7358SJed Brown   PetscFunctionReturn(0);
2361552f7358SJed Brown }
2362552f7358SJed Brown 
2363276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2364552f7358SJed Brown {
2365552f7358SJed Brown   PetscErrorCode ierr;
2366552f7358SJed Brown 
2367552f7358SJed Brown   PetscFunctionBegin;
23684d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
23694d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2370c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2371736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2372f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2373f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2374f94b4a02SBlaise Bourdin 
23753dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
23763dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2377e87a4003SBarry Smith     ierr = DMGetSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2378f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2379f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2380f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2381f94b4a02SBlaise Bourdin 
2382f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2383c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2384f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2385f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2386f94b4a02SBlaise Bourdin   }
2387552f7358SJed Brown   PetscFunctionReturn(0);
2388552f7358SJed Brown }
2389552f7358SJed Brown 
23902adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
23912adcc780SMatthew G. Knepley {
23922adcc780SMatthew G. Knepley   PetscErrorCode ierr;
23933dcd263cSBlaise Bourdin   PetscInt       i = 0;
23942adcc780SMatthew G. Knepley 
23952adcc780SMatthew G. Knepley   PetscFunctionBegin;
2396435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
23972adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
2398c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
23993dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
24003dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
24013dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
24023dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
24033dcd263cSBlaise Bourdin 
24043dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
24053dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2406c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
2407cf0b7c11SKarl Rupp       ierr = DMGetSection((*superdm), &section);CHKERRQ(ierr);
24083dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
24093dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
24103dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
24113dcd263cSBlaise Bourdin 
24123dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2413c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
24143dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
24153dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
24163dcd263cSBlaise Bourdin       break;
24173dcd263cSBlaise Bourdin     }
24183dcd263cSBlaise Bourdin   }
24192adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
24202adcc780SMatthew G. Knepley }
24212adcc780SMatthew G. Knepley 
2422552f7358SJed Brown /*@
2423eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2424552f7358SJed Brown 
2425552f7358SJed Brown   Not collective
2426552f7358SJed Brown 
2427552f7358SJed Brown   Input Parameter:
2428552f7358SJed Brown . mesh - The DMPlex
2429552f7358SJed Brown 
2430552f7358SJed Brown   Output Parameter:
2431552f7358SJed Brown 
2432552f7358SJed Brown   Note:
2433552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2434552f7358SJed Brown 
2435552f7358SJed Brown   Level: beginner
2436552f7358SJed Brown 
2437552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2438552f7358SJed Brown @*/
2439552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2440552f7358SJed Brown {
2441552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2442552f7358SJed Brown   PetscInt      *offsets;
2443552f7358SJed Brown   PetscInt       supportSize;
2444552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2445552f7358SJed Brown   PetscErrorCode ierr;
2446552f7358SJed Brown 
2447552f7358SJed Brown   PetscFunctionBegin;
2448552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
244982f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2450552f7358SJed Brown   /* Calculate support sizes */
2451552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2452552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2453552f7358SJed Brown     PetscInt dof, off, c;
2454552f7358SJed Brown 
2455552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2456552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2457552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2458552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2459552f7358SJed Brown     }
2460552f7358SJed Brown   }
2461552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2462552f7358SJed Brown     PetscInt dof;
2463552f7358SJed Brown 
2464552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
24650d644c17SKarl Rupp 
2466552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2467552f7358SJed Brown   }
2468552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2469552f7358SJed Brown   /* Calculate supports */
2470552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
24711795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
24721795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2473552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2474552f7358SJed Brown     PetscInt dof, off, c;
2475552f7358SJed Brown 
2476552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2477552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2478552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2479552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2480552f7358SJed Brown       PetscInt       offS;
2481552f7358SJed Brown 
2482552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
24830d644c17SKarl Rupp 
2484552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2485552f7358SJed Brown       ++offsets[q];
2486552f7358SJed Brown     }
2487552f7358SJed Brown   }
2488552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2489552f7358SJed Brown   PetscFunctionReturn(0);
2490552f7358SJed Brown }
2491552f7358SJed Brown 
24927d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
24937d5acc75SStefano Zampini 
2494552f7358SJed Brown /*@
2495eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2496eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2497552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2498552f7358SJed Brown   the DAG.
2499552f7358SJed Brown 
2500bf4602e4SToby Isaac   Collective on dm
2501552f7358SJed Brown 
2502552f7358SJed Brown   Input Parameter:
2503552f7358SJed Brown . mesh - The DMPlex
2504552f7358SJed Brown 
2505552f7358SJed Brown   Output Parameter:
2506552f7358SJed Brown 
2507552f7358SJed Brown   Notes:
2508150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2509150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2510c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2511150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2512552f7358SJed Brown 
2513150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2514552f7358SJed Brown 
2515552f7358SJed Brown   Level: beginner
2516552f7358SJed Brown 
2517552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2518552f7358SJed Brown @*/
2519552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2520552f7358SJed Brown {
2521df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2522aa50250dSMatthew G. Knepley   DMLabel        label;
2523552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2524552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
25257d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2526552f7358SJed Brown   PetscErrorCode ierr;
2527552f7358SJed Brown 
2528552f7358SJed Brown   PetscFunctionBegin;
2529552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2530552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2531552f7358SJed Brown   /* Calculate depth */
2532aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2533c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2534aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2535552f7358SJed Brown   /* Initialize roots and count leaves */
2536552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2537552f7358SJed Brown     PetscInt coneSize, supportSize;
2538552f7358SJed Brown 
2539552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2540552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2541552f7358SJed Brown     if (!coneSize && supportSize) {
2542552f7358SJed Brown       ++numRoots;
2543aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2544552f7358SJed Brown     } else if (!supportSize && coneSize) {
2545552f7358SJed Brown       ++numLeaves;
2546552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2547552f7358SJed Brown       /* Isolated points */
2548aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2549552f7358SJed Brown     }
2550552f7358SJed Brown   }
2551552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2552552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2553552f7358SJed Brown       PetscInt coneSize, supportSize;
2554552f7358SJed Brown 
2555552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2556552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2557552f7358SJed Brown       if (!supportSize && coneSize) {
2558aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2559552f7358SJed Brown       }
2560552f7358SJed Brown     }
2561552f7358SJed Brown   } else {
256274ef644bSMatthew G. Knepley     IS       pointIS;
256374ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2564552f7358SJed Brown 
256574ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
256674ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
256774ef644bSMatthew G. Knepley     while (numPoints) {
256874ef644bSMatthew G. Knepley       const PetscInt *points;
256974ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
257074ef644bSMatthew G. Knepley 
257174ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
257274ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
257374ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
257474ef644bSMatthew G. Knepley         const PetscInt *support;
257574ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
257674ef644bSMatthew G. Knepley 
257774ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
257874ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
257974ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
258074ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2581552f7358SJed Brown         }
2582552f7358SJed Brown       }
25835edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
258474ef644bSMatthew G. Knepley       ++level;
258574ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
258674ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
258774ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
258874ef644bSMatthew G. Knepley       else         {numPoints = 0;}
258974ef644bSMatthew G. Knepley     }
259074ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
259174ef644bSMatthew G. Knepley   }
2592bf4602e4SToby Isaac   { /* just in case there is an empty process */
2593bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2594bf4602e4SToby Isaac 
2595bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
25964de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
25974de306b1SToby Isaac       IS pointIS;
25984de306b1SToby Isaac 
25994de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
26004de306b1SToby Isaac       if (pointIS) {
26014de306b1SToby Isaac         PetscInt  min, max, numPoints;
26024de306b1SToby Isaac         PetscInt  start;
26034de306b1SToby Isaac         PetscBool contig;
26044de306b1SToby Isaac 
26054de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
26064de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
26074de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
26084de306b1SToby Isaac         if (start == 0 && contig) {
26094de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
26104de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
26114de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
26124de306b1SToby Isaac         }
26134de306b1SToby Isaac       }
26144de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
26154de306b1SToby Isaac     }
26166d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2617bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2618367003a6SStefano Zampini       ierr = DMLabelAddStratum(label,v);CHKERRQ(ierr);
2619bf4602e4SToby Isaac     }
2620bf4602e4SToby Isaac   }
2621df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
26227d5acc75SStefano Zampini 
26237d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
26247d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
26257d5acc75SStefano Zampini     DMLabel  dimLabel;
26267d5acc75SStefano Zampini     PetscInt dim;
26277d5acc75SStefano Zampini 
26287d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26297d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
26307d5acc75SStefano Zampini     if (!dimLabel) {
26317d5acc75SStefano Zampini       ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
26327d5acc75SStefano Zampini       ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
26337d5acc75SStefano Zampini     }
26347d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
26357d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
26367d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
26377d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
26387d5acc75SStefano Zampini   }
2639552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2640552f7358SJed Brown   PetscFunctionReturn(0);
2641552f7358SJed Brown }
2642552f7358SJed Brown 
2643552f7358SJed Brown /*@C
2644552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2645552f7358SJed Brown 
2646552f7358SJed Brown   Not Collective
2647552f7358SJed Brown 
2648552f7358SJed Brown   Input Parameters:
2649552f7358SJed Brown + dm - The DMPlex object
2650552f7358SJed Brown . numPoints - The number of input points for the join
2651552f7358SJed Brown - points - The input points
2652552f7358SJed Brown 
2653552f7358SJed Brown   Output Parameters:
2654552f7358SJed Brown + numCoveredPoints - The number of points in the join
2655552f7358SJed Brown - coveredPoints - The points in the join
2656552f7358SJed Brown 
2657552f7358SJed Brown   Level: intermediate
2658552f7358SJed Brown 
2659552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2660552f7358SJed Brown 
26613813dfbdSMatthew G Knepley   Fortran Notes:
26623813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26633813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26643813dfbdSMatthew G Knepley 
26653813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26663813dfbdSMatthew G Knepley 
2667552f7358SJed Brown .keywords: mesh
2668552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2669552f7358SJed Brown @*/
2670552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2671552f7358SJed Brown {
2672552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2673552f7358SJed Brown   PetscInt      *join[2];
2674552f7358SJed Brown   PetscInt       joinSize, i = 0;
2675552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2676552f7358SJed Brown   PetscErrorCode ierr;
2677552f7358SJed Brown 
2678552f7358SJed Brown   PetscFunctionBegin;
2679552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2680552f7358SJed Brown   PetscValidPointer(points, 2);
2681552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2682552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
268369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
268469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2685552f7358SJed Brown   /* Copy in support of first point */
2686552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2687552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2688552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2689552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2690552f7358SJed Brown   }
2691552f7358SJed Brown   /* Check each successive support */
2692552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2693552f7358SJed Brown     PetscInt newJoinSize = 0;
2694552f7358SJed Brown 
2695552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2696552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2697552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2698552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2699552f7358SJed Brown 
2700552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2701552f7358SJed Brown         if (point == join[i][m]) {
2702552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2703552f7358SJed Brown           break;
2704552f7358SJed Brown         }
2705552f7358SJed Brown       }
2706552f7358SJed Brown     }
2707552f7358SJed Brown     joinSize = newJoinSize;
2708552f7358SJed Brown     i        = 1-i;
2709552f7358SJed Brown   }
2710552f7358SJed Brown   *numCoveredPoints = joinSize;
2711552f7358SJed Brown   *coveredPoints    = join[i];
271269291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2713552f7358SJed Brown   PetscFunctionReturn(0);
2714552f7358SJed Brown }
2715552f7358SJed Brown 
2716552f7358SJed Brown /*@C
2717552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2718552f7358SJed Brown 
2719552f7358SJed Brown   Not Collective
2720552f7358SJed Brown 
2721552f7358SJed Brown   Input Parameters:
2722552f7358SJed Brown + dm - The DMPlex object
2723552f7358SJed Brown . numPoints - The number of input points for the join
2724552f7358SJed Brown - points - The input points
2725552f7358SJed Brown 
2726552f7358SJed Brown   Output Parameters:
2727552f7358SJed Brown + numCoveredPoints - The number of points in the join
2728552f7358SJed Brown - coveredPoints - The points in the join
2729552f7358SJed Brown 
27303813dfbdSMatthew G Knepley   Fortran Notes:
27313813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27323813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27333813dfbdSMatthew G Knepley 
27343813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27353813dfbdSMatthew G Knepley 
2736552f7358SJed Brown   Level: intermediate
2737552f7358SJed Brown 
2738552f7358SJed Brown .keywords: mesh
2739552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2740552f7358SJed Brown @*/
2741552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2742552f7358SJed Brown {
2743552f7358SJed Brown   PetscErrorCode ierr;
2744552f7358SJed Brown 
2745552f7358SJed Brown   PetscFunctionBegin;
2746552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2747d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2748d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2749d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
275069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2751d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2752552f7358SJed Brown   PetscFunctionReturn(0);
2753552f7358SJed Brown }
2754552f7358SJed Brown 
2755552f7358SJed Brown /*@C
2756552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2757552f7358SJed Brown 
2758552f7358SJed Brown   Not Collective
2759552f7358SJed Brown 
2760552f7358SJed Brown   Input Parameters:
2761552f7358SJed Brown + dm - The DMPlex object
2762552f7358SJed Brown . numPoints - The number of input points for the join
2763552f7358SJed Brown - points - The input points
2764552f7358SJed Brown 
2765552f7358SJed Brown   Output Parameters:
2766552f7358SJed Brown + numCoveredPoints - The number of points in the join
2767552f7358SJed Brown - coveredPoints - The points in the join
2768552f7358SJed Brown 
27693813dfbdSMatthew G Knepley   Fortran Notes:
27703813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27713813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27723813dfbdSMatthew G Knepley 
27733813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27743813dfbdSMatthew G Knepley 
2775552f7358SJed Brown   Level: intermediate
2776552f7358SJed Brown 
2777552f7358SJed Brown .keywords: mesh
2778552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2779552f7358SJed Brown @*/
2780552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2781552f7358SJed Brown {
2782552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2783552f7358SJed Brown   PetscInt      *offsets, **closures;
2784552f7358SJed Brown   PetscInt      *join[2];
2785552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
278624c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2787552f7358SJed Brown   PetscErrorCode ierr;
2788552f7358SJed Brown 
2789552f7358SJed Brown   PetscFunctionBegin;
2790552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2791552f7358SJed Brown   PetscValidPointer(points, 2);
2792552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2793552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2794552f7358SJed Brown 
2795552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
27961795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
279769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
279824c766afSToby Isaac   ms      = mesh->maxSupportSize;
279924c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
280069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
280169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2802552f7358SJed Brown 
2803552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2804552f7358SJed Brown     PetscInt closureSize;
2805552f7358SJed Brown 
2806552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
28070d644c17SKarl Rupp 
2808552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2809552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2810552f7358SJed Brown       PetscInt pStart, pEnd, i;
2811552f7358SJed Brown 
2812552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2813552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2814552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2815552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2816552f7358SJed Brown           break;
2817552f7358SJed Brown         }
2818552f7358SJed Brown       }
2819552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2820552f7358SJed Brown     }
282182f516ccSBarry Smith     if (offsets[p*(depth+2)+depth+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(depth+2)+depth+1], closureSize);
2822552f7358SJed Brown   }
2823552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2824552f7358SJed Brown     PetscInt dof;
2825552f7358SJed Brown 
2826552f7358SJed Brown     /* Copy in support of first point */
2827552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2828552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2829552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2830552f7358SJed Brown     }
2831552f7358SJed Brown     /* Check each successive cone */
2832552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2833552f7358SJed Brown       PetscInt newJoinSize = 0;
2834552f7358SJed Brown 
2835552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2836552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2837552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2838552f7358SJed Brown 
2839552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2840552f7358SJed Brown           if (point == join[i][m]) {
2841552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2842552f7358SJed Brown             break;
2843552f7358SJed Brown           }
2844552f7358SJed Brown         }
2845552f7358SJed Brown       }
2846552f7358SJed Brown       joinSize = newJoinSize;
2847552f7358SJed Brown       i        = 1-i;
2848552f7358SJed Brown     }
2849552f7358SJed Brown     if (joinSize) break;
2850552f7358SJed Brown   }
2851552f7358SJed Brown   *numCoveredPoints = joinSize;
2852552f7358SJed Brown   *coveredPoints    = join[i];
2853552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
28540298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2855552f7358SJed Brown   }
2856552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
285769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
285869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2859552f7358SJed Brown   PetscFunctionReturn(0);
2860552f7358SJed Brown }
2861552f7358SJed Brown 
2862552f7358SJed Brown /*@C
2863552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2864552f7358SJed Brown 
2865552f7358SJed Brown   Not Collective
2866552f7358SJed Brown 
2867552f7358SJed Brown   Input Parameters:
2868552f7358SJed Brown + dm - The DMPlex object
2869552f7358SJed Brown . numPoints - The number of input points for the meet
2870552f7358SJed Brown - points - The input points
2871552f7358SJed Brown 
2872552f7358SJed Brown   Output Parameters:
2873552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2874552f7358SJed Brown - coveredPoints - The points in the meet
2875552f7358SJed Brown 
2876552f7358SJed Brown   Level: intermediate
2877552f7358SJed Brown 
2878552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2879552f7358SJed Brown 
28803813dfbdSMatthew G Knepley   Fortran Notes:
28813813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
28823813dfbdSMatthew G Knepley   include petsc.h90 in your code.
28833813dfbdSMatthew G Knepley 
28843813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28853813dfbdSMatthew G Knepley 
2886552f7358SJed Brown .keywords: mesh
2887552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2888552f7358SJed Brown @*/
2889552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2890552f7358SJed Brown {
2891552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2892552f7358SJed Brown   PetscInt      *meet[2];
2893552f7358SJed Brown   PetscInt       meetSize, i = 0;
2894552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2895552f7358SJed Brown   PetscErrorCode ierr;
2896552f7358SJed Brown 
2897552f7358SJed Brown   PetscFunctionBegin;
2898552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2899552f7358SJed Brown   PetscValidPointer(points, 2);
2900552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2901552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
290269291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
290369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2904552f7358SJed Brown   /* Copy in cone of first point */
2905552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2906552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2907552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2908552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2909552f7358SJed Brown   }
2910552f7358SJed Brown   /* Check each successive cone */
2911552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2912552f7358SJed Brown     PetscInt newMeetSize = 0;
2913552f7358SJed Brown 
2914552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2915552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2916552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2917552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2918552f7358SJed Brown 
2919552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2920552f7358SJed Brown         if (point == meet[i][m]) {
2921552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2922552f7358SJed Brown           break;
2923552f7358SJed Brown         }
2924552f7358SJed Brown       }
2925552f7358SJed Brown     }
2926552f7358SJed Brown     meetSize = newMeetSize;
2927552f7358SJed Brown     i        = 1-i;
2928552f7358SJed Brown   }
2929552f7358SJed Brown   *numCoveringPoints = meetSize;
2930552f7358SJed Brown   *coveringPoints    = meet[i];
293169291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2932552f7358SJed Brown   PetscFunctionReturn(0);
2933552f7358SJed Brown }
2934552f7358SJed Brown 
2935552f7358SJed Brown /*@C
2936552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2937552f7358SJed Brown 
2938552f7358SJed Brown   Not Collective
2939552f7358SJed Brown 
2940552f7358SJed Brown   Input Parameters:
2941552f7358SJed Brown + dm - The DMPlex object
2942552f7358SJed Brown . numPoints - The number of input points for the meet
2943552f7358SJed Brown - points - The input points
2944552f7358SJed Brown 
2945552f7358SJed Brown   Output Parameters:
2946552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2947552f7358SJed Brown - coveredPoints - The points in the meet
2948552f7358SJed Brown 
2949552f7358SJed Brown   Level: intermediate
2950552f7358SJed Brown 
29513813dfbdSMatthew G Knepley   Fortran Notes:
29523813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29533813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29543813dfbdSMatthew G Knepley 
29553813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29563813dfbdSMatthew G Knepley 
2957552f7358SJed Brown .keywords: mesh
2958552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2959552f7358SJed Brown @*/
2960552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2961552f7358SJed Brown {
2962552f7358SJed Brown   PetscErrorCode ierr;
2963552f7358SJed Brown 
2964552f7358SJed Brown   PetscFunctionBegin;
2965552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2966d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2967d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2968d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
296969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2970d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2971552f7358SJed Brown   PetscFunctionReturn(0);
2972552f7358SJed Brown }
2973552f7358SJed Brown 
2974552f7358SJed Brown /*@C
2975552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2976552f7358SJed Brown 
2977552f7358SJed Brown   Not Collective
2978552f7358SJed Brown 
2979552f7358SJed Brown   Input Parameters:
2980552f7358SJed Brown + dm - The DMPlex object
2981552f7358SJed Brown . numPoints - The number of input points for the meet
2982552f7358SJed Brown - points - The input points
2983552f7358SJed Brown 
2984552f7358SJed Brown   Output Parameters:
2985552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2986552f7358SJed Brown - coveredPoints - The points in the meet
2987552f7358SJed Brown 
2988552f7358SJed Brown   Level: intermediate
2989552f7358SJed Brown 
29903813dfbdSMatthew G Knepley   Fortran Notes:
29913813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29923813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29933813dfbdSMatthew G Knepley 
29943813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29953813dfbdSMatthew G Knepley 
2996552f7358SJed Brown .keywords: mesh
2997552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2998552f7358SJed Brown @*/
2999552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3000552f7358SJed Brown {
3001552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3002552f7358SJed Brown   PetscInt      *offsets, **closures;
3003552f7358SJed Brown   PetscInt      *meet[2];
3004552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
300524c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3006552f7358SJed Brown   PetscErrorCode ierr;
3007552f7358SJed Brown 
3008552f7358SJed Brown   PetscFunctionBegin;
3009552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3010552f7358SJed Brown   PetscValidPointer(points, 2);
3011552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3012552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3013552f7358SJed Brown 
3014552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3015785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
301669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
301724c766afSToby Isaac   mc      = mesh->maxConeSize;
301824c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
301969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
302069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3021552f7358SJed Brown 
3022552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3023552f7358SJed Brown     PetscInt closureSize;
3024552f7358SJed Brown 
3025552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
30260d644c17SKarl Rupp 
3027552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3028552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3029552f7358SJed Brown       PetscInt pStart, pEnd, i;
3030552f7358SJed Brown 
3031552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3032552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3033552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3034552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3035552f7358SJed Brown           break;
3036552f7358SJed Brown         }
3037552f7358SJed Brown       }
3038552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3039552f7358SJed Brown     }
304082f516ccSBarry Smith     if (offsets[p*(height+2)+height+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(height+2)+height+1], closureSize);
3041552f7358SJed Brown   }
3042552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3043552f7358SJed Brown     PetscInt dof;
3044552f7358SJed Brown 
3045552f7358SJed Brown     /* Copy in cone of first point */
3046552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3047552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3048552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3049552f7358SJed Brown     }
3050552f7358SJed Brown     /* Check each successive cone */
3051552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3052552f7358SJed Brown       PetscInt newMeetSize = 0;
3053552f7358SJed Brown 
3054552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3055552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3056552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3057552f7358SJed Brown 
3058552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3059552f7358SJed Brown           if (point == meet[i][m]) {
3060552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3061552f7358SJed Brown             break;
3062552f7358SJed Brown           }
3063552f7358SJed Brown         }
3064552f7358SJed Brown       }
3065552f7358SJed Brown       meetSize = newMeetSize;
3066552f7358SJed Brown       i        = 1-i;
3067552f7358SJed Brown     }
3068552f7358SJed Brown     if (meetSize) break;
3069552f7358SJed Brown   }
3070552f7358SJed Brown   *numCoveredPoints = meetSize;
3071552f7358SJed Brown   *coveredPoints    = meet[i];
3072552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
30730298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3074552f7358SJed Brown   }
3075552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
307669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
307769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3078552f7358SJed Brown   PetscFunctionReturn(0);
3079552f7358SJed Brown }
3080552f7358SJed Brown 
30814e3744c5SMatthew G. Knepley /*@C
30824e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
30834e3744c5SMatthew G. Knepley 
30844e3744c5SMatthew G. Knepley   Not Collective
30854e3744c5SMatthew G. Knepley 
30864e3744c5SMatthew G. Knepley   Input Parameters:
30874e3744c5SMatthew G. Knepley + dmA - A DMPlex object
30884e3744c5SMatthew G. Knepley - dmB - A DMPlex object
30894e3744c5SMatthew G. Knepley 
30904e3744c5SMatthew G. Knepley   Output Parameters:
30914e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
30924e3744c5SMatthew G. Knepley 
30934e3744c5SMatthew G. Knepley   Level: intermediate
30944e3744c5SMatthew G. Knepley 
30954e3744c5SMatthew G. Knepley   Notes:
30964e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
30974e3744c5SMatthew G. Knepley 
30984e3744c5SMatthew G. Knepley .keywords: mesh
30994e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
31004e3744c5SMatthew G. Knepley @*/
31014e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
31024e3744c5SMatthew G. Knepley {
31034e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
31044e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
31054e3744c5SMatthew G. Knepley 
31064e3744c5SMatthew G. Knepley   PetscFunctionBegin;
31074e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
31084e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
31094e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
31104e3744c5SMatthew G. Knepley 
31114e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
31124e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
31134e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
31144e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
31154e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
31164e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
31174e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
31184e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
31194e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
31204e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
31214e3744c5SMatthew G. Knepley 
31224e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
31234e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
31244e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
31254e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
31264e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
31274e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
31284e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
31294e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
31304e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
31314e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
31324e3744c5SMatthew G. Knepley     }
31334e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
31344e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
31354e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
31364e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
31374e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
31384e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
31394e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
31404e3744c5SMatthew G. Knepley     }
31414e3744c5SMatthew G. Knepley   }
31424e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
31434e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
31444e3744c5SMatthew G. Knepley }
31454e3744c5SMatthew G. Knepley 
31467cd05799SMatthew G. Knepley /*@C
31477cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
31487cd05799SMatthew G. Knepley 
31497cd05799SMatthew G. Knepley   Not Collective
31507cd05799SMatthew G. Knepley 
31517cd05799SMatthew G. Knepley   Input Parameters:
31527cd05799SMatthew G. Knepley + dm         - The DMPlex
31537cd05799SMatthew G. Knepley . cellDim    - The cell dimension
31547cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
31557cd05799SMatthew G. Knepley 
31567cd05799SMatthew G. Knepley   Output Parameters:
31577cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
31587cd05799SMatthew G. Knepley 
31597cd05799SMatthew G. Knepley   Level: developer
31607cd05799SMatthew G. Knepley 
31617cd05799SMatthew G. Knepley   Notes:
31627cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
31637cd05799SMatthew G. Knepley 
31647cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
31657cd05799SMatthew G. Knepley @*/
316618ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3167a6dfd86eSKarl Rupp {
316882f516ccSBarry Smith   MPI_Comm       comm;
3169552f7358SJed Brown   PetscErrorCode ierr;
3170552f7358SJed Brown 
3171552f7358SJed Brown   PetscFunctionBegin;
317282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3173552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3174552f7358SJed Brown   switch (cellDim) {
3175552f7358SJed Brown   case 0:
3176552f7358SJed Brown     *numFaceVertices = 0;
3177552f7358SJed Brown     break;
3178552f7358SJed Brown   case 1:
3179552f7358SJed Brown     *numFaceVertices = 1;
3180552f7358SJed Brown     break;
3181552f7358SJed Brown   case 2:
3182552f7358SJed Brown     switch (numCorners) {
318319436ca2SJed Brown     case 3: /* triangle */
318419436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3185552f7358SJed Brown       break;
318619436ca2SJed Brown     case 4: /* quadrilateral */
318719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3188552f7358SJed Brown       break;
318919436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
319019436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3191552f7358SJed Brown       break;
319219436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
319319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3194552f7358SJed Brown       break;
3195552f7358SJed Brown     default:
3196f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3197552f7358SJed Brown     }
3198552f7358SJed Brown     break;
3199552f7358SJed Brown   case 3:
3200552f7358SJed Brown     switch (numCorners) {
320119436ca2SJed Brown     case 4: /* tetradehdron */
320219436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3203552f7358SJed Brown       break;
320419436ca2SJed Brown     case 6: /* tet cohesive cells */
320519436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3206552f7358SJed Brown       break;
320719436ca2SJed Brown     case 8: /* hexahedron */
320819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3209552f7358SJed Brown       break;
321019436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
321119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3212552f7358SJed Brown       break;
321319436ca2SJed Brown     case 10: /* quadratic tetrahedron */
321419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3215552f7358SJed Brown       break;
321619436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
321719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3218552f7358SJed Brown       break;
321919436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
322019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3221552f7358SJed Brown       break;
322219436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
322319436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3224552f7358SJed Brown       break;
3225552f7358SJed Brown     default:
3226f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3227552f7358SJed Brown     }
3228552f7358SJed Brown     break;
3229552f7358SJed Brown   default:
3230f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3231552f7358SJed Brown   }
3232552f7358SJed Brown   PetscFunctionReturn(0);
3233552f7358SJed Brown }
3234552f7358SJed Brown 
3235552f7358SJed Brown /*@
3236aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3237552f7358SJed Brown 
3238552f7358SJed Brown   Not Collective
3239552f7358SJed Brown 
3240aa50250dSMatthew G. Knepley   Input Parameter:
3241552f7358SJed Brown . dm    - The DMPlex object
3242552f7358SJed Brown 
3243aa50250dSMatthew G. Knepley   Output Parameter:
3244aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3245552f7358SJed Brown 
3246552f7358SJed Brown   Level: developer
3247552f7358SJed Brown 
3248aa50250dSMatthew G. Knepley .keywords: mesh, points
3249aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3250aa50250dSMatthew G. Knepley @*/
3251aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3252aa50250dSMatthew G. Knepley {
3253aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3254aa50250dSMatthew G. Knepley 
3255aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3256aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3257aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3258c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3259c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3260aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3261aa50250dSMatthew G. Knepley }
3262aa50250dSMatthew G. Knepley 
3263aa50250dSMatthew G. Knepley /*@
3264aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3265aa50250dSMatthew G. Knepley 
3266aa50250dSMatthew G. Knepley   Not Collective
3267aa50250dSMatthew G. Knepley 
3268aa50250dSMatthew G. Knepley   Input Parameter:
3269aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3270aa50250dSMatthew G. Knepley 
3271aa50250dSMatthew G. Knepley   Output Parameter:
3272aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3273aa50250dSMatthew G. Knepley 
3274aa50250dSMatthew G. Knepley   Level: developer
3275552f7358SJed Brown 
3276552f7358SJed Brown .keywords: mesh, points
3277aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3278552f7358SJed Brown @*/
3279552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3280552f7358SJed Brown {
3281aa50250dSMatthew G. Knepley   DMLabel        label;
3282aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3283552f7358SJed Brown   PetscErrorCode ierr;
3284552f7358SJed Brown 
3285552f7358SJed Brown   PetscFunctionBegin;
3286552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3287552f7358SJed Brown   PetscValidPointer(depth, 2);
3288aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3289aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3290552f7358SJed Brown   *depth = d-1;
3291552f7358SJed Brown   PetscFunctionReturn(0);
3292552f7358SJed Brown }
3293552f7358SJed Brown 
3294552f7358SJed Brown /*@
3295552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3296552f7358SJed Brown 
3297552f7358SJed Brown   Not Collective
3298552f7358SJed Brown 
3299552f7358SJed Brown   Input Parameters:
3300552f7358SJed Brown + dm           - The DMPlex object
3301552f7358SJed Brown - stratumValue - The requested depth
3302552f7358SJed Brown 
3303552f7358SJed Brown   Output Parameters:
3304552f7358SJed Brown + start - The first point at this depth
3305552f7358SJed Brown - end   - One beyond the last point at this depth
3306552f7358SJed Brown 
3307552f7358SJed Brown   Level: developer
3308552f7358SJed Brown 
3309552f7358SJed Brown .keywords: mesh, points
3310552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3311552f7358SJed Brown @*/
33120adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
33130adebc6cSBarry Smith {
3314aa50250dSMatthew G. Knepley   DMLabel        label;
331563d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3316552f7358SJed Brown   PetscErrorCode ierr;
3317552f7358SJed Brown 
3318552f7358SJed Brown   PetscFunctionBegin;
3319552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
332163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3322552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33230d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
332463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
332563d1a920SMatthew G. Knepley     if (start) *start = pStart;
332663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
332763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3328552f7358SJed Brown   }
3329aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
333063d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
333163d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3332552f7358SJed Brown   PetscFunctionReturn(0);
3333552f7358SJed Brown }
3334552f7358SJed Brown 
3335552f7358SJed Brown /*@
3336552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3337552f7358SJed Brown 
3338552f7358SJed Brown   Not Collective
3339552f7358SJed Brown 
3340552f7358SJed Brown   Input Parameters:
3341552f7358SJed Brown + dm           - The DMPlex object
3342552f7358SJed Brown - stratumValue - The requested height
3343552f7358SJed Brown 
3344552f7358SJed Brown   Output Parameters:
3345552f7358SJed Brown + start - The first point at this height
3346552f7358SJed Brown - end   - One beyond the last point at this height
3347552f7358SJed Brown 
3348552f7358SJed Brown   Level: developer
3349552f7358SJed Brown 
3350552f7358SJed Brown .keywords: mesh, points
3351552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3352552f7358SJed Brown @*/
33530adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
33540adebc6cSBarry Smith {
3355aa50250dSMatthew G. Knepley   DMLabel        label;
335663d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3357552f7358SJed Brown   PetscErrorCode ierr;
3358552f7358SJed Brown 
3359552f7358SJed Brown   PetscFunctionBegin;
3360552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
336163d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
336263d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3363552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33640d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
336563d1a920SMatthew G. Knepley   if (stratumValue < 0) {
336663d1a920SMatthew G. Knepley     if (start) *start = pStart;
336763d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
336863d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3369552f7358SJed Brown   }
3370aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
337113903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
337263d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
337363d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3374552f7358SJed Brown   PetscFunctionReturn(0);
3375552f7358SJed Brown }
3376552f7358SJed Brown 
3377552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
33787cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3379a6dfd86eSKarl Rupp {
3380ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3381470f9322SMatthew G. Knepley   PetscInt       depth, cellHeight, pStart = 0, pEnd = 0;
3382ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3383fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3384552f7358SJed Brown   PetscErrorCode ierr;
3385552f7358SJed Brown 
3386552f7358SJed Brown   PetscFunctionBegin;
3387fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3388ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3389fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3390fa716be8SMatthew G. Knepley     PetscObject  obj;
3391fa716be8SMatthew G. Knepley     PetscClassId id;
3392fa716be8SMatthew G. Knepley 
3393ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3394ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3395fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3396fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3397fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3398fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3399552f7358SJed Brown   }
340082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3401552f7358SJed Brown   if (numFields > 0) {
3402552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3403552f7358SJed Brown     if (numComp) {
3404552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3405552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3406d484f18aSToby Isaac         if (isFE[f]) {
3407d484f18aSToby Isaac           PetscFE           fe;
3408d484f18aSToby Isaac           PetscDualSpace    dspace;
3409d484f18aSToby Isaac           const PetscInt    ***perms;
3410d484f18aSToby Isaac           const PetscScalar ***flips;
3411d484f18aSToby Isaac           const PetscInt    *numDof;
3412d484f18aSToby Isaac 
3413d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3414d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3415d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3416d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3417d484f18aSToby Isaac           if (perms || flips) {
3418d484f18aSToby Isaac             DM               K;
3419d484f18aSToby Isaac             DMLabel          depthLabel;
3420d484f18aSToby Isaac             PetscInt         depth, h;
3421d484f18aSToby Isaac             PetscSectionSym  sym;
3422d484f18aSToby Isaac 
3423d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3424d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3425d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3426d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3427d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3428d484f18aSToby Isaac               PetscDualSpace    hspace;
3429d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3430d484f18aSToby Isaac               PetscInt          kConeSize;
3431d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3432d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3433d484f18aSToby Isaac 
3434d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3435d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3436d484f18aSToby Isaac               if (!hspace) continue;
3437d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3438d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3439d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3440d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3441d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3442d484f18aSToby Isaac               ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3443d484f18aSToby Isaac             }
3444d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3445d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3446d484f18aSToby Isaac           }
3447d484f18aSToby Isaac         }
3448552f7358SJed Brown       }
3449552f7358SJed Brown     }
3450552f7358SJed Brown   }
3451552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3452552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3453916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
34549ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
34559ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
3456470f9322SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
3457470f9322SMatthew G. Knepley   for (dep = 0; dep <= depth - cellHeight; ++dep) {
3458916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3459916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3460fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3461552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3462ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3463ee55978aSMatthew G. Knepley 
3464552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3465fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3466552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3467ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3468552f7358SJed Brown       }
3469ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3470552f7358SJed Brown     }
3471552f7358SJed Brown   }
34729ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3473fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3474552f7358SJed Brown   PetscFunctionReturn(0);
3475552f7358SJed Brown }
3476552f7358SJed Brown 
3477552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3478ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3479552f7358SJed Brown */
34807cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3481a6dfd86eSKarl Rupp {
3482552f7358SJed Brown   PetscInt       numFields;
3483552f7358SJed Brown   PetscInt       bc;
3484a68b90caSToby Isaac   PetscSection   aSec;
3485552f7358SJed Brown   PetscErrorCode ierr;
3486552f7358SJed Brown 
3487552f7358SJed Brown   PetscFunctionBegin;
3488552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3489552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3490552f7358SJed Brown     PetscInt        field = 0;
3491ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3492552f7358SJed Brown     const PetscInt *idx;
3493ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3494552f7358SJed Brown 
34950d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3496ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3497ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3498552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3499552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3500552f7358SJed Brown     for (i = 0; i < n; ++i) {
3501552f7358SJed Brown       const PetscInt p = idx[i];
350206ff1081SMatthew G. Knepley       PetscInt       numConst;
3503552f7358SJed Brown 
3504552f7358SJed Brown       if (numFields) {
3505552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3506552f7358SJed Brown       } else {
3507552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3508552f7358SJed Brown       }
350906ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
3510*e8ecbf3fSStefano Zampini       /* TODO: Matt, this only works if there is one node on the point.  We need to handle numDofs > NumComponents */
351106ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
351206ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3513552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3514552f7358SJed Brown     }
3515552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
351606ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3517552f7358SJed Brown   }
3518a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3519a68b90caSToby Isaac   if (aSec) {
3520a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3521a68b90caSToby Isaac 
3522a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3523a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3524ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3525a68b90caSToby Isaac 
3526a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3527a68b90caSToby Isaac       if (dof) {
3528a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3529a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3530a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3531a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3532a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3533a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3534a68b90caSToby Isaac         }
3535a68b90caSToby Isaac       }
3536a68b90caSToby Isaac     }
3537a68b90caSToby Isaac   }
3538552f7358SJed Brown   PetscFunctionReturn(0);
3539552f7358SJed Brown }
3540552f7358SJed Brown 
3541ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3542ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3543ab1d0545SMatthew G. Knepley */
35447cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
35450adebc6cSBarry Smith {
3546ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3547ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3548503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3549552f7358SJed Brown   PetscErrorCode ierr;
3550552f7358SJed Brown 
3551552f7358SJed Brown   PetscFunctionBegin;
3552552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3553ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3554ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3555552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3556503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3557ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3558ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3559ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3560ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3561ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3562ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3563ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3564ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3565552f7358SJed Brown 
3566ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3567ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3568ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3569ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3570ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3571ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3572ab1d0545SMatthew G. Knepley       const PetscInt *find;
3573503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3574ab1d0545SMatthew G. Knepley 
3575503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3576503335f2SSander Arens       if (!fdof) continue;
3577ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3578503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3579503335f2SSander Arens         fcdof = fdof;
3580552f7358SJed Brown       } else {
3581503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3582ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3583ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3584503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3585503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3586503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3587503335f2SSander Arens         fcdof = d;
3588552f7358SJed Brown       }
3589503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3590ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3591552f7358SJed Brown     }
3592ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3593ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3594552f7358SJed Brown   }
3595ab1d0545SMatthew G. Knepley   /* Handle anchors */
3596ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3597ab1d0545SMatthew G. Knepley   if (aSec) {
3598ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3599552f7358SJed Brown 
3600ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3601ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3602ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3603503335f2SSander Arens       PetscInt dof, f;
3604ab1d0545SMatthew G. Knepley 
3605ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3606ab1d0545SMatthew G. Knepley       if (dof) {
3607ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3608ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3609ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3610ab1d0545SMatthew G. Knepley         }
3611ab1d0545SMatthew G. Knepley       }
3612ab1d0545SMatthew G. Knepley     }
3613ab1d0545SMatthew G. Knepley   }
3614ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3615ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3616ab1d0545SMatthew G. Knepley }
3617ab1d0545SMatthew G. Knepley 
3618ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
36197cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3620ab1d0545SMatthew G. Knepley {
3621ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3622ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3623ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3624ab1d0545SMatthew G. Knepley 
3625ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3626ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3627ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3628ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3629ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3630ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3631552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3632552f7358SJed Brown     PetscInt cdof, d;
3633552f7358SJed Brown 
3634552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3635552f7358SJed Brown     if (cdof) {
3636552f7358SJed Brown       if (numFields) {
3637552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3638552f7358SJed Brown 
3639552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3640ab1d0545SMatthew G. Knepley           const PetscInt *find;
3641ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3642552f7358SJed Brown 
3643552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3644ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3645ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3646ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3647ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3648ab1d0545SMatthew G. Knepley           numConst += fcdof;
3649552f7358SJed Brown           foff     += fdof;
3650552f7358SJed Brown         }
3651503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3652552f7358SJed Brown       } else {
36530d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3654552f7358SJed Brown       }
3655552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3656552f7358SJed Brown     }
3657552f7358SJed Brown   }
3658552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3659552f7358SJed Brown   PetscFunctionReturn(0);
3660552f7358SJed Brown }
3661552f7358SJed Brown 
3662552f7358SJed Brown /*@C
3663552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3664552f7358SJed Brown 
3665552f7358SJed Brown   Not Collective
3666552f7358SJed Brown 
3667552f7358SJed Brown   Input Parameters:
3668552f7358SJed Brown + dm        - The DMPlex object
3669552f7358SJed Brown . dim       - The spatial dimension of the problem
3670552f7358SJed Brown . numFields - The number of fields in the problem
3671552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3672552f7358SJed Brown . numDof    - An array of size numFields*(dim+1) which holds the number of dof for each field on a mesh piece of dimension d
3673552f7358SJed Brown . numBC     - The number of boundary conditions
3674552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3675ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3676ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3677f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3678552f7358SJed Brown 
3679552f7358SJed Brown   Output Parameter:
3680552f7358SJed Brown . section - The PetscSection object
3681552f7358SJed Brown 
368295452b02SPatrick Sanan   Notes:
368395452b02SPatrick Sanan     numDof[f*(dim+1)+d] gives the number of dof for field f on points of dimension d. For instance, numDof[1] is the
3684f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3685f8f126e8SMatthew G. Knepley 
3686f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3687552f7358SJed Brown 
3688552f7358SJed Brown   Level: developer
3689552f7358SJed Brown 
36903813dfbdSMatthew G Knepley   Fortran Notes:
36913813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
36923813dfbdSMatthew G Knepley 
3693552f7358SJed Brown .keywords: mesh, elements
3694f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3695552f7358SJed Brown @*/
3696ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], IS perm, PetscSection *section)
3697a6dfd86eSKarl Rupp {
3698a68b90caSToby Isaac   PetscSection   aSec;
3699552f7358SJed Brown   PetscErrorCode ierr;
3700552f7358SJed Brown 
3701552f7358SJed Brown   PetscFunctionBegin;
3702552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3703ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3704f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
37055e5b47fcSMatthew G. Knepley   ierr = PetscSectionSetFromOptions(*section);CHKERRQ(ierr);
3706552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3707a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3708ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3709ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3710ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3711ab1d0545SMatthew G. Knepley   }
3712ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3713552f7358SJed Brown   PetscFunctionReturn(0);
3714552f7358SJed Brown }
3715552f7358SJed Brown 
37160adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
37170adebc6cSBarry Smith {
3718efe440bfSMatthew G. Knepley   PetscSection   section, s;
3719efe440bfSMatthew G. Knepley   Mat            m;
37203e922f36SToby Isaac   PetscInt       maxHeight;
3721552f7358SJed Brown   PetscErrorCode ierr;
3722552f7358SJed Brown 
3723552f7358SJed Brown   PetscFunctionBegin;
372438221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
37253e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
37263e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
372782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3728e87a4003SBarry Smith   ierr = DMSetSection(*cdm, section);CHKERRQ(ierr);
37291d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3730efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3731efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3732efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3733efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3734efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3735552f7358SJed Brown   PetscFunctionReturn(0);
3736552f7358SJed Brown }
3737552f7358SJed Brown 
3738f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3739f19dbd58SToby Isaac {
3740f19dbd58SToby Isaac   Vec            coordsLocal;
3741f19dbd58SToby Isaac   DM             coordsDM;
3742f19dbd58SToby Isaac   PetscErrorCode ierr;
3743f19dbd58SToby Isaac 
3744f19dbd58SToby Isaac   PetscFunctionBegin;
3745f19dbd58SToby Isaac   *field = NULL;
3746f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3747f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3748f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3749f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3750f19dbd58SToby Isaac   }
3751f19dbd58SToby Isaac   PetscFunctionReturn(0);
3752f19dbd58SToby Isaac }
3753f19dbd58SToby Isaac 
37547cd05799SMatthew G. Knepley /*@C
37557cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
37567cd05799SMatthew G. Knepley 
37577cd05799SMatthew G. Knepley   Not Collective
37587cd05799SMatthew G. Knepley 
37597cd05799SMatthew G. Knepley   Input Parameters:
37607cd05799SMatthew G. Knepley . dm        - The DMPlex object
37617cd05799SMatthew G. Knepley 
37627cd05799SMatthew G. Knepley   Output Parameter:
37637cd05799SMatthew G. Knepley . section - The PetscSection object
37647cd05799SMatthew G. Knepley 
37657cd05799SMatthew G. Knepley   Level: developer
37667cd05799SMatthew G. Knepley 
37677cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
37687cd05799SMatthew G. Knepley @*/
37690adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
37700adebc6cSBarry Smith {
3771552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3772552f7358SJed Brown 
3773552f7358SJed Brown   PetscFunctionBegin;
3774552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3775552f7358SJed Brown   if (section) *section = mesh->coneSection;
3776552f7358SJed Brown   PetscFunctionReturn(0);
3777552f7358SJed Brown }
3778552f7358SJed Brown 
37797cd05799SMatthew G. Knepley /*@C
37807cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
37817cd05799SMatthew G. Knepley 
37827cd05799SMatthew G. Knepley   Not Collective
37837cd05799SMatthew G. Knepley 
37847cd05799SMatthew G. Knepley   Input Parameters:
37857cd05799SMatthew G. Knepley . dm        - The DMPlex object
37867cd05799SMatthew G. Knepley 
37877cd05799SMatthew G. Knepley   Output Parameter:
37887cd05799SMatthew G. Knepley . section - The PetscSection object
37897cd05799SMatthew G. Knepley 
37907cd05799SMatthew G. Knepley   Level: developer
37917cd05799SMatthew G. Knepley 
37927cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
37937cd05799SMatthew G. Knepley @*/
37948cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
37958cb4d582SMatthew G. Knepley {
37968cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
37978cb4d582SMatthew G. Knepley 
37988cb4d582SMatthew G. Knepley   PetscFunctionBegin;
37998cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38008cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
38018cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
38028cb4d582SMatthew G. Knepley }
38038cb4d582SMatthew G. Knepley 
38047cd05799SMatthew G. Knepley /*@C
38057cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
38067cd05799SMatthew G. Knepley 
38077cd05799SMatthew G. Knepley   Not Collective
38087cd05799SMatthew G. Knepley 
38097cd05799SMatthew G. Knepley   Input Parameters:
38107cd05799SMatthew G. Knepley . dm        - The DMPlex object
38117cd05799SMatthew G. Knepley 
38127cd05799SMatthew G. Knepley   Output Parameter:
38137cd05799SMatthew G. Knepley . cones - The cone for each point
38147cd05799SMatthew G. Knepley 
38157cd05799SMatthew G. Knepley   Level: developer
38167cd05799SMatthew G. Knepley 
38177cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
38187cd05799SMatthew G. Knepley @*/
3819a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3820a6dfd86eSKarl Rupp {
3821552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3822552f7358SJed Brown 
3823552f7358SJed Brown   PetscFunctionBegin;
3824552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3825552f7358SJed Brown   if (cones) *cones = mesh->cones;
3826552f7358SJed Brown   PetscFunctionReturn(0);
3827552f7358SJed Brown }
3828552f7358SJed Brown 
38297cd05799SMatthew G. Knepley /*@C
38307cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
38317cd05799SMatthew G. Knepley 
38327cd05799SMatthew G. Knepley   Not Collective
38337cd05799SMatthew G. Knepley 
38347cd05799SMatthew G. Knepley   Input Parameters:
38357cd05799SMatthew G. Knepley . dm        - The DMPlex object
38367cd05799SMatthew G. Knepley 
38377cd05799SMatthew G. Knepley   Output Parameter:
38387cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
38397cd05799SMatthew G. Knepley 
38407cd05799SMatthew G. Knepley   Level: developer
38417cd05799SMatthew G. Knepley 
38427cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
38437cd05799SMatthew G. Knepley @*/
3844a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3845a6dfd86eSKarl Rupp {
3846552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3847552f7358SJed Brown 
3848552f7358SJed Brown   PetscFunctionBegin;
3849552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3850552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3851552f7358SJed Brown   PetscFunctionReturn(0);
3852552f7358SJed Brown }
3853552f7358SJed Brown 
3854552f7358SJed Brown /******************************** FEM Support **********************************/
3855552f7358SJed Brown 
38567391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
38573194fc30SMatthew G. Knepley {
38587391a63aSMatthew G. Knepley   DMLabel        label;
38593194fc30SMatthew G. Knepley   PetscInt      *perm;
38607391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
38613194fc30SMatthew G. Knepley   PetscErrorCode ierr;
38623194fc30SMatthew G. Knepley 
38633194fc30SMatthew G. Knepley   PetscFunctionBegin;
38647391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
38653194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
38667391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
38677391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
38687391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
38697391a63aSMatthew G. Knepley   else if  (depth == dim) {
38707391a63aSMatthew G. Knepley     const PetscInt *cone;
38717391a63aSMatthew G. Knepley 
38727391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3873d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
3874d4e6627bSStefano Zampini     else if (dim == 3) {
3875d4e6627bSStefano Zampini       const PetscInt *cone2;
3876d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
3877d4e6627bSStefano Zampini       eStart = cone2[0];
3878d4e6627bSStefano Zampini     } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3879d4e6627bSStefano Zampini   } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3880e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
38813194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
388289eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
38833194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
38843194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
38853194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
38863194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
38873194fc30SMatthew G. Knepley     k = k/Nc + 1;
388889eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
38893194fc30SMatthew G. Knepley   }
38903194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
38913194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
389289eabcffSMatthew G. Knepley     switch (dim) {
389389eabcffSMatthew G. Knepley     case 2:
38943194fc30SMatthew 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} */
38953194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
38963194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
38973194fc30SMatthew G. Knepley       k = k/Nc + 1;
38983194fc30SMatthew G. Knepley       /* The SEM order is
38993194fc30SMatthew G. Knepley 
39003194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
390189eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
39023194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
39033194fc30SMatthew G. Knepley       */
39043194fc30SMatthew G. Knepley       {
39053194fc30SMatthew G. Knepley         const PetscInt of   = 0;
39063194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
39073194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
39083194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
39093194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
39103194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
39113194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
39123194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
39133194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
39143194fc30SMatthew G. Knepley         PetscInt       o;
39153194fc30SMatthew G. Knepley 
39163194fc30SMatthew G. Knepley         /* bottom */
39173194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
39183194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
39193194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
39203194fc30SMatthew G. Knepley         /* middle */
39213194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
39223194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
39233194fc30SMatthew 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;
39243194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
39253194fc30SMatthew G. Knepley         }
39263194fc30SMatthew G. Knepley         /* top */
39273194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
39283194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
39293194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
39303194fc30SMatthew G. Knepley         foffset = offset;
39313194fc30SMatthew G. Knepley       }
393289eabcffSMatthew G. Knepley       break;
393389eabcffSMatthew G. Knepley     case 3:
393489eabcffSMatthew G. Knepley       /* The original hex closure is
393589eabcffSMatthew G. Knepley 
393689eabcffSMatthew G. Knepley          {c,
393789eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
393889eabcffSMatthew 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,
393989eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
394089eabcffSMatthew G. Knepley       */
394189eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
394289eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
394389eabcffSMatthew G. Knepley       k = k/Nc + 1;
394489eabcffSMatthew G. Knepley       /* The SEM order is
394589eabcffSMatthew G. Knepley          Bottom Slice
394689eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
394789eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
394889eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
394989eabcffSMatthew G. Knepley 
395089eabcffSMatthew G. Knepley          Middle Slice (j)
395189eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
395289eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
395389eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
395489eabcffSMatthew G. Knepley 
395589eabcffSMatthew G. Knepley          Top Slice
395689eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
395789eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
395889eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
395989eabcffSMatthew G. Knepley       */
396089eabcffSMatthew G. Knepley       {
396189eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
396289eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
396389eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
396489eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
396589eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
396689eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
396789eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
396889eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
396989eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
397089eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
397189eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
397289eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
397389eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
397489eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
397589eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
397689eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
397789eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
397889eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
397989eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
398089eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
398189eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
398289eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
398389eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
398489eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
398589eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
398689eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
398789eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
398889eabcffSMatthew G. Knepley         PetscInt       o, n;
398989eabcffSMatthew G. Knepley 
399089eabcffSMatthew G. Knepley         /* Bottom Slice */
399189eabcffSMatthew G. Knepley         /*   bottom */
399289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
399389eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
399489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
399589eabcffSMatthew G. Knepley         /*   middle */
399689eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
399789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3998316b7f87SMax 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;}
399989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
40003194fc30SMatthew G. Knepley         }
400189eabcffSMatthew G. Knepley         /*   top */
400289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
400389eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
400489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
400589eabcffSMatthew G. Knepley 
400689eabcffSMatthew G. Knepley         /* Middle Slice */
400789eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
400889eabcffSMatthew G. Knepley           /*   bottom */
400989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
401089eabcffSMatthew 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;
401189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
401289eabcffSMatthew G. Knepley           /*   middle */
401389eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
401489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
401589eabcffSMatthew 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;
401689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
401789eabcffSMatthew G. Knepley           }
401889eabcffSMatthew G. Knepley           /*   top */
401989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
402089eabcffSMatthew 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;
402189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
402289eabcffSMatthew G. Knepley         }
402389eabcffSMatthew G. Knepley 
402489eabcffSMatthew G. Knepley         /* Top Slice */
402589eabcffSMatthew G. Knepley         /*   bottom */
402689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
402789eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
402889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
402989eabcffSMatthew G. Knepley         /*   middle */
403089eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
403189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
403289eabcffSMatthew 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;
403389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
403489eabcffSMatthew G. Knepley         }
403589eabcffSMatthew G. Knepley         /*   top */
403689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
403789eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
403889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
403989eabcffSMatthew G. Knepley 
404089eabcffSMatthew G. Knepley         foffset = offset;
404189eabcffSMatthew G. Knepley       }
404289eabcffSMatthew G. Knepley       break;
404389eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
404489eabcffSMatthew G. Knepley     }
404589eabcffSMatthew G. Knepley   }
404689eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
40473194fc30SMatthew G. Knepley   /* Check permutation */
40483194fc30SMatthew G. Knepley   {
40493194fc30SMatthew G. Knepley     PetscInt *check;
40503194fc30SMatthew G. Knepley 
40513194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
40523194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {check[i] = -1; if (perm[i] < 0 || perm[i] >= size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid permutation index p[%D] = %D", i, perm[i]);}
40533194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
40543194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
40553194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
40563194fc30SMatthew G. Knepley   }
40571fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
40583194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
40593194fc30SMatthew G. Knepley }
40603194fc30SMatthew G. Knepley 
4061e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4062e071409bSToby Isaac {
4063e071409bSToby Isaac   PetscDS        prob;
4064e071409bSToby Isaac   PetscInt       depth, Nf, h;
4065e071409bSToby Isaac   DMLabel        label;
4066e071409bSToby Isaac   PetscErrorCode ierr;
4067e071409bSToby Isaac 
4068e071409bSToby Isaac   PetscFunctionBeginHot;
4069e071409bSToby Isaac   prob    = dm->prob;
4070e071409bSToby Isaac   Nf      = prob->Nf;
4071e071409bSToby Isaac   label   = dm->depthLabel;
4072e071409bSToby Isaac   *dspace = NULL;
4073e071409bSToby Isaac   if (field < Nf) {
4074e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4075e071409bSToby Isaac 
4076e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4077e071409bSToby Isaac       PetscDualSpace dsp;
4078e071409bSToby Isaac 
4079e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4080e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4081e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4082e071409bSToby Isaac       h    = depth - 1 - h;
4083e071409bSToby Isaac       if (h) {
4084e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4085e071409bSToby Isaac       } else {
4086e071409bSToby Isaac         *dspace = dsp;
4087e071409bSToby Isaac       }
4088e071409bSToby Isaac     }
4089e071409bSToby Isaac   }
4090e071409bSToby Isaac   PetscFunctionReturn(0);
4091e071409bSToby Isaac }
4092e071409bSToby Isaac 
4093e071409bSToby Isaac 
40941a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4095a6dfd86eSKarl Rupp {
4096552f7358SJed Brown   PetscScalar    *array, *vArray;
4097d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
40981a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4099552f7358SJed Brown   PetscErrorCode  ierr;
4100552f7358SJed Brown 
41011b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
41022a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
41035a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
41045a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
41055a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
41063f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
41079df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
41089df71ca4SMatthew G. Knepley       PetscInt dof;
4109d9917b9dSMatthew G. Knepley 
41109df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
41119df71ca4SMatthew G. Knepley       size += dof;
41129df71ca4SMatthew G. Knepley     }
41139df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
41149df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
41152a3aaacfSMatthew G. Knepley       PetscInt       dof;
41165a1bb5cfSMatthew G. Knepley 
41175a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
41182a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
41195a1bb5cfSMatthew G. Knepley       size += dof;
41205a1bb5cfSMatthew G. Knepley     }
41213f7cbbe7SMatthew G. Knepley     if (!values) {
41223f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
41233f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
41243f7cbbe7SMatthew G. Knepley     }
412569291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4126982e9ed1SMatthew G. Knepley   } else {
4127982e9ed1SMatthew G. Knepley     array = *values;
4128982e9ed1SMatthew G. Knepley   }
41299df71ca4SMatthew G. Knepley   size = 0;
41305a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
41319df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
41329df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
41339df71ca4SMatthew G. Knepley     PetscScalar *varr;
4134d9917b9dSMatthew G. Knepley 
41359df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
41369df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
41379df71ca4SMatthew G. Knepley     varr = &vArray[off];
41381a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
41391a271a75SMatthew G. Knepley       array[offset] = varr[d];
41409df71ca4SMatthew G. Knepley     }
41419df71ca4SMatthew G. Knepley     size += dof;
41429df71ca4SMatthew G. Knepley   }
41439df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
41449df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
41459df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
41465a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
41475a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
41485a1bb5cfSMatthew G. Knepley 
414952ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
41505a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
41515a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
41525a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
41535a1bb5cfSMatthew G. Knepley     if (o >= 0) {
41541a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
41551a271a75SMatthew G. Knepley         array[offset] = varr[d];
41565a1bb5cfSMatthew G. Knepley       }
41575a1bb5cfSMatthew G. Knepley     } else {
41581a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
41591a271a75SMatthew G. Knepley         array[offset] = varr[d];
41605a1bb5cfSMatthew G. Knepley       }
41615a1bb5cfSMatthew G. Knepley     }
41629df71ca4SMatthew G. Knepley     size += dof;
41635a1bb5cfSMatthew G. Knepley   }
41645a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
41659df71ca4SMatthew G. Knepley   if (!*values) {
41665a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
41675a1bb5cfSMatthew G. Knepley     *values = array;
41689df71ca4SMatthew G. Knepley   } else {
41698ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
41708c312ff3SMatthew G. Knepley     *csize = size;
41719df71ca4SMatthew G. Knepley   }
41725a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
41735a1bb5cfSMatthew G. Knepley }
4174d9917b9dSMatthew G. Knepley 
4175923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4176923c78e0SToby Isaac {
4177923c78e0SToby Isaac   const PetscInt *cla;
4178923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4179923c78e0SToby Isaac   PetscErrorCode ierr;
4180923c78e0SToby Isaac 
4181923c78e0SToby Isaac   PetscFunctionBeginHot;
4182923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
4183923c78e0SToby Isaac   if (!*clPoints) {
4184923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
4185923c78e0SToby Isaac 
4186923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4187923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
4188923c78e0SToby Isaac     /* Compress out points not in the section */
4189923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
4190923c78e0SToby Isaac       PetscInt r = pts[2*p];
4191923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
4192923c78e0SToby Isaac         pts[q*2]   = r;
4193923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
4194923c78e0SToby Isaac         ++q;
4195923c78e0SToby Isaac       }
4196923c78e0SToby Isaac     }
4197923c78e0SToby Isaac     np = q;
4198923c78e0SToby Isaac     cla = NULL;
4199923c78e0SToby Isaac   } else {
4200923c78e0SToby Isaac     PetscInt dof, off;
4201923c78e0SToby Isaac 
4202923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4203923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4204923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4205923c78e0SToby Isaac     np   = dof/2;
4206923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4207923c78e0SToby Isaac   }
4208923c78e0SToby Isaac   *numPoints = np;
4209923c78e0SToby Isaac   *points    = pts;
4210923c78e0SToby Isaac   *clp       = cla;
4211923c78e0SToby Isaac 
4212923c78e0SToby Isaac   PetscFunctionReturn(0);
4213923c78e0SToby Isaac }
4214923c78e0SToby Isaac 
4215923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4216923c78e0SToby Isaac {
4217923c78e0SToby Isaac   PetscErrorCode ierr;
4218923c78e0SToby Isaac 
4219923c78e0SToby Isaac   PetscFunctionBeginHot;
4220923c78e0SToby Isaac   if (!*clPoints) {
4221923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4222923c78e0SToby Isaac   } else {
4223923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4224923c78e0SToby Isaac   }
4225923c78e0SToby Isaac   *numPoints = 0;
4226923c78e0SToby Isaac   *points    = NULL;
4227923c78e0SToby Isaac   *clSec     = NULL;
4228923c78e0SToby Isaac   *clPoints  = NULL;
4229923c78e0SToby Isaac   *clp       = NULL;
4230923c78e0SToby Isaac   PetscFunctionReturn(0);
4231923c78e0SToby Isaac }
4232923c78e0SToby Isaac 
423397e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
42341a271a75SMatthew G. Knepley {
42351a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
423697e99dd9SToby Isaac   const PetscInt    **perms = NULL;
423797e99dd9SToby Isaac   const PetscScalar **flips = NULL;
42381a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
42391a271a75SMatthew G. Knepley 
42401a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4241fe02ba77SJed Brown   *size = 0;
424297e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
424397e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
424497e99dd9SToby Isaac     const PetscInt    point = points[2*p];
424597e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
424697e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
42471a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
42481a271a75SMatthew G. Knepley     const PetscScalar *varr;
42491a271a75SMatthew G. Knepley 
42501a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
42511a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
42521a271a75SMatthew G. Knepley     varr = &vArray[off];
425397e99dd9SToby Isaac     if (clperm) {
425497e99dd9SToby Isaac       if (perm) {
425597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
42561a271a75SMatthew G. Knepley       } else {
425797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
425897e99dd9SToby Isaac       }
425997e99dd9SToby Isaac       if (flip) {
426097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
426197e99dd9SToby Isaac       }
426297e99dd9SToby Isaac     } else {
426397e99dd9SToby Isaac       if (perm) {
426497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
426597e99dd9SToby Isaac       } else {
426697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
426797e99dd9SToby Isaac       }
426897e99dd9SToby Isaac       if (flip) {
426997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
42701a271a75SMatthew G. Knepley       }
42711a271a75SMatthew G. Knepley     }
427297e99dd9SToby Isaac     offset += dof;
427397e99dd9SToby Isaac   }
427497e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
42751a271a75SMatthew G. Knepley   *size = offset;
42761a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
42771a271a75SMatthew G. Knepley }
42781a271a75SMatthew G. Knepley 
427997e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
42801a271a75SMatthew G. Knepley {
42811a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
42821a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
42831a271a75SMatthew G. Knepley 
42841a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4285fe02ba77SJed Brown   *size = 0;
42861a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
428797e99dd9SToby Isaac     PetscInt          p;
428897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
428997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
42901a271a75SMatthew G. Knepley 
429197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
429297e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
429397e99dd9SToby Isaac       const PetscInt    point = points[2*p];
429497e99dd9SToby Isaac       PetscInt          fdof, foff, b;
42951a271a75SMatthew G. Knepley       const PetscScalar *varr;
429697e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
429797e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
42981a271a75SMatthew G. Knepley 
42991a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
43001a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43011a271a75SMatthew G. Knepley       varr = &vArray[foff];
430297e99dd9SToby Isaac       if (clperm) {
430397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
430497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
430597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
43061a271a75SMatthew G. Knepley       } else {
430797e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
430897e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
430997e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
43101a271a75SMatthew G. Knepley       }
431197e99dd9SToby Isaac       offset += fdof;
43121a271a75SMatthew G. Knepley     }
431397e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43141a271a75SMatthew G. Knepley   }
43151a271a75SMatthew G. Knepley   *size = offset;
43161a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
43171a271a75SMatthew G. Knepley }
43181a271a75SMatthew G. Knepley 
4319552f7358SJed Brown /*@C
4320552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4321552f7358SJed Brown 
4322552f7358SJed Brown   Not collective
4323552f7358SJed Brown 
4324552f7358SJed Brown   Input Parameters:
4325552f7358SJed Brown + dm - The DM
4326552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4327552f7358SJed Brown . v - The local vector
432822c1ee49SMatthew G. Knepley . point - The point in the DM
432922c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
433022c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4331552f7358SJed Brown 
4332552f7358SJed Brown   Output Parameters:
433322c1ee49SMatthew G. Knepley + csize - The number of values in the closure
433422c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
433522c1ee49SMatthew G. Knepley 
433622c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
433722c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
433822c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
433922c1ee49SMatthew G. Knepley $
434022c1ee49SMatthew G. Knepley $ A typical use could be
434122c1ee49SMatthew G. Knepley $
434222c1ee49SMatthew G. Knepley $  values = NULL;
434322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
434422c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
434522c1ee49SMatthew G. Knepley $    <Compute on closure>
434622c1ee49SMatthew G. Knepley $  }
434722c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
434822c1ee49SMatthew G. Knepley $
434922c1ee49SMatthew G. Knepley $ or
435022c1ee49SMatthew G. Knepley $
435122c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
435222c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
435322c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
435422c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
435522c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
435622c1ee49SMatthew G. Knepley $      <Compute on closure>
435722c1ee49SMatthew G. Knepley $    }
435822c1ee49SMatthew G. Knepley $  }
435922c1ee49SMatthew G. Knepley $  PetscFree(values);
4360552f7358SJed Brown 
4361552f7358SJed Brown   Fortran Notes:
4362552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4363552f7358SJed Brown   include petsc.h90 in your code.
4364552f7358SJed Brown 
4365552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4366552f7358SJed Brown 
4367552f7358SJed Brown   Level: intermediate
4368552f7358SJed Brown 
4369552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4370552f7358SJed Brown @*/
4371552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4372552f7358SJed Brown {
4373552f7358SJed Brown   PetscSection       clSection;
4374d9917b9dSMatthew G. Knepley   IS                 clPoints;
43758a84db2dSMatthew G. Knepley   PetscScalar       *array;
43768a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4377552f7358SJed Brown   PetscInt          *points = NULL;
43788f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
43791a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4380552f7358SJed Brown   PetscErrorCode     ierr;
4381552f7358SJed Brown 
4382d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4383552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4384e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
43851a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43861a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4387552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4388552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4389552f7358SJed Brown   if (depth == 1 && numFields < 2) {
43901a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4391552f7358SJed Brown     PetscFunctionReturn(0);
4392552f7358SJed Brown   }
43931a271a75SMatthew G. Knepley   /* Get points */
4394923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43951fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
43961a271a75SMatthew G. Knepley   /* Get array */
4397bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
43981a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4399552f7358SJed Brown 
44001a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4401552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
44021a271a75SMatthew G. Knepley       asize += dof;
4403552f7358SJed Brown     }
4404bd6c0d32SMatthew G. Knepley     if (!values) {
4405923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44061a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4407bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4408bd6c0d32SMatthew G. Knepley     }
440969291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4410d0f6b257SMatthew G. Knepley   } else {
4411d0f6b257SMatthew G. Knepley     array = *values;
4412d0f6b257SMatthew G. Knepley   }
44138a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
44141a271a75SMatthew G. Knepley   /* Get values */
441597e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
441697e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
44171a271a75SMatthew G. Knepley   /* Cleanup points */
4418923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44191a271a75SMatthew G. Knepley   /* Cleanup array */
44208a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4421d0f6b257SMatthew G. Knepley   if (!*values) {
4422552f7358SJed Brown     if (csize) *csize = size;
4423552f7358SJed Brown     *values = array;
4424d0f6b257SMatthew G. Knepley   } else {
4425f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4426d0f6b257SMatthew G. Knepley     *csize = size;
4427d0f6b257SMatthew G. Knepley   }
4428552f7358SJed Brown   PetscFunctionReturn(0);
4429552f7358SJed Brown }
4430552f7358SJed Brown 
4431552f7358SJed Brown /*@C
4432552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4433552f7358SJed Brown 
4434552f7358SJed Brown   Not collective
4435552f7358SJed Brown 
4436552f7358SJed Brown   Input Parameters:
4437552f7358SJed Brown + dm - The DM
44380298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4439552f7358SJed Brown . v - The local vector
4440eaf898f9SPatrick Sanan . point - The point in the DM
44410298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4442552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4443552f7358SJed Brown 
444422c1ee49SMatthew 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()
444522c1ee49SMatthew G. Knepley 
44463813dfbdSMatthew G Knepley   Fortran Notes:
44473813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
44483813dfbdSMatthew G Knepley   include petsc.h90 in your code.
44493813dfbdSMatthew G Knepley 
44503813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
44513813dfbdSMatthew G Knepley 
4452552f7358SJed Brown   Level: intermediate
4453552f7358SJed Brown 
4454552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4455552f7358SJed Brown @*/
44567c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4457a6dfd86eSKarl Rupp {
4458552f7358SJed Brown   PetscInt       size = 0;
4459552f7358SJed Brown   PetscErrorCode ierr;
4460552f7358SJed Brown 
4461552f7358SJed Brown   PetscFunctionBegin;
4462552f7358SJed Brown   /* Should work without recalculating size */
446369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4464c9fdaa05SMatthew G. Knepley   *values = NULL;
4465552f7358SJed Brown   PetscFunctionReturn(0);
4466552f7358SJed Brown }
4467552f7358SJed Brown 
4468552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4469552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4470552f7358SJed Brown 
447197e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4472552f7358SJed Brown {
4473552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4474552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4475552f7358SJed Brown   PetscScalar    *a;
4476552f7358SJed Brown   PetscInt        off, cind = 0, k;
4477552f7358SJed Brown   PetscErrorCode  ierr;
4478552f7358SJed Brown 
4479552f7358SJed Brown   PetscFunctionBegin;
4480552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4481552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4482552f7358SJed Brown   a    = &array[off];
4483552f7358SJed Brown   if (!cdof || setBC) {
448497e99dd9SToby Isaac     if (clperm) {
448597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
448697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4487552f7358SJed Brown     } else {
448897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
448997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4490552f7358SJed Brown     }
4491552f7358SJed Brown   } else {
4492552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
449397e99dd9SToby Isaac     if (clperm) {
449497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4495552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
449697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4497552f7358SJed Brown         }
4498552f7358SJed Brown       } else {
4499552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4500552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
450197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
450297e99dd9SToby Isaac         }
450397e99dd9SToby Isaac       }
450497e99dd9SToby Isaac     } else {
450597e99dd9SToby Isaac       if (perm) {
450697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
450797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
450897e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
450997e99dd9SToby Isaac         }
451097e99dd9SToby Isaac       } else {
451197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
451297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
451397e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
451497e99dd9SToby Isaac         }
4515552f7358SJed Brown       }
4516552f7358SJed Brown     }
4517552f7358SJed Brown   }
4518552f7358SJed Brown   PetscFunctionReturn(0);
4519552f7358SJed Brown }
4520552f7358SJed Brown 
452197e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4522a5e93ea8SMatthew G. Knepley {
4523a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4524a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4525a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4526a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4527a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4528a5e93ea8SMatthew G. Knepley 
4529a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4530a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4531a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4532a5e93ea8SMatthew G. Knepley   a    = &array[off];
4533a5e93ea8SMatthew G. Knepley   if (cdof) {
4534a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
453597e99dd9SToby Isaac     if (clperm) {
453697e99dd9SToby Isaac       if (perm) {
4537a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4538a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
453997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
454097e99dd9SToby Isaac             cind++;
4541a5e93ea8SMatthew G. Knepley           }
4542a5e93ea8SMatthew G. Knepley         }
4543a5e93ea8SMatthew G. Knepley       } else {
4544a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4545a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
454697e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
454797e99dd9SToby Isaac             cind++;
454897e99dd9SToby Isaac           }
454997e99dd9SToby Isaac         }
455097e99dd9SToby Isaac       }
455197e99dd9SToby Isaac     } else {
455297e99dd9SToby Isaac       if (perm) {
455397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
455497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
455597e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
455697e99dd9SToby Isaac             cind++;
455797e99dd9SToby Isaac           }
455897e99dd9SToby Isaac         }
455997e99dd9SToby Isaac       } else {
456097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
456197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
456297e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
456397e99dd9SToby Isaac             cind++;
456497e99dd9SToby Isaac           }
4565a5e93ea8SMatthew G. Knepley         }
4566a5e93ea8SMatthew G. Knepley       }
4567a5e93ea8SMatthew G. Knepley     }
4568a5e93ea8SMatthew G. Knepley   }
4569a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4570a5e93ea8SMatthew G. Knepley }
4571a5e93ea8SMatthew G. Knepley 
457297e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, const PetscInt *perm, const PetscScalar *flip, PetscInt f, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4573a6dfd86eSKarl Rupp {
4574552f7358SJed Brown   PetscScalar    *a;
45751a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
45761a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
457797e99dd9SToby Isaac   PetscInt        cind = 0, b;
4578552f7358SJed Brown   PetscErrorCode  ierr;
4579552f7358SJed Brown 
4580552f7358SJed Brown   PetscFunctionBegin;
4581552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4582552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
45831a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
45841a271a75SMatthew G. Knepley   a    = &array[foff];
4585552f7358SJed Brown   if (!fcdof || setBC) {
458697e99dd9SToby Isaac     if (clperm) {
458797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
458897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4589552f7358SJed Brown     } else {
459097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
459197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4592552f7358SJed Brown     }
4593552f7358SJed Brown   } else {
4594552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
459597e99dd9SToby Isaac     if (clperm) {
459697e99dd9SToby Isaac       if (perm) {
459797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
459897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
459997e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4600552f7358SJed Brown         }
4601552f7358SJed Brown       } else {
460297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
460397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
460497e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
460597e99dd9SToby Isaac         }
460697e99dd9SToby Isaac       }
460797e99dd9SToby Isaac     } else {
460897e99dd9SToby Isaac       if (perm) {
460997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
461097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
461197e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
461297e99dd9SToby Isaac         }
461397e99dd9SToby Isaac       } else {
461497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
461597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
461697e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4617552f7358SJed Brown         }
4618552f7358SJed Brown       }
4619552f7358SJed Brown     }
4620552f7358SJed Brown   }
46211a271a75SMatthew G. Knepley   *offset += fdof;
4622552f7358SJed Brown   PetscFunctionReturn(0);
4623552f7358SJed Brown }
4624552f7358SJed Brown 
4625ba322698SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, PetscInt Ncc, const PetscInt comps[], void (*fuse)(PetscScalar*, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4626a5e93ea8SMatthew G. Knepley {
4627a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
46281a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
46291a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4630ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4631ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4632a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4633a5e93ea8SMatthew G. Knepley 
4634a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4635a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4636a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
46371a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
46381a271a75SMatthew G. Knepley   a    = &array[foff];
4639a5e93ea8SMatthew G. Knepley   if (fcdof) {
4640ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4641a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
464297e99dd9SToby Isaac     if (clperm) {
464397e99dd9SToby Isaac       if (perm) {
4644ba322698SMatthew G. Knepley         if (comps) {
4645ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4646ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4647ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4648ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4649ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4650ba322698SMatthew G. Knepley           }
4651ba322698SMatthew G. Knepley         } else {
465297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
465397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
465497e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4655a5e93ea8SMatthew G. Knepley               ++cind;
4656a5e93ea8SMatthew G. Knepley             }
4657a5e93ea8SMatthew G. Knepley           }
4658ba322698SMatthew G. Knepley         }
4659ba322698SMatthew G. Knepley       } else {
4660ba322698SMatthew G. Knepley         if (comps) {
4661ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4662ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4663ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4664ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4665ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4666ba322698SMatthew G. Knepley           }
4667a5e93ea8SMatthew G. Knepley         } else {
466897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
466997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
467097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
467197e99dd9SToby Isaac               ++cind;
467297e99dd9SToby Isaac             }
467397e99dd9SToby Isaac           }
467497e99dd9SToby Isaac         }
4675ba322698SMatthew G. Knepley       }
467697e99dd9SToby Isaac     } else {
467797e99dd9SToby Isaac       if (perm) {
4678ba322698SMatthew G. Knepley         if (comps) {
4679ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4680ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4681ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4682ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4683ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4684ba322698SMatthew G. Knepley           }
4685ba322698SMatthew G. Knepley         } else {
468697e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
468797e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
468897e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
468997e99dd9SToby Isaac               ++cind;
469097e99dd9SToby Isaac             }
469197e99dd9SToby Isaac           }
4692ba322698SMatthew G. Knepley         }
4693ba322698SMatthew G. Knepley       } else {
4694ba322698SMatthew G. Knepley         if (comps) {
4695ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4696ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4697ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4698ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4699ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4700ba322698SMatthew G. Knepley           }
470197e99dd9SToby Isaac         } else {
470297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
470397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
470497e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4705a5e93ea8SMatthew G. Knepley               ++cind;
4706a5e93ea8SMatthew G. Knepley             }
4707a5e93ea8SMatthew G. Knepley           }
4708a5e93ea8SMatthew G. Knepley         }
4709a5e93ea8SMatthew G. Knepley       }
4710a5e93ea8SMatthew G. Knepley     }
4711ba322698SMatthew G. Knepley   }
47121a271a75SMatthew G. Knepley   *offset += fdof;
4713a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4714a5e93ea8SMatthew G. Knepley }
4715a5e93ea8SMatthew G. Knepley 
47168f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4717a6dfd86eSKarl Rupp {
4718552f7358SJed Brown   PetscScalar    *array;
47191b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
47201b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4721552f7358SJed Brown   PetscErrorCode  ierr;
4722552f7358SJed Brown 
47231b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4724b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4725b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4726b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4727b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4728b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4729b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4730b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4731b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4732b6ebb6e6SMatthew G. Knepley 
4733b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4734b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4735b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4736b6ebb6e6SMatthew G. Knepley     {
4737b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4738b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4739b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4740b6ebb6e6SMatthew G. Knepley 
4741b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4742b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4743b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4744b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4745b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4746b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4747b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4748b6ebb6e6SMatthew G. Knepley           }
4749b6ebb6e6SMatthew G. Knepley         } else {
4750b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4751b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4752b6ebb6e6SMatthew G. Knepley           }
4753b6ebb6e6SMatthew G. Knepley         }
4754b6ebb6e6SMatthew G. Knepley       } else {
4755b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4756b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4757b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4758b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4759b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4760b6ebb6e6SMatthew G. Knepley           }
4761b6ebb6e6SMatthew G. Knepley         } else {
4762b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4763b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4764b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4765b6ebb6e6SMatthew G. Knepley           }
4766b6ebb6e6SMatthew G. Knepley         }
4767b6ebb6e6SMatthew G. Knepley       }
4768b6ebb6e6SMatthew G. Knepley     }
4769b6ebb6e6SMatthew G. Knepley   }
4770b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4771b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4772b6ebb6e6SMatthew G. Knepley }
47731b406b76SMatthew G. Knepley 
47741b406b76SMatthew G. Knepley /*@C
47751b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
47761b406b76SMatthew G. Knepley 
47771b406b76SMatthew G. Knepley   Not collective
47781b406b76SMatthew G. Knepley 
47791b406b76SMatthew G. Knepley   Input Parameters:
47801b406b76SMatthew G. Knepley + dm - The DM
47811b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
47821b406b76SMatthew G. Knepley . v - The local vector
4783eaf898f9SPatrick Sanan . point - The point in the DM
47841b406b76SMatthew G. Knepley . values - The array of values
478522c1ee49SMatthew 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,
478622c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
47871b406b76SMatthew G. Knepley 
47881b406b76SMatthew G. Knepley   Fortran Notes:
47891b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
47901b406b76SMatthew G. Knepley 
47911b406b76SMatthew G. Knepley   Level: intermediate
47921b406b76SMatthew G. Knepley 
47931b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
47941b406b76SMatthew G. Knepley @*/
47951b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
47961b406b76SMatthew G. Knepley {
47971b406b76SMatthew G. Knepley   PetscSection    clSection;
47981b406b76SMatthew G. Knepley   IS              clPoints;
47991b406b76SMatthew G. Knepley   PetscScalar    *array;
48001b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
480197e99dd9SToby Isaac   const PetscInt *clp, *clperm;
48021a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
48031b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
48041b406b76SMatthew G. Knepley 
48051a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
48061b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4807e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
48081a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
48091a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
48101b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
48111b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
48121b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
48138f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
48141b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
48151b406b76SMatthew G. Knepley   }
48161a271a75SMatthew G. Knepley   /* Get points */
481797e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4818923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
48191a271a75SMatthew G. Knepley   /* Get array */
4820552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
48211a271a75SMatthew G. Knepley   /* Get values */
4822ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
482397e99dd9SToby Isaac     PetscInt offset = 0, f;
4824552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
482597e99dd9SToby Isaac       const PetscInt    **perms = NULL;
482697e99dd9SToby Isaac       const PetscScalar **flips = NULL;
482797e99dd9SToby Isaac 
482897e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4829552f7358SJed Brown       switch (mode) {
4830552f7358SJed Brown       case INSERT_VALUES:
483197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
483297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
483397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
483497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
483597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4836552f7358SJed Brown         } break;
4837552f7358SJed Brown       case INSERT_ALL_VALUES:
483897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
483997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
484097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
484197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
484297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4843552f7358SJed Brown         } break;
4844a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
484597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
484697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
484797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
484897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4849ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4850a5e93ea8SMatthew G. Knepley         } break;
4851552f7358SJed Brown       case ADD_VALUES:
485297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
485397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
485497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
485597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
485697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4857552f7358SJed Brown         } break;
4858552f7358SJed Brown       case ADD_ALL_VALUES:
485997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
486097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
486197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
486297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
486397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4864552f7358SJed Brown         } break;
4865304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
486697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
486797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
486897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
486997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4870ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4871304ab55fSMatthew G. Knepley         } break;
4872552f7358SJed Brown       default:
4873f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4874552f7358SJed Brown       }
487597e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
48761a271a75SMatthew G. Knepley     }
4877552f7358SJed Brown   } else {
48781a271a75SMatthew G. Knepley     PetscInt dof, off;
487997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
488097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
48811a271a75SMatthew G. Knepley 
488297e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4883552f7358SJed Brown     switch (mode) {
4884552f7358SJed Brown     case INSERT_VALUES:
488597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
488697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
488797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
488897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
488997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
489097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4891552f7358SJed Brown       } break;
4892552f7358SJed Brown     case INSERT_ALL_VALUES:
489397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
489497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
489597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
489697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
489797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
489897e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4899552f7358SJed Brown       } break;
4900a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
490197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
490297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
490397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
490497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
490597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
490697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4907a5e93ea8SMatthew G. Knepley       } break;
4908552f7358SJed Brown     case ADD_VALUES:
490997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
491097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
491197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
491297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
491397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
491497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4915552f7358SJed Brown       } break;
4916552f7358SJed Brown     case ADD_ALL_VALUES:
491797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
491897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
491997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
492097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
492197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
492297e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4923552f7358SJed Brown       } break;
4924304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
492597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
492697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
492797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
492897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
492997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
493097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4931304ab55fSMatthew G. Knepley       } break;
4932552f7358SJed Brown     default:
4933f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4934552f7358SJed Brown     }
493597e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4936552f7358SJed Brown   }
49371a271a75SMatthew G. Knepley   /* Cleanup points */
4938923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
49391a271a75SMatthew G. Knepley   /* Cleanup array */
4940552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4941552f7358SJed Brown   PetscFunctionReturn(0);
4942552f7358SJed Brown }
4943552f7358SJed Brown 
4944ba322698SMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], const PetscScalar values[], InsertMode mode)
4945e07394fbSMatthew G. Knepley {
4946e07394fbSMatthew G. Knepley   PetscSection      clSection;
4947e07394fbSMatthew G. Knepley   IS                clPoints;
4948e07394fbSMatthew G. Knepley   PetscScalar       *array;
4949e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4950b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4951e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
495297e99dd9SToby Isaac   PetscInt          offset = 0, f;
4953e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4954e07394fbSMatthew G. Knepley 
4955e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4956e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4957e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
4958e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4959e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4960e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4961e07394fbSMatthew G. Knepley   /* Get points */
4962b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4963923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4964e07394fbSMatthew G. Knepley   /* Get array */
4965e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4966e07394fbSMatthew G. Knepley   /* Get values */
4967e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
496897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
496997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
497097e99dd9SToby Isaac 
4971e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4972e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4973e07394fbSMatthew G. Knepley         PetscInt fdof;
4974e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4975e07394fbSMatthew G. Knepley         offset += fdof;
4976e07394fbSMatthew G. Knepley       }
4977e07394fbSMatthew G. Knepley       continue;
4978e07394fbSMatthew G. Knepley     }
497997e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4980e07394fbSMatthew G. Knepley     switch (mode) {
4981e07394fbSMatthew G. Knepley     case INSERT_VALUES:
498297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
498397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
498497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
498597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4986b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4987e07394fbSMatthew G. Knepley       } break;
4988e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
498997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
499097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
499197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
499297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4993b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4994e07394fbSMatthew G. Knepley         } break;
4995e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
499697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
499797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
499897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
499997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5000ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
5001e07394fbSMatthew G. Knepley       } break;
5002e07394fbSMatthew G. Knepley     case ADD_VALUES:
500397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
500497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
500597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
500697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5007b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5008e07394fbSMatthew G. Knepley       } break;
5009e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
501097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
501197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
501297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
501397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5014b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5015e07394fbSMatthew G. Knepley       } break;
5016e07394fbSMatthew G. Knepley     default:
5017f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5018e07394fbSMatthew G. Knepley     }
501997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5020e07394fbSMatthew G. Knepley   }
5021e07394fbSMatthew G. Knepley   /* Cleanup points */
5022923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5023e07394fbSMatthew G. Knepley   /* Cleanup array */
5024e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5025e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5026e07394fbSMatthew G. Knepley }
5027e07394fbSMatthew G. Knepley 
50287cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5029552f7358SJed Brown {
5030552f7358SJed Brown   PetscMPIInt    rank;
5031552f7358SJed Brown   PetscInt       i, j;
5032552f7358SJed Brown   PetscErrorCode ierr;
5033552f7358SJed Brown 
5034552f7358SJed Brown   PetscFunctionBegin;
503582f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
5036eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5037e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5038e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5039b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5040b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5041e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5042b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5043519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
50447eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5045552f7358SJed Brown #else
5046b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5047552f7358SJed Brown #endif
5048552f7358SJed Brown     }
504977a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5050552f7358SJed Brown   }
5051552f7358SJed Brown   PetscFunctionReturn(0);
5052552f7358SJed Brown }
5053552f7358SJed Brown 
5054552f7358SJed Brown /* . off - The global offset of this point */
50554acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
5056a6dfd86eSKarl Rupp {
5057e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
5058552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5059552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5060552f7358SJed Brown   PetscInt        cind = 0, k;
5061552f7358SJed Brown   PetscErrorCode  ierr;
5062552f7358SJed Brown 
5063552f7358SJed Brown   PetscFunctionBegin;
5064552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5065552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5066552f7358SJed Brown   if (!cdof || setBC) {
50674acb8e1eSToby Isaac     if (perm) {
50684acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
5069552f7358SJed Brown     } else {
50704acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
5071552f7358SJed Brown     }
5072552f7358SJed Brown   } else {
5073552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
50744acb8e1eSToby Isaac     if (perm) {
50754acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
50764acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
50774acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
50784acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
50794acb8e1eSToby Isaac           ++cind;
50804acb8e1eSToby Isaac         } else {
50814acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
50824acb8e1eSToby Isaac         }
50834acb8e1eSToby Isaac       }
50844acb8e1eSToby Isaac     } else {
5085552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5086552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
5087552f7358SJed Brown           /* Insert check for returning constrained indices */
5088e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
5089552f7358SJed Brown           ++cind;
5090552f7358SJed Brown         } else {
5091e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
5092552f7358SJed Brown         }
5093552f7358SJed Brown       }
5094552f7358SJed Brown     }
5095552f7358SJed Brown   }
5096e6ccafaeSMatthew G Knepley   *loff += dof;
5097552f7358SJed Brown   PetscFunctionReturn(0);
5098552f7358SJed Brown }
5099552f7358SJed Brown 
51007e29afd2SMatthew G. Knepley /*
51017e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
51027e29afd2SMatthew G. Knepley 
51037e29afd2SMatthew G. Knepley  . off - The global offset of this point
51047e29afd2SMatthew G. Knepley */
51054acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
5106a6dfd86eSKarl Rupp {
5107552f7358SJed Brown   PetscInt       numFields, foff, f;
5108552f7358SJed Brown   PetscErrorCode ierr;
5109552f7358SJed Brown 
5110552f7358SJed Brown   PetscFunctionBegin;
5111552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5112552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
51134acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5114552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
51154acb8e1eSToby Isaac     PetscInt        cind = 0, b;
51164acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5117552f7358SJed Brown 
5118552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5119552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5120552f7358SJed Brown     if (!cfdof || setBC) {
51214acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
51224acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
5123552f7358SJed Brown     } else {
5124552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
51254acb8e1eSToby Isaac       if (perm) {
51264acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
51274acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
51284acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
5129552f7358SJed Brown             ++cind;
5130552f7358SJed Brown           } else {
51314acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
5132552f7358SJed Brown           }
5133552f7358SJed Brown         }
5134552f7358SJed Brown       } else {
51354acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
51364acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
51374acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
5138552f7358SJed Brown             ++cind;
5139552f7358SJed Brown           } else {
51404acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
5141552f7358SJed Brown           }
5142552f7358SJed Brown         }
5143552f7358SJed Brown       }
5144552f7358SJed Brown     }
5145a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
5146552f7358SJed Brown     foffs[f] += fdof;
5147552f7358SJed Brown   }
5148552f7358SJed Brown   PetscFunctionReturn(0);
5149552f7358SJed Brown }
5150552f7358SJed Brown 
51517e29afd2SMatthew G. Knepley /*
51527e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
51537e29afd2SMatthew G. Knepley 
51547e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
51557e29afd2SMatthew G. Knepley */
51567e29afd2SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
51577e29afd2SMatthew G. Knepley {
51587e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
51597e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
51607e29afd2SMatthew G. Knepley 
51617e29afd2SMatthew G. Knepley   PetscFunctionBegin;
51627e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
51637e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
51647e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
51657e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
51667e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
51677e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
51687e29afd2SMatthew G. Knepley 
51697e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
51707e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
51717e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
51727e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
51737e29afd2SMatthew G. Knepley       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = foff+b;}}
51747e29afd2SMatthew G. Knepley       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = foff+b;}}
51757e29afd2SMatthew G. Knepley     } else {
51767e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
51777e29afd2SMatthew G. Knepley       if (perm) {
51787e29afd2SMatthew G. Knepley         for (b = 0; b < fdof; b++) {
51797e29afd2SMatthew G. Knepley           if ((cind < cfdof) && (b == fcdofs[cind])) {
51807e29afd2SMatthew G. Knepley             indices[foffs[f]+perm[b]] = -(foff+b+1);
51817e29afd2SMatthew G. Knepley             ++cind;
51827e29afd2SMatthew G. Knepley           } else {
51837e29afd2SMatthew G. Knepley             indices[foffs[f]+perm[b]] = foff+b-cind;
51847e29afd2SMatthew G. Knepley           }
51857e29afd2SMatthew G. Knepley         }
51867e29afd2SMatthew G. Knepley       } else {
51877e29afd2SMatthew G. Knepley         for (b = 0; b < fdof; b++) {
51887e29afd2SMatthew G. Knepley           if ((cind < cfdof) && (b == fcdofs[cind])) {
51897e29afd2SMatthew G. Knepley             indices[foffs[f]+b] = -(foff+b+1);
51907e29afd2SMatthew G. Knepley             ++cind;
51917e29afd2SMatthew G. Knepley           } else {
51927e29afd2SMatthew G. Knepley             indices[foffs[f]+b] = foff+b-cind;
51937e29afd2SMatthew G. Knepley           }
51947e29afd2SMatthew G. Knepley         }
51957e29afd2SMatthew G. Knepley       }
51967e29afd2SMatthew G. Knepley     }
51977e29afd2SMatthew G. Knepley     foffs[f] += fdof;
51987e29afd2SMatthew G. Knepley   }
51997e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
52007e29afd2SMatthew G. Knepley }
52017e29afd2SMatthew G. Knepley 
52024acb8e1eSToby 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)
5203d3d1a6afSToby Isaac {
5204d3d1a6afSToby Isaac   Mat             cMat;
5205d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5206d3d1a6afSToby Isaac   IS              aIS;
5207d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5208d3d1a6afSToby Isaac   const PetscInt  *anchors;
5209e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5210d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5211d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5212d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5213d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5214d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5215d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5216d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
52176ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5218d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5219d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5220d3d1a6afSToby Isaac 
5221d3d1a6afSToby Isaac   PetscFunctionBegin;
5222d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5223d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5224d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5225d3d1a6afSToby Isaac 
5226a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5227d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5228d3d1a6afSToby Isaac   if (aSec) {
5229d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5230d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5231d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5232d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5233d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5234d3d1a6afSToby Isaac      * into the global matrix anyway) */
5235d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5236d3d1a6afSToby Isaac       PetscInt b    = points[p];
52374b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5238d3d1a6afSToby Isaac 
52394b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
52404b2f2278SToby Isaac       if (!bSecDof) {
52414b2f2278SToby Isaac         continue;
52424b2f2278SToby Isaac       }
5243d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5244d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5245d3d1a6afSToby Isaac       }
5246d3d1a6afSToby Isaac       if (bDof) {
5247d3d1a6afSToby Isaac         /* this point is constrained */
5248d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5249d3d1a6afSToby Isaac         PetscInt bOff, q;
5250d3d1a6afSToby Isaac 
5251d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5252d3d1a6afSToby Isaac         newNumPoints  += bDof;
5253d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5254d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5255d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5256d3d1a6afSToby Isaac           PetscInt aDof;
5257d3d1a6afSToby Isaac 
5258d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5259d3d1a6afSToby Isaac           newNumIndices += aDof;
5260d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5261d3d1a6afSToby Isaac             PetscInt fDof;
5262d3d1a6afSToby Isaac 
5263d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5264d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5265d3d1a6afSToby Isaac           }
5266d3d1a6afSToby Isaac         }
5267d3d1a6afSToby Isaac       }
5268d3d1a6afSToby Isaac       else {
5269d3d1a6afSToby Isaac         /* this point is not constrained */
5270d3d1a6afSToby Isaac         newNumPoints++;
52714b2f2278SToby Isaac         newNumIndices += bSecDof;
5272d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5273d3d1a6afSToby Isaac           PetscInt fDof;
5274d3d1a6afSToby Isaac 
5275d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5276d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5277d3d1a6afSToby Isaac         }
5278d3d1a6afSToby Isaac       }
5279d3d1a6afSToby Isaac     }
5280d3d1a6afSToby Isaac   }
5281d3d1a6afSToby Isaac   if (!anyConstrained) {
528272b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
528372b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
528472b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
528572b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
528672b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5287d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5288d3d1a6afSToby Isaac   }
5289d3d1a6afSToby Isaac 
52906ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
52916ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
52926ecaa68aSToby Isaac 
5293f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5294d3d1a6afSToby Isaac 
52956ecaa68aSToby Isaac   if (!outPoints && !outValues) {
52966ecaa68aSToby Isaac     if (offsets) {
52976ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
52986ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
52996ecaa68aSToby Isaac       }
53006ecaa68aSToby Isaac     }
53016ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
53026ecaa68aSToby Isaac     PetscFunctionReturn(0);
53036ecaa68aSToby Isaac   }
53046ecaa68aSToby Isaac 
5305f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5306d3d1a6afSToby Isaac 
5307f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5308d3d1a6afSToby Isaac 
5309d3d1a6afSToby Isaac   /* workspaces */
5310d3d1a6afSToby Isaac   if (numFields) {
5311d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
531269291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
531369291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5314d3d1a6afSToby Isaac     }
5315d3d1a6afSToby Isaac   }
5316d3d1a6afSToby Isaac   else {
531769291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
531869291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5319d3d1a6afSToby Isaac   }
5320d3d1a6afSToby Isaac 
5321d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5322d3d1a6afSToby Isaac   if (numFields) {
53234b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
53244b2f2278SToby Isaac 
5325d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5326d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
53274b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5328d3d1a6afSToby Isaac 
53294b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
53304b2f2278SToby Isaac       if (!bSecDof) {
53314b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
53324b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
53334b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
53344b2f2278SToby Isaac         }
53354b2f2278SToby Isaac         continue;
53364b2f2278SToby Isaac       }
5337d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5338d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5339d3d1a6afSToby Isaac       }
5340d3d1a6afSToby Isaac       if (bDof) {
5341d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5342d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5343d3d1a6afSToby Isaac 
5344d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5345d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5346d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5347d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5348d3d1a6afSToby Isaac             PetscInt aFDof;
5349d3d1a6afSToby Isaac 
5350d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5351d3d1a6afSToby Isaac             allFDof += aFDof;
5352d3d1a6afSToby Isaac           }
5353d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5354d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5355d3d1a6afSToby Isaac         }
5356d3d1a6afSToby Isaac       }
5357d3d1a6afSToby Isaac       else {
5358d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5359d3d1a6afSToby Isaac           PetscInt fDof;
5360d3d1a6afSToby Isaac 
5361d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5362d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5363d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5364d3d1a6afSToby Isaac         }
5365d3d1a6afSToby Isaac       }
5366d3d1a6afSToby Isaac     }
53674b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
53684b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
53694b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5370d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5371d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5372d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5373d3d1a6afSToby Isaac       }
537419f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
537519f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
537669291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5377d3d1a6afSToby Isaac     }
5378d3d1a6afSToby Isaac   }
5379d3d1a6afSToby Isaac   else {
5380d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5381d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
53824b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5383d3d1a6afSToby Isaac 
53844b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
53854b2f2278SToby Isaac       if (!bSecDof) {
53864b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
53874b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
53884b2f2278SToby Isaac         continue;
53894b2f2278SToby Isaac       }
5390d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5391d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5392d3d1a6afSToby Isaac       }
5393d3d1a6afSToby Isaac       if (bDof) {
53944b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5395d3d1a6afSToby Isaac 
5396d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5397d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5398d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5399d3d1a6afSToby Isaac 
5400d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5401d3d1a6afSToby Isaac           allDof += aDof;
5402d3d1a6afSToby Isaac         }
5403d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
54044b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5405d3d1a6afSToby Isaac       }
5406d3d1a6afSToby Isaac       else {
54074b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5408d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5409d3d1a6afSToby Isaac       }
5410d3d1a6afSToby Isaac     }
5411d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5412d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5413d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5414d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5415d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5416d3d1a6afSToby Isaac     }
541769291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5418d3d1a6afSToby Isaac   }
5419d3d1a6afSToby Isaac 
54206ecaa68aSToby Isaac   /* output arrays */
542169291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54226ecaa68aSToby Isaac 
5423d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5424d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5425d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
542669291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
542769291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5428d3d1a6afSToby Isaac   if (numFields) {
5429d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5430d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5431d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
54324b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5433d3d1a6afSToby Isaac 
54344b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
54354b2f2278SToby Isaac       if (!bSecDof) {
54364b2f2278SToby Isaac         continue;
54374b2f2278SToby Isaac       }
5438d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5439d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5440d3d1a6afSToby Isaac       }
5441d3d1a6afSToby Isaac       if (bDof) {
5442d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5443d3d1a6afSToby Isaac 
5444d3d1a6afSToby Isaac         fStart[0] = 0;
5445d3d1a6afSToby Isaac         fEnd[0]   = 0;
5446d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5447d3d1a6afSToby Isaac           PetscInt fDof;
5448d3d1a6afSToby Isaac 
5449d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5450d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5451d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5452d3d1a6afSToby Isaac         }
5453d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
54544acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5455d3d1a6afSToby Isaac 
5456d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5457d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5458d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5459d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5460d3d1a6afSToby Isaac 
5461d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5462d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5463d3d1a6afSToby Isaac         }
5464d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5465d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5466d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5467d3d1a6afSToby Isaac 
5468d3d1a6afSToby 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 */
5469d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5470d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5471302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
54724acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5473d3d1a6afSToby Isaac         }
5474d3d1a6afSToby Isaac         newP += bDof;
5475d3d1a6afSToby Isaac 
54766ecaa68aSToby Isaac         if (outValues) {
5477d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5478d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5479d3d1a6afSToby 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);
5480d3d1a6afSToby Isaac           }
5481d3d1a6afSToby Isaac         }
54826ecaa68aSToby Isaac       }
5483d3d1a6afSToby Isaac       else {
5484d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5485d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5486d3d1a6afSToby Isaac         newP++;
5487d3d1a6afSToby Isaac       }
5488d3d1a6afSToby Isaac     }
5489d3d1a6afSToby Isaac   } else {
5490d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5491d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5492d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
54934b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5494d3d1a6afSToby Isaac 
54954b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
54964b2f2278SToby Isaac       if (!bSecDof) {
54974b2f2278SToby Isaac         continue;
54984b2f2278SToby Isaac       }
5499d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5500d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5501d3d1a6afSToby Isaac       }
5502d3d1a6afSToby Isaac       if (bDof) {
5503d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5504d3d1a6afSToby Isaac 
5505d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
55064acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5507d3d1a6afSToby Isaac 
5508d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5509d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5510d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5511d3d1a6afSToby Isaac 
5512d3d1a6afSToby 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 */
5513d3d1a6afSToby Isaac 
5514d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5515d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5516302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
55174acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5518d3d1a6afSToby Isaac         }
5519d3d1a6afSToby Isaac         newP += bDof;
5520d3d1a6afSToby Isaac 
5521d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
55226ecaa68aSToby Isaac         if (outValues) {
5523d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5524d3d1a6afSToby Isaac         }
55256ecaa68aSToby Isaac       }
5526d3d1a6afSToby Isaac       else {
5527d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5528d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5529d3d1a6afSToby Isaac         newP++;
5530d3d1a6afSToby Isaac       }
5531d3d1a6afSToby Isaac     }
5532d3d1a6afSToby Isaac   }
5533d3d1a6afSToby Isaac 
55346ecaa68aSToby Isaac   if (outValues) {
553569291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5536d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5537d3d1a6afSToby Isaac     /* multiply constraints on the right */
5538d3d1a6afSToby Isaac     if (numFields) {
5539d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5540d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5541d3d1a6afSToby Isaac 
5542d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5543d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5544d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5545d3d1a6afSToby Isaac           PetscInt c, r, k;
5546d3d1a6afSToby Isaac           PetscInt dof;
5547d3d1a6afSToby Isaac 
5548d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
55494b2f2278SToby Isaac           if (!dof) {
55504b2f2278SToby Isaac             continue;
55514b2f2278SToby Isaac           }
5552d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5553d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5554d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5555d3d1a6afSToby Isaac 
5556d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5557d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5558d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
55594acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5560d3d1a6afSToby Isaac                 }
5561d3d1a6afSToby Isaac               }
5562d3d1a6afSToby Isaac             }
5563d3d1a6afSToby Isaac           }
5564d3d1a6afSToby Isaac           else {
5565d3d1a6afSToby Isaac             /* copy this column as is */
5566d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5567d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5568d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5569d3d1a6afSToby Isaac               }
5570d3d1a6afSToby Isaac             }
5571d3d1a6afSToby Isaac           }
5572d3d1a6afSToby Isaac           oldOff += dof;
5573d3d1a6afSToby Isaac         }
5574d3d1a6afSToby Isaac       }
5575d3d1a6afSToby Isaac     }
5576d3d1a6afSToby Isaac     else {
5577d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5578d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5579d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5580d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5581d3d1a6afSToby Isaac         PetscInt c, r, k;
5582d3d1a6afSToby Isaac         PetscInt dof;
5583d3d1a6afSToby Isaac 
5584d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
55854b2f2278SToby Isaac         if (!dof) {
55864b2f2278SToby Isaac           continue;
55874b2f2278SToby Isaac         }
5588d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5589d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5590d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5591d3d1a6afSToby Isaac 
5592d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5593d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5594d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5595d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5596d3d1a6afSToby Isaac               }
5597d3d1a6afSToby Isaac             }
5598d3d1a6afSToby Isaac           }
5599d3d1a6afSToby Isaac         }
5600d3d1a6afSToby Isaac         else {
5601d3d1a6afSToby Isaac           /* copy this column as is */
5602d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5603d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5604d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5605d3d1a6afSToby Isaac             }
5606d3d1a6afSToby Isaac           }
5607d3d1a6afSToby Isaac         }
5608d3d1a6afSToby Isaac         oldOff += dof;
5609d3d1a6afSToby Isaac       }
5610d3d1a6afSToby Isaac     }
5611d3d1a6afSToby Isaac 
56126ecaa68aSToby Isaac     if (multiplyLeft) {
561369291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5614d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5615d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5616d3d1a6afSToby Isaac       if (numFields) {
5617d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5618d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5619d3d1a6afSToby Isaac 
5620d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5621d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5622d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5623d3d1a6afSToby Isaac             PetscInt c, r, k;
5624d3d1a6afSToby Isaac             PetscInt dof;
5625d3d1a6afSToby Isaac 
5626d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5627d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5628d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5629d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5630d3d1a6afSToby Isaac 
5631d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5632d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5633d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5634d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5635d3d1a6afSToby Isaac                   }
5636d3d1a6afSToby Isaac                 }
5637d3d1a6afSToby Isaac               }
5638d3d1a6afSToby Isaac             }
5639d3d1a6afSToby Isaac             else {
5640d3d1a6afSToby Isaac               /* copy this row as is */
5641d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5642d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5643d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5644d3d1a6afSToby Isaac                 }
5645d3d1a6afSToby Isaac               }
5646d3d1a6afSToby Isaac             }
5647d3d1a6afSToby Isaac             oldOff += dof;
5648d3d1a6afSToby Isaac           }
5649d3d1a6afSToby Isaac         }
5650d3d1a6afSToby Isaac       }
5651d3d1a6afSToby Isaac       else {
5652d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5653d3d1a6afSToby Isaac 
5654d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5655d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5656d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5657d3d1a6afSToby Isaac           PetscInt c, r, k;
5658d3d1a6afSToby Isaac           PetscInt dof;
5659d3d1a6afSToby Isaac 
5660d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5661d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5662d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5663d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5664d3d1a6afSToby Isaac 
5665d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5666d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5667d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5668d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5669d3d1a6afSToby Isaac                 }
5670d3d1a6afSToby Isaac               }
5671d3d1a6afSToby Isaac             }
5672d3d1a6afSToby Isaac           }
5673d3d1a6afSToby Isaac           else {
5674d3d1a6afSToby Isaac             /* copy this row as is */
56759fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5676d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5677d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5678d3d1a6afSToby Isaac               }
5679d3d1a6afSToby Isaac             }
5680d3d1a6afSToby Isaac           }
5681d3d1a6afSToby Isaac           oldOff += dof;
5682d3d1a6afSToby Isaac         }
5683d3d1a6afSToby Isaac       }
5684d3d1a6afSToby Isaac 
568569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
56866ecaa68aSToby Isaac     }
56876ecaa68aSToby Isaac     else {
56886ecaa68aSToby Isaac       newValues = tmpValues;
56896ecaa68aSToby Isaac     }
56906ecaa68aSToby Isaac   }
56916ecaa68aSToby Isaac 
5692d3d1a6afSToby Isaac   /* clean up */
569369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
569469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
56956ecaa68aSToby Isaac 
5696d3d1a6afSToby Isaac   if (numFields) {
5697d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
569869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
569969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
570069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5701d3d1a6afSToby Isaac     }
5702d3d1a6afSToby Isaac   }
5703d3d1a6afSToby Isaac   else {
570469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
570569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
570669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5707d3d1a6afSToby Isaac   }
5708d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5709d3d1a6afSToby Isaac 
5710d3d1a6afSToby Isaac   /* output */
57116ecaa68aSToby Isaac   if (outPoints) {
5712d3d1a6afSToby Isaac     *outPoints = newPoints;
57136ecaa68aSToby Isaac   }
57146ecaa68aSToby Isaac   else {
571569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
57166ecaa68aSToby Isaac   }
571731620726SToby Isaac   if (outValues) {
5718d3d1a6afSToby Isaac     *outValues = newValues;
57196ecaa68aSToby Isaac   }
57206ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5721d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5722d3d1a6afSToby Isaac   }
5723d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5724d3d1a6afSToby Isaac }
5725d3d1a6afSToby Isaac 
57267cd05799SMatthew G. Knepley /*@C
5727a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
57287cd05799SMatthew G. Knepley 
57297cd05799SMatthew G. Knepley   Not collective
57307cd05799SMatthew G. Knepley 
57317cd05799SMatthew G. Knepley   Input Parameters:
57327cd05799SMatthew G. Knepley + dm - The DM
57337cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
57347cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
57357cd05799SMatthew G. Knepley - point - The mesh point
57367cd05799SMatthew G. Knepley 
57377cd05799SMatthew G. Knepley   Output parameters:
57387cd05799SMatthew G. Knepley + numIndices - The number of indices
57397cd05799SMatthew G. Knepley . indices - The indices
57407cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
57417cd05799SMatthew G. Knepley 
57427cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
57437cd05799SMatthew G. Knepley 
57447cd05799SMatthew G. Knepley   Level: advanced
57457cd05799SMatthew G. Knepley 
57467cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
57477cd05799SMatthew G. Knepley @*/
57486ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
57497773e69fSMatthew G. Knepley {
57507773e69fSMatthew G. Knepley   PetscSection    clSection;
57517773e69fSMatthew G. Knepley   IS              clPoints;
57527773e69fSMatthew G. Knepley   const PetscInt *clp;
57534acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
57547773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
57557773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
57567773e69fSMatthew G. Knepley   PetscInt        offsets[32];
57577773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
57587773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
57597773e69fSMatthew G. Knepley 
57607773e69fSMatthew G. Knepley   PetscFunctionBegin;
57617773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57627773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
57637773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
57647773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
57657773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
57667773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
57677773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
57687773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
57697773e69fSMatthew G. Knepley   /* Get points in closure */
5770923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
57717773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
57727773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
57737773e69fSMatthew G. Knepley     PetscInt dof, fdof;
57747773e69fSMatthew G. Knepley 
57757773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
57767773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
57777773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
57787773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
57797773e69fSMatthew G. Knepley     }
57807773e69fSMatthew G. Knepley     Nind += dof;
57817773e69fSMatthew G. Knepley   }
57827773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
57838ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
57844acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
57854acb8e1eSToby Isaac   /* Get dual space symmetries */
57864acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
57874acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
57884acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
57894acb8e1eSToby Isaac   }
57907773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
57917773e69fSMatthew G. Knepley   {
57924acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
57937773e69fSMatthew G. Knepley     if (numPointsNew) {
57944acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
57954acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
57964acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
57974acb8e1eSToby Isaac       }
57984acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
57994acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
58004acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
58014acb8e1eSToby Isaac       }
5802923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
58037773e69fSMatthew G. Knepley       numPoints = numPointsNew;
58047773e69fSMatthew G. Knepley       Nind      = NindNew;
58057773e69fSMatthew G. Knepley       points    = pointsNew;
58067773e69fSMatthew G. Knepley     }
58077773e69fSMatthew G. Knepley   }
58087773e69fSMatthew G. Knepley   /* Calculate indices */
580969291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
58107773e69fSMatthew G. Knepley   if (Nf) {
58116ecaa68aSToby Isaac     if (outOffsets) {
58126ecaa68aSToby Isaac       PetscInt f;
58136ecaa68aSToby Isaac 
581446bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
58156ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
58166ecaa68aSToby Isaac       }
58176ecaa68aSToby Isaac     }
58184acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
58194acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
58204acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
58217773e69fSMatthew G. Knepley     }
58227773e69fSMatthew G. Knepley   } else {
58234acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
58244acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
58254acb8e1eSToby Isaac 
58264acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
58274acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
58287773e69fSMatthew G. Knepley     }
58297773e69fSMatthew G. Knepley   }
58307773e69fSMatthew G. Knepley   /* Cleanup points */
58314acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
58324acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58334acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58344acb8e1eSToby Isaac   }
58357773e69fSMatthew G. Knepley   if (numPointsNew) {
583669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
58377773e69fSMatthew G. Knepley   } else {
5838923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
58397773e69fSMatthew G. Knepley   }
58407773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
58417773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
58427773e69fSMatthew G. Knepley }
58437773e69fSMatthew G. Knepley 
58447cd05799SMatthew G. Knepley /*@C
58457cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
58467cd05799SMatthew G. Knepley 
58477cd05799SMatthew G. Knepley   Not collective
58487cd05799SMatthew G. Knepley 
58497cd05799SMatthew G. Knepley   Input Parameters:
58507cd05799SMatthew G. Knepley + dm - The DM
58517cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
58527cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
58537cd05799SMatthew G. Knepley . point - The mesh point
58547cd05799SMatthew G. Knepley . numIndices - The number of indices
58557cd05799SMatthew G. Knepley . indices - The indices
58567cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
58577cd05799SMatthew G. Knepley 
58587cd05799SMatthew G. Knepley   Level: advanced
58597cd05799SMatthew G. Knepley 
58607cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
58617cd05799SMatthew G. Knepley @*/
586246bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
58637773e69fSMatthew G. Knepley {
58647773e69fSMatthew G. Knepley   PetscErrorCode ierr;
58657773e69fSMatthew G. Knepley 
58667773e69fSMatthew G. Knepley   PetscFunctionBegin;
58677773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58687773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
586969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
58707773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
58717773e69fSMatthew G. Knepley }
58727773e69fSMatthew G. Knepley 
58737f5d1fdeSMatthew G. Knepley /*@C
58747f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
58757f5d1fdeSMatthew G. Knepley 
58767f5d1fdeSMatthew G. Knepley   Not collective
58777f5d1fdeSMatthew G. Knepley 
58787f5d1fdeSMatthew G. Knepley   Input Parameters:
58797f5d1fdeSMatthew G. Knepley + dm - The DM
5880ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5881ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
58827f5d1fdeSMatthew G. Knepley . A - The matrix
5883eaf898f9SPatrick Sanan . point - The point in the DM
58847f5d1fdeSMatthew G. Knepley . values - The array of values
58857f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
58867f5d1fdeSMatthew G. Knepley 
58877f5d1fdeSMatthew G. Knepley   Fortran Notes:
58887f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
58897f5d1fdeSMatthew G. Knepley 
58907f5d1fdeSMatthew G. Knepley   Level: intermediate
58917f5d1fdeSMatthew G. Knepley 
58927f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
58937f5d1fdeSMatthew G. Knepley @*/
58947c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5895552f7358SJed Brown {
5896552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
58971b406b76SMatthew G. Knepley   PetscSection        clSection;
58981b406b76SMatthew G. Knepley   IS                  clPoints;
5899d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
59001b406b76SMatthew G. Knepley   const PetscInt     *clp;
5901552f7358SJed Brown   PetscInt           *indices;
5902552f7358SJed Brown   PetscInt            offsets[32];
59034acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
59044acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5905923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
59064acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5907d3d1a6afSToby Isaac   PetscScalar        *newValues;
5908552f7358SJed Brown   PetscErrorCode      ierr;
5909552f7358SJed Brown 
5910552f7358SJed Brown   PetscFunctionBegin;
5911552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5912e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
59133dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5914e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
59153dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
59163dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5917552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
591882f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5919552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5920923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5921552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5922552f7358SJed Brown     PetscInt fdof;
5923552f7358SJed Brown 
5924552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5925552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5926552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5927552f7358SJed Brown       offsets[f+1] += fdof;
5928552f7358SJed Brown     }
5929552f7358SJed Brown     numIndices += dof;
5930552f7358SJed Brown   }
59310d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
59320d644c17SKarl Rupp 
59338ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
59344acb8e1eSToby Isaac   /* Get symmetries */
59354acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
59364acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59374acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59384acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
59394acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
59404acb8e1eSToby Isaac 
59414acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
59424acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
59434acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
59444acb8e1eSToby Isaac 
59454acb8e1eSToby Isaac         if (!numFields) {
59464acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
59474acb8e1eSToby Isaac         } else {
59484acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
59494acb8e1eSToby Isaac         }
59504acb8e1eSToby Isaac         if (flip) {
59514acb8e1eSToby Isaac           PetscInt i, j, k;
59524acb8e1eSToby Isaac 
59534acb8e1eSToby Isaac           if (!valCopy) {
595469291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
59554acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
59564acb8e1eSToby Isaac             values = valCopy;
59574acb8e1eSToby Isaac           }
59584acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5959775f7be2SToby Isaac             PetscScalar fval = flip[i];
59604acb8e1eSToby Isaac 
59614acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
59624acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
59634acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
59644acb8e1eSToby Isaac             }
59654acb8e1eSToby Isaac           }
59664acb8e1eSToby Isaac         }
59674acb8e1eSToby Isaac         foffset += fdof;
59684acb8e1eSToby Isaac       }
59694acb8e1eSToby Isaac     }
59704acb8e1eSToby Isaac   }
59714acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5972d3d1a6afSToby Isaac   if (newNumPoints) {
59734acb8e1eSToby Isaac     if (valCopy) {
597469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
59754acb8e1eSToby Isaac     }
59764acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
59774acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59784acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59794acb8e1eSToby Isaac     }
59804acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
59814acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
59824acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
59834acb8e1eSToby Isaac     }
5984923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5985d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5986d3d1a6afSToby Isaac     numIndices = newNumIndices;
5987d3d1a6afSToby Isaac     points     = newPoints;
5988d3d1a6afSToby Isaac     values     = newValues;
5989d3d1a6afSToby Isaac   }
599069291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5991552f7358SJed Brown   if (numFields) {
59927e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
59937e29afd2SMatthew G. Knepley 
59947e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
59957e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
59967e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
59977e29afd2SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, indices);
59987e29afd2SMatthew G. Knepley       }
59997e29afd2SMatthew G. Knepley     } else {
60004acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
60014acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
60024acb8e1eSToby Isaac         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
6003552f7358SJed Brown       }
60047e29afd2SMatthew G. Knepley     }
6005552f7358SJed Brown   } else {
60064acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
60074acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
60084acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
60094acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
6010552f7358SJed Brown     }
6011552f7358SJed Brown   }
6012b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
6013552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6014ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
6015ab1d0545SMatthew G. Knepley     PetscInt i;
6016ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
60178ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
6018ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
6019ab1d0545SMatthew G. Knepley   }
6020552f7358SJed Brown   if (ierr) {
6021552f7358SJed Brown     PetscMPIInt    rank;
6022552f7358SJed Brown     PetscErrorCode ierr2;
6023552f7358SJed Brown 
602482f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6025e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6026b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
602769291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
6028552f7358SJed Brown     CHKERRQ(ierr);
6029552f7358SJed Brown   }
60304acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
60314acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60324acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60334acb8e1eSToby Isaac   }
6034d3d1a6afSToby Isaac   if (newNumPoints) {
603569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
603669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
6037d3d1a6afSToby Isaac   }
6038d3d1a6afSToby Isaac   else {
60394acb8e1eSToby Isaac     if (valCopy) {
604069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
60414acb8e1eSToby Isaac     }
6042923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6043d3d1a6afSToby Isaac   }
604469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6045552f7358SJed Brown   PetscFunctionReturn(0);
6046552f7358SJed Brown }
6047552f7358SJed Brown 
6048de41b84cSMatthew 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)
6049de41b84cSMatthew G. Knepley {
6050de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6051de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6052de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6053de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
6054de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6055de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
60564ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6057de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6058de41b84cSMatthew G. Knepley 
6059de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6060de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6061de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
6062e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
6063de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
6064e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
6065de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6066e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6067de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6068e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6069de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6070de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6071de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6072de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6073de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
6074de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
6075de41b84cSMatthew G. Knepley   /* Column indices */
6076de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
60774ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6078de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6079de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6080de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6081de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6082de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6083de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6084de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6085de41b84cSMatthew G. Knepley       ++q;
6086de41b84cSMatthew G. Knepley     }
6087de41b84cSMatthew G. Knepley   }
6088de41b84cSMatthew G. Knepley   numCPoints = q;
6089de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6090de41b84cSMatthew G. Knepley     PetscInt fdof;
6091de41b84cSMatthew G. Knepley 
6092de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
60934ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6094de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6095de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6096de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6097de41b84cSMatthew G. Knepley     }
6098de41b84cSMatthew G. Knepley     numCIndices += dof;
6099de41b84cSMatthew G. Knepley   }
6100de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6101de41b84cSMatthew G. Knepley   /* Row indices */
6102de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
6103de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
610469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6105de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6106de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6107de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6108de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6109de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6110de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6111de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
61124ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
61134ca5e9f5SMatthew G. Knepley         if (!dof) continue;
61144ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
61154ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6116de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6117de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6118de41b84cSMatthew G. Knepley         ++q;
6119de41b84cSMatthew G. Knepley       }
6120de41b84cSMatthew G. Knepley     }
6121de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6122de41b84cSMatthew G. Knepley   }
6123de41b84cSMatthew G. Knepley   numFPoints = q;
6124de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6125de41b84cSMatthew G. Knepley     PetscInt fdof;
6126de41b84cSMatthew G. Knepley 
6127de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
61284ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6129de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6130de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6131de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6132de41b84cSMatthew G. Knepley     }
6133de41b84cSMatthew G. Knepley     numFIndices += dof;
6134de41b84cSMatthew G. Knepley   }
6135de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6136de41b84cSMatthew G. Knepley 
61378ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
61388ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
613969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
614069291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6141de41b84cSMatthew G. Knepley   if (numFields) {
61424acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
61434acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
61444acb8e1eSToby Isaac 
61454acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
61464acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
61474acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6148de41b84cSMatthew G. Knepley     }
61494acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
61504acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
6151367003a6SStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);CHKERRQ(ierr);
61524acb8e1eSToby Isaac     }
61534acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
61544acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
6155367003a6SStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);CHKERRQ(ierr);
61564acb8e1eSToby Isaac     }
61574acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
61584acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
61594acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6160de41b84cSMatthew G. Knepley     }
6161de41b84cSMatthew G. Knepley   } else {
61624acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
61634acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
61644acb8e1eSToby Isaac 
61654acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
61664acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
61674acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
61684acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
61694acb8e1eSToby Isaac 
61704acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
61714acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
6172de41b84cSMatthew G. Knepley     }
61734acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
61744acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
61754acb8e1eSToby Isaac 
61764acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
61774acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
6178de41b84cSMatthew G. Knepley     }
61794acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
61804acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6181de41b84cSMatthew G. Knepley   }
6182de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
61834acb8e1eSToby Isaac   /* TODO: flips */
6184de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6185de41b84cSMatthew G. Knepley   if (ierr) {
6186de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6187de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6188de41b84cSMatthew G. Knepley 
6189de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6190e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6191de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
619269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
619369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6194de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6195de41b84cSMatthew G. Knepley   }
619669291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6197de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
619869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
619969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6200de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6201de41b84cSMatthew G. Knepley }
6202de41b84cSMatthew G. Knepley 
62037c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
62047c927364SMatthew G. Knepley {
62057c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
62067c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
62077c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
62087c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
62097c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
62107c927364SMatthew G. Knepley   PetscErrorCode ierr;
62117c927364SMatthew G. Knepley 
62127c927364SMatthew G. Knepley   PetscFunctionBegin;
62137c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
62147c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
6215e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
62167c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
6217e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
62187c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6219e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
62207c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6221e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
62227c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
62237c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
62247c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
62257c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
62267c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
62277c927364SMatthew G. Knepley   /* Column indices */
62287c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
62297c927364SMatthew G. Knepley   maxFPoints = numCPoints;
62307c927364SMatthew G. Knepley   /* Compress out points not in the section */
62317c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
62327c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
62337c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
62347c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
62357c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
62367c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
62377c927364SMatthew G. Knepley       ++q;
62387c927364SMatthew G. Knepley     }
62397c927364SMatthew G. Knepley   }
62407c927364SMatthew G. Knepley   numCPoints = q;
62417c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
62427c927364SMatthew G. Knepley     PetscInt fdof;
62437c927364SMatthew G. Knepley 
62447c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
62457c927364SMatthew G. Knepley     if (!dof) continue;
62467c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
62477c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
62487c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
62497c927364SMatthew G. Knepley     }
62507c927364SMatthew G. Knepley     numCIndices += dof;
62517c927364SMatthew G. Knepley   }
62527c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
62537c927364SMatthew G. Knepley   /* Row indices */
62547c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
62557c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
625669291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
62577c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
62587c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
62597c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
62607c927364SMatthew G. Knepley     /* Compress out points not in the section */
62617c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
62627c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
62637c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
62647c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
62657c927364SMatthew G. Knepley         if (!dof) continue;
62667c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
62677c927364SMatthew G. Knepley         if (s < q) continue;
62687c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
62697c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
62707c927364SMatthew G. Knepley         ++q;
62717c927364SMatthew G. Knepley       }
62727c927364SMatthew G. Knepley     }
62737c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
62747c927364SMatthew G. Knepley   }
62757c927364SMatthew G. Knepley   numFPoints = q;
62767c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
62777c927364SMatthew G. Knepley     PetscInt fdof;
62787c927364SMatthew G. Knepley 
62797c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
62807c927364SMatthew G. Knepley     if (!dof) continue;
62817c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
62827c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
62837c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
62847c927364SMatthew G. Knepley     }
62857c927364SMatthew G. Knepley     numFIndices += dof;
62867c927364SMatthew G. Knepley   }
62877c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
62887c927364SMatthew G. Knepley 
62898ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
62908ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
62917c927364SMatthew G. Knepley   if (numFields) {
62924acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
62934acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
62944acb8e1eSToby Isaac 
62954acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
62964acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
62974acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
62987c927364SMatthew G. Knepley     }
62994acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
63004acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
63014acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
63024acb8e1eSToby Isaac     }
63034acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
63044acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
63054acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
63064acb8e1eSToby Isaac     }
63074acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
63084acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
63094acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
63107c927364SMatthew G. Knepley     }
63117c927364SMatthew G. Knepley   } else {
63124acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
63134acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
63144acb8e1eSToby Isaac 
63154acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
63164acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
63174acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
63184acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
63194acb8e1eSToby Isaac 
63204acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
63214acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
63227c927364SMatthew G. Knepley     }
63234acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
63244acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
63254acb8e1eSToby Isaac 
63264acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
63274acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
63287c927364SMatthew G. Knepley     }
63294acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
63304acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
63317c927364SMatthew G. Knepley   }
633269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
63337c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
63347c927364SMatthew G. Knepley   PetscFunctionReturn(0);
63357c927364SMatthew G. Knepley }
63367c927364SMatthew G. Knepley 
6337f96281f8SMatthew G. Knepley /*@
6338f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6339f96281f8SMatthew G. Knepley 
6340f96281f8SMatthew G. Knepley   Input Parameter:
6341f96281f8SMatthew G. Knepley . dm - The DMPlex object
6342f96281f8SMatthew G. Knepley 
6343f96281f8SMatthew G. Knepley   Output Parameters:
6344f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6345dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6346dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6347dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6348f96281f8SMatthew G. Knepley 
6349f96281f8SMatthew G. Knepley   Level: developer
6350f96281f8SMatthew G. Knepley 
6351dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6352f96281f8SMatthew G. Knepley @*/
6353770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6354552f7358SJed Brown {
6355552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6356770b213bSMatthew G Knepley   PetscInt       dim;
6357770b213bSMatthew G Knepley   PetscErrorCode ierr;
6358552f7358SJed Brown 
6359552f7358SJed Brown   PetscFunctionBegin;
6360552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6361c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
63627d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6363770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
63647d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6365770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6366770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6367552f7358SJed Brown   PetscFunctionReturn(0);
6368552f7358SJed Brown }
6369552f7358SJed Brown 
6370aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
63714a3e9fdbSToby Isaac {
63724a3e9fdbSToby Isaac   IS             is, his;
63737d5acc75SStefano Zampini   PetscInt       first = 0, stride;
63744a3e9fdbSToby Isaac   PetscBool      isStride;
63754a3e9fdbSToby Isaac   PetscErrorCode ierr;
63764a3e9fdbSToby Isaac 
63774a3e9fdbSToby Isaac   PetscFunctionBegin;
63784a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
63794a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
63804a3e9fdbSToby Isaac   if (isStride) {
63814a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
63824a3e9fdbSToby Isaac   }
63837d5acc75SStefano Zampini   if (is && (!isStride || stride != 1)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM is not stratified: depth %D IS is not contiguous", d);
63844a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6385aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
63864a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
63874a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
63884a3e9fdbSToby Isaac   PetscFunctionReturn(0);
63894a3e9fdbSToby Isaac }
63904a3e9fdbSToby Isaac 
6391dc5a3409SMatthew G. Knepley /*@
6392dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6393dc5a3409SMatthew G. Knepley 
6394dc5a3409SMatthew G. Knepley   Input Parameters:
6395dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6396dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6397dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6398dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6399dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6400dc5a3409SMatthew G. Knepley 
6401dc5a3409SMatthew G. Knepley   Level: developer
6402dc5a3409SMatthew G. Knepley 
6403dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6404dc5a3409SMatthew G. Knepley @*/
6405770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6406552f7358SJed Brown {
6407552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6408770b213bSMatthew G Knepley   PetscInt       dim;
6409770b213bSMatthew G Knepley   PetscErrorCode ierr;
6410552f7358SJed Brown 
6411552f7358SJed Brown   PetscFunctionBegin;
6412552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6413c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64147d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6415770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
64167d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6417770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6418770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6419552f7358SJed Brown   PetscFunctionReturn(0);
6420552f7358SJed Brown }
6421552f7358SJed Brown 
64227cd05799SMatthew G. Knepley /*@C
64237cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
64247cd05799SMatthew G. Knepley 
64257cd05799SMatthew G. Knepley   Input Parameter:
64267cd05799SMatthew G. Knepley . dm   - The DMPlex object
64277cd05799SMatthew G. Knepley 
64287cd05799SMatthew G. Knepley   Output Parameter:
64297cd05799SMatthew G. Knepley . cellHeight - The height of a cell
64307cd05799SMatthew G. Knepley 
64317cd05799SMatthew G. Knepley   Level: developer
64327cd05799SMatthew G. Knepley 
64337cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
64347cd05799SMatthew G. Knepley @*/
6435552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6436552f7358SJed Brown {
6437552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6438552f7358SJed Brown 
6439552f7358SJed Brown   PetscFunctionBegin;
6440552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6441552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6442552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6443552f7358SJed Brown   PetscFunctionReturn(0);
6444552f7358SJed Brown }
6445552f7358SJed Brown 
64467cd05799SMatthew G. Knepley /*@C
64477cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
64487cd05799SMatthew G. Knepley 
64497cd05799SMatthew G. Knepley   Input Parameters:
64507cd05799SMatthew G. Knepley + dm   - The DMPlex object
64517cd05799SMatthew G. Knepley - cellHeight - The height of a cell
64527cd05799SMatthew G. Knepley 
64537cd05799SMatthew G. Knepley   Level: developer
64547cd05799SMatthew G. Knepley 
64557cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
64567cd05799SMatthew G. Knepley @*/
6457552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6458552f7358SJed Brown {
6459552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6460552f7358SJed Brown 
6461552f7358SJed Brown   PetscFunctionBegin;
6462552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6463552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6464552f7358SJed Brown   PetscFunctionReturn(0);
6465552f7358SJed Brown }
6466552f7358SJed Brown 
6467552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6468ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6469552f7358SJed Brown {
6470552f7358SJed Brown   PetscSection   section, globalSection;
6471552f7358SJed Brown   PetscInt      *numbers, p;
6472552f7358SJed Brown   PetscErrorCode ierr;
6473552f7358SJed Brown 
6474552f7358SJed Brown   PetscFunctionBegin;
647582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6476552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6477552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6478552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6479552f7358SJed Brown   }
6480552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
648115b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6482854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6483552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6484552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6485ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6486ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6487552f7358SJed Brown   }
648882f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6489ef48cebcSMatthew G. Knepley   if (globalSize) {
6490ef48cebcSMatthew G. Knepley     PetscLayout layout;
6491ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6492ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6493ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6494ef48cebcSMatthew G. Knepley   }
6495552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6496552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6497552f7358SJed Brown   PetscFunctionReturn(0);
6498552f7358SJed Brown }
6499552f7358SJed Brown 
650081ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6501552f7358SJed Brown {
6502552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6503552f7358SJed Brown   PetscErrorCode ierr;
6504552f7358SJed Brown 
6505552f7358SJed Brown   PetscFunctionBegin;
6506552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6507552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
65080298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
650981ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
651081ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
651181ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6512552f7358SJed Brown }
651381ed3555SMatthew G. Knepley 
65148dab3259SMatthew G. Knepley /*@
65157cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
65167cd05799SMatthew G. Knepley 
65177cd05799SMatthew G. Knepley   Input Parameter:
65187cd05799SMatthew G. Knepley . dm   - The DMPlex object
65197cd05799SMatthew G. Knepley 
65207cd05799SMatthew G. Knepley   Output Parameter:
65217cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
65227cd05799SMatthew G. Knepley 
65237cd05799SMatthew G. Knepley   Level: developer
65247cd05799SMatthew G. Knepley 
65257cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
65267cd05799SMatthew G. Knepley @*/
652781ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
652881ed3555SMatthew G. Knepley {
652981ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
653081ed3555SMatthew G. Knepley   PetscErrorCode ierr;
653181ed3555SMatthew G. Knepley 
653281ed3555SMatthew G. Knepley   PetscFunctionBegin;
653381ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
653481ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6535552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6536552f7358SJed Brown   PetscFunctionReturn(0);
6537552f7358SJed Brown }
6538552f7358SJed Brown 
653981ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
654081ed3555SMatthew G. Knepley {
654181ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
654281ed3555SMatthew G. Knepley   PetscErrorCode ierr;
654381ed3555SMatthew G. Knepley 
654481ed3555SMatthew G. Knepley   PetscFunctionBegin;
654581ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
654681ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
654781ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
654881ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
654981ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
655081ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
655181ed3555SMatthew G. Knepley }
655281ed3555SMatthew G. Knepley 
65538dab3259SMatthew G. Knepley /*@
65547cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
65557cd05799SMatthew G. Knepley 
65567cd05799SMatthew G. Knepley   Input Parameter:
65577cd05799SMatthew G. Knepley . dm   - The DMPlex object
65587cd05799SMatthew G. Knepley 
65597cd05799SMatthew G. Knepley   Output Parameter:
65607cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
65617cd05799SMatthew G. Knepley 
65627cd05799SMatthew G. Knepley   Level: developer
65637cd05799SMatthew G. Knepley 
65647cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
65657cd05799SMatthew G. Knepley @*/
6566552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6567552f7358SJed Brown {
6568552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6569552f7358SJed Brown   PetscErrorCode ierr;
6570552f7358SJed Brown 
6571552f7358SJed Brown   PetscFunctionBegin;
6572552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
657381ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6574552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6575552f7358SJed Brown   PetscFunctionReturn(0);
6576552f7358SJed Brown }
6577552f7358SJed Brown 
65788dab3259SMatthew G. Knepley /*@
65797cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
65807cd05799SMatthew G. Knepley 
65817cd05799SMatthew G. Knepley   Input Parameter:
65827cd05799SMatthew G. Knepley . dm   - The DMPlex object
65837cd05799SMatthew G. Knepley 
65847cd05799SMatthew G. Knepley   Output Parameter:
65857cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
65867cd05799SMatthew G. Knepley 
65877cd05799SMatthew G. Knepley   Level: developer
65887cd05799SMatthew G. Knepley 
65897cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
65907cd05799SMatthew G. Knepley @*/
6591ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6592ef48cebcSMatthew G. Knepley {
6593ef48cebcSMatthew G. Knepley   IS             nums[4];
6594862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6595ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6596ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6597ef48cebcSMatthew G. Knepley 
6598ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6599ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6600ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
66018abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
66028abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6603862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6604862913ffSStefano Zampini     PetscInt end;
6605862913ffSStefano Zampini 
6606862913ffSStefano Zampini     depths[d] = depth-d;
6607862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6608862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6609862913ffSStefano Zampini   }
6610862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6611862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6612862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6613862913ffSStefano Zampini     if (starts[d] >= 0 && depths[d] != gdepths[d]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected depth %D, found %D",depths[d],gdepths[d]);
6614862913ffSStefano Zampini   }
6615ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6616ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6617ef48cebcSMatthew G. Knepley 
6618862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
6619ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6620ef48cebcSMatthew G. Knepley     shift += gsize;
6621ef48cebcSMatthew G. Knepley   }
6622302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6623ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6624ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6625ef48cebcSMatthew G. Knepley }
6626ef48cebcSMatthew G. Knepley 
662708a22f4bSMatthew G. Knepley 
662808a22f4bSMatthew G. Knepley /*@
662908a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
663008a22f4bSMatthew G. Knepley 
663108a22f4bSMatthew G. Knepley   Input Parameter:
663208a22f4bSMatthew G. Knepley . dm - The DMPlex object
663308a22f4bSMatthew G. Knepley 
663408a22f4bSMatthew G. Knepley   Output Parameter:
663508a22f4bSMatthew G. Knepley . ranks - The rank field
663608a22f4bSMatthew G. Knepley 
663708a22f4bSMatthew G. Knepley   Options Database Keys:
663808a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
663908a22f4bSMatthew G. Knepley 
664008a22f4bSMatthew G. Knepley   Level: intermediate
664108a22f4bSMatthew G. Knepley 
664208a22f4bSMatthew G. Knepley .seealso: DMView()
664308a22f4bSMatthew G. Knepley @*/
664408a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
664508a22f4bSMatthew G. Knepley {
664608a22f4bSMatthew G. Knepley   DM             rdm;
664708a22f4bSMatthew G. Knepley   PetscDS        prob;
664808a22f4bSMatthew G. Knepley   PetscFE        fe;
664908a22f4bSMatthew G. Knepley   PetscScalar   *r;
665008a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
665108a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
665208a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
665308a22f4bSMatthew G. Knepley 
665408a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6655f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6656f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
665708a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
665808a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
665908a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6660f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
666108a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
666208a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
666308a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
666408a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
666508a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
666608a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
666708a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
666808a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
666908a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
667008a22f4bSMatthew G. Knepley     PetscScalar *lr;
667108a22f4bSMatthew G. Knepley 
667208a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
667308a22f4bSMatthew G. Knepley     *lr = rank;
667408a22f4bSMatthew G. Knepley   }
667508a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
667608a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
667708a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
667808a22f4bSMatthew G. Knepley }
667908a22f4bSMatthew G. Knepley 
6680ca8062c8SMatthew G. Knepley /*@
668118e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
668218e14f0cSMatthew G. Knepley 
668318e14f0cSMatthew G. Knepley   Input Parameters:
668418e14f0cSMatthew G. Knepley + dm    - The DMPlex
668518e14f0cSMatthew G. Knepley - label - The DMLabel
668618e14f0cSMatthew G. Knepley 
668718e14f0cSMatthew G. Knepley   Output Parameter:
668818e14f0cSMatthew G. Knepley . val - The label value field
668918e14f0cSMatthew G. Knepley 
669018e14f0cSMatthew G. Knepley   Options Database Keys:
669118e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
669218e14f0cSMatthew G. Knepley 
669318e14f0cSMatthew G. Knepley   Level: intermediate
669418e14f0cSMatthew G. Knepley 
669518e14f0cSMatthew G. Knepley .seealso: DMView()
669618e14f0cSMatthew G. Knepley @*/
669718e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
669818e14f0cSMatthew G. Knepley {
669918e14f0cSMatthew G. Knepley   DM             rdm;
670018e14f0cSMatthew G. Knepley   PetscDS        prob;
670118e14f0cSMatthew G. Knepley   PetscFE        fe;
670218e14f0cSMatthew G. Knepley   PetscScalar   *v;
670318e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
670418e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
670518e14f0cSMatthew G. Knepley 
670618e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
670718e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
670818e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
670918e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
671018e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
671118e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
671218e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
671318e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
671418e14f0cSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
671518e14f0cSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
671618e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
671718e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
671818e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
6719effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
672018e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
672118e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
672218e14f0cSMatthew G. Knepley     PetscScalar *lv;
672318e14f0cSMatthew G. Knepley     PetscInt     cval;
672418e14f0cSMatthew G. Knepley 
672518e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
672618e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
672718e14f0cSMatthew G. Knepley     *lv = cval;
672818e14f0cSMatthew G. Knepley   }
672918e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
673018e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
673118e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
673218e14f0cSMatthew G. Knepley }
673318e14f0cSMatthew G. Knepley 
673418e14f0cSMatthew G. Knepley /*@
6735ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6736ca8062c8SMatthew G. Knepley 
673769916449SMatthew G. Knepley   Input Parameter:
673869916449SMatthew G. Knepley . dm - The DMPlex object
6739ca8062c8SMatthew G. Knepley 
6740ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6741ca8062c8SMatthew G. Knepley 
6742ca8062c8SMatthew G. Knepley   Level: developer
6743ca8062c8SMatthew G. Knepley 
67447d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6745ca8062c8SMatthew G. Knepley @*/
6746ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6747ca8062c8SMatthew G. Knepley {
6748ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6749ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6750ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
675157beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
675257beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
6753ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6754ca8062c8SMatthew G. Knepley 
6755ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6756ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6757ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6758ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6759ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6760ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6761ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6762ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6763ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6764ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
676542e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
676642e66dfaSMatthew G. Knepley       PetscInt  d;
676742e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
676842e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
676942e66dfaSMatthew G. Knepley       }
6770ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6771ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6772ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6773ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6774ca8062c8SMatthew G. Knepley       }
677542e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
67768ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6777ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
67788ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6779ca8062c8SMatthew G. Knepley         }
6780302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
67818ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6782ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
67838ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6784ca8062c8SMatthew G. Knepley         }
6785302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
67868ccfff9cSToby Isaac         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not repeatedly found in support of repeated cone point %D", p, cone[c]);
67878ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6788ca8062c8SMatthew G. Knepley       }
678942e66dfaSMatthew G. Knepley     }
679057beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
679157beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6792ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6793ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6794ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6795ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6796ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6797ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
679857beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
679957beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
6800ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6801ca8062c8SMatthew G. Knepley       }
6802ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
68038ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6804ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
68058ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6806ca8062c8SMatthew G. Knepley         }
6807302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68088ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6809ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
68108ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6811ca8062c8SMatthew G. Knepley         }
6812302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68138ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6814ca8062c8SMatthew G. Knepley       }
6815ca8062c8SMatthew G. Knepley     }
6816ca8062c8SMatthew G. Knepley   }
681757beb4faSStefano Zampini   if (storagecheck) {
6818ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6819ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
68208ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
682157beb4faSStefano Zampini   }
6822ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6823ca8062c8SMatthew G. Knepley }
6824ca8062c8SMatthew G. Knepley 
6825ca8062c8SMatthew G. Knepley /*@
6826ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6827ca8062c8SMatthew G. Knepley 
6828ca8062c8SMatthew G. Knepley   Input Parameters:
6829ca8062c8SMatthew G. Knepley + dm - The DMPlex object
683058723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
683158723a97SMatthew G. Knepley - cellHeight - Normally 0
6832ca8062c8SMatthew G. Knepley 
6833ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6834ca8062c8SMatthew G. Knepley 
6835ca8062c8SMatthew G. Knepley   Level: developer
6836ca8062c8SMatthew G. Knepley 
68377d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6838ca8062c8SMatthew G. Knepley @*/
683958723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6840ca8062c8SMatthew G. Knepley {
684142363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6842ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6843ca8062c8SMatthew G. Knepley 
6844ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6845ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6846c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6847ca8062c8SMatthew G. Knepley   switch (dim) {
684842363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
684942363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
685042363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6851ca8062c8SMatthew G. Knepley   default:
68528ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6853ca8062c8SMatthew G. Knepley   }
685458723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
685558723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6856ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6857ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6858ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
685958723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
686058723a97SMatthew G. Knepley 
686158723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
686258723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
686358723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
686458723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
686558723a97SMatthew G. Knepley     }
686658723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
68678ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6868ca8062c8SMatthew G. Knepley   }
686942363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
687042363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
687142363296SMatthew G. Knepley 
687242363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
687342363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
687442363296SMatthew G. Knepley       const PetscInt p = closure[cl];
687542363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
687642363296SMatthew G. Knepley     }
687742363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
68788ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
687942363296SMatthew G. Knepley   }
6880ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6881ca8062c8SMatthew G. Knepley }
68829bf0dad6SMatthew G. Knepley 
68839bf0dad6SMatthew G. Knepley /*@
68849bf0dad6SMatthew 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
68859bf0dad6SMatthew G. Knepley 
68869bf0dad6SMatthew G. Knepley   Input Parameters:
68879bf0dad6SMatthew G. Knepley + dm - The DMPlex object
68889bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
68899bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
68909bf0dad6SMatthew G. Knepley 
68919bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
68929bf0dad6SMatthew G. Knepley 
68939bf0dad6SMatthew G. Knepley   Level: developer
68949bf0dad6SMatthew G. Knepley 
68957d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
68969bf0dad6SMatthew G. Knepley @*/
68979bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
68989bf0dad6SMatthew G. Knepley {
68993554e41dSMatthew G. Knepley   PetscInt       pMax[4];
69003554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
69019bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
69029bf0dad6SMatthew G. Knepley 
69039bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
69049bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6905c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
69069bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
690725abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
69083554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
69093554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
69103554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
69119bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
69129bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
69139bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
69149bf0dad6SMatthew G. Knepley 
691525abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
69169bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
69179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
69189bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
69199bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
69209bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
69219bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
69229bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
69239bf0dad6SMatthew G. Knepley       }
69243554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
69258ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
69269bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
69279bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
69289bf0dad6SMatthew G. Knepley 
69299bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
69309bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
69319bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
69329bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
69339bf0dad6SMatthew G. Knepley         }
69348ccfff9cSToby Isaac         if (fnumCorners != faceSize) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%D) of cell %D has %D vertices but should have %D", cone[f], f, c, fnumCorners, faceSize);
69359bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
69368ccfff9cSToby Isaac           if (fclosure[v] != faces[f*faceSize+v]) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%d) of cell %D vertex %D, %D != %D", cone[f], f, c, v, fclosure[v], faces[f*faceSize+v]);
69379bf0dad6SMatthew G. Knepley         }
69389bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
69399bf0dad6SMatthew G. Knepley       }
69409bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
69419bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
69429bf0dad6SMatthew G. Knepley     }
69433554e41dSMatthew G. Knepley   }
6944552f7358SJed Brown   PetscFunctionReturn(0);
6945552f7358SJed Brown }
69463913d7c8SMatthew G. Knepley 
694703da9461SVaclav Hapla static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
694803da9461SVaclav Hapla {
694903da9461SVaclav Hapla   PetscInt i,l,n;
695003da9461SVaclav Hapla   const PetscInt *cone;
695103da9461SVaclav Hapla   PetscErrorCode ierr;
695203da9461SVaclav Hapla 
695303da9461SVaclav Hapla   PetscFunctionBegin;
695403da9461SVaclav Hapla   *missingPoint = -1;
695503da9461SVaclav Hapla   ierr = DMPlexGetConeSize(dm, p, &n);CHKERRQ(ierr);
695603da9461SVaclav Hapla   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
695703da9461SVaclav Hapla   for (i=0; i<n; i++) {
695803da9461SVaclav Hapla     ierr = PetscFindInt(cone[i], npoints, points, &l);CHKERRQ(ierr);
695903da9461SVaclav Hapla     if (l < 0) {
696003da9461SVaclav Hapla       *missingPoint = cone[i];
696103da9461SVaclav Hapla       break;
696203da9461SVaclav Hapla     }
696303da9461SVaclav Hapla   }
696403da9461SVaclav Hapla   PetscFunctionReturn(0);
696503da9461SVaclav Hapla }
696603da9461SVaclav Hapla 
696703da9461SVaclav Hapla /*@
696803da9461SVaclav Hapla   DMPlexCheckPointSF - Check that several sufficient conditions are met for the point SF of this plex.
696903da9461SVaclav Hapla 
697003da9461SVaclav Hapla   Input Parameters:
697103da9461SVaclav Hapla . dm - The DMPlex object
697203da9461SVaclav Hapla 
697303da9461SVaclav Hapla   Note: This is mainly intended for debugging/testing purposes.
697403da9461SVaclav Hapla 
697503da9461SVaclav Hapla   Level: developer
697603da9461SVaclav Hapla 
697703da9461SVaclav Hapla .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
697803da9461SVaclav Hapla @*/
697903da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
698003da9461SVaclav Hapla {
698103da9461SVaclav Hapla   PetscSF sf;
698203da9461SVaclav Hapla   PetscInt d,depth,i,nleaves,p,plo,phi,missingPoint;
698303da9461SVaclav Hapla   const PetscInt *locals;
698403da9461SVaclav Hapla   PetscErrorCode ierr;
698503da9461SVaclav Hapla 
698603da9461SVaclav Hapla   PetscFunctionBegin;
698703da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
698803da9461SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
698903da9461SVaclav Hapla   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
699003da9461SVaclav Hapla   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &locals, NULL);CHKERRQ(ierr);
699103da9461SVaclav Hapla 
6992ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
699303da9461SVaclav Hapla   ierr = DMPlexGetVTKCellHeight(dm, &d);CHKERRQ(ierr);
699403da9461SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, d, &plo, &phi);CHKERRQ(ierr);
699503da9461SVaclav Hapla   for (i=0; i<nleaves; i++) {
699603da9461SVaclav Hapla     p = locals[i];
699703da9461SVaclav Hapla     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
699803da9461SVaclav Hapla   }
6999ece87651SVaclav Hapla 
7000ece87651SVaclav Hapla   /* 2) if some point is in interface, then all its cone points must be also in interface  */
7001ece87651SVaclav Hapla   for (i=0; i<nleaves; i++) {
7002ece87651SVaclav Hapla     p = locals[i];
7003ece87651SVaclav Hapla     ierr = DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);CHKERRQ(ierr);
7004ece87651SVaclav Hapla     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
7005ece87651SVaclav Hapla   }
700603da9461SVaclav Hapla   PetscFunctionReturn(0);
700703da9461SVaclav Hapla }
700803da9461SVaclav Hapla 
7009bceba477SMatthew G. Knepley /* Pointwise interpolation
7010bceba477SMatthew G. Knepley      Just code FEM for now
7011bceba477SMatthew G. Knepley      u^f = I u^c
70124ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
70134ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
70144ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
7015bceba477SMatthew G. Knepley */
7016bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7017bceba477SMatthew G. Knepley {
7018bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
7019bceba477SMatthew G. Knepley   PetscInt       m, n;
7020a063dac3SMatthew G. Knepley   void          *ctx;
702168132eb9SMatthew G. Knepley   DM             cdm;
7022fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
7023bceba477SMatthew G. Knepley   PetscErrorCode ierr;
7024bceba477SMatthew G. Knepley 
7025bceba477SMatthew G. Knepley   PetscFunctionBegin;
7026e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7027bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7028e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7029bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
703068132eb9SMatthew G. Knepley 
7031fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
7032bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
7033bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7034fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
7035a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
703668132eb9SMatthew G. Knepley 
7037a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
703868132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
703968132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
704068132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
704168132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
70424db47ee9SStefano Zampini   if (scaling) {
70435d1c2e58SMatthew G. Knepley     /* Use naive scaling */
70445d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
70454db47ee9SStefano Zampini   }
7046a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
7047a063dac3SMatthew G. Knepley }
7048bceba477SMatthew G. Knepley 
70496dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7050a063dac3SMatthew G. Knepley {
705190748bafSMatthew G. Knepley   PetscErrorCode ierr;
70526dbf9973SLawrence Mitchell   VecScatter     ctx;
705390748bafSMatthew G. Knepley 
7054a063dac3SMatthew G. Knepley   PetscFunctionBegin;
70556dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
70566dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
70576dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
7058bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7059bceba477SMatthew G. Knepley }
7060bceba477SMatthew G. Knepley 
7061bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7062bd041c0cSMatthew G. Knepley {
7063bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
7064bd041c0cSMatthew G. Knepley   PetscInt       m, n;
7065bd041c0cSMatthew G. Knepley   void          *ctx;
7066bd041c0cSMatthew G. Knepley   DM             cdm;
7067bd041c0cSMatthew G. Knepley   PetscBool      regular;
7068bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
7069bd041c0cSMatthew G. Knepley 
7070bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
7071e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7072bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7073e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7074bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
7075bd041c0cSMatthew G. Knepley 
7076bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
7077bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7078bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
7079bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
7080bd041c0cSMatthew G. Knepley 
7081bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
7082bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
7083bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7084bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7085bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
7086bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
7087bd041c0cSMatthew G. Knepley }
7088bd041c0cSMatthew G. Knepley 
7089fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
7090fd59a867SMatthew G. Knepley {
7091fd59a867SMatthew G. Knepley   PetscSection   section;
7092ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
7093ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
7094286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
7095ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
7096ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
7097fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
7098fd59a867SMatthew G. Knepley 
7099fd59a867SMatthew G. Knepley   PetscFunctionBegin;
7100ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
7101ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
7102ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
7103ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
7104ebb3236fSMatthew G. Knepley     PetscObject  obj;
7105ebb3236fSMatthew G. Knepley     PetscClassId id;
7106ebb3236fSMatthew G. Knepley 
7107ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
7108ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
7109ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
7110ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
71118ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
7112ebb3236fSMatthew G. Knepley   }
71132f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
7114286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7115c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7116ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7117ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
7118dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
7119fdafae62SVaclav Hapla   if (!numFields && numBd) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "number of fields is zero and number of boundary conditions is nonzero (this should never happen)");
7120fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
71212f900235SMatthew G. Knepley     PetscInt                field;
7122f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
7123385532a5SToby Isaac     const char             *labelName;
7124385532a5SToby Isaac     DMLabel                 label;
71252f900235SMatthew G. Knepley 
7126f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
7127385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
7128f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
7129fd59a867SMatthew G. Knepley   }
71302f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
7131ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
71326c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
7133ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
7134ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
7135ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
7136ebb3236fSMatthew G. Knepley 
7137ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
7138ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
7139ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
7140ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
7141ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
7142ebb3236fSMatthew G. Knepley   }
7143ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
7144ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
7145fd59a867SMatthew G. Knepley     const char             *bdLabel;
7146fd59a867SMatthew G. Knepley     DMLabel                 label;
71470e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
7148fd59a867SMatthew G. Knepley     const PetscInt         *values;
71490e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
7150f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
7151f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
7152fd59a867SMatthew G. Knepley 
7153f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
7154c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
7155385532a5SToby Isaac     if (!isFE[field] || !label) continue;
7156ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
71577391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
71587391c1cbSMatthew G. Knepley       const char *bdname;
7159dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
71607391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
71617391c1cbSMatthew G. Knepley       if (duplicate) break;
71627391c1cbSMatthew G. Knepley     }
7163ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
7164b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
7165092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
71667391c1cbSMatthew G. Knepley     }
7167ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
7168f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
716993a5981aSMatthew G. Knepley       PetscInt       *newidx;
7170ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
717193a5981aSMatthew G. Knepley 
7172fd59a867SMatthew G. Knepley       bcFields[bc] = field;
71730e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
7174ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
7175ebb3236fSMatthew G. Knepley         IS              tmp;
7176ebb3236fSMatthew G. Knepley         const PetscInt *idx;
7177ebb3236fSMatthew G. Knepley 
7178c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
7179ebb3236fSMatthew G. Knepley         if (!tmp) continue;
718093a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
718193a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
7182ebb3236fSMatthew G. Knepley         if (isFE[field]) {
718393a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
7184ebb3236fSMatthew G. Knepley         } else {
7185ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
7186ebb3236fSMatthew G. Knepley         }
718793a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
718893a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
7189fd59a867SMatthew G. Knepley       }
7190ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
7191ebb3236fSMatthew G. Knepley       newn = 0;
7192ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
7193ebb3236fSMatthew G. Knepley         IS              tmp;
7194ebb3236fSMatthew G. Knepley         const PetscInt *idx;
7195ebb3236fSMatthew G. Knepley 
7196c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
7197ebb3236fSMatthew G. Knepley         if (!tmp) continue;
7198ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
7199ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
7200ebb3236fSMatthew G. Knepley         if (isFE[field]) {
7201ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
7202ebb3236fSMatthew G. Knepley         } else {
7203ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
7204ebb3236fSMatthew G. Knepley         }
7205ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
7206ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
7207ebb3236fSMatthew G. Knepley       }
7208ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
7209ebb3236fSMatthew G. Knepley     }
7210fd59a867SMatthew G. Knepley   }
7211fd59a867SMatthew G. Knepley   /* Handle discretization */
7212ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
7213fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
72140bacfa0aSMatthew G. Knepley     PetscObject obj;
72150bacfa0aSMatthew G. Knepley 
72160bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
7217ebb3236fSMatthew G. Knepley     if (isFE[f]) {
72180bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
7219286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
7220fd59a867SMatthew G. Knepley       PetscInt        d;
7221fd59a867SMatthew G. Knepley 
7222fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
7223286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
7224286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
7225ebb3236fSMatthew G. Knepley     } else {
72260bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
72270bacfa0aSMatthew G. Knepley 
72280bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
72290bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
7230ebb3236fSMatthew G. Knepley     }
7231fd59a867SMatthew G. Knepley   }
7232286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
7233286d8613SMatthew G. Knepley     PetscInt d;
7234286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
7235286d8613SMatthew G. Knepley       if ((numDof[f*(dim+1)+d] > 0) && (depth < dim)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated when unknowns are specified on edges or faces.");
7236286d8613SMatthew G. Knepley     }
7237286d8613SMatthew G. Knepley   }
7238ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
7239fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
7240fd59a867SMatthew G. Knepley     PetscFE     fe;
7241fd59a867SMatthew G. Knepley     const char *name;
724218abd763SMatthew G. Knepley 
724318abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
7244fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
7245fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
7246fd59a867SMatthew G. Knepley   }
7247e87a4003SBarry Smith   ierr = DMSetSection(dm, section);CHKERRQ(ierr);
7248fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
72490e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
72500e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
7251286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
7252ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
7253bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7254bceba477SMatthew G. Knepley }
7255bceba477SMatthew G. Knepley 
72560aef6b92SMatthew G. Knepley /*@
72570aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
72580aef6b92SMatthew G. Knepley 
72590aef6b92SMatthew G. Knepley   Input Parameter:
72600aef6b92SMatthew G. Knepley . dm - The DMPlex object
72610aef6b92SMatthew G. Knepley 
72620aef6b92SMatthew G. Knepley   Output Parameter:
72630aef6b92SMatthew G. Knepley . regular - The flag
72640aef6b92SMatthew G. Knepley 
72650aef6b92SMatthew G. Knepley   Level: intermediate
72660aef6b92SMatthew G. Knepley 
72670aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
72680aef6b92SMatthew G. Knepley @*/
72690aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
72700aef6b92SMatthew G. Knepley {
72710aef6b92SMatthew G. Knepley   PetscFunctionBegin;
72720aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72730aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
72740aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
72750aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
72760aef6b92SMatthew G. Knepley }
72770aef6b92SMatthew G. Knepley 
72780aef6b92SMatthew G. Knepley /*@
72790aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
72800aef6b92SMatthew G. Knepley 
72810aef6b92SMatthew G. Knepley   Input Parameters:
72820aef6b92SMatthew G. Knepley + dm - The DMPlex object
72830aef6b92SMatthew G. Knepley - regular - The flag
72840aef6b92SMatthew G. Knepley 
72850aef6b92SMatthew G. Knepley   Level: intermediate
72860aef6b92SMatthew G. Knepley 
72870aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
72880aef6b92SMatthew G. Knepley @*/
72890aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
72900aef6b92SMatthew G. Knepley {
72910aef6b92SMatthew G. Knepley   PetscFunctionBegin;
72920aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72930aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
72940aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
72950aef6b92SMatthew G. Knepley }
72960aef6b92SMatthew G. Knepley 
7297f7c74593SToby Isaac /* anchors */
7298a68b90caSToby Isaac /*@
7299f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7300f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
7301a68b90caSToby Isaac 
7302e228b242SToby Isaac   not collective
7303a68b90caSToby Isaac 
7304a68b90caSToby Isaac   Input Parameters:
7305a68b90caSToby Isaac . dm - The DMPlex object
7306a68b90caSToby Isaac 
7307a68b90caSToby Isaac   Output Parameters:
7308a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7309a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7310a68b90caSToby Isaac 
7311a68b90caSToby Isaac 
7312a68b90caSToby Isaac   Level: intermediate
7313a68b90caSToby Isaac 
7314f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7315a68b90caSToby Isaac @*/
7316a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7317a68b90caSToby Isaac {
7318a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
731941e6d900SToby Isaac   PetscErrorCode ierr;
7320a68b90caSToby Isaac 
7321a68b90caSToby Isaac   PetscFunctionBegin;
7322a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
732341e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7324a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7325a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7326a68b90caSToby Isaac   PetscFunctionReturn(0);
7327a68b90caSToby Isaac }
7328a68b90caSToby Isaac 
7329a68b90caSToby Isaac /*@
7330f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7331f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7332a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7333a68b90caSToby Isaac 
7334a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7335f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7336a68b90caSToby Isaac 
7337e228b242SToby Isaac   collective on dm
7338a68b90caSToby Isaac 
7339a68b90caSToby Isaac   Input Parameters:
7340a68b90caSToby Isaac + dm - The DMPlex object
7341e228b242SToby 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).
7342e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7343a68b90caSToby Isaac 
7344a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7345a68b90caSToby Isaac 
7346a68b90caSToby Isaac   Level: intermediate
7347a68b90caSToby Isaac 
7348f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7349a68b90caSToby Isaac @*/
7350a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7351a68b90caSToby Isaac {
7352a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7353e228b242SToby Isaac   PetscMPIInt    result;
7354a68b90caSToby Isaac   PetscErrorCode ierr;
7355a68b90caSToby Isaac 
7356a68b90caSToby Isaac   PetscFunctionBegin;
7357a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7358e228b242SToby Isaac   if (anchorSection) {
7359e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7360e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7361f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7362e228b242SToby Isaac   }
7363e228b242SToby Isaac   if (anchorIS) {
7364e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7365e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7366f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7367e228b242SToby Isaac   }
7368a68b90caSToby Isaac 
7369a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7370a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7371a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7372a68b90caSToby Isaac 
7373a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7374a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7375a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7376a68b90caSToby Isaac 
7377a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7378a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7379a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7380a68b90caSToby Isaac     const PetscInt *anchors;
7381a68b90caSToby Isaac 
7382a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7383a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7384a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7385a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7386a68b90caSToby Isaac       PetscInt p;
7387a68b90caSToby Isaac 
7388a68b90caSToby Isaac       p = anchors[a];
7389a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7390a68b90caSToby Isaac         PetscInt dof;
7391a68b90caSToby Isaac 
7392a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7393a68b90caSToby Isaac         if (dof) {
7394a68b90caSToby Isaac           PetscErrorCode ierr2;
7395a68b90caSToby Isaac 
7396a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
73978ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7398a68b90caSToby Isaac         }
7399a68b90caSToby Isaac       }
7400a68b90caSToby Isaac     }
7401a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7402a68b90caSToby Isaac   }
7403a68b90caSToby Isaac #endif
7404f7c74593SToby Isaac   /* reset the generic constraints */
7405f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7406a68b90caSToby Isaac   PetscFunctionReturn(0);
7407a68b90caSToby Isaac }
7408a68b90caSToby Isaac 
7409f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7410a68b90caSToby Isaac {
7411f7c74593SToby Isaac   PetscSection anchorSection;
74126995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7413a68b90caSToby Isaac   PetscErrorCode ierr;
7414a68b90caSToby Isaac 
7415a68b90caSToby Isaac   PetscFunctionBegin;
7416a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7417a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7418e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7419a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
74206995de1eSToby Isaac   if (numFields) {
7421719ab38cSToby Isaac     PetscInt f;
7422a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7423719ab38cSToby Isaac 
7424719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7425719ab38cSToby Isaac       PetscInt numComp;
7426719ab38cSToby Isaac 
7427719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7428719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7429719ab38cSToby Isaac     }
74306995de1eSToby Isaac   }
7431a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
74326995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
74336995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
74346995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
74356995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7436a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7437a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7438a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7439a68b90caSToby Isaac     if (dof) {
7440a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7441a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7442a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7443a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7444a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7445a68b90caSToby Isaac       }
7446a68b90caSToby Isaac     }
7447a68b90caSToby Isaac   }
7448a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7449a68b90caSToby Isaac   PetscFunctionReturn(0);
7450a68b90caSToby Isaac }
7451a68b90caSToby Isaac 
7452f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7453a68b90caSToby Isaac {
7454f7c74593SToby Isaac   PetscSection aSec;
74550ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
74560ac89760SToby Isaac   const PetscInt *anchors;
74570ac89760SToby Isaac   PetscInt numFields, f;
745866ad2231SToby Isaac   IS aIS;
74590ac89760SToby Isaac   PetscErrorCode ierr;
74600ac89760SToby Isaac 
74610ac89760SToby Isaac   PetscFunctionBegin;
74620ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74630ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
74640ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
74650ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
74660ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7467302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7468a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
746966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
74706995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
74716995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
74720ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
74730ac89760SToby Isaac   i[0] = 0;
74740ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
74750ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7476f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7477f19733c5SToby Isaac 
7478f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7479f19733c5SToby Isaac     if (!rDof) continue;
7480f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
74810ac89760SToby Isaac     if (numFields) {
74820ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
74830ac89760SToby Isaac         annz = 0;
7484f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7485f19733c5SToby Isaac           a = anchors[rOff + r];
74860ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
74870ac89760SToby Isaac           annz += aDof;
74880ac89760SToby Isaac         }
74890ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
74900ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
74910ac89760SToby Isaac         for (q = 0; q < dof; q++) {
74920ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
74930ac89760SToby Isaac         }
74940ac89760SToby Isaac       }
74950ac89760SToby Isaac     }
74960ac89760SToby Isaac     else {
74970ac89760SToby Isaac       annz = 0;
74980ac89760SToby Isaac       for (q = 0; q < dof; q++) {
74990ac89760SToby Isaac         a = anchors[off + q];
75000ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
75010ac89760SToby Isaac         annz += aDof;
75020ac89760SToby Isaac       }
75030ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
75040ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
75050ac89760SToby Isaac       for (q = 0; q < dof; q++) {
75060ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
75070ac89760SToby Isaac       }
75080ac89760SToby Isaac     }
75090ac89760SToby Isaac   }
75100ac89760SToby Isaac   nnz = i[m];
75110ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
75120ac89760SToby Isaac   offset = 0;
75130ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
75140ac89760SToby Isaac     if (numFields) {
75150ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
75160ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
75170ac89760SToby Isaac         for (q = 0; q < dof; q++) {
75180ac89760SToby Isaac           PetscInt rDof, rOff, r;
75190ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
75200ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
75210ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
75220ac89760SToby Isaac             PetscInt s;
75230ac89760SToby Isaac 
75240ac89760SToby Isaac             a = anchors[rOff + r];
75250ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
75260ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
75270ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
75280ac89760SToby Isaac               j[offset++] = aOff + s;
75290ac89760SToby Isaac             }
75300ac89760SToby Isaac           }
75310ac89760SToby Isaac         }
75320ac89760SToby Isaac       }
75330ac89760SToby Isaac     }
75340ac89760SToby Isaac     else {
75350ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
75360ac89760SToby Isaac       for (q = 0; q < dof; q++) {
75370ac89760SToby Isaac         PetscInt rDof, rOff, r;
75380ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
75390ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
75400ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
75410ac89760SToby Isaac           PetscInt s;
75420ac89760SToby Isaac 
75430ac89760SToby Isaac           a = anchors[rOff + r];
75440ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
75450ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
75460ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
75470ac89760SToby Isaac             j[offset++] = aOff + s;
75480ac89760SToby Isaac           }
75490ac89760SToby Isaac         }
75500ac89760SToby Isaac       }
75510ac89760SToby Isaac     }
75520ac89760SToby Isaac   }
75530ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
755425570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
755525570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
755666ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
75570ac89760SToby Isaac   PetscFunctionReturn(0);
75580ac89760SToby Isaac }
75590ac89760SToby Isaac 
756066ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
756166ad2231SToby Isaac {
7562f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7563f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
756466ad2231SToby Isaac   Mat            cMat;
756566ad2231SToby Isaac   PetscErrorCode ierr;
756666ad2231SToby Isaac 
756766ad2231SToby Isaac   PetscFunctionBegin;
756866ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7569a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
757066ad2231SToby Isaac   if (anchorSection) {
7571e228b242SToby Isaac     PetscDS  ds;
7572e228b242SToby Isaac     PetscInt nf;
7573e228b242SToby Isaac 
7574e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
7575f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7576f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7577e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7578302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7579e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
758066ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
758166ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
758266ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
758366ad2231SToby Isaac   }
758466ad2231SToby Isaac   PetscFunctionReturn(0);
758566ad2231SToby Isaac }
7586a93c429eSMatthew G. Knepley 
7587a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7588a93c429eSMatthew G. Knepley {
7589a93c429eSMatthew G. Knepley   PetscDS        prob;
7590a93c429eSMatthew G. Knepley   IS             subis;
7591a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
7592a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
7593a93c429eSMatthew G. Knepley 
7594a93c429eSMatthew G. Knepley   PetscFunctionBegin;
7595a93c429eSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
7596a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7597a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7598a93c429eSMatthew G. Knepley   /* Create subdomain */
7599a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
7600a93c429eSMatthew G. Knepley   /* Create submodel */
7601a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
7602a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
7603a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
7604a93c429eSMatthew G. Knepley   ierr = DMSetDefaultSection(*subdm, subsection);CHKERRQ(ierr);
7605a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
7606a93c429eSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
7607a93c429eSMatthew G. Knepley   ierr = DMSetDS(*subdm, prob);CHKERRQ(ierr);
7608a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
7609a93c429eSMatthew G. Knepley   if (is) {
7610a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
7611a93c429eSMatthew G. Knepley     IS              spIS;
7612a93c429eSMatthew G. Knepley     const PetscInt *spmap;
7613a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
7614a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7615a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
7616a93c429eSMatthew G. Knepley 
7617a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
7618a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
7619a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
7620a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
7621a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
7622a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
7623a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7624a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
7625a93c429eSMatthew G. Knepley 
7626a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
7627a93c429eSMatthew G. Knepley       if (gdof > 0) {
7628a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7629a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
7630a93c429eSMatthew G. Knepley 
7631a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
7632a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
7633a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
7634a93c429eSMatthew G. Knepley         }
7635a93c429eSMatthew G. Knepley         subSize += pSubSize;
7636a93c429eSMatthew G. Knepley         if (pSubSize) {
7637a93c429eSMatthew G. Knepley           if (bs < 0) {
7638a93c429eSMatthew G. Knepley             bs = pSubSize;
7639a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
7640a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
7641a93c429eSMatthew G. Knepley             bs = 1;
7642a93c429eSMatthew G. Knepley           }
7643a93c429eSMatthew G. Knepley         }
7644a93c429eSMatthew G. Knepley       }
7645a93c429eSMatthew G. Knepley     }
7646a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
7647a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7648a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
7649a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7650a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
7651a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
7652a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7653a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
7654a93c429eSMatthew G. Knepley 
7655a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
7656a93c429eSMatthew G. Knepley       if (gdof > 0) {
7657a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
7658a93c429eSMatthew G. Knepley 
7659a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
7660a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7661a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
7662a93c429eSMatthew G. Knepley 
7663a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
7664a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
7665a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
7666a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
7667a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
7668a93c429eSMatthew G. Knepley           }
7669a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
7670a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
7671a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7672a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
7673a93c429eSMatthew G. Knepley           }
7674a93c429eSMatthew G. Knepley         }
7675a93c429eSMatthew G. Knepley       }
7676a93c429eSMatthew G. Knepley     }
7677a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
7678a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
7679a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
7680a93c429eSMatthew G. Knepley     if (bs > 1) {
7681a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
7682a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
7683a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
7684a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
7685a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7686a93c429eSMatthew G. Knepley         }
7687a93c429eSMatthew G. Knepley       }
7688a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
7689a93c429eSMatthew G. Knepley     }
7690a93c429eSMatthew G. Knepley     /* Attach nullspace */
7691a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
7692a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7693a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
7694a93c429eSMatthew G. Knepley     }
7695a93c429eSMatthew G. Knepley     if (f < Nf) {
7696a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
7697a93c429eSMatthew G. Knepley 
7698a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
7699a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
7700a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
7701a93c429eSMatthew G. Knepley     }
7702a93c429eSMatthew G. Knepley   }
7703a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
7704a93c429eSMatthew G. Knepley }
7705